1 package com.atlassian.plugins.codegen.modules;
2
3 import java.io.File;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.util.Properties;
7
8 import com.atlassian.plugins.codegen.util.CodeTemplateHelper;
9 import com.atlassian.plugins.codegen.util.PluginXmlHelper;
10
11 import org.apache.commons.io.FileUtils;
12 import org.apache.commons.io.IOUtils;
13 import org.apache.commons.lang.StringUtils;
14
15
16
17
18 public abstract class AbstractPluginModuleCreator<T extends PluginModuleProperties> implements PluginModuleCreator<T>
19 {
20 public static final String DEFAULT_I18N_NAME = "atlassian-plugin";
21 public static final String FUNC_TEST_PACKAGE = "it";
22 public static final String TEST_SUFFIX = "Test";
23 public static final String FUNCT_TEST_SUFFIX = "FuncTest";
24 public static final String GENERIC_TEMPLATE_PREFIX = "templates/generic/";
25 public static final String GENERIC_TEST_TEMPLATE = GENERIC_TEMPLATE_PREFIX + "GenericTest.java.vtl";
26
27 protected CodeTemplateHelper templateHelper;
28
29 protected AbstractPluginModuleCreator()
30 {
31 this(new CodeTemplateHelper());
32 }
33
34 protected AbstractPluginModuleCreator(CodeTemplateHelper templateHelper)
35 {
36 this.templateHelper = templateHelper;
37 }
38
39 @Override
40 public abstract void createModule(PluginModuleLocation location, T props) throws Exception;
41
42
43 protected void addModuleToPluginXml(String templatePath, PluginModuleLocation location, PluginModuleProperties props) throws Exception
44 {
45 PluginXmlHelper pluginXmlHelper = new PluginXmlHelper(location.getPluginXml());
46 pluginXmlHelper.addModuleAsLastChild(templateHelper.getStringFromTemplate(templatePath, props));
47 pluginXmlHelper.addI18nResource(DEFAULT_I18N_NAME);
48 pluginXmlHelper.savePluginXml();
49
50 createI18nProperties(location, props);
51 }
52
53 protected void addPluginInfoParamToPluginXml(PluginModuleLocation location, String name, String value) throws Exception
54 {
55 PluginXmlHelper pluginXmlHelper = new PluginXmlHelper(location.getPluginXml());
56 pluginXmlHelper.addPluginInfoParam(name, value);
57 pluginXmlHelper.savePluginXml();
58 }
59
60 private void createI18nProperties(PluginModuleLocation location, PluginModuleProperties props) throws Exception
61 {
62 if (location.getResourcesDir() != null && !props.getI18nProperties()
63 .isEmpty())
64 {
65 File i18nFile = new File(location.getResourcesDir(), DEFAULT_I18N_NAME + ".properties");
66
67 if (!i18nFile.exists())
68 {
69 i18nFile.createNewFile();
70 }
71
72 Properties currentProps = new Properties();
73 InputStream is = FileUtils.openInputStream(i18nFile);
74 currentProps.load(is);
75 IOUtils.closeQuietly(is);
76
77 currentProps.putAll(props.getI18nProperties());
78
79 OutputStream os = FileUtils.openOutputStream(i18nFile);
80 currentProps.store(os, "");
81 IOUtils.closeQuietly(os);
82
83 }
84 }
85
86 protected String testClassname(String classname)
87 {
88 return classname + TEST_SUFFIX;
89 }
90
91 protected String funcTestClassname(String classname)
92 {
93 return classname + FUNCT_TEST_SUFFIX;
94 }
95
96 protected String funcTestPackageName(String basePackage)
97 {
98 return (StringUtils.isBlank(basePackage) ? FUNC_TEST_PACKAGE : FUNC_TEST_PACKAGE + ".") + basePackage;
99 }
100 }