1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@version |
59 |
|
@since |
60 |
|
|
|
|
| 29.3% |
Uncovered Elements: 87 (123) |
Complexity: 34 |
Complexity Density: 0.4 |
|
61 |
|
public class XmlUtil |
62 |
|
{ |
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
63 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
0 |
public static void formatXML(Document xml, Document xsl, Writer output)... |
80 |
|
throws Exception |
81 |
|
{ |
82 |
0 |
formatXML(xml, xsl, null, output); |
83 |
|
} |
84 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
85 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
109 |
0 |
public static void formatXML(Reader xml, Reader xsl, Writer output)... |
110 |
|
throws Exception |
111 |
|
{ |
112 |
0 |
formatXML(xml, xsl, null, output); |
113 |
|
} |
114 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
115 |
0 |
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 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
@return |
140 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
141 |
5 |
private static DocumentBuilder getDocumentBuilder()... |
142 |
|
throws ParserConfigurationException |
143 |
|
{ |
144 |
5 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
145 |
5 |
factory.setNamespaceAware(true); |
146 |
5 |
try { |
147 |
5 |
factory.setFeature("http://xml.org/sax/features/namespaces", true); |
148 |
|
} catch (Throwable t) { |
149 |
|
|
150 |
|
} |
151 |
5 |
DocumentBuilder builder = factory.newDocumentBuilder(); |
152 |
5 |
return builder; |
153 |
|
} |
154 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 7 |
Complexity Density: 0.78 |
|
155 |
0 |
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 |
|
|
177 |
|
|
178 |
|
|
179 |
|
@param |
180 |
|
@param |
181 |
|
@return |
182 |
|
|
183 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
184 |
0 |
@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 |
|
|
193 |
|
|
194 |
|
@return |
195 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
196 |
5 |
public static Document newDocument() throws ParserConfigurationException... |
197 |
|
{ |
198 |
5 |
DocumentBuilder builder = getDocumentBuilder(); |
199 |
5 |
return builder.newDocument(); |
200 |
|
} |
201 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
202 |
0 |
public static Element newTemplate(String xml) throws Exception... |
203 |
|
{ |
204 |
0 |
Document doc = readXML(xml); |
205 |
0 |
return doc.getDocumentElement(); |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
@param |
213 |
|
@return |
214 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
215 |
0 |
public static Document readXML(InputStream input) throws Exception... |
216 |
|
{ |
217 |
0 |
return readXML(new InputStreamReader(input, "UTF-8")); |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
@param |
225 |
|
@return |
226 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
227 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
239 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
248 |
0 |
public static String write(Document doc) throws Exception... |
249 |
|
{ |
250 |
0 |
return write(doc.getDocumentElement()); |
251 |
|
} |
252 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
253 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
260 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
266 |
0 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
275 |
6 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
288 |
1 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
297 |
6 |
public static void write(Source input, Result output)... |
298 |
|
throws TransformerConfigurationException, |
299 |
|
TransformerFactoryConfigurationError, |
300 |
|
TransformerException |
301 |
|
{ |
302 |
6 |
write(input, output, true); |
303 |
|
} |
304 |
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
305 |
11 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
331 |
0 |
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 |
|
} |