1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.internal.xml; |
21 |
|
|
22 |
|
import java.io.ByteArrayOutputStream; |
23 |
|
import java.io.IOException; |
24 |
|
import java.io.InputStream; |
25 |
|
import java.io.Reader; |
26 |
|
import java.util.EmptyStackException; |
27 |
|
|
28 |
|
import org.apache.commons.codec.binary.Base64OutputStream; |
29 |
|
import org.apache.commons.io.IOUtils; |
30 |
|
import org.dom4j.Document; |
31 |
|
import org.dom4j.Element; |
32 |
|
import org.dom4j.io.OutputFormat; |
33 |
|
import org.xml.sax.Attributes; |
34 |
|
import org.xml.sax.SAXException; |
35 |
|
|
36 |
|
|
37 |
|
@link |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
@link@link |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@version |
90 |
|
|
|
|
| 37.9% |
Uncovered Elements: 36 (58) |
Complexity: 21 |
Complexity Density: 0.62 |
|
91 |
|
public class DOMXMLWriter extends XMLWriter |
92 |
|
{ |
93 |
|
|
94 |
|
@link |
95 |
|
|
96 |
|
private Document doc; |
97 |
|
|
98 |
|
|
99 |
|
@link |
100 |
|
|
101 |
|
private OutputFormat format; |
102 |
|
|
103 |
|
|
104 |
|
@link |
105 |
|
|
106 |
|
|
107 |
|
@param@link |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
109 |
2 |
public DOMXMLWriter(Document doc)... |
110 |
|
{ |
111 |
2 |
this.format = DEFAULT_FORMAT; |
112 |
2 |
this.doc = doc; |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
@link |
117 |
|
@link |
118 |
|
|
119 |
|
@param@link |
120 |
|
@param@link |
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
122 |
1 |
public DOMXMLWriter(Document doc, OutputFormat format)... |
123 |
|
{ |
124 |
1 |
this.format = format; |
125 |
1 |
this.doc = doc; |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
@inheritDoc |
130 |
|
|
131 |
|
@link |
132 |
|
@link |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
@see |
137 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
138 |
0 |
@Override... |
139 |
|
public void write(Element element, InputStream is) throws IOException |
140 |
|
{ |
141 |
0 |
element.addText(IOUtils.toString(is, this.format.getEncoding())); |
142 |
0 |
write(element); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
@inheritDoc |
147 |
|
|
148 |
|
@link |
149 |
|
@link |
150 |
|
|
151 |
|
|
152 |
|
@see |
153 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
154 |
0 |
@Override... |
155 |
|
public void write(Element element, Reader rd) throws IOException |
156 |
|
{ |
157 |
0 |
element.addText(IOUtils.toString(rd)); |
158 |
0 |
write(element); |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
@inheritDoc |
163 |
|
|
164 |
|
@link |
165 |
|
|
166 |
|
@link |
167 |
|
|
168 |
|
|
169 |
|
@see |
170 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
171 |
0 |
@Override... |
172 |
|
public void writeBase64(Element element, InputStream is) throws IOException |
173 |
|
{ |
174 |
0 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
175 |
0 |
Base64OutputStream out = new Base64OutputStream(baos, true, 0, null); |
176 |
0 |
IOUtils.copy(is, out); |
177 |
0 |
out.close(); |
178 |
0 |
element.addText(baos.toString(this.format.getEncoding())); |
179 |
0 |
write(element); |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
@inheritDoc |
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
@see |
189 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
190 |
0 |
@Override... |
191 |
|
public void writeDocumentEnd(Document doc) throws IOException |
192 |
|
{ |
193 |
0 |
if (!this.parent.isEmpty()) { |
194 |
0 |
writeClose(this.parent.firstElement()); |
195 |
|
} |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
|
@inheritDoc |
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
@see |
205 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
206 |
0 |
@Override... |
207 |
|
public void writeDocumentStart(Document doc) throws IOException |
208 |
|
{ |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
@inheritDoc |
213 |
|
|
214 |
|
@link |
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
@see |
219 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
220 |
4 |
@Override... |
221 |
|
public void write(Element element) throws IOException |
222 |
|
{ |
223 |
4 |
if (this.parent.isEmpty()) { |
224 |
3 |
this.doc.setRootElement((Element) element.clone()); |
225 |
|
} else { |
226 |
1 |
this.parent.peek().add((Element) element.clone()); |
227 |
|
} |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
@inheritDoc |
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
@see |
237 |
|
|
|
|
| 57.1% |
Uncovered Elements: 3 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
238 |
1 |
@Override... |
239 |
|
public void writeClose(Element element) throws IOException |
240 |
|
{ |
241 |
1 |
try { |
242 |
1 |
while (this.parent.peek() != element) { |
243 |
0 |
this.parent.pop(); |
244 |
|
} |
245 |
1 |
this.parent.pop(); |
246 |
|
} catch (EmptyStackException e) { |
247 |
0 |
throw new IOException("FATAL: Closing a element that have never been opened"); |
248 |
|
} |
249 |
|
} |
250 |
|
|
251 |
|
|
252 |
|
@inheritDoc |
253 |
|
|
254 |
|
@link |
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
@see |
259 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
260 |
1 |
@Override... |
261 |
|
public void writeOpen(Element element) throws IOException |
262 |
|
{ |
263 |
1 |
if (this.parent.isEmpty()) { |
264 |
1 |
this.doc.setRootElement(element); |
265 |
|
} else { |
266 |
0 |
this.parent.add(element); |
267 |
|
} |
268 |
|
|
269 |
1 |
this.parent.push(element); |
270 |
|
} |
271 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
272 |
0 |
@Override... |
273 |
|
public void startCDATA() throws SAXException |
274 |
|
{ |
275 |
0 |
throw new SAXUnsupportedException(); |
276 |
|
} |
277 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
278 |
0 |
@Override... |
279 |
|
public void startElement(String namespaceURI, String localName, String qName, Attributes attributes) |
280 |
|
throws SAXException |
281 |
|
{ |
282 |
0 |
throw new SAXUnsupportedException(); |
283 |
|
} |
284 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
285 |
0 |
@Override... |
286 |
|
public void startEntity(String name) throws SAXException |
287 |
|
{ |
288 |
0 |
throw new SAXUnsupportedException(); |
289 |
|
} |
290 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
291 |
0 |
@Override... |
292 |
|
public void endCDATA() throws SAXException |
293 |
|
{ |
294 |
0 |
throw new SAXUnsupportedException(); |
295 |
|
} |
296 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
297 |
0 |
@Override... |
298 |
|
public void endElement(String namespaceURI, String localName, String qName) throws SAXException |
299 |
|
{ |
300 |
0 |
throw new SAXUnsupportedException(); |
301 |
|
} |
302 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
303 |
0 |
@Override... |
304 |
|
public void endEntity(String name) throws SAXException |
305 |
|
{ |
306 |
0 |
throw new SAXUnsupportedException(); |
307 |
|
} |
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
312 |
|
public class SAXUnsupportedException extends RuntimeException |
313 |
|
{ |
314 |
|
|
315 |
|
|
316 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
317 |
0 |
public SAXUnsupportedException()... |
318 |
|
{ |
319 |
0 |
super("SAX api is not supported"); |
320 |
|
} |
321 |
|
} |
322 |
|
} |