1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.filter.xml.internal.output

File FilterStreamXMLStreamWriter.java

 

Coverage histogram

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

Code metrics

10
56
16
1
229
151
34
0.61
3.5
16
2.12

Classes

Class Line # Actions
FilterStreamXMLStreamWriter 37 56 0% 34 36
0.560975656.1%
 

Contributing tests

This file is covered by 29 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: 9931a760c44f1c1f193af270dbc928942e9d432f $
35    * @since 6.2M1
36    */
 
37    public class FilterStreamXMLStreamWriter
38    {
39    private final XMLStreamWriter writer;
40   
41    private final boolean printNullValue;
42   
 
43  0 toggle public FilterStreamXMLStreamWriter(XMLStreamWriter writer, boolean printNullValue)
44    {
45  0 this.writer = writer;
46  0 this.printNullValue = printNullValue;
47    }
48   
 
49  0 toggle public FilterStreamXMLStreamWriter(OutputStream outputStream, String encoding, boolean format,
50    boolean printNullValue) throws FilterException
51    {
52  0 try {
53  0 XMLStreamWriter streamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream, encoding);
54   
55  0 if (format) {
56  0 this.writer = new IndentingXMLStreamWriter(streamWriter);
57    } else {
58  0 this.writer = streamWriter;
59    }
60    } catch (Exception e) {
61  0 throw new FilterException("Failed to create XML writer", e);
62    }
63   
64  0 this.printNullValue = printNullValue;
65    }
66   
 
67  7604 toggle public FilterStreamXMLStreamWriter(XMLOutputProperties properties, boolean printNullValue) throws FilterException
68    {
69  7604 try {
70  7604 this.writer = XMLOutputFilterStreamUtils.createXMLStreamWriter(properties);
71    } catch (Exception e) {
72  0 throw new FilterException("Failed to create XML writer", e);
73    }
74   
75  7604 this.printNullValue = printNullValue;
76    }
77   
 
78  29763 toggle public XMLStreamWriter getWriter()
79    {
80  29763 return this.writer;
81    }
82   
83    //
84   
85    /**
86    * Write the XML Declaration. Defaults the XML version to 1.0, and the encoding to utf-8.
87    *
88    * @throws FilterException
89    */
 
90  0 toggle public void writeStartDocument() throws FilterException
91    {
92  0 try {
93  0 this.writer.writeStartDocument();
94    } catch (XMLStreamException e) {
95  0 throw new FilterException("Failed to write start document", e);
96    }
97    }
98   
99    /**
100    * Write the XML Declaration.
101    *
102    * @param encoding the XML version
103    * @param version the XML encoding
104    * @throws FilterException
105    */
 
106  6343 toggle public void writeStartDocument(String encoding, String version) throws FilterException
107    {
108  6343 try {
109  6343 this.writer.writeStartDocument(encoding, version);
110    } catch (XMLStreamException e) {
111  0 throw new FilterException("Failed to write start document", e);
112    }
113    }
114   
115    /**
116    * Closes any start tags and writes corresponding end tags.
117    *
118    * @throws FilterException
119    */
 
120  6343 toggle public void writeEndDocument() throws FilterException
121    {
122  6343 try {
123  6343 this.writer.writeEndDocument();
124    } catch (XMLStreamException e) {
125  0 throw new FilterException("Failed to write end document", e);
126    }
127    }
128   
 
129  122077 toggle public void writeEmptyElement(String localName) throws FilterException
130    {
131  122077 try {
132  122077 this.writer.writeEmptyElement(localName);
133    } catch (XMLStreamException e) {
134  0 throw new FilterException(String.format("Failed to write empty element [%s]", localName), e);
135    }
136    }
137   
 
138  898495 toggle public void writeElement(String localName, String value) throws FilterException
139    {
140  898495 if (value != null) {
141  898493 if (value.isEmpty()) {
142  122075 writeEmptyElement(localName);
143    } else {
144  776418 writeStartElement(localName);
145  776418 writeCharacters(value);
146  776418 writeEndElement();
147    }
148  2 } else if (this.printNullValue) {
149  2 writeEmptyElement(localName);
150    }
151    }
152   
 
153  776445 toggle public void writeCharacters(String text) throws FilterException
154    {
155  776445 try {
156  776445 this.writer.writeCharacters(text);
157    } catch (XMLStreamException e) {
158  0 throw new FilterException(String.format("Failed to write characters [%s]", text), e);
159    }
160    }
161   
 
162  928925 toggle public void writeStartElement(String localName) throws FilterException
163    {
164  928925 try {
165  928925 this.writer.writeStartElement(localName);
166    } catch (XMLStreamException e) {
167  0 throw new FilterException(String.format("Failed to write start element [%s]", localName), e);
168    }
169    }
170   
 
171  928925 toggle public void writeEndElement() throws FilterException
172    {
173  928925 try {
174  928925 this.writer.writeEndElement();
175    } catch (XMLStreamException e) {
176  0 throw new FilterException("Failed to write end element", e);
177    }
178    }
179   
 
180  19029 toggle public void writeAttribute(String localName, String value) throws FilterException
181    {
182  19029 if (value != null) {
183  19029 try {
184  19029 this.writer.writeAttribute(localName, value);
185    } catch (XMLStreamException e) {
186  0 throw new FilterException(
187    String.format("Failed to write attribute [%s] with value [%s]", localName, value), e);
188    }
189    }
190    }
191   
 
192  0 toggle public void writeCharacters(char[] text, int start, int len) throws FilterException
193    {
194  0 try {
195  0 this.writer.writeCharacters(text, start, len);
196    } catch (XMLStreamException e) {
197  0 throw new FilterException("Failed to write characters", e);
198    }
199    }
200   
201    /**
202    * Close this writer and free any resources associated with the writer. This must not close the underlying output
203    * stream.
204    *
205    * @throws FilterException
206    */
 
207  7604 toggle public void close() throws FilterException
208    {
209  7604 try {
210  7604 this.writer.close();
211    } catch (XMLStreamException e) {
212  0 throw new FilterException("Failed to close writer", e);
213    }
214    }
215   
216    /**
217    * Write any cached data to the underlying output mechanism.
218    *
219    * @throws FilterException
220    */
 
221  0 toggle public void flush() throws FilterException
222    {
223  0 try {
224  0 this.writer.flush();
225    } catch (XMLStreamException e) {
226  0 throw new FilterException("Failed to flush writer", e);
227    }
228    }
229    }