cbce6e803bf06f1f1ee7213e9a647a8fb672c68a
[vfc/nfvo/wfengine.git] / wso2bpel-ext / wso2bpel-core / BPEL4RESTLight / src / main / java / de / unistuttgart / iaas / xml / DomXmlConverter.java
1 /**
2  * 
3  * Copyright 2011 IAAS University of Stuttgart <br>
4  * <br>
5  * 
6  * @author uwe.breitenbuecher@iaas.uni-stuttgart.de
7  * 
8  */
9 package de.unistuttgart.iaas.xml;
10
11 import java.io.StringWriter;
12 import java.util.ArrayList;
13 import java.util.List;
14
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;
26
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Node;
29 import org.w3c.dom.NodeList;
30
31
32 public class DomXmlConverter {
33         
34         // Single instance of transformer
35         private static Transformer xmlTransformer;
36         
37         
38         /**
39          * Converts a Node to its String-representation
40          * 
41          * @param node Node which has to be converted
42          * @return String representation of the passed node
43          */
44         public static String nodeToString(Node node, String wrapperElement) {
45                 try {
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();
51                         }
52                         
53                         Source source = new DOMSource(node);
54                         
55                         StringWriter writer = new StringWriter();
56                         Result result = new StreamResult(writer);
57                         
58                         Transformer transformer = DomXmlConverter.getTransformer();
59                         transformer.transform(source, result);
60                         
61                         return writer.toString();
62                 } catch (Exception e) {
63                         e.printStackTrace();
64                 }
65                 return "Parsing error";
66         }
67         
68         /**
69          * Singleton implementation of transformer access
70          * 
71          * @return Transformer
72          * @throws Exception
73          */
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");
80                 }
81                 return DomXmlConverter.xmlTransformer;
82         }
83         
84         /**
85          * This method converts a NodeList into a List of Strings. Each string
86          * represents the TextContent of each Node contained in the NodeList
87          * 
88          * @param nodeList which contains the Nodes
89          * @return List of TextContents of each node
90          */
91         public static List<String> convertNodeListToStringList(NodeList nodeList) {
92                 List<String> resultList = new ArrayList<String>();
93                 
94                 for (int i = 0; i < nodeList.getLength(); i++) {
95                         resultList.add(nodeList.item(i).getTextContent());
96                 }
97                 
98                 return resultList;
99         }
100         
101         /**
102          * This method converts a NodeList into a List of Nodes
103          * 
104          * @param nodeList
105          * @return List of Nodes
106          */
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));
111                 }
112                 return resultList;
113         }
114         
115         
116         /**
117          * Helper-Class for converting an Object into its DOM-Representation. The
118          * SerializingContainer is a Wrapper to enable the serialization of any
119          * object via JAXB.
120          */
121         @XmlRootElement
122         private static class SerializingContainer {
123                 
124                 Object object;
125                 
126                 
127                 public Object getObject() {
128                         return this.object;
129                 }
130                 
131                 public void setObject(Object object) {
132                         this.object = object;
133                 }
134                 
135         }
136         
137         
138         /**
139          * This methods converts an Object into its DOM-Representation
140          * 
141          * @param object which have to be converted
142          * @return DOM-Representation of the object
143          */
144         public static Node convertObjectToDom(Object object) {
145                 try {
146                         
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);
153                         
154                         // Create empty Document
155                         Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
156                         
157                         // Create JAXBContext and bind classes
158                         Class<?>[] classesToBeBound = new Class[] {SerializingContainer.class, container.getObject().getClass()};
159                         JAXBContext jaxbContext = JAXBContext.newInstance(classesToBeBound, null);
160                         
161                         Marshaller marshaller = jaxbContext.createMarshaller();
162                         
163                         // Set some properties
164                         marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
165                         marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
166                         
167                         // Marshall container into document.
168                         marshaller.marshal(container, document);
169                         
170                         // Extract only the contained information in the serialized
171                         // DOM-Representation of the SerializingContainer
172                         return document.getFirstChild().getFirstChild();
173                         
174                 } catch (Exception e) {
175                         return null;
176                 }
177         }
178         
179 }