1 package com.atlassian.plugins.codegen.util;
2
3 import java.io.File;
4 import java.io.StringWriter;
5 import java.net.URL;
6 import java.util.Map;
7 import java.util.regex.Matcher;
8
9 import com.atlassian.plugins.codegen.modules.BasicClassModuleProperties;
10 import com.atlassian.plugins.codegen.modules.ClassBasedModuleProperties;
11 import com.atlassian.plugins.codegen.modules.PluginModuleProperties;
12
13 import org.apache.commons.io.FileUtils;
14 import org.apache.log4j.Logger;
15 import org.apache.velocity.Template;
16 import org.apache.velocity.VelocityContext;
17 import org.apache.velocity.app.Velocity;
18 import org.apache.velocity.runtime.RuntimeConstants;
19 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
20
21
22
23
24 public class CodeTemplateHelper
25 {
26
27 private static Logger LOG = Logger.getLogger(CodeTemplateHelper.class);
28 public static final String UTF8 = "UTF-8";
29
30 static
31 {
32 Velocity.setProperty(RuntimeConstants.INPUT_ENCODING, UTF8);
33 Velocity.setProperty(RuntimeConstants.PARSER_POOL_SIZE, 3);
34 Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
35 Velocity.setProperty("classpath." + RuntimeConstants.RESOURCE_LOADER + ".class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
36 Velocity.setProperty(Velocity.VM_LIBRARY, "templates/macros.vm");
37 Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem");
38 try
39 {
40 Velocity.init();
41 } catch (Exception e)
42 {
43 LOG.error("Unable to init velocity", e);
44 }
45 }
46
47 public String parseTemplate(String templatePath, Map<Object, Object> props) throws Exception
48 {
49 VelocityContext ctx = new VelocityContext();
50 ctx.put("parseCheck", new TemplateChecker());
51
52 for (Map.Entry<Object, Object> entry: props.entrySet())
53 {
54 ctx.put(entry.getKey().toString(), entry.getValue());
55 }
56
57 final StringWriter stringWriter = new StringWriter();
58 Template template = Velocity.getTemplate(templatePath);
59
60 template.merge(ctx, stringWriter);
61
62 return stringWriter.toString();
63 }
64
65 public void writeJavaClassFromTemplate(String templatePath, String className, File sourceDirectory, String packageName, ClassBasedModuleProperties props) throws Exception
66 {
67 String originalClass = props.getClassId().getFullName();
68 PluginModuleProperties overrideProps = new BasicClassModuleProperties(originalClass);
69
70 overrideProps.putAll(props);
71 overrideProps.setProperty("CLASSNAME", className);
72 overrideProps.setProperty("PACKAGE", packageName);
73
74 String content = parseTemplate(templatePath, overrideProps);
75 String packagePath = packageName.length() == 0 ? "" : packageName.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
76
77 File packageFile = sourceDirectory;
78 if (!packagePath.equals(""))
79 {
80 packageFile = new File(sourceDirectory, packagePath);
81 }
82 packageFile.mkdirs();
83
84 File javaFile = new File(packageFile, className + ".java");
85 FileUtils.writeStringToFile(javaFile, content, UTF8);
86
87 }
88
89 public void writeFileFromTemplate(String templatePath, String fileName, File directory, PluginModuleProperties props) throws Exception
90 {
91 String content = parseTemplate(templatePath, props);
92 File newFile = new File(directory, fileName);
93
94 FileUtils.writeStringToFile(newFile, content, UTF8);
95
96 }
97
98 public String getStringFromTemplate(String templatePath, Map<Object, Object> props) throws Exception
99 {
100 return parseTemplate(templatePath, props);
101 }
102
103 public class TemplateChecker
104 {
105 public synchronized boolean templateExists(String templatePath)
106 {
107 URL resourceUrl = ClasspathResourceLoader.class.getResource(templatePath);
108
109 return resourceUrl != null;
110 }
111 }
112 }