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.ByteArrayInputStream; |
23 |
|
import java.io.ByteArrayOutputStream; |
24 |
|
import java.io.IOException; |
25 |
|
import java.io.InputStream; |
26 |
|
|
27 |
|
import org.dom4j.Document; |
28 |
|
import org.dom4j.DocumentException; |
29 |
|
import org.dom4j.io.OutputFormat; |
30 |
|
import org.dom4j.io.SAXReader; |
31 |
|
import org.xml.sax.SAXException; |
32 |
|
import org.xwiki.component.annotation.ComponentRole; |
33 |
|
import org.xwiki.store.serialization.xml.XMLSerializer; |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
@param |
39 |
|
@param |
40 |
|
@version |
41 |
|
@since |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
@ComponentRole |
|
|
| 86.4% |
Uncovered Elements: 3 (22) |
Complexity: 5 |
Complexity Density: 0.25 |
|
47 |
|
public abstract class AbstractXMLSerializer<R, P extends R> implements XMLSerializer<R, P> |
48 |
|
{ |
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0.25 |
|
49 |
6 |
@Override... |
50 |
|
public InputStream serialize(final R object) throws IOException |
51 |
|
{ |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
6 |
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
56 |
|
|
57 |
6 |
final XMLWriter writer; |
58 |
6 |
try { |
59 |
|
|
60 |
6 |
final OutputFormat of = new OutputFormat(" ", true, "UTF-8"); |
61 |
6 |
writer = new XMLWriter(baos, of); |
62 |
6 |
writer.startDocument(); |
63 |
|
} catch (SAXException e) { |
64 |
0 |
throw new IOException("Could not open the XML writer."); |
65 |
|
} |
66 |
|
|
67 |
6 |
this.serialize(object, writer); |
68 |
|
|
69 |
6 |
try { |
70 |
6 |
writer.endDocument(); |
71 |
|
} catch (SAXException e) { |
72 |
0 |
throw new IOException("Could not close the XML writer."); |
73 |
|
} |
74 |
|
|
75 |
6 |
return new ByteArrayInputStream(baos.toByteArray()); |
76 |
|
} |
77 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
78 |
7 |
@Override... |
79 |
|
public P parse(final InputStream stream) throws IOException |
80 |
|
{ |
81 |
7 |
final SAXReader reader = new SAXReader(); |
82 |
|
|
83 |
|
|
84 |
7 |
reader.setStripWhitespaceText(true); |
85 |
7 |
reader.setMergeAdjacentText(true); |
86 |
|
|
87 |
7 |
final Document domdoc; |
88 |
7 |
try { |
89 |
7 |
domdoc = reader.read(stream); |
90 |
|
} catch (DocumentException e) { |
91 |
0 |
throw new IOException("Failed to parse XML, probably malformed input."); |
92 |
|
} |
93 |
7 |
return this.parse(domdoc.getRootElement()); |
94 |
|
} |
95 |
|
} |