1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.filter.xml.internal.output

File FilterStreamXMLStreamWriter.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart6.png
72% of files have more coverage

Code metrics

10
56
16
1
231
152
34
0.61
3.5
16
2.12

Classes

Class Line # Actions
FilterStreamXMLStreamWriter 37 56 0% 34 33
0.59756159.8%
 

Contributing tests

This file is covered by 32 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.filter.xml.internal.output;
21   
22    import java.io.OutputStream;
23   
24    import javax.xml.stream.XMLOutputFactory;
25    import javax.xml.stream.XMLStreamException;
26    import javax.xml.stream.XMLStreamWriter;
27   
28    import org.xwiki.filter.FilterException;
29    import org.xwiki.filter.xml.output.XMLOutputProperties;
30   
31    import javanet.staxutils.IndentingXMLStreamWriter;
32   
33    /**
34    * @version $Id: 9e91755a671d8f199d32f01366f07a2bb3c3d1ec $
35    * @since 6.2M1
36    */
 
37    public class FilterStreamXMLStreamWriter
38    {
39    private static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newInstance();
40   
41    private final XMLStreamWriter writer;
42   
43    private final boolean printNullValue;
44   
 
45  0 toggle public FilterStreamXMLStreamWriter(XMLStreamWriter writer, boolean printNullValue)
46    {
47  0 this.writer = writer;
48  0 this.printNullValue = printNullValue;
49    }
50   
 
51  194 toggle public FilterStreamXMLStreamWriter(OutputStream outputStream, String encoding, boolean format,
52    boolean printNullValue) throws FilterException
53    {
54  194 try {
55  194 XMLStreamWriter streamWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(outputStream, encoding);
56   
57  194 if (format) {
58  194 this.writer = new IndentingXMLStreamWriter(streamWriter);
59    } else {
60  0 this.writer = streamWriter;
61    }
62    } catch (Exception e) {
63  0 throw new FilterException("Failed to create XML writer", e);
64    }
65   
66  194 this.printNullValue = printNullValue;
67    }
68   
 
69  15166 toggle public FilterStreamXMLStreamWriter(XMLOutputProperties properties, boolean printNullValue) throws FilterException
70    {
71  15166 try {
72  15166 this.writer = XMLOutputFilterStreamUtils.createXMLStreamWriter(properties);
73    } catch (Exception e) {
74  0 throw new FilterException("Failed to create XML writer", e);
75    }
76   
77  15166 this.printNullValue = printNullValue;
78    }
79   
 
80  112039 toggle public XMLStreamWriter getWriter()
81    {
82  112039 return this.writer;
83    }
84   
85    //
86   
87    /**
88    * Write the XML Declaration. Defaults the XML version to 1.0, and the encoding to utf-8.
89    *
90    * @throws FilterException
91    */
 
92  0 toggle public void writeStartDocument() throws FilterException
93    {
94  0 try {
95  0 this.writer.writeStartDocument();
96    } catch (XMLStreamException e) {
97  0 throw new FilterException("Failed to write start document", e);
98    }
99    }
100   
101    /**
102    * Write the XML Declaration.
103    *
104    * @param encoding the XML version
105    * @param version the XML encoding
106    * @throws FilterException
107    */
 
108  13473 toggle public void writeStartDocument(String encoding, String version) throws FilterException
109    {
110  13473 try {
111  13473 this.writer.writeStartDocument(encoding, version);
112    } catch (XMLStreamException e) {
113  0 throw new FilterException("Failed to write start document", e);
114    }
115    }
116   
117    /**
118    * Closes any start tags and writes corresponding end tags.
119    *
120    * @throws FilterException
121    */
 
122  13473 toggle public void writeEndDocument() throws FilterException
123    {
124  13473 try {
125  13473 this.writer.writeEndDocument();
126    } catch (XMLStreamException e) {
127  0 throw new FilterException("Failed to write end document", e);
128    }
129    }
130   
 
131  416107 toggle public void writeEmptyElement(String localName) throws FilterException
132    {
133  416107 try {
134  416106 this.writer.writeEmptyElement(localName);
135    } catch (XMLStreamException e) {
136  0 throw new FilterException(String.format("Failed to write empty element [%s]", localName), e);
137    }
138    }
139   
 
140  2374130 toggle public void writeElement(String localName, String value) throws FilterException
141    {
142  2374131 if (value != null) {
143  2374134 if (value.isEmpty()) {
144  416107 writeEmptyElement(localName);
145    } else {
146  1958027 writeStartElement(localName);
147  1958024 writeCharacters(value);
148  1958028 writeEndElement();
149    }
150  0 } else if (this.printNullValue) {
151  0 writeEmptyElement(localName);
152    }
153    }
154   
 
155  1967004 toggle public void writeCharacters(String text) throws FilterException
156    {
157  1967004 try {
158  1967004 this.writer.writeCharacters(text);
159    } catch (XMLStreamException e) {
160  0 throw new FilterException(String.format("Failed to write characters [%s]", text), e);
161    }
162    }
163   
 
164  2411362 toggle public void writeStartElement(String localName) throws FilterException
165    {
166  2411362 try {
167  2411361 this.writer.writeStartElement(localName);
168    } catch (XMLStreamException e) {
169  0 throw new FilterException(String.format("Failed to write start element [%s]", localName), e);
170    }
171    }
172   
 
173  2411359 toggle public void writeEndElement() throws FilterException
174    {
175  2411359 try {
176  2411359 this.writer.writeEndElement();
177    } catch (XMLStreamException e) {
178  0 throw new FilterException("Failed to write end element", e);
179    }
180    }
181   
 
182  40419 toggle public void writeAttribute(String localName, String value) throws FilterException
183    {
184  40419 if (value != null) {
185  40419 try {
186  40418 this.writer.writeAttribute(localName, value);
187    } catch (XMLStreamException e) {
188  0 throw new FilterException(
189    String.format("Failed to write attribute [%s] with value [%s]", localName, value), e);
190    }
191    }
192    }
193   
 
194  0 toggle public void writeCharacters(char[] text, int start, int len) throws FilterException
195    {
196  0 try {
197  0 this.writer.writeCharacters(text, start, len);
198    } catch (XMLStreamException e) {
199  0 throw new FilterException("Failed to write characters", e);
200    }
201    }
202   
203    /**
204    * Close this writer and free any resources associated with the writer. This must not close the underlying output
205    * stream.
206    *
207    * @throws FilterException
208    */
 
209  15360 toggle public void close() throws FilterException
210    {
211  15360 try {
212  15360 this.writer.close();
213    } catch (XMLStreamException e) {
214  0 throw new FilterException("Failed to close writer", e);
215    }
216    }
217   
218    /**
219    * Write any cached data to the underlying output mechanism.
220    *
221    * @throws FilterException
222    */
 
223  0 toggle public void flush() throws FilterException
224    {
225  0 try {
226  0 this.writer.flush();
227    } catch (XMLStreamException e) {
228  0 throw new FilterException("Failed to flush writer", e);
229    }
230    }
231    }