PageRenderTime 11ms CodeModel.GetById 6ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/plugin-module-codegen-api/src/main/java/com/atlassian/plugins/codegen/ModuleDescriptor.java

https://bitbucket.org/mmeinhold/amps
Java | 62 lines | 48 code | 10 blank | 4 comment | 0 complexity | 1347501f1bf37148819281180e8088c5 MD5 | raw file
 1package com.atlassian.plugins.codegen;
 2
 3import static com.google.common.base.Preconditions.checkNotNull;
 4
 5import org.dom4j.Document;
 6import org.dom4j.DocumentHelper;
 7import 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 */
13public 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}