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

File StAXUtils.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
66% of files have more coverage

Code metrics

22
35
7
1
192
91
18
0.51
5
7
2.57

Classes

Class Line # Actions
StAXUtils 43 35 0% 18 19
0.70312570.3%
 

Contributing tests

This file is covered by 300 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.xml.stax;
21   
22    import javax.xml.stream.XMLEventReader;
23    import javax.xml.stream.XMLInputFactory;
24    import javax.xml.stream.XMLOutputFactory;
25    import javax.xml.stream.XMLStreamException;
26    import javax.xml.stream.XMLStreamReader;
27    import javax.xml.stream.XMLStreamWriter;
28    import javax.xml.transform.Result;
29    import javax.xml.transform.Source;
30    import javax.xml.transform.sax.SAXResult;
31    import javax.xml.transform.stax.StAXResult;
32    import javax.xml.transform.stax.StAXSource;
33   
34    import javanet.staxutils.XMLEventStreamWriter;
35    import javanet.staxutils.XMLStreamEventReader;
36   
37    /**
38    * Various tools related to StAX API.
39    *
40    * @version $Id: 59091a430133922a9150207634e74d385ae1507d $
41    * @since 5.2M1
42    */
 
43    public final class StAXUtils
44    {
45    private static final XMLInputFactory XML_INPUT_FACTORY = XMLInputFactory.newInstance();
46   
47    private static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newInstance();
48   
49    /**
50    * Utility class.
51    */
 
52  0 toggle private StAXUtils()
53    {
54    }
55   
56    /**
57    * Extract or create an instance of {@link XMLStreamReader} from the provided {@link Source}.
58    *
59    * @param source the source
60    * @return the {@link XMLStreamReader}
61    * @throws XMLStreamException when failing to extract xml stream reader
62    */
 
63  2 toggle public static XMLStreamReader getXMLStreamReader(Source source) throws XMLStreamException
64    {
65  2 return getXMLStreamReader(XML_INPUT_FACTORY, source);
66    }
67   
68    /**
69    * Extract or create an instance of {@link XMLStreamReader} from the provided {@link Source}.
70    *
71    * @param factory the {@link XMLStreamReader} to use (if needed)
72    * @param source the source
73    * @return the {@link XMLStreamReader}
74    * @throws XMLStreamException when failing to extract xml stream reader
75    * @since 9.5
76    * @since 9.6RC1
77    */
 
78  2 toggle public static XMLStreamReader getXMLStreamReader(XMLInputFactory factory, Source source) throws XMLStreamException
79    {
80  2 XMLStreamReader xmlStreamReader;
81   
82  2 if (source instanceof StAXSource) {
83    // StAXSource is not supported by standard XMLInputFactory
84  1 StAXSource staxSource = (StAXSource) source;
85  1 if (staxSource.getXMLStreamReader() != null) {
86  1 xmlStreamReader = staxSource.getXMLStreamReader();
87    } else {
88    // TODO: add support for XMLStreamReader -> XMLEventReader
89  0 throw new XMLStreamException("XMLEventReader is not supported as source");
90    }
91    } else {
92  1 xmlStreamReader = factory.createXMLStreamReader(source);
93    }
94   
95  2 return xmlStreamReader;
96    }
97   
98    /**
99    * Extract or create an instance of {@link XMLEventReader} from the provided {@link Source}.
100    *
101    * @param source the source
102    * @return the {@link XMLEventReader}
103    * @throws XMLStreamException when failing to extract xml event reader
104    */
 
105  3 toggle public static XMLEventReader getXMLEventReader(Source source) throws XMLStreamException
106    {
107  3 XMLEventReader xmlEventReader;
108   
109  3 if (source instanceof StAXSource) {
110    // StAXSource is not supported by standard XMLInputFactory
111  2 StAXSource staxSource = (StAXSource) source;
112  2 if (staxSource.getXMLEventReader() != null) {
113  1 xmlEventReader = staxSource.getXMLEventReader();
114    } else {
115  1 xmlEventReader = new XMLStreamEventReader(staxSource.getXMLStreamReader());
116    }
117    } else {
118  1 xmlEventReader = XML_INPUT_FACTORY.createXMLEventReader(source);
119    }
120   
121  3 return xmlEventReader;
122    }
123   
124    /**
125    * Extract or create an instance of {@link XMLStreamWriter} from the provided {@link Result}.
126    *
127    * @param result the result
128    * @return the {@link XMLStreamWriter}
129    * @throws XMLStreamException when failing to extract xml stream writer
130    */
 
131  5 toggle public static XMLStreamWriter getXMLStreamWriter(Result result) throws XMLStreamException
132    {
133  5 return getXMLStreamWriter(XML_OUTPUT_FACTORY, result);
134    }
135   
136    /**
137    * Extract or create an instance of {@link XMLStreamWriter} from the provided {@link Result}.
138    *
139    * @param factory the {@link XMLOutputFactory} to use (if needed)
140    * @param result the result
141    * @return the {@link XMLStreamWriter}
142    * @throws XMLStreamException when failing to extract xml stream writer
143    * @since 9.5.2
144    * @since 9.6RC1
145    */
 
146  534 toggle public static XMLStreamWriter getXMLStreamWriter(XMLOutputFactory factory, Result result) throws XMLStreamException
147    {
148  534 XMLStreamWriter xmlStreamWriter;
149   
150  534 if (result instanceof SAXResult) {
151    // SAXResult is not supported by the standard XMLOutputFactory
152  489 xmlStreamWriter = new XMLEventStreamWriter(new SAXEventWriter(((SAXResult) result).getHandler()));
153  45 } else if (result instanceof StAXResult) {
154    // XMLEventWriter is not supported as result of XMLOutputFactory#createXMLStreamWriter
155  32 StAXResult staxResult = (StAXResult) result;
156  32 if (staxResult.getXMLStreamWriter() != null) {
157  31 xmlStreamWriter = staxResult.getXMLStreamWriter();
158    } else {
159  1 xmlStreamWriter = new XMLEventStreamWriter(staxResult.getXMLEventWriter());
160    }
161    } else {
162  13 xmlStreamWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(result);
163    }
164   
165  534 return xmlStreamWriter;
166    }
167   
168    /**
169    * Go to the end of the current element. This include skipping any children element.
170    *
171    * @param xmlReader the XML stream reader
172    * @return the type of the new current event
173    * @throws XMLStreamException if there is an error processing the underlying XML source
174    * @since 5.3M1
175    */
 
176  0 toggle public static int skipElement(XMLStreamReader xmlReader) throws XMLStreamException
177    {
178  0 if (!xmlReader.isStartElement()) {
179  0 throw new XMLStreamException("Current node is not start element");
180    }
181   
182  0 if (!xmlReader.isEndElement()) {
183  0 for (xmlReader.next(); !xmlReader.isEndElement(); xmlReader.next()) {
184  0 if (xmlReader.isStartElement()) {
185  0 skipElement(xmlReader);
186    }
187    }
188    }
189   
190  0 return xmlReader.getEventType();
191    }
192    }