1   package com.atlassian.plugins.codegen.util;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.IOException;
6   import java.util.List;
7   
8   import com.atlassian.fugue.Option;
9   import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
10  
11  import org.dom4j.Document;
12  import org.dom4j.DocumentException;
13  import org.dom4j.Element;
14  import org.dom4j.io.SAXReader;
15  
16  import static com.atlassian.fugue.Option.none;
17  import static com.atlassian.fugue.Option.some;
18  
19  /**
20   * Provides useful read-only operations on atlassian-plugin.xml.
21   */
22  public class PluginXmlHelper
23  {
24      private final File xmlFile;
25      private final Document document;
26      private final PluginModuleLocation location;
27  
28      public PluginXmlHelper(PluginModuleLocation location) throws IOException, DocumentException
29      {
30          this(location, "atlassian-plugin.xml");
31      }
32      
33      public PluginXmlHelper(PluginModuleLocation location, String fileName) throws IOException, DocumentException
34      {
35          this.location = location;
36          this.xmlFile = new File(location.getResourcesDir(), fileName);
37  
38          final SAXReader reader = new SAXReader();
39          reader.setMergeAdjacentText(true);
40          reader.setStripWhitespaceText(false);
41          this.document = reader.read(new FileInputStream(xmlFile));
42      }
43  
44      public PluginXmlHelper(File pluginXmlFile) throws IOException, DocumentException
45      {
46          this.location = null;
47          this.xmlFile = pluginXmlFile;
48  
49          final SAXReader reader = new SAXReader();
50          reader.setMergeAdjacentText(true);
51          reader.setStripWhitespaceText(false);
52          this.document = reader.read(new FileInputStream(xmlFile));
53      }
54      
55      public Document getDocument()
56      {
57          return document;
58      }
59      
60      public File getXmlFile()
61      {
62          return xmlFile;
63      }
64      
65      public String getPluginKey()
66      {
67          String key = document.getRootElement().attributeValue("key");
68          if (key == null)
69          {
70              throw new IllegalStateException("atlassian-plugin element does not have required attribute: key");
71          }
72          return key.replace("${project.groupId}", location.getGroupId()).replace("${project.artifactId}", location.getArtifactId());
73      }
74      
75      public String getDefaultI18nName()
76      {
77          return "i18n";
78      }
79      
80      @SuppressWarnings("unchecked")
81      public String getDefaultI18nLocation()
82      {
83          List<Element> i18nElements = (List<Element>) document.selectNodes("//resource[@type='i18n']");
84          if (!i18nElements.isEmpty())
85          {
86              return i18nElements.get(0).attributeValue("location");
87          }
88          return getPluginKey();
89      }
90      
91      @SuppressWarnings("unchecked")
92      public static Option<Element> findElementByTypeAndAttribute(Element parent, String type, String attributeName, String attributeValue)
93      {
94          for (Element e : (List<Element>) parent.elements(type))
95          {
96              if (attributeValue.equals(e.attributeValue(attributeName)))
97              {
98                  return some(e);
99              }
100         }
101         return none();
102     }
103 }