1   package com.atlassian.maven.plugins.amps.util;
2   
3   import java.io.File;
4   import java.net.MalformedURLException;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import com.atlassian.maven.plugins.amps.MavenContext;
9   
10  import org.apache.commons.lang.StringUtils;
11  import org.dom4j.Document;
12  import org.dom4j.DocumentException;
13  import org.dom4j.Node;
14  import org.dom4j.io.SAXReader;
15  
16  import static com.atlassian.maven.plugins.amps.util.FileUtils.file;
17  
18  /**
19   * @since 3.6.1
20   */
21  public class PluginXmlUtils
22  {
23      private static final String XPATH_PLUGIN = "/atlassian-plugin";
24      private static final String XPATH_PLUGIN_INFO = XPATH_PLUGIN + "/plugin-info";
25      private static final String XPATH_REST = XPATH_PLUGIN + "/rest";
26      private static final String XPATH_PACKAGE_RELATIVE = "package";
27  
28      public static PluginInfo getPluginInfo(MavenContext context)
29      {
30          File pluginXml = getPluginXml(context);
31          PluginInfo pluginInfo = null;
32  
33          if (pluginXml.exists())
34          {
35              try
36              {
37                  Document pluginDoc = getXmlDocument(pluginXml);
38                  Node root = pluginDoc.selectSingleNode(XPATH_PLUGIN);
39                  String name = "";
40                  String desc = "";
41  
42                  if(null != root) {
43                      name = root.valueOf("@name");
44                      Node infoNode = pluginDoc.selectSingleNode(XPATH_PLUGIN_INFO);
45  
46                      if(null != infoNode) {
47                          Node descNode = infoNode.selectSingleNode("description");
48                          if (null != descNode)
49                          {
50                              desc = descNode.valueOf("text()");
51                          }
52                      }
53                  }
54  
55                  pluginInfo = new PluginInfo(name,desc);
56  
57  
58              } catch (Exception e)
59              {
60                  //print the error but continue since this is not a critical feature
61                  e.printStackTrace();
62              }
63          }
64  
65          if(null == pluginInfo) {
66              pluginInfo = new PluginInfo("unknown plugin","");
67          }
68  
69          return pluginInfo;
70      }
71  
72      public static List<RESTModuleInfo> getRestModules(MavenContext context)
73      {
74          File pluginXml = getPluginXml(context);
75          List<RESTModuleInfo> modules = new ArrayList<RESTModuleInfo>();
76  
77          if (pluginXml.exists())
78          {
79              try
80              {
81                  Document pluginDoc = getXmlDocument(pluginXml);
82                  List<Node> restNodes = pluginDoc.selectNodes(XPATH_REST);
83  
84                  for (Node restNode : restNodes)
85                  {
86                      String name = restNode.valueOf("@name");
87                      String desc = restNode.valueOf("@description");
88                      List<String> packagesToScan = new ArrayList<String>();
89  
90                      if (StringUtils.isBlank(desc))
91                      {
92                          Node descNode = restNode.selectSingleNode("description");
93                          if (null != descNode)
94                          {
95                              desc = descNode.valueOf("text()");
96                          }
97                      }
98  
99                      List<Node> packageNodes = restNode.selectNodes(XPATH_PACKAGE_RELATIVE);
100                     for (Node packageNode : packageNodes)
101                     {
102                         packagesToScan.add(packageNode.valueOf("text()"));
103                     }
104 
105                     modules.add(new RESTModuleInfo(name, desc, packagesToScan));
106                 }
107 
108             } catch (Exception e)
109             {
110                 //print the error but continue since this is not a critical feature
111                 e.printStackTrace();
112             }
113         }
114 
115         return modules;
116     }
117 
118     public static boolean hasPluginXml(MavenContext context)
119     {
120         return getPluginXml(context).exists();
121     }
122 
123     public static File getPluginXml(MavenContext context)
124     {
125         return file(context.getProject()
126                 .getBuild()
127                 .getOutputDirectory(), "atlassian-plugin.xml");
128     }
129 
130     protected static Document getXmlDocument(File xmlFile) throws MalformedURLException, DocumentException
131     {
132         SAXReader reader = new SAXReader();
133         return reader.read(xmlFile);
134     }
135 
136     public static class RESTModuleInfo
137     {
138         private String name;
139         private String description;
140         private List<String> packagesToScan;
141 
142         public RESTModuleInfo(String name, String description, List<String> packagesToScan)
143         {
144             this.name = name;
145             this.description = description;
146             this.packagesToScan = packagesToScan;
147         }
148 
149         public String getName()
150         {
151             return name;
152         }
153 
154         public List<String> getPackagesToScan()
155         {
156             return packagesToScan;
157         }
158 
159         public String getDescription()
160         {
161             return description;
162         }
163 
164     }
165 
166     public static class PluginInfo
167     {
168         private String name;
169         private String description;
170 
171         public PluginInfo(String name, String description)
172         {
173             this.name = name;
174             this.description = description;
175         }
176 
177         public String getName()
178         {
179             return name;
180         }
181 
182         public String getDescription()
183         {
184             return description;
185         }
186 
187     }
188 }