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

File SAXEventWriter.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
69% 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 82
0.5340909453.4%
 

Contributing tests

This file is covered by 306 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  560 toggle public SAXEventWriter(ContentHandler handler)
74    {
75  560 this.filter = new XMLFilterImplEx();
76  560 this.filter.setContentHandler(handler);
77   
78  560 if (handler instanceof LexicalHandler) {
79  528 this.filter.setLexicalHandler((LexicalHandler) handler);
80    }
81  560 if (handler instanceof ErrorHandler) {
82  560 this.filter.setErrorHandler((ErrorHandler) handler);
83    }
84    }
85   
 
86  45027 toggle @Override
87    protected void sendEvent(XMLEvent event) throws XMLStreamException
88    {
89  45027 convertEvent(event);
90    }
91   
92    /**
93    * @param event the XML event to convert
94    * @throws XMLStreamException
95    */
 
96  45027 toggle private void convertEvent(XMLEvent event) throws XMLStreamException
97    {
98  45027 try {
99  45027 if (event.isStartDocument()) {
100  33 this.handleStartDocument(event);
101  44994 } else if (event.isEndDocument()) {
102  33 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  44961 switch (event.getEventType()) {
108  16210 case XMLStreamConstants.START_ELEMENT:
109  16210 this.depth++;
110  16210 this.handleStartElement(event.asStartElement());
111  16210 break;
112  16210 case XMLStreamConstants.END_ELEMENT:
113  16210 this.handleEndElement(event.asEndElement());
114  16210 this.depth--;
115  16210 if (this.depth == 0) {
116  560 break;
117    }
118  15650 break;
119  12541 case XMLStreamConstants.CHARACTERS:
120  12541 this.handleCharacters(event.asCharacters());
121  12541 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  0 case XMLStreamConstants.CDATA:
129  0 this.handleCDATA((Characters) event);
130  0 break;
131  0 default:
132  0 break;
133    }
134    }
135    } catch (SAXException e) {
136  0 throw new XMLStreamException(e);
137    }
138    }
139   
 
140  33 toggle private void handleEndDocument() throws SAXException
141    {
142  33 this.filter.endDocument();
143    }
144   
 
145  33 toggle private void handleStartDocument(final XMLEvent event) throws SAXException
146    {
147  33 final Location location = event.getLocation();
148  33 if (location != null) {
149  33 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  33 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  12541 toggle private void handleCharacters(Characters event) throws SAXException
187    {
188  12541 this.filter.characters(event.getData().toCharArray(), 0, event.getData().length());
189    }
190   
 
191  16210 toggle private void handleEndElement(EndElement event) throws XMLStreamException
192    {
193  16210 QName qName = event.getName();
194   
195  16210 try {
196    // fire endElement
197  16210 String prefix = qName.getPrefix();
198  16210 String rawname;
199  16210 if (prefix == null || prefix.length() == 0) {
200  16210 rawname = qName.getLocalPart();
201    } else {
202  0 rawname = prefix + ':' + qName.getLocalPart();
203    }
204   
205  16210 this.filter.endElement(qName.getNamespaceURI(), qName.getLocalPart(), rawname);
206   
207    // end namespace bindings
208  16210 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  16210 toggle private void handleStartElement(StartElement event) throws XMLStreamException
222    {
223  16210 try {
224    // start namespace bindings
225  16210 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  16210 QName qName = event.getName();
236  16210 String prefix = qName.getPrefix();
237  16210 String rawname;
238  16210 if (prefix == null || prefix.length() == 0) {
239  16210 rawname = qName.getLocalPart();
240    } else {
241  0 rawname = prefix + ':' + qName.getLocalPart();
242    }
243  16210 Attributes saxAttrs = this.getAttributes(event);
244  16210 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  16210 toggle private Attributes getAttributes(StartElement event)
257    {
258  16210 AttributesImpl attrs = new AttributesImpl();
259   
260  16210 if (!event.isStartElement()) {
261  0 throw new InternalError("getAttributes() attempting to process: " + event);
262    }
263   
264    // Add namspace declarations if required
265  16210 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  17868 for (@SuppressWarnings("unchecked") Iterator<Attribute> i = event.getAttributes(); i.hasNext();) {
290  1658 Attribute staxAttr = i.next();
291   
292  1658 String uri = staxAttr.getName().getNamespaceURI();
293  1658 if (uri == null) {
294  0 uri = "";
295    }
296  1658 String localName = staxAttr.getName().getLocalPart();
297  1658 String prefix = staxAttr.getName().getPrefix();
298  1658 String qName;
299  1658 if (prefix == null || prefix.length() == 0) {
300  1658 qName = localName;
301    } else {
302  0 qName = prefix + ':' + localName;
303    }
304  1658 String type = staxAttr.getDTDType();
305  1658 String value = staxAttr.getValue();
306   
307  1658 attrs.addAttribute(uri, localName, qName, type, value);
308    }
309   
310  16210 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  0 toggle private void handleCDATA(Characters event) throws SAXException
324    {
325  0 this.filter.startCDATA();
326  0 this.filter.characters(event.getData().toCharArray(), 0, event.getData().length());
327  0 this.filter.endCDATA();
328    }
329    }