View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      You may obtain a copy of the License at
5   
6        http://www.apache.org/licenses/LICENSE-2.0
7   
8      Unless required by applicable law or agreed to in writing, software
9      distributed under the License is distributed on an "AS IS" BASIS,
10     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11     See the License for the specific language governing permissions and
12     limitations under the License.
13  */
14  package uk.nhs.interoperability.util.xml;
15  
16  import java.io.IOException;
17  import java.io.StringReader;
18  import java.io.StringWriter;
19  
20  import javax.xml.parsers.DocumentBuilder;
21  import javax.xml.parsers.DocumentBuilderFactory;
22  import javax.xml.parsers.ParserConfigurationException;
23  
24  import org.w3c.dom.DOMConfiguration;
25  import org.w3c.dom.DOMImplementation;
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Node;
28  import org.w3c.dom.ls.DOMImplementationLS;
29  import org.w3c.dom.ls.LSOutput;
30  import org.w3c.dom.ls.LSSerializer;
31  import org.xml.sax.InputSource;
32  import org.xml.sax.SAXException;
33  
34  /**
35   * The Class DomUtils.
36   *
37   * @author Michael Odling-Smee
38   * @author Nicholas Jones
39   * @since 0.1
40   */
41  public class DomUtils {
42  
43  	/**
44  	 * Instantiates a new dom utils. Private to
45  	 * ensure static methods are used
46  	 */
47  	private DomUtils() {
48  	}
49  	
50  	/** The Constant PRETTY_PRINT. */
51  	public static final boolean PRETTY_PRINT = true;
52  	
53  	/**
54  	 * Serialise to xml.
55  	 *
56  	 * @param document the document
57  	 * @return the string
58  	 */
59  	public static final String serialiseToXML(Document document) {
60  		return serialiseToXML(document, false);
61  	}
62  	
63  	/**
64  	 * Serialise to xml.
65  	 *
66  	 * @param document the document
67  	 * @param prettyPrint the pretty print
68  	 * @return the string
69  	 */
70  	public static final String serialiseToXML(Document document, boolean prettyPrint) {
71  		if (document != null) {
72  			DOMImplementation domImplementation = document.getImplementation();
73  			if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
74  				DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS", "3.0");
75  				LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
76  				DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
77  				if (prettyPrint && domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) {
78  					lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
79  				}
80  				LSOutput lsOutput = domImplementationLS.createLSOutput();
81  				lsOutput.setEncoding("UTF-8");
82  				StringWriter stringWriter = new StringWriter();
83  				lsOutput.setCharacterStream(stringWriter);
84  				lsSerializer.write(document, lsOutput);
85  				return stringWriter.toString();
86  			} else {
87  				throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported.");
88  			}
89  		}
90  		return null;
91  	}
92  	
93  	/**
94  	 * Creates the document from node.
95  	 *
96  	 * @param node the node
97  	 * @return the document
98  	 * @throws ParserConfigurationException the parser configuration exception
99  	 */
100 	public static final Document createDocumentFromNode(Node node) throws ParserConfigurationException {
101 		if (node != null) {
102 			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
103 			DocumentBuilder builder = factory.newDocumentBuilder();
104 			Document doc =  builder.newDocument();
105 			Node clone = node.cloneNode(true);
106 			doc.adoptNode(clone);
107 			doc.appendChild(clone);
108 			return doc;
109 		} 
110 		return null;
111 	}
112 	
113 	/**
114 	 * Parses the.
115 	 *
116 	 * @param xmlString the xml string
117 	 * @return the document
118 	 * @throws SAXException the sAX exception
119 	 * @throws IOException Signals that an I/O exception has occurred.
120 	 * @throws ParserConfigurationException the parser configuration exception
121 	 */
122 	public static final Document parse(String xmlString) throws SAXException, IOException, ParserConfigurationException {
123 		if (xmlString != null) {
124 			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
125 			factory.setNamespaceAware(true);
126 			DocumentBuilder builder = factory.newDocumentBuilder();			
127 			InputSource is = new InputSource(new StringReader(xmlString));
128 			return builder.parse(is);
129 		}
130 		return null;
131 	}
132 
133 }