View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import org.dom4j.DocumentHelper;
4   
5   import java.util.Arrays;
6   import java.util.HashMap;
7   import java.util.Map;
8   
9   import static java.util.Arrays.asList;
10  
11  /**
12   *
13   */
14  public class Dom4jFluent
15  {
16      public static Map<String,String> attributes(String... attrs)
17      {
18          Map<String, String> result = new HashMap<String, String>();
19          for (int x=0; x<attrs.length; x+= 2)
20          {
21              result.put(attrs[x], attrs[x+1]);
22          }
23          return result;
24      }
25      /**
26       * Constructs the element with a textual body
27       *
28       * @param name  The element name
29       * @param value The element text value
30       * @return The element object
31       */
32      public static Element element(String name, String value)
33      {
34          return new Element(name, null, value, new Element[0]);
35      }
36  
37      /**
38       * Constructs the element containg child elements
39       *
40       * @param name     The element name
41       * @param elements The child elements
42       * @return The Element object
43       */
44      public static Element element(String name, Element... elements)
45      {
46          return new Element(name, elements);
47      }
48  
49      /**
50       * Constructs the element containg child elements
51       *
52       * @param name     The element name
53       * @param elements The child elements
54       * @return The Element object
55       */
56      public static Element element(String name, Iterable<Iterable<Element>> elements)
57      {
58          return new Element(name, elements);
59      }
60  
61      /**
62       * Constructs the element containg child elements
63       *
64       * @param name     The element name
65       * @param elements The child elements
66       * @return The Element object
67       */
68      public static Element element(String name, Map<String, String> attributes, Element... elements)
69      {
70          return new Element(name, attributes, elements);
71      }
72  
73      /**
74       * Constructs the element containg child elements
75       *
76       * @param name     The element name
77       * @param elements The child elements
78       * @return The Element object
79       */
80      public static Element element(String name, Map<String, String> attributes, Iterable<Iterable<Element>> elements)
81      {
82          return new Element(name, attributes, elements);
83      }
84  
85      /**
86       * Element wrapper class for configuration elements
87       */
88      public static class Element
89      {
90          private final Iterable<Iterable<Element>> children;
91          private final String name;
92          private final Map<String, String> attributes;
93          private final String text;
94  
95          public Element(String name, Element... children)
96          {
97              this(name, null, children);
98          }
99  
100         public Element(String name, Iterable<Iterable<Element>> children)
101         {
102             this(name, null, children);
103         }
104 
105         public Element(String name, Map<String, String> attributes, Element... children)
106         {
107             this(name, attributes, null, children);
108         }
109 
110         public Element(String name, Map<String, String> attributes, Iterable<Iterable<Element>> children)
111         {
112             this(name, attributes, null, children);
113         }
114 
115         public Element(String name, Map<String, String> attributes, String text, Element... children)
116         {
117             this(name, attributes, text, Arrays.<Iterable<Element>>asList(asList(children)));
118         }
119 
120         public Element(String name, Map<String, String> attributes, String text, Iterable<Iterable<Element>> children)
121         {
122             this.name = name;
123             this.attributes = attributes;
124             this.text = text;
125 
126             this.children = children;
127         }
128 
129         public org.dom4j.Element toDom()
130         {
131 
132             org.dom4j.Element dom = DocumentHelper.createElement(name);
133             if (attributes != null)
134             {
135                 for (Map.Entry<String,String> entry : attributes.entrySet())
136                 {
137                     dom.addAttribute(entry.getKey(), entry.getValue());
138                 }
139             }
140             if (text != null)
141             {
142                 dom.setText(text);
143             }
144             if (children != null)
145             {
146                 for (Iterable<Element> i : children)
147                 {
148                     for (Element e : i)
149                     {
150                         dom.add(e.toDom());
151                     }
152                 }
153             }
154 
155             return dom;
156         }
157     }
158 
159 }