3 * Copyright 2011 IAAS University of Stuttgart <br>
6 * @author uwe.breitenbuecher@iaas.uni-stuttgart.de
9 package de.unistuttgart.iaas.xml;
11 import java.io.StringWriter;
12 import java.util.ArrayList;
13 import java.util.List;
15 import javax.xml.bind.JAXBContext;
16 import javax.xml.bind.Marshaller;
17 import javax.xml.bind.annotation.XmlRootElement;
18 import javax.xml.parsers.DocumentBuilderFactory;
19 import javax.xml.transform.OutputKeys;
20 import javax.xml.transform.Result;
21 import javax.xml.transform.Source;
22 import javax.xml.transform.Transformer;
23 import javax.xml.transform.TransformerFactory;
24 import javax.xml.transform.dom.DOMSource;
25 import javax.xml.transform.stream.StreamResult;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Node;
29 import org.w3c.dom.NodeList;
32 public class DomXmlConverter {
34 // Single instance of transformer
35 private static Transformer xmlTransformer;
39 * Converts a Node to its String-representation
41 * @param node Node which has to be converted
42 * @return String representation of the passed node
44 public static String nodeToString(Node node, String wrapperElement) {
46 System.out.println("\n\n\n");
47 System.out.println("check if node got a namespace: " + node.getNamespaceURI());
48 if (wrapperElement != null) {
49 // this hack is need as ODE wrapps simpletypes in such elements
50 return node.getTextContent();
53 Source source = new DOMSource(node);
55 StringWriter writer = new StringWriter();
56 Result result = new StreamResult(writer);
58 Transformer transformer = DomXmlConverter.getTransformer();
59 transformer.transform(source, result);
61 return writer.toString();
62 } catch (Exception e) {
65 return "Parsing error";
69 * Singleton implementation of transformer access
74 private static synchronized Transformer getTransformer() throws Exception {
75 if (DomXmlConverter.xmlTransformer == null) {
76 DomXmlConverter.xmlTransformer = TransformerFactory.newInstance().newTransformer();
77 DomXmlConverter.xmlTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
78 DomXmlConverter.xmlTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
79 DomXmlConverter.xmlTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
81 return DomXmlConverter.xmlTransformer;
85 * This method converts a NodeList into a List of Strings. Each string
86 * represents the TextContent of each Node contained in the NodeList
88 * @param nodeList which contains the Nodes
89 * @return List of TextContents of each node
91 public static List<String> convertNodeListToStringList(NodeList nodeList) {
92 List<String> resultList = new ArrayList<String>();
94 for (int i = 0; i < nodeList.getLength(); i++) {
95 resultList.add(nodeList.item(i).getTextContent());
102 * This method converts a NodeList into a List of Nodes
105 * @return List of Nodes
107 public static List<Node> convertNodeListToList(NodeList nodeList) {
108 List<Node> resultList = new ArrayList<Node>(nodeList.getLength());
109 for (int i = 0; i < nodeList.getLength(); i++) {
110 resultList.add(nodeList.item(i));
117 * Helper-Class for converting an Object into its DOM-Representation. The
118 * SerializingContainer is a Wrapper to enable the serialization of any
122 private static class SerializingContainer {
127 public Object getObject() {
131 public void setObject(Object object) {
132 this.object = object;
139 * This methods converts an Object into its DOM-Representation
141 * @param object which have to be converted
142 * @return DOM-Representation of the object
144 public static Node convertObjectToDom(Object object) {
147 // Create new SerializingContainer and pack the object, which has to
148 // be serialized, into it. This has to be done, because JAXB
149 // only marshalls objects of classes annotated with the
150 // @XmlRootElement-Annotation.
151 SerializingContainer container = new SerializingContainer();
152 container.setObject(object);
154 // Create empty Document
155 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
157 // Create JAXBContext and bind classes
158 Class<?>[] classesToBeBound = new Class[] {SerializingContainer.class, container.getObject().getClass()};
159 JAXBContext jaxbContext = JAXBContext.newInstance(classesToBeBound, null);
161 Marshaller marshaller = jaxbContext.createMarshaller();
163 // Set some properties
164 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
165 marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
167 // Marshall container into document.
168 marshaller.marshal(container, document);
170 // Extract only the contained information in the serialized
171 // DOM-Representation of the SerializingContainer
172 return document.getFirstChild().getFirstChild();
174 } catch (Exception e) {