1 package com.atlassian.plugins.codegen;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 import java.util.Map;
10
11 import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
12 import com.atlassian.plugins.codegen.util.PluginXmlHelper;
13
14 import org.apache.commons.io.FileUtils;
15 import org.dom4j.DocumentException;
16
17
18
19
20
21 public class PluginXmlRewriter implements ProjectRewriter
22 {
23 private final File xmlFile;
24
25 public PluginXmlRewriter(File xmlFile)
26 {
27 this.xmlFile = xmlFile;
28 }
29
30 public PluginXmlRewriter(PluginModuleLocation location)
31 {
32 this(new File(location.getResourcesDir(), "atlassian-plugin.xml"));
33 }
34
35 @Override
36 public void applyChanges(PluginProjectChangeset changes) throws IOException
37 {
38 FileInputStream in = new FileInputStream(xmlFile);
39 ByteArrayOutputStream out = new ByteArrayOutputStream();
40 applyPluginXmlChanges(in, changes, out);
41 in.close();
42 out.close();
43 FileUtils.writeByteArrayToFile(xmlFile, out.toByteArray());
44 }
45
46 public void applyPluginXmlChanges(InputStream in, PluginProjectChangeset changes, OutputStream out) throws IOException
47 {
48 try
49 {
50 PluginXmlHelper pluginXmlHelper = new PluginXmlHelper(in);
51
52 if (!changes.getI18nProperties().isEmpty())
53 {
54 pluginXmlHelper.addI18nResource(DEFAULT_I18N_NAME);
55 }
56
57 for (Map.Entry<String, String> pluginParam : changes.getPluginParameters().entrySet())
58 {
59 pluginXmlHelper.addPluginInfoParam(pluginParam.getKey(), pluginParam.getValue());
60 }
61
62 for (ComponentImport componentImport : changes.getComponentImports())
63 {
64 pluginXmlHelper.addComponentImport(componentImport);
65 }
66
67 for (ComponentDeclaration component : changes.getComponentDeclarations())
68 {
69 pluginXmlHelper.addComponentDeclaration(component);
70 }
71
72 for (ModuleDescriptor moduleDescriptor : changes.getModuleDescriptors())
73 {
74 pluginXmlHelper.addModuleAsLastChild(moduleDescriptor.getContent());
75 }
76
77 pluginXmlHelper.savePluginXml(out);
78 }
79 catch (DocumentException e)
80 {
81 throw new IOException(e);
82 }
83 }
84 }