summaryrefslogtreecommitdiffstats
path: root/wso2/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/xml/DomXmlConverter.java
blob: cbce6e803bf06f1f1ee7213e9a647a8fb672c68a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
 * 
 * Copyright 2011 IAAS University of Stuttgart <br>
 * <br>
 * 
 * @author uwe.breitenbuecher@iaas.uni-stuttgart.de
 * 
 */
package de.unistuttgart.iaas.xml;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class DomXmlConverter {
	
	// Single instance of transformer
	private static Transformer xmlTransformer;
	
	
	/**
	 * Converts a Node to its String-representation
	 * 
	 * @param node Node which has to be converted
	 * @return String representation of the passed node
	 */
	public static String nodeToString(Node node, String wrapperElement) {
		try {
			System.out.println("\n\n\n");
			System.out.println("check if node got a namespace: " + node.getNamespaceURI());
			if (wrapperElement != null) {
				// this hack is need as ODE wrapps simpletypes in such elements
				return node.getTextContent();
			}
			
			Source source = new DOMSource(node);
			
			StringWriter writer = new StringWriter();
			Result result = new StreamResult(writer);
			
			Transformer transformer = DomXmlConverter.getTransformer();
			transformer.transform(source, result);
			
			return writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "Parsing error";
	}
	
	/**
	 * Singleton implementation of transformer access
	 * 
	 * @return Transformer
	 * @throws Exception
	 */
	private static synchronized Transformer getTransformer() throws Exception {
		if (DomXmlConverter.xmlTransformer == null) {
			DomXmlConverter.xmlTransformer = TransformerFactory.newInstance().newTransformer();
			DomXmlConverter.xmlTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
			DomXmlConverter.xmlTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
			DomXmlConverter.xmlTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
		}
		return DomXmlConverter.xmlTransformer;
	}
	
	/**
	 * This method converts a NodeList into a List of Strings. Each string
	 * represents the TextContent of each Node contained in the NodeList
	 * 
	 * @param nodeList which contains the Nodes
	 * @return List of TextContents of each node
	 */
	public static List<String> convertNodeListToStringList(NodeList nodeList) {
		List<String> resultList = new ArrayList<String>();
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			resultList.add(nodeList.item(i).getTextContent());
		}
		
		return resultList;
	}
	
	/**
	 * This method converts a NodeList into a List of Nodes
	 * 
	 * @param nodeList
	 * @return List of Nodes
	 */
	public static List<Node> convertNodeListToList(NodeList nodeList) {
		List<Node> resultList = new ArrayList<Node>(nodeList.getLength());
		for (int i = 0; i < nodeList.getLength(); i++) {
			resultList.add(nodeList.item(i));
		}
		return resultList;
	}
	
	
	/**
	 * Helper-Class for converting an Object into its DOM-Representation. The
	 * SerializingContainer is a Wrapper to enable the serialization of any
	 * object via JAXB.
	 */
	@XmlRootElement
	private static class SerializingContainer {
		
		Object object;
		
		
		public Object getObject() {
			return this.object;
		}
		
		public void setObject(Object object) {
			this.object = object;
		}
		
	}
	
	
	/**
	 * This methods converts an Object into its DOM-Representation
	 * 
	 * @param object which have to be converted
	 * @return DOM-Representation of the object
	 */
	public static Node convertObjectToDom(Object object) {
		try {
			
			// Create new SerializingContainer and pack the object, which has to
			// be serialized, into it. This has to be done, because JAXB
			// only marshalls objects of classes annotated with the
			// @XmlRootElement-Annotation.
			SerializingContainer container = new SerializingContainer();
			container.setObject(object);
			
			// Create empty Document
			Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
			
			// Create JAXBContext and bind classes
			Class<?>[] classesToBeBound = new Class[] {SerializingContainer.class, container.getObject().getClass()};
			JAXBContext jaxbContext = JAXBContext.newInstance(classesToBeBound, null);
			
			Marshaller marshaller = jaxbContext.createMarshaller();
			
			// Set some properties
			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
			
			// Marshall container into document.
			marshaller.marshal(container, document);
			
			// Extract only the contained information in the serialized
			// DOM-Representation of the SerializingContainer
			return document.getFirstChild().getFirstChild();
			
		} catch (Exception e) {
			return null;
		}
	}
	
}