1   package com.atlassian.plugins.codegen;
2   
3   import static com.google.common.base.Preconditions.checkNotNull;
4   
5   import org.dom4j.Document;
6   import org.dom4j.DocumentHelper;
7   import org.dom4j.Element;
8   
9   /**
10   * Describes a plugin module element that should be added to the plugin XML file.
11   * This is provided as an arbitrary XML fragment string.
12   */
13  public class ModuleDescriptor implements PluginProjectChange
14  {
15      private final Element content;
16      
17      public static ModuleDescriptor moduleDescriptor(String content)
18      {
19          return new ModuleDescriptor(parseXml(content));
20      }
21      
22      public static ModuleDescriptor moduleDescriptor(Element content)
23      {
24          return new ModuleDescriptor(content);
25      }
26      
27      private ModuleDescriptor(Element content)
28      {
29          this.content = checkNotNull(content, "content");
30      }
31  
32      public String getType()
33      {
34          return content.getName();
35      }
36      
37      public Element getContent()
38      {
39          return content;
40      }
41      
42      @Override
43      public String toString()
44      {
45          return "[module: " + getType() + "]";
46      }
47      
48      private static Element parseXml(String content)
49      {
50          try 
51          {
52              Document doc = DocumentHelper.parseText(content);
53              Element root = doc.getRootElement();
54              root.detach();
55              return root;
56          }
57          catch (Exception e)
58          {
59              throw new IllegalArgumentException("Invalid XML content for module descriptor", e);
60          }
61      }
62  }