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

File XMLScriptService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

12
36
16
1
272
121
26
0.72
2.25
16
1.62

Classes

Class Line # Actions
XMLScriptService 52 36 0% 26 9
0.85937585.9%
 

Contributing tests

This file is covered by 27 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.xml.script;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.InputStream;
24    import java.io.StringReader;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29    import javax.xml.transform.Source;
30    import javax.xml.transform.dom.DOMSource;
31    import javax.xml.transform.stream.StreamSource;
32   
33    import org.slf4j.Logger;
34    import org.w3c.dom.Document;
35    import org.w3c.dom.Node;
36    import org.w3c.dom.bootstrap.DOMImplementationRegistry;
37    import org.w3c.dom.ls.DOMImplementationLS;
38    import org.w3c.dom.ls.LSInput;
39    import org.xwiki.component.annotation.Component;
40    import org.xwiki.script.service.ScriptService;
41    import org.xwiki.xml.XMLUtils;
42   
43    /**
44    * Provides Scripting APIs for handling XML.
45    *
46    * @version $Id: fb55a3bf545301e69e1d84280a0d889a1ab2685a $
47    * @since 2.7M1
48    */
49    @Component
50    @Named("xml")
51    @Singleton
 
52    public class XMLScriptService implements ScriptService
53    {
54    /**
55    * The logger to log.
56    */
57    @Inject
58    private Logger logger;
59   
60    /** Helper object for manipulating DOM Level 3 Load and Save APIs. */
61    private DOMImplementationLS lsImpl;
62   
63    /**
64    * Default component constructor.
65    */
 
66  27 toggle public XMLScriptService()
67    {
68  27 try {
69  27 this.lsImpl = (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS 3.0");
70    } catch (Exception ex) {
71  0 this.logger.warn("Cannot initialize the XML Script Service", ex);
72    }
73    }
74   
75    /**
76    * Escapes all the XML special characters in a <code>String</code> using numerical XML entities. Specifically,
77    * escapes &lt;, &gt;, ", ' and &amp;.
78    *
79    * @param content the text to escape, may be {@code null}
80    * @return a new escaped {@code String}, {@code null} if {@code null} input
81    */
 
82  0 toggle public static String escape(Object content)
83    {
84  0 return XMLUtils.escape(content);
85    }
86   
87    /**
88    * Escapes all the XML special characters in a <code>String</code> using numerical XML entities, so that the
89    * resulting string can safely be used as an XML attribute value. Specifically, escapes &lt;, &gt;, ", ' and &amp;.
90    *
91    * @param content the text to escape, may be {@code null}
92    * @return a new escaped {@code String}, {@code null} if {@code null} input
93    */
 
94  0 toggle public static String escapeForAttributeValue(Object content)
95    {
96  0 return XMLUtils.escapeAttributeValue(content);
97    }
98   
99    /**
100    * Escapes the XML special characters in a <code>String</code> using numerical XML entities, so that the resulting
101    * string can safely be used as an XML text node. Specifically, escapes &lt;, &gt;, and &amp;.
102    *
103    * @param content the text to escape, may be {@code null}
104    * @return a new escaped {@code String}, {@code null} if {@code null} input
105    */
 
106  0 toggle public static String escapeForElementContent(Object content)
107    {
108  0 return XMLUtils.escapeElementContent(content);
109    }
110   
111    /**
112    * Unescape encoded special XML characters. Only &gt;, &lt; &amp;, " and ' are unescaped, since they are the only
113    * ones that affect the resulting markup.
114    *
115    * @param content the text to decode, may be {@code null}
116    * @return unescaped content, {@code null} if {@code null} input
117    */
 
118  0 toggle public static String unescape(Object content)
119    {
120  0 return XMLUtils.unescape(content);
121    }
122   
123    /**
124    * Construct a new (empty) DOM Document and return it.
125    *
126    * @return an empty DOM Document
127    */
 
128  4 toggle public Document createDOMDocument()
129    {
130  4 return XMLUtils.createDOMDocument();
131    }
132   
133    /**
134    * Parse a DOM Document from a source.
135    *
136    * @param source the source input to parse
137    * @return the equivalent DOM Document, or {@code null} if the parsing failed.
138    */
 
139  25 toggle public Document parse(LSInput source)
140    {
141  25 return XMLUtils.parse(source);
142    }
143   
144    /**
145    * Parse a {@code byte[]} into a DOM Document.
146    *
147    * @param content the content to parse
148    * @return a DOM Document corresponding to the input, {@code null} if the content can't be parsed successfully
149    */
 
150  4 toggle public Document parse(byte[] content)
151    {
152  4 if (content == null) {
153  1 return null;
154    }
155  3 LSInput input = this.lsImpl.createLSInput();
156  3 input.setByteStream(new ByteArrayInputStream(content));
157  3 return parse(input);
158    }
159   
160    /**
161    * Parse a {@code String} into a DOM Document.
162    *
163    * @param content the content to parse
164    * @return a DOM Document corresponding to the input, {@code null} if the content can't be parsed successfully
165    */
 
166  21 toggle public Document parse(String content)
167    {
168  21 if (content == null) {
169  1 return null;
170    }
171  20 LSInput input = this.lsImpl.createLSInput();
172  20 input.setCharacterStream(new StringReader(content));
173  20 return parse(input);
174    }
175   
176    /**
177    * Parse an {@code InputStream} into a DOM Document.
178    *
179    * @param stream the content input to parse
180    * @return a DOM Document corresponding to the input, {@code null} if the content can't be parsed successfully
181    */
 
182  2 toggle public Document parse(InputStream stream)
183    {
184  2 if (stream == null) {
185  1 return null;
186    }
187  1 LSInput input = this.lsImpl.createLSInput();
188  1 input.setByteStream(stream);
189  1 return parse(input);
190    }
191   
192    /**
193    * Serialize a DOM Node into a string, including the XML declaration at the start.
194    *
195    * @param node the node to export
196    * @return the serialized node, or an empty string if the serialization fails
197    */
 
198  7 toggle public String serialize(Node node)
199    {
200  7 return XMLUtils.serialize(node);
201    }
202   
203    /**
204    * Serialize a DOM Node into a string, with an optional XML declaration at the start.
205    *
206    * @param node the node to export
207    * @param withXmlDeclaration whether to output the XML declaration or not
208    * @return the serialized node, or an empty string if the serialization fails or the node is {@code null}
209    */
 
210  7 toggle public String serialize(Node node, boolean withXmlDeclaration)
211    {
212  7 return XMLUtils.serialize(node, withXmlDeclaration);
213    }
214   
215    /**
216    * Apply an XSLT transformation to a Document.
217    *
218    * @param xml the document to transform
219    * @param xslt the stylesheet to apply
220    * @return the transformation result, or {@code null} if an error occurs or {@code null} xml or xslt input
221    */
 
222  8 toggle public String transform(Source xml, Source xslt)
223    {
224  8 return XMLUtils.transform(xml, xslt);
225    }
226   
227    /**
228    * Apply an XSLT transformation to a Document, both given as DOM Documents.
229    *
230    * @param xml the document to transform
231    * @param xslt the stylesheet to apply
232    * @return the transformation result, or {@code null} if an error occurs or {@code null} xml or xslt input
233    */
 
234  6 toggle public String transform(Document xml, Document xslt)
235    {
236  6 if (xml == null || xslt == null) {
237  3 return null;
238    }
239  3 return transform(new DOMSource(xml), new DOMSource(xslt));
240    }
241   
242    /**
243    * Apply an XSLT transformation to a Document, both given as byte arrays.
244    *
245    * @param xml the document to transform
246    * @param xslt the stylesheet to apply
247    * @return the transformation result, or {@code null} if an error occurs or {@code null} xml or xslt input
248    */
 
249  4 toggle public String transform(byte[] xml, byte[] xslt)
250    {
251  4 if (xml == null || xslt == null) {
252  3 return null;
253    }
254  1 return transform(new StreamSource(new ByteArrayInputStream(xml)),
255    new StreamSource(new ByteArrayInputStream(xslt)));
256    }
257   
258    /**
259    * Apply an XSLT transformation to a Document, both given as strings.
260    *
261    * @param xml the document to transform
262    * @param xslt the stylesheet to apply
263    * @return the transformation result, or {@code null} if an error occurs or {@code null} xml or xslt input
264    */
 
265  4 toggle public String transform(String xml, String xslt)
266    {
267  4 if (xml == null || xslt == null) {
268  3 return null;
269    }
270  1 return transform(new StreamSource(new StringReader(xml)), new StreamSource(new StringReader(xslt)));
271    }
272    }