/src/main/java/nl/bitbrains/nebu/common/util/xml/XMLConverter.java

https://github.com/deltaforge/nebu-common-java · Java · 206 lines · 96 code · 19 blank · 91 comment · 4 complexity · e4edb269a2ec35258c6c506dc67b6ca0 MD5 · raw file

  1. package nl.bitbrains.nebu.common.util.xml;
  2. import java.text.ParseException;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9. import nl.bitbrains.nebu.common.interfaces.IBuilder;
  10. import nl.bitbrains.nebu.common.util.ErrorChecker;
  11. import org.jdom2.Document;
  12. import org.jdom2.Element;
  13. import org.jdom2.JDOMException;
  14. import org.jdom2.input.DOMBuilder;
  15. import org.jdom2.output.DOMOutputter;
  16. /**
  17. * @author Jesse Donkervliet, Tim Hegeman, and Stefan Hugtenburg
  18. *
  19. */
  20. public abstract class XMLConverter {
  21. public static final String TAG_LIST = "list";
  22. public static final String TAG_ITEM = "item";
  23. public static final String TAG_MAP = "map";
  24. public static final String TAG_MAP_ENTRY = "entry";
  25. public static final String TAG_MAP_ENTRY_KEY = "key";
  26. public static final String TAG_MAP_ENTRY_VALUE = "value";
  27. /**
  28. * Converts a JDOM element to a W3C Document.
  29. *
  30. * @param elem
  31. * to convert.
  32. * @return the converted document.
  33. * @throws JDOMException
  34. * if it can not be converted.
  35. */
  36. public static org.w3c.dom.Document convertJDOMElementW3CDocument(final org.jdom2.Element elem)
  37. throws JDOMException {
  38. final DOMOutputter converter = new DOMOutputter();
  39. final Element elem2 = elem.clone();
  40. final org.jdom2.Document jdomDoc = new Document(elem2);
  41. return converter.output(jdomDoc);
  42. }
  43. /**
  44. * @param document
  45. * document to convert
  46. * @return the converted element.
  47. */
  48. public static Element convertW3CDocumentJDOMElement(final org.w3c.dom.Document document) {
  49. final org.jdom2.input.DOMBuilder converter = new DOMBuilder();
  50. return converter.build(document).getRootElement();
  51. }
  52. /**
  53. * Converts a Collection to a JDOM element.
  54. *
  55. * @param list
  56. * to convert.
  57. * @param factory
  58. * to use in converting individual elements.
  59. * @param <T>
  60. * type of the elements in the list.
  61. * @return the converted element.
  62. */
  63. public static <T> Element convertCollectionToJDOMElement(final Collection<T> list,
  64. final XMLWriter<T> factory) {
  65. return XMLConverter.convertCollectionToJDOMElement(list,
  66. factory,
  67. XMLConverter.TAG_LIST,
  68. XMLConverter.TAG_ITEM);
  69. }
  70. /**
  71. * Converts a Collection to a JDOM element.
  72. *
  73. * @param list
  74. * to convert.
  75. * @param factory
  76. * to use in converting individual elements.
  77. * @param rootElem
  78. * name of root element.
  79. * @param item
  80. * name of item elements
  81. * @param <T>
  82. * type of the elements in the list.
  83. * @return the converted element.
  84. */
  85. public static <T> Element convertCollectionToJDOMElement(final Collection<T> list,
  86. final XMLWriter<T> factory, final String rootElem, final String item) {
  87. ErrorChecker.throwIfNullArgument(list, rootElem);
  88. final Element elem = new Element(rootElem);
  89. for (final T object : list) {
  90. elem.addContent(factory.toXML(object));
  91. }
  92. return elem;
  93. }
  94. /**
  95. * Convert a JDOM list to a Java list.
  96. *
  97. * @param xml
  98. * to convert.
  99. * @param factory
  100. * to use in conversion.
  101. * @param <T>
  102. * the type to place in the list.
  103. * @return the parsed list.
  104. * @throws ParseException
  105. * if one element can not be parsed.
  106. */
  107. public static <T> List<T> convertJDOMElementToList(final Element xml,
  108. final XMLReader<IBuilder<T>> factory) throws ParseException {
  109. ErrorChecker.throwIfNullArgument(xml, "xml");
  110. ErrorChecker.throwIfNullArgument(factory, "factory");
  111. final List<T> list = new ArrayList<T>();
  112. for (final Element child : xml.getChildren()) {
  113. list.add(factory.fromXML(child).build());
  114. }
  115. return list;
  116. }
  117. /**
  118. * Convert a {@link Map} to an {@link Element}.
  119. *
  120. * @param map
  121. * The map to convert.
  122. * @param keyFactory
  123. * The {@link XMLWriter} that should be used to convert the keys
  124. * to XML.
  125. * @param valFactory
  126. * The {@link XMLWriter} that should be used to convert the
  127. * values to XML.
  128. * @param <K>
  129. * Key type of map.
  130. * @param <V>
  131. * Value type of map.
  132. * @return An XML {@link Element} that represents the original {@link Map}.
  133. */
  134. public static <K, V> Element convertMapToJDOMElement(final Map<K, V> map,
  135. final XMLWriter<K> keyFactory, final XMLWriter<V> valFactory) {
  136. ErrorChecker.throwIfNullArgument(map, "map");
  137. ErrorChecker.throwIfNullArgument(keyFactory, "key factory");
  138. ErrorChecker.throwIfNullArgument(valFactory, "value factory");
  139. final Element root = new Element(XMLConverter.TAG_MAP);
  140. for (final Entry<K, V> entry : map.entrySet()) {
  141. final Element entryElem = new Element(XMLConverter.TAG_MAP_ENTRY);
  142. final Element entryKey = new Element(XMLConverter.TAG_MAP_ENTRY_KEY);
  143. final Element entryVal = new Element(XMLConverter.TAG_MAP_ENTRY_VALUE);
  144. final Element key = keyFactory.toXML(entry.getKey());
  145. final Element val = valFactory.toXML(entry.getValue());
  146. root.addContent(entryElem);
  147. entryElem.addContent(entryKey);
  148. entryElem.addContent(entryVal);
  149. entryKey.addContent(key);
  150. entryVal.addContent(val);
  151. }
  152. return root;
  153. }
  154. /**
  155. * Convert an JDOM {@link Element} to a {@link Map}.
  156. *
  157. * @param elem
  158. * The {@link Element} to convert.
  159. * @param keyFactory
  160. * The {@link XMLReader} that should be used for the keys.
  161. * @param valFactory
  162. * The {@link XMLReader} that should be used for the values.
  163. * @param <K>
  164. * Key type of map.
  165. * @param <V>
  166. * Value type of map.
  167. * @return A {@link Map} representation of the given {@link Element}.
  168. * @throws ParseException
  169. * when the given {@link Element} has an unexpected format.
  170. */
  171. public static <K, V> Map<K, V> convertJDOMElementToMap(final Element elem,
  172. final XMLReader<IBuilder<K>> keyFactory, final XMLReader<IBuilder<V>> valFactory)
  173. throws ParseException {
  174. ErrorChecker.throwIfNullArgument(elem, "element");
  175. ErrorChecker.throwIfNullArgument(keyFactory, "key factory");
  176. ErrorChecker.throwIfNullArgument(valFactory, "value factory");
  177. final Map<K, V> map = new HashMap<K, V>();
  178. for (final Element entry : elem.getChildren()) {
  179. final K key = keyFactory.fromXML(entry.getChild(XMLConverter.TAG_MAP_ENTRY_KEY))
  180. .build();
  181. final V val = valFactory.fromXML(entry.getChild(XMLConverter.TAG_MAP_ENTRY_VALUE))
  182. .build();
  183. map.put(key, val);
  184. }
  185. return map;
  186. }
  187. }