PageRenderTime 52ms CodeModel.GetById 15ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

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