1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
45 |
|
|
46 |
|
@version |
47 |
|
@since |
48 |
|
|
49 |
|
@Component |
50 |
|
@Named("xml") |
51 |
|
@Singleton |
|
|
| 85.9% |
Uncovered Elements: 9 (64) |
Complexity: 26 |
Complexity Density: 0.72 |
|
52 |
|
public class XMLScriptService implements ScriptService |
53 |
|
{ |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@Inject |
58 |
|
private Logger logger; |
59 |
|
|
60 |
|
|
61 |
|
private DOMImplementationLS lsImpl; |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
66 |
27 |
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 |
|
|
77 |
|
|
78 |
|
|
79 |
|
@param |
80 |
|
@return |
81 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
82 |
0 |
public static String escape(Object content)... |
83 |
|
{ |
84 |
0 |
return XMLUtils.escape(content); |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
@param |
92 |
|
@return |
93 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
94 |
0 |
public static String escapeForAttributeValue(Object content)... |
95 |
|
{ |
96 |
0 |
return XMLUtils.escapeAttributeValue(content); |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@param |
104 |
|
@return |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
0 |
public static String escapeForElementContent(Object content)... |
107 |
|
{ |
108 |
0 |
return XMLUtils.escapeElementContent(content); |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
@param |
116 |
|
@return |
117 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
118 |
0 |
public static String unescape(Object content)... |
119 |
|
{ |
120 |
0 |
return XMLUtils.unescape(content); |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
@return |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
128 |
4 |
public Document createDOMDocument()... |
129 |
|
{ |
130 |
4 |
return XMLUtils.createDOMDocument(); |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
@param |
137 |
|
@return |
138 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
139 |
25 |
public Document parse(LSInput source)... |
140 |
|
{ |
141 |
25 |
return XMLUtils.parse(source); |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
@param |
148 |
|
@return |
149 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
150 |
4 |
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 |
|
|
162 |
|
|
163 |
|
@param |
164 |
|
@return |
165 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
166 |
21 |
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 |
|
|
178 |
|
|
179 |
|
@param |
180 |
|
@return |
181 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
182 |
2 |
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 |
|
|
194 |
|
|
195 |
|
@param |
196 |
|
@return |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
198 |
7 |
public String serialize(Node node)... |
199 |
|
{ |
200 |
7 |
return XMLUtils.serialize(node); |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
@param |
207 |
|
@param |
208 |
|
@return |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
210 |
7 |
public String serialize(Node node, boolean withXmlDeclaration)... |
211 |
|
{ |
212 |
7 |
return XMLUtils.serialize(node, withXmlDeclaration); |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
@param |
219 |
|
@param |
220 |
|
@return |
221 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
222 |
8 |
public String transform(Source xml, Source xslt)... |
223 |
|
{ |
224 |
8 |
return XMLUtils.transform(xml, xslt); |
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
@param |
231 |
|
@param |
232 |
|
@return |
233 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
234 |
6 |
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 |
|
|
244 |
|
|
245 |
|
@param |
246 |
|
@param |
247 |
|
@return |
248 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
249 |
4 |
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 |
|
|
260 |
|
|
261 |
|
@param |
262 |
|
@param |
263 |
|
@return |
264 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
265 |
4 |
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 |
|
} |