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 try
38 {
39 Velocity.init();
40 } catch (Exception e)
41 {
42 LOG.error("Unable to init velocity", e);
43 }
44 }
45
46 public String parseTemplate(String templatePath, Map<Object, Object> props) throws Exception
47 {
48 VelocityContext ctx = new VelocityContext();
49 ctx.put("parseCheck", new TemplateChecker());
50
51 for (Map.Entry<Object, Object> entry: props.entrySet())
52 {
53 ctx.put(entry.getKey().toString(), entry.getValue());
54 }
55
56 final StringWriter stringWriter = new StringWriter();
57 Template template = Velocity.getTemplate(templatePath);
58
59 template.merge(ctx, stringWriter);
60
61 return stringWriter.toString();
62 }
63
64 public void writeJavaClassFromTemplate(String templatePath, String className, File sourceDirectory, String packageName, ClassBasedModuleProperties props) throws Exception
65 {
66 String originalClass = props.getFullyQualifiedClassname();
67 PluginModuleProperties overrideProps = new BasicClassModuleProperties(originalClass);
68
69 overrideProps.putAll(props);
70 overrideProps.setProperty("CLASSNAME", className);
71 overrideProps.setProperty("PACKAGE", packageName);
72
73 String content = parseTemplate(templatePath, overrideProps);
74 String packagePath = packageName.length() == 0 ? "" : packageName.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
75
76 File packageFile = sourceDirectory;
77 if (!packagePath.equals(""))
78 {
79 packageFile = new File(sourceDirectory, packagePath);
80 }
81 packageFile.mkdirs();
82
83 File javaFile = new File(packageFile, className + ".java");
84 FileUtils.writeStringToFile(javaFile, content, UTF8);
85
86 }
87
88 public void writeFileFromTemplate(String templatePath, String fileName, File directory, PluginModuleProperties props) throws Exception
89 {
90 String content = parseTemplate(templatePath, props);
91 File newFile = new File(directory, fileName);
92
93 FileUtils.writeStringToFile(newFile, content, UTF8);
94
95 }
96
97 public String getStringFromTemplate(String templatePath, PluginModuleProperties props) throws Exception
98 {
99 return parseTemplate(templatePath, props);
100 }
101
102 public class TemplateChecker
103 {
104 public synchronized boolean templateExists(String templatePath)
105 {
106 System.out
107 .println("templatePath = " + templatePath);
108 URL resourceUrl = ClasspathResourceLoader.class.getResource(templatePath);
109
110 return resourceUrl != null;
111 }
112 }
113 }