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

File SAXEventWriter.java

 

Coverage histogram

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

Code metrics

42
118
16
1
329
238
50
0.42
7.38
16
3.12

Classes

Class Line # Actions
SAXEventWriter 58 118 0% 50 74
0.5795454458%
 

Contributing tests

This file is covered by 293 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 java.util.Iterator;
23   
24    import javax.xml.namespace.QName;
25    import javax.xml.stream.Location;
26    import javax.xml.stream.XMLStreamConstants;
27    import javax.xml.stream.XMLStreamException;
28    import javax.xml.stream.events.Attribute;
29    import javax.xml.stream.events.Characters;
30    import javax.xml.stream.events.Comment;
31    import javax.xml.stream.events.EndElement;
32    import javax.xml.stream.events.Namespace;
33    import javax.xml.stream.events.ProcessingInstruction;
34    import javax.xml.stream.events.StartElement;
35    import javax.xml.stream.events.XMLEvent;
36    import javax.xml.stream.util.XMLEventConsumer;
37   
38    import org.xml.sax.Attributes;
39    import org.xml.sax.ContentHandler;
40    import org.xml.sax.ErrorHandler;
41    import org.xml.sax.Locator;
42    import org.xml.sax.SAXException;
43    import org.xml.sax.ext.LexicalHandler;
44    import org.xml.sax.helpers.AttributesImpl;
45   
46    import javanet.staxutils.BaseXMLEventWriter;
47    import javanet.staxutils.DummyLocator;
48    import javanet.staxutils.helpers.XMLFilterImplEx;
49   
50    /**
51    * Receive and convert StAX events to SAX events.
52    * <p>
53    * Extends {@link XMLEventConsumer} with {@link javax.xml.stream.XMLEventWriter} methods.
54    *
55    * @version $Id: 46951b1671279840414c9ef11c3eb5fb5970f295 $
56    * @since 5.2M1
57    */
 
58    public class SAXEventWriter extends BaseXMLEventWriter
59    {
60    /**
61    * The SAX filter.
62    */
63    private XMLFilterImplEx filter;
64   
65    /**
66    * The depth of XML elements.
67    */
68    private int depth;
69   
70    /**
71    * @param handler the content handler
72    */
 
73  526 toggle public SAXEventWriter(ContentHandler handler)
74    {
75  526 this.filter = new XMLFilterImplEx();
76  526 this.filter.setContentHandler(handler);
77   
78  526 if (handler instanceof LexicalHandler) {
79  489 this.filter.setLexicalHandler((LexicalHandler) handler);
80    }
81  526 if (handler instanceof ErrorHandler) {
82  525 this.filter.setErrorHandler((ErrorHandler) handler);
83    }
84    }
85   
 
86  45465 toggle @Override
87    protected void sendEvent(XMLEvent event) throws XMLStreamException
88    {
89  45465 convertEvent(event);
90    }
91   
92    /**
93    * @param event the XML event to convert
94    * @throws XMLStreamException
95    */
 
96  45465 toggle private void convertEvent(XMLEvent event) throws XMLStreamException
97    {
98  45465 try {
99  45465 if (event.isStartDocument()) {
100  38 this.handleStartDocument(event);
101  45427 } else if (event.isEndDocument()) {
102  38 this.handleEndDocument();
103    } else {
104    // These are all of the events listed in the javadoc for
105    // XMLEvent.
106    // The spec only really describes 11 of them.
107  45389 switch (event.getEventType()) {
108  15980 case XMLStreamConstants.START_ELEMENT:
109  15980 this.depth++;
110  15980 this.handleStartElement(event.asStartElement());
111  15980 break;
112  15980 case XMLStreamConstants.END_ELEMENT:
113  15980 this.handleEndElement(event.asEndElement());
114  15980 this.depth--;
115  15980 if (this.depth == 0) {
116  526 break;
117    }
118  15454 break;
119  13428 case XMLStreamConstants.CHARACTERS:
120  13428 this.handleCharacters(event.asCharacters());
121  13428 break;
122  0 case XMLStreamConstants.PROCESSING_INSTRUCTION:
123  0 this.handlePI((ProcessingInstruction) event);
124  0 break;
125  0 case XMLStreamConstants.COMMENT:
126  0 this.handleComment((Comment) event);
127  0 break;
128  1 case XMLStreamConstants.CDATA:
129  1 this.handleCDATA((Characters) event);
130  1 break;
131  0 default:
132  0 break;
133    }
134    }
135    } catch (SAXException e) {
136  0 throw new XMLStreamException(e);
137    }
138    }
139   
 
140  38 toggle private void handleEndDocument() throws SAXException
141    {
142  38 this.filter.endDocument();
143    }
144   
 
145  38 toggle private void handleStartDocument(final XMLEvent event) throws SAXException
146    {
147  38 final Location location = event.getLocation();
148  38 if (location != null) {
149  38 this.filter.setDocumentLocator(new Locator()
150    {
 
151  0 toggle @Override
152    public int getColumnNumber()
153    {
154  0 return location.getColumnNumber();
155    }
156   
 
157  0 toggle @Override
158    public int getLineNumber()
159    {
160  0 return location.getLineNumber();
161    }
162   
 
163  0 toggle @Override
164    public String getPublicId()
165    {
166  0 return location.getPublicId();
167    }
168   
 
169  0 toggle @Override
170    public String getSystemId()
171    {
172  0 return location.getSystemId();
173    }
174    });
175    } else {
176  0 this.filter.setDocumentLocator(new DummyLocator());
177    }
178  38 this.filter.startDocument();
179    }
180   
 
181  0 toggle private void handlePI(ProcessingInstruction event) throws SAXException
182    {
183  0 this.filter.processingInstruction(event.getTarget(), event.getData());
184    }
185   
 
186  13428 toggle private void handleCharacters(Characters event) throws SAXException
187    {
188  13428 this.filter.characters(event.getData().toCharArray(), 0, event.getData().length());
189    }
190   
 
191  15980 toggle private void handleEndElement(EndElement event) throws XMLStreamException
192    {
193  15980 QName qName = event.getName();
194   
195  15980 try {
196    // fire endElement
197  15980 String prefix = qName.getPrefix();
198  15980 String rawname;
199  15980 if (prefix == null || prefix.length() == 0) {
200  15980 rawname = qName.getLocalPart();
201    } else {
202  0 rawname = prefix + ':' + qName.getLocalPart();
203    }
204   
205  15980 this.filter.endElement(qName.getNamespaceURI(), qName.getLocalPart(), rawname);
206   
207    // end namespace bindings
208  15980 for (@SuppressWarnings("unchecked") Iterator<Namespace> i = event.getNamespaces(); i.hasNext();) {
209  0 String nsprefix = i.next().getPrefix();
210    // true for default namespace
211  0 if (nsprefix == null) {
212  0 nsprefix = "";
213    }
214  0 this.filter.endPrefixMapping(nsprefix);
215    }
216    } catch (SAXException e) {
217  0 throw new XMLStreamException(e);
218    }
219    }
220   
 
221  15980 toggle private void handleStartElement(StartElement event) throws XMLStreamException
222    {
223  15980 try {
224    // start namespace bindings
225  15980 for (@SuppressWarnings("unchecked") Iterator<Namespace> i = event.getNamespaces(); i.hasNext();) {
226  0 String prefix = i.next().getPrefix();
227    // true for default namespace
228  0 if (prefix == null) {
229  0 prefix = "";
230    }
231  0 this.filter.startPrefixMapping(prefix, event.getNamespaceURI(prefix));
232    }
233   
234    // fire startElement
235  15980 QName qName = event.getName();
236  15980 String prefix = qName.getPrefix();
237  15980 String rawname;
238  15980 if (prefix == null || prefix.length() == 0) {
239  15980 rawname = qName.getLocalPart();
240    } else {
241  0 rawname = prefix + ':' + qName.getLocalPart();
242    }
243  15980 Attributes saxAttrs = this.getAttributes(event);
244  15980 this.filter.startElement(qName.getNamespaceURI(), qName.getLocalPart(), rawname, saxAttrs);
245    } catch (SAXException e) {
246  0 throw new XMLStreamException(e);
247    }
248    }
249   
250    /**
251    * Get the attributes associated with the given START_ELEMENT StAXevent.
252    *
253    * @param event the StAX start element event
254    * @return the StAX attributes converted to an org.xml.sax.Attributes
255    */
 
256  15980 toggle private Attributes getAttributes(StartElement event)
257    {
258  15980 AttributesImpl attrs = new AttributesImpl();
259   
260  15980 if (!event.isStartElement()) {
261  0 throw new InternalError("getAttributes() attempting to process: " + event);
262    }
263   
264    // Add namspace declarations if required
265  15980 if (this.filter.getNamespacePrefixes()) {
266  0 for (@SuppressWarnings("unchecked") Iterator<Namespace> i = event.getNamespaces(); i.hasNext();) {
267  0 Namespace staxNamespace = i.next();
268  0 String uri = staxNamespace.getNamespaceURI();
269  0 if (uri == null) {
270  0 uri = "";
271    }
272   
273  0 String prefix = staxNamespace.getPrefix();
274  0 if (prefix == null) {
275  0 prefix = "";
276    }
277   
278  0 String qName = "xmlns";
279  0 if (prefix.length() == 0) {
280  0 prefix = qName;
281    } else {
282  0 qName = qName + ':' + prefix;
283    }
284  0 attrs.addAttribute("http://www.w3.org/2000/xmlns/", prefix, qName, "CDATA", uri);
285    }
286    }
287   
288    // gather non-namespace attrs
289  17915 for (@SuppressWarnings("unchecked") Iterator<Attribute> i = event.getAttributes(); i.hasNext();) {
290  1935 Attribute staxAttr = i.next();
291   
292  1935 String uri = staxAttr.getName().getNamespaceURI();
293  1935 if (uri == null) {
294  0 uri = "";
295    }
296  1935 String localName = staxAttr.getName().getLocalPart();
297  1935 String prefix = staxAttr.getName().getPrefix();
298  1935 String qName;
299  1935 if (prefix == null || prefix.length() == 0) {
300  1935 qName = localName;
301    } else {
302  0 qName = prefix + ':' + localName;
303    }
304  1935 String type = staxAttr.getDTDType();
305  1935 String value = staxAttr.getValue();
306   
307  1935 attrs.addAttribute(uri, localName, qName, type, value);
308    }
309   
310  15980 return attrs;
311    }
312   
 
313  0 toggle private void handleComment(Comment comment) throws XMLStreamException
314    {
315  0 try {
316  0 String text = comment.getText();
317  0 this.filter.comment(text.toCharArray(), 0, text.length());
318    } catch (SAXException e) {
319  0 throw new XMLStreamException(e);
320    }
321    }
322   
 
323  1 toggle private void handleCDATA(Characters event) throws SAXException
324    {
325  1 this.filter.startCDATA();
326  1 this.filter.characters(event.getData().toCharArray(), 0, event.getData().length());
327  1 this.filter.endCDATA();
328    }
329    }