View Javadoc

1   package com.atlassian.user.configuration.xml;
2   
3   import org.dom4j.Element;
4   import org.dom4j.Attribute;
5   
6   import java.util.HashMap;
7   import java.util.List;
8   import java.util.Map;
9   
10  import com.atlassian.user.configuration.Configuration;
11  
12  /**
13   * Some XML-related functions shared by the config parser classes
14   */
15  final class XMLConfigUtil
16  {
17      ///CLOVER:OFF
18      /** Do not construct me. */
19      private XMLConfigUtil()
20      {
21      }
22      ///CLOVER:ON
23  
24      /**
25       * Parses all elements, which are assumed to be holding Strings representing classes to instantiate, and stores them
26       * as key/value pairs in the HashMap.
27       *
28       * @param repositoryElement the XML element defining the repository.
29       * @return {@link java.util.HashMap} of key/value pairs representing the classes to instantiate.
30       */
31      public static HashMap<String, String> parseRepositoryElementForClassNames(Element repositoryElement)
32      {
33          HashMap<String, String> values = new HashMap<String, String>();
34          Element classesElement = repositoryElement.element(Configuration.CLASSES);
35  
36          if (classesElement == null)
37              return values;
38  
39          List subElements = classesElement.elements();
40  
41          for (Object subElement : subElements)
42          {
43              Element element = (Element) subElement;
44  
45              if (element.getName().equals(Configuration.PARAM))
46              {
47                  Attribute paramAttribute = element.attribute(Configuration.NAME);
48                  values.put(paramAttribute.getText(), element.getText());
49              }
50              else
51                  values.put(element.getName(), element.getText());
52          }
53  
54          Attribute attr = repositoryElement.attribute(Configuration.CLASS);
55  
56          if (attr != null)
57          {
58              values.put(Configuration.CLASS, attr.getText());
59          }
60  
61          return values;
62      }
63  
64      /**
65       * Returns a hashmap of attribute values on the repository element, and child elements of the repository element
66       * with their text content except for the 'classes' element.
67       *
68       * @param repositoryElement the XML element defining the repository.
69       * @return {@link HashMap} of key/value pairs representing String data to be directly copied into the object.
70       */
71      public static Map<String, String> parseRepositoryElementForStringData(Element repositoryElement)
72      {
73          HashMap<String, String> values = new HashMap<String, String>();
74  
75          for (Object o : repositoryElement.attributes())
76          {
77              Attribute attr = (Attribute) o;
78  
79              if (attr.getName().equals(Configuration.CLASS))
80                  continue;
81  
82              values.put(attr.getName(), attr.getText());
83          }
84  
85          for (Object o : repositoryElement.elements())
86          {
87              Element element = (Element) o;
88  
89              if (element.getName().equals(Configuration.CLASSES))
90                  continue;
91  
92              values.put(element.getName(), element.getText());
93          }
94  
95          return values;
96      }
97  
98  }