1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.filter.xml.internal.output; |
21 |
|
|
22 |
|
import java.io.Closeable; |
23 |
|
import java.io.IOException; |
24 |
|
|
25 |
|
import javax.xml.stream.FactoryConfigurationError; |
26 |
|
import javax.xml.stream.XMLOutputFactory; |
27 |
|
import javax.xml.stream.XMLStreamException; |
28 |
|
import javax.xml.stream.XMLStreamWriter; |
29 |
|
import javax.xml.transform.Result; |
30 |
|
import javax.xml.transform.stax.StAXResult; |
31 |
|
|
32 |
|
import org.xwiki.filter.FilterException; |
33 |
|
import org.xwiki.filter.output.OutputFilterStream; |
34 |
|
import org.xwiki.filter.output.OutputStreamOutputTarget; |
35 |
|
import org.xwiki.filter.output.OutputTarget; |
36 |
|
import org.xwiki.filter.output.WriterOutputTarget; |
37 |
|
import org.xwiki.filter.xml.output.ResultOutputTarget; |
38 |
|
import org.xwiki.filter.xml.output.XMLOutputProperties; |
39 |
|
|
40 |
|
import javanet.staxutils.IndentingXMLStreamWriter; |
41 |
|
|
42 |
|
|
43 |
|
@param |
44 |
|
@version |
45 |
|
@since |
46 |
|
|
|
|
| 80.5% |
Uncovered Elements: 8 (41) |
Complexity: 11 |
Complexity Density: 0.44 |
|
47 |
|
public abstract class AbstractXMLOutputFilterStream<P extends XMLOutputProperties> implements OutputFilterStream |
48 |
|
{ |
49 |
|
protected final P properties; |
50 |
|
|
51 |
|
protected final Result result; |
52 |
|
|
53 |
|
protected Object filter; |
54 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
55 |
25 |
public AbstractXMLOutputFilterStream(P properties) throws FilterException, XMLStreamException, IOException... |
56 |
|
{ |
57 |
25 |
this.properties = properties; |
58 |
25 |
this.result = createResult(this.properties); |
59 |
|
} |
60 |
|
|
|
|
| 78.3% |
Uncovered Elements: 5 (23) |
Complexity: 5 |
Complexity Density: 0.33 |
|
61 |
25 |
protected Result createResult(P properties) throws FilterException, XMLStreamException, IOException... |
62 |
|
{ |
63 |
25 |
OutputTarget target = properties.getTarget(); |
64 |
|
|
65 |
25 |
Result result; |
66 |
|
|
67 |
25 |
if (target instanceof ResultOutputTarget) { |
68 |
0 |
result = ((ResultOutputTarget) target).getResult(); |
69 |
|
} else { |
70 |
25 |
XMLOutputFactory factory = XMLOutputFactory.newInstance(); |
71 |
|
|
72 |
25 |
XMLStreamWriter xmlStreamWriter; |
73 |
|
|
74 |
25 |
if (target instanceof WriterOutputTarget) { |
75 |
24 |
xmlStreamWriter = factory.createXMLStreamWriter(((WriterOutputTarget) target).getWriter()); |
76 |
1 |
} else if (target instanceof OutputStreamOutputTarget) { |
77 |
1 |
xmlStreamWriter = |
78 |
|
factory.createXMLStreamWriter(((OutputStreamOutputTarget) target).getOutputStream(), |
79 |
|
properties.getEncoding()); |
80 |
|
} else { |
81 |
0 |
throw new FilterException("Unknown target type [" + target.getClass() + "]"); |
82 |
|
} |
83 |
|
|
84 |
25 |
if (properties.isFormat()) { |
85 |
25 |
xmlStreamWriter = new IndentingXMLStreamWriter(xmlStreamWriter); |
86 |
|
} |
87 |
|
|
88 |
25 |
result = new StAXResult(xmlStreamWriter); |
89 |
|
} |
90 |
|
|
91 |
25 |
return result; |
92 |
|
} |
93 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
94 |
25 |
@Override... |
95 |
|
public Object getFilter() throws FilterException |
96 |
|
{ |
97 |
25 |
if (this.filter == null) { |
98 |
25 |
try { |
99 |
25 |
this.filter = createFilter(this.properties); |
100 |
|
} catch (Exception e) { |
101 |
0 |
throw new FilterException("Failed to create filter", e); |
102 |
|
} |
103 |
|
} |
104 |
|
|
105 |
25 |
return this.filter; |
106 |
|
} |
107 |
|
|
108 |
|
protected abstract Object createFilter(P parameters) throws XMLStreamException, FactoryConfigurationError, |
109 |
|
FilterException; |
110 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
111 |
25 |
@Override... |
112 |
|
public void close() throws IOException |
113 |
|
{ |
114 |
25 |
if (this.filter instanceof Closeable) { |
115 |
25 |
((Closeable) this.filter).close(); |
116 |
|
} |
117 |
|
|
118 |
25 |
this.properties.getTarget().close(); |
119 |
|
} |
120 |
|
} |