1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.wikimodel.xml

File XmlUtil.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart3.png
80% of files have more coverage

Code metrics

16
85
22
1
339
249
34
0.4
3.86
22
1.55

Classes

Class Line # Actions
XmlUtil 61 85 0% 34 87
0.2926829229.3%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.wikimodel.xml;
21   
22    import java.io.ByteArrayOutputStream;
23    import java.io.InputStream;
24    import java.io.InputStreamReader;
25    import java.io.OutputStream;
26    import java.io.OutputStreamWriter;
27    import java.io.Reader;
28    import java.io.StringReader;
29    import java.io.Writer;
30   
31    import javax.xml.parsers.DocumentBuilder;
32    import javax.xml.parsers.DocumentBuilderFactory;
33    import javax.xml.parsers.ParserConfigurationException;
34    import javax.xml.transform.Result;
35    import javax.xml.transform.Source;
36    import javax.xml.transform.Templates;
37    import javax.xml.transform.Transformer;
38    import javax.xml.transform.TransformerConfigurationException;
39    import javax.xml.transform.TransformerException;
40    import javax.xml.transform.TransformerFactory;
41    import javax.xml.transform.TransformerFactoryConfigurationError;
42    import javax.xml.transform.URIResolver;
43    import javax.xml.transform.dom.DOMSource;
44    import javax.xml.transform.sax.SAXSource;
45    import javax.xml.transform.stream.StreamResult;
46   
47    import org.w3c.dom.Document;
48    import org.w3c.dom.Element;
49    import org.w3c.dom.NamedNodeMap;
50    import org.w3c.dom.Node;
51    import org.xml.sax.InputSource;
52    import org.xml.sax.XMLReader;
53   
54    /**
55    * This class contains various utility methods used to manipulate with DOM
56    * nodes.
57    *
58    * @version $Id: 95df085a81a79c308aa4330bbcc694a55b37ca83 $
59    * @since 4.0M1
60    */
 
61    public class XmlUtil
62    {
 
63  0 toggle public static void formatXML(
64    Document xml,
65    Document xsl,
66    URIResolver resolver,
67    Writer output) throws Exception
68    {
69  0 try {
70  0 DOMSource xslSource = new DOMSource(xsl);
71  0 DOMSource xmlSource = new DOMSource(xml);
72  0 Result result = new StreamResult(output);
73  0 formatXML(xmlSource, xslSource, resolver, result);
74    } finally {
75  0 output.close();
76    }
77    }
78   
 
79  0 toggle public static void formatXML(Document xml, Document xsl, Writer output)
80    throws Exception
81    {
82  0 formatXML(xml, xsl, null, output);
83    }
84   
 
85  0 toggle public static void formatXML(
86    Reader xml,
87    Reader xsl,
88    URIResolver resolver,
89    Writer output) throws Exception
90    {
91  0 try {
92  0 try {
93  0 try {
94  0 Source xmlSource = new SAXSource(new InputSource(xml));
95  0 Source xslSource = new SAXSource(new InputSource(xsl));
96  0 Result result = new StreamResult(output);
97  0 formatXML(xmlSource, xslSource, resolver, result);
98    } finally {
99  0 output.close();
100    }
101    } finally {
102  0 xsl.close();
103    }
104    } finally {
105  0 xml.close();
106    }
107    }
108   
 
109  0 toggle public static void formatXML(Reader xml, Reader xsl, Writer output)
110    throws Exception
111    {
112  0 formatXML(xml, xsl, null, output);
113    }
114   
 
115  0 toggle public static void formatXML(
116    Source xmlSource,
117    Source xslSource,
118    URIResolver resolver,
119    Result result)
120    throws TransformerFactoryConfigurationError,
121    TransformerConfigurationException,
122    TransformerException
123    {
124  0 TransformerFactory factory = TransformerFactory.newInstance();
125  0 Templates t = factory.newTemplates(xslSource);
126  0 Transformer transformer = t.newTransformer();
127  0 if (resolver != null) {
128  0 transformer.setURIResolver(resolver);
129    }
130  0 transformer.transform(xmlSource, result);
131    }
132   
133    /**
134    * Creates and returns an new document builder factory. This method tries to
135    * configure the namespace support for the builder. If the underlying parser
136    * does not support namespaces then this method returns a simple
137    * DocumentBuilder object.
138    *
139    * @return a new document builder
140    */
 
141  5 toggle private static DocumentBuilder getDocumentBuilder()
142    throws ParserConfigurationException
143    {
144  5 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
145  5 factory.setNamespaceAware(true); // never forget this!
146  5 try {
147  5 factory.setFeature("http://xml.org/sax/features/namespaces", true);
148    } catch (Throwable t) {
149    // Just skip it...
150    }
151  5 DocumentBuilder builder = factory.newDocumentBuilder();
152  5 return builder;
153    }
154   
 
155  0 toggle public static String lookupNamespaceURI(Node root, String specifiedPrefix)
156    {
157  0 if (root == null) {
158  0 return null;
159    }
160  0 if (root.hasAttributes()) {
161  0 NamedNodeMap nnm = root.getAttributes();
162  0 for (int i = 0; i < nnm.getLength(); i++) {
163  0 Node n = nnm.item(i);
164  0 if (("xmlns".equals(n.getPrefix()) && specifiedPrefix.equals(n
165    .getNodeName()))
166    || ("xmlns:" + specifiedPrefix).equals(n.getNodeName()))
167    {
168  0 return n.getNodeValue();
169    }
170    }
171    }
172  0 return lookupNamespaceURI(root.getParentNode(), specifiedPrefix);
173    }
174   
175    /**
176    * Returns a new copy of the given node using the specified document as a
177    * factory for new nodes.
178    *
179    * @param doc the document used as a factory for new nodes
180    * @param node the node to copy
181    * @return a new copy of the given node using the specified document as a
182    * factory for new nodes.
183    */
 
184  0 toggle @SuppressWarnings("unchecked")
185    public static <E extends Node> E newCopy(Document doc, E node)
186    {
187  0 E result = (E) node.cloneNode(true);
188  0 return (E) doc.adoptNode(result);
189    }
190   
191    /**
192    * Creates and returns a new empty DOM document.
193    *
194    * @return a newly created DOM document
195    */
 
196  5 toggle public static Document newDocument() throws ParserConfigurationException
197    {
198  5 DocumentBuilder builder = getDocumentBuilder();
199  5 return builder.newDocument();
200    }
201   
 
202  0 toggle public static Element newTemplate(String xml) throws Exception
203    {
204  0 Document doc = readXML(xml);
205  0 return doc.getDocumentElement();
206    }
207   
208    /**
209    * Parses the given input stream and returns the corresponding desirialized
210    * XML document.
211    *
212    * @param input the input stream containing the serialized XML document
213    * @return the deserialized DOM document
214    */
 
215  0 toggle public static Document readXML(InputStream input) throws Exception
216    {
217  0 return readXML(new InputStreamReader(input, "UTF-8"));
218    }
219   
220    /**
221    * Parses the given input stream and returns the corresponding desirialized
222    * XML document.
223    *
224    * @param reader the reader containing the serialized XML document
225    * @return the deserialized DOM document
226    */
 
227  0 toggle public static Document readXML(Reader reader) throws Exception
228    {
229  0 try {
230  0 DocumentBuilder builder = getDocumentBuilder();
231  0 InputSource source = new InputSource(reader);
232  0 Document doc = builder.parse(source);
233  0 return doc;
234    } finally {
235  0 reader.close();
236    }
237    }
238   
 
239  0 toggle public static Document readXML(String str) throws Exception
240    {
241  0 if (str == null) {
242  0 return null;
243    }
244  0 Reader reader = new StringReader(str);
245  0 return readXML(reader);
246    }
247   
 
248  0 toggle public static String write(Document doc) throws Exception
249    {
250  0 return write(doc.getDocumentElement());
251    }
252   
 
253  0 toggle public static String write(Element e) throws Exception
254    {
255  0 ByteArrayOutputStream baos = new ByteArrayOutputStream();
256  0 write(e, baos);
257  0 return new String(baos.toByteArray(), "UTF-8");
258    }
259   
 
260  0 toggle public static void write(Element root, OutputStream os) throws Exception
261    {
262  0 Writer writer = new OutputStreamWriter(os);
263  0 write(root, writer);
264    }
265   
 
266  0 toggle public static void write(Element root, Writer writer)
267    throws TransformerConfigurationException,
268    TransformerFactoryConfigurationError,
269    TransformerException
270    {
271  0 Source input = new DOMSource(root);
272  0 write(input, writer);
273    }
274   
 
275  6 toggle public static void write(Reader reader, XMLReader parser, Result output)
276    throws TransformerConfigurationException,
277    TransformerFactoryConfigurationError,
278    TransformerException
279    {
280  6 SAXSource input = new SAXSource();
281  6 InputSource inputSource = new InputSource();
282  6 inputSource.setCharacterStream(reader);
283  6 input.setInputSource(inputSource);
284  6 input.setXMLReader(parser);
285  6 write(input, output);
286    }
287   
 
288  1 toggle public static void write(Reader reader, XMLReader parser, Writer writer)
289    throws TransformerConfigurationException,
290    TransformerFactoryConfigurationError,
291    TransformerException
292    {
293  1 Result output = new StreamResult(writer);
294  1 write(reader, parser, output);
295    }
296   
 
297  6 toggle public static void write(Source input, Result output)
298    throws TransformerConfigurationException,
299    TransformerFactoryConfigurationError,
300    TransformerException
301    {
302  6 write(input, output, true);
303    }
304   
 
305  11 toggle public static void write(Source input, Result output, boolean indent)
306    throws TransformerConfigurationException,
307    TransformerFactoryConfigurationError,
308    TransformerException
309    {
310  11 boolean omitxmldeclaration = true;
311  11 Transformer idTransform = TransformerFactory
312    .newInstance()
313    .newTransformer();
314  11 if (omitxmldeclaration) {
315  11 idTransform.setOutputProperty("omit-xml-declaration", "yes");
316    }
317  11 idTransform.setOutputProperty("encoding", "UTF-8");
318  11 if (indent) {
319  6 idTransform.setOutputProperty("indent", "yes");
320  6 try {
321  6 idTransform.setOutputProperty(
322    "{http://xml.apache.org/xslt}indent-amount",
323    "4");
324    } catch (Exception e) {
325    //
326    }
327    }
328  11 idTransform.transform(input, output);
329    }
330   
 
331  0 toggle public static void write(Source input, Writer writer)
332    throws TransformerConfigurationException,
333    TransformerFactoryConfigurationError,
334    TransformerException
335    {
336  0 Result output = new StreamResult(writer);
337  0 write(input, output);
338    }
339    }