1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.store.serialization.xml.internal; |
21 |
|
|
22 |
|
import java.io.FilterWriter; |
23 |
|
import java.io.IOException; |
24 |
|
import java.io.InputStream; |
25 |
|
import java.io.OutputStream; |
26 |
|
import java.io.Reader; |
27 |
|
import java.io.UnsupportedEncodingException; |
28 |
|
import java.io.Writer; |
29 |
|
import java.util.Stack; |
30 |
|
|
31 |
|
import org.apache.commons.codec.binary.Base64OutputStream; |
32 |
|
import org.apache.commons.io.IOUtils; |
33 |
|
import org.apache.commons.io.output.CloseShieldOutputStream; |
34 |
|
import org.dom4j.Document; |
35 |
|
import org.dom4j.Element; |
36 |
|
import org.dom4j.io.OutputFormat; |
37 |
|
|
38 |
|
|
39 |
|
@link |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
@link |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@version |
55 |
|
@since |
56 |
|
|
|
|
| 67.2% |
Uncovered Elements: 21 (64) |
Complexity: 16 |
Complexity Density: 0.36 |
|
57 |
|
public class XMLWriter extends org.dom4j.io.XMLWriter |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private static final int BASE64_WIDTH = 80; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
private static final byte[] NEWLINE; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
private static final char CLOSE_ANGLE_BRACKET = '>'; |
73 |
|
|
74 |
|
|
75 |
|
@link@link |
76 |
|
@link |
77 |
|
|
78 |
|
@link |
79 |
|
|
80 |
|
protected Stack<Element> parent = new Stack<Element>(); |
81 |
|
|
82 |
|
|
83 |
|
@link |
84 |
|
|
85 |
|
private OutputStream out; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
private LastCharWriter lcWriter; |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
96 |
2 |
static {... |
97 |
2 |
try { |
98 |
2 |
NEWLINE = System.getProperty("line.separator").getBytes("UTF-8"); |
99 |
|
} catch (UnsupportedEncodingException e) { |
100 |
0 |
throw new RuntimeException("No UTF-8, this Java VM is not standards compliant!", e); |
101 |
|
} |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
@link |
106 |
|
|
107 |
|
@see |
108 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
109 |
0 |
protected XMLWriter()... |
110 |
|
{ |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
@link |
120 |
|
|
121 |
|
|
122 |
|
@param@link |
123 |
|
@param@link |
124 |
|
|
125 |
|
@throws |
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
127 |
9 |
public XMLWriter(final OutputStream out, final OutputFormat format)... |
128 |
|
throws UnsupportedEncodingException |
129 |
|
{ |
130 |
9 |
super(out, format); |
131 |
9 |
this.lcWriter = new LastCharWriter(super.writer); |
132 |
9 |
super.writer = this.lcWriter; |
133 |
9 |
this.out = out; |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
|
@link@link |
138 |
|
|
139 |
|
|
140 |
|
@param@link |
141 |
|
@link |
142 |
|
@throws |
143 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
144 |
0 |
public void writeDocumentStart(final Document doc) throws IOException... |
145 |
|
{ |
146 |
0 |
writeDeclaration(); |
147 |
|
|
148 |
0 |
if (doc.getDocType() != null) { |
149 |
0 |
super.indent(); |
150 |
0 |
super.writeDocType(doc.getDocType()); |
151 |
|
} |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
@link |
157 |
|
|
158 |
|
|
159 |
|
@link |
160 |
|
|
161 |
|
@param@link |
162 |
|
@throws |
163 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
164 |
0 |
public void writeDocumentEnd(final Document doc) throws IOException... |
165 |
|
{ |
166 |
0 |
if (!this.parent.isEmpty()) { |
167 |
0 |
this.writeClose(this.parent.firstElement()); |
168 |
|
} |
169 |
0 |
super.writePrintln(); |
170 |
0 |
super.flush(); |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
@link@link |
175 |
|
@link |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
@param@link |
183 |
|
@param@link |
184 |
|
|
185 |
|
@throws |
186 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
187 |
1 |
public void write(final Element element, final Reader rd) throws IOException... |
188 |
|
{ |
189 |
1 |
this.writeOpen(element); |
190 |
1 |
IOUtils.copy(rd, this.lcWriter); |
191 |
1 |
this.writeClose(element); |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
@link@link |
196 |
|
|
197 |
|
@link |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
@param@link |
204 |
|
@param@link |
205 |
|
|
206 |
|
@throws |
207 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
208 |
0 |
public void write(final Element element, final InputStream is) throws IOException... |
209 |
|
{ |
210 |
0 |
this.writeOpen(element); |
211 |
0 |
super.flush(); |
212 |
0 |
IOUtils.copy(is, this.out); |
213 |
|
|
214 |
|
|
215 |
|
|
216 |
0 |
super.writeClose(element); |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
@link@link |
221 |
|
|
222 |
|
@link |
223 |
|
|
224 |
|
@param@link |
225 |
|
@param@link |
226 |
|
|
227 |
|
@throws |
228 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
229 |
1 |
public void writeBase64(final Element element, final InputStream is) throws IOException... |
230 |
|
{ |
231 |
1 |
this.writeOpen(element); |
232 |
1 |
super.writePrintln(); |
233 |
|
|
234 |
1 |
super.flush(); |
235 |
1 |
final Base64OutputStream base64 = |
236 |
|
new Base64OutputStream(new CloseShieldOutputStream(this.out), true, BASE64_WIDTH, NEWLINE); |
237 |
1 |
IOUtils.copy(is, base64); |
238 |
1 |
base64.close(); |
239 |
|
|
240 |
|
|
241 |
1 |
super.setIndentLevel(this.parent.size() - 1); |
242 |
1 |
super.indent(); |
243 |
|
|
244 |
1 |
this.writeClose(element); |
245 |
|
} |
246 |
|
|
247 |
|
|
248 |
|
@link |
249 |
|
@link |
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
@param@link |
255 |
|
@throws |
256 |
|
@see |
257 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
258 |
27 |
@Override... |
259 |
|
public void writeOpen(final Element element) throws IOException |
260 |
|
{ |
261 |
27 |
if (this.lcWriter.getLastChar() == CLOSE_ANGLE_BRACKET) { |
262 |
19 |
super.writePrintln(); |
263 |
19 |
super.indent(); |
264 |
|
} |
265 |
27 |
super.writeOpen(element); |
266 |
27 |
this.parent.push(element); |
267 |
27 |
super.setIndentLevel(this.parent.size()); |
268 |
|
} |
269 |
|
|
270 |
|
|
271 |
|
@link |
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
@param@link |
279 |
|
@throws |
280 |
|
@see |
281 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
282 |
27 |
@Override... |
283 |
|
public void writeClose(final Element element) throws IOException |
284 |
|
{ |
285 |
32 |
while (!this.parent.peek().getQualifiedName().equals(element.getQualifiedName())) { |
286 |
5 |
this.writeClose(this.parent.peek()); |
287 |
|
} |
288 |
|
|
289 |
27 |
super.setIndentLevel(this.parent.size() - 1); |
290 |
27 |
if (this.lcWriter.getLastChar() == CLOSE_ANGLE_BRACKET) { |
291 |
25 |
super.writePrintln(); |
292 |
25 |
super.indent(); |
293 |
|
} |
294 |
|
|
295 |
27 |
super.writeClose(this.parent.pop()); |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 5 |
Complexity Density: 0.62 |
|
301 |
|
private static class LastCharWriter extends FilterWriter |
302 |
|
{ |
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
private char lastChar; |
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
@param |
312 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
313 |
9 |
LastCharWriter(final Writer toWrap)... |
314 |
|
{ |
315 |
9 |
super(toWrap); |
316 |
|
} |
317 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
318 |
1 |
@Override... |
319 |
|
public void write(final char[] buffer, final int offset, final int count) |
320 |
|
throws IOException |
321 |
|
{ |
322 |
1 |
super.write(buffer, offset, count); |
323 |
1 |
this.lastChar = buffer[offset + count - 1]; |
324 |
|
} |
325 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
326 |
1197 |
@Override... |
327 |
|
public void write(final String str, final int offset, final int count) |
328 |
|
throws IOException |
329 |
|
{ |
330 |
1197 |
super.write(str, offset, count); |
331 |
1197 |
this.lastChar = str.charAt(offset + count - 1); |
332 |
|
} |
333 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
334 |
34 |
@Override... |
335 |
|
public void write(final int oneChar) throws IOException |
336 |
|
{ |
337 |
34 |
super.write(oneChar); |
338 |
34 |
this.lastChar = (char) oneChar; |
339 |
|
} |
340 |
|
|
341 |
|
|
342 |
|
@return |
343 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
344 |
54 |
public char getLastChar()... |
345 |
|
{ |
346 |
54 |
return lastChar; |
347 |
|
} |
348 |
|
} |
349 |
|
} |