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

File StAXUtils.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

22
33
5
1
162
83
16
0.48
6.6
5
3.2

Classes

Class Line # Actions
StAXUtils 43 33 0% 16 32
0.4666666746.7%
 

Contributing tests

This file is covered by 309 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: 25fc5b177f03c189d4b27a3ee8f891581a4f2dc5 $
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  0 toggle public static XMLStreamReader getXMLStreamReader(Source source) throws XMLStreamException
64    {
65  0 XMLStreamReader xmlStreamReader;
66   
67  0 if (source instanceof StAXSource) {
68    // StAXSource is not supported by standard XMLInputFactory
69  0 StAXSource staxSource = (StAXSource) source;
70  0 if (staxSource.getXMLStreamReader() != null) {
71  0 xmlStreamReader = staxSource.getXMLStreamReader();
72    } else {
73    // TODO: add support for XMLStreamReader -> XMLEventReader
74  0 throw new XMLStreamException("XMLEventReader is not supported as source");
75    }
76    } else {
77  0 xmlStreamReader = XML_INPUT_FACTORY.createXMLStreamReader(source);
78    }
79   
80  0 return xmlStreamReader;
81    }
82   
83    /**
84    * Extract or create an instance of {@link XMLEventReader} from the provided {@link Source}.
85    *
86    * @param source the source
87    * @return the {@link XMLEventReader}
88    * @throws XMLStreamException when failing to extract xml event reader
89    */
 
90  0 toggle public static XMLEventReader getXMLEventReader(Source source) throws XMLStreamException
91    {
92  0 XMLEventReader xmlEventReader;
93   
94  0 if (source instanceof StAXSource) {
95    // StAXSource is not supported by standard XMLInputFactory
96  0 StAXSource staxSource = (StAXSource) source;
97  0 if (staxSource.getXMLEventReader() != null) {
98  0 xmlEventReader = staxSource.getXMLEventReader();
99    } else {
100  0 xmlEventReader = new XMLStreamEventReader(staxSource.getXMLStreamReader());
101    }
102    } else {
103  0 xmlEventReader = XML_INPUT_FACTORY.createXMLEventReader(source);
104    }
105   
106  0 return xmlEventReader;
107    }
108   
109    /**
110    * Extract or create an instance of {@link XMLStreamWriter} from the provided {@link Result}.
111    *
112    * @param result the result
113    * @return the {@link XMLStreamWriter}
114    * @throws XMLStreamException when failing to extract xml stream writer
115    */
 
116  565 toggle public static XMLStreamWriter getXMLStreamWriter(Result result) throws XMLStreamException
117    {
118  565 XMLStreamWriter xmlStreamWriter;
119   
120  565 if (result instanceof SAXResult) {
121    // SAXResult is not supported by the standard XMLOutputFactory
122  528 xmlStreamWriter = new XMLEventStreamWriter(new SAXEventWriter(((SAXResult) result).getHandler()));
123  37 } else if (result instanceof StAXResult) {
124    // XMLEventWriter is not supported as result of XMLOutputFactory#createXMLStreamWriter
125  25 StAXResult staxResult = (StAXResult) result;
126  25 if (staxResult.getXMLStreamWriter() != null) {
127  25 xmlStreamWriter = staxResult.getXMLStreamWriter();
128    } else {
129  0 xmlStreamWriter = new XMLEventStreamWriter(staxResult.getXMLEventWriter());
130    }
131    } else {
132  12 xmlStreamWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(result);
133    }
134   
135  565 return xmlStreamWriter;
136    }
137   
138    /**
139    * Go to the end of the current element. This include skipping any children element.
140    *
141    * @param xmlReader the XML stream reader
142    * @return the type of the new current event
143    * @throws XMLStreamException if there is an error processing the underlying XML source
144    * @since 5.3M1
145    */
 
146  452 toggle public static int skipElement(XMLStreamReader xmlReader) throws XMLStreamException
147    {
148  452 if (!xmlReader.isStartElement()) {
149  0 throw new XMLStreamException("Current node is not start element");
150    }
151   
152  452 if (!xmlReader.isEndElement()) {
153  1172 for (xmlReader.next(); !xmlReader.isEndElement(); xmlReader.next()) {
154  720 if (xmlReader.isStartElement()) {
155  239 skipElement(xmlReader);
156    }
157    }
158    }
159   
160  452 return xmlReader.getEventType();
161    }
162    }