Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SimpleXMLReader |
|
| 1.5;1.5 |
1 | // Copyright 2008-2014 severally by the contributors | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package net.sf.practicalxml.util; | |
16 | ||
17 | import java.io.IOException; | |
18 | ||
19 | import javax.xml.parsers.SAXParser; | |
20 | import javax.xml.parsers.SAXParserFactory; | |
21 | ||
22 | import net.sf.practicalxml.XmlException; | |
23 | ||
24 | import org.xml.sax.InputSource; | |
25 | import org.xml.sax.SAXException; | |
26 | import org.xml.sax.helpers.XMLFilterImpl; | |
27 | ||
28 | ||
29 | /** | |
30 | * An implementation of {@link org.xml.sax.XMLReader} based on {@link | |
31 | * XMLFilterImpl}, which implements the <code>parse()</code> methods | |
32 | * (default behavior of <code>XMLFilterImpl</code> is to delegate to a | |
33 | * parent <code>XMLReader</code>, and throw <code>NullPointerException</code> | |
34 | * if there is no parent). | |
35 | * <p> | |
36 | * Since this class extends <code>XMLFilterImpl</code>, it exposes all | |
37 | * public handler methods, and can be used to write a filtered source | |
38 | * for an XML transform. | |
39 | * | |
40 | * @since 1.1.1 | |
41 | */ | |
42 | public class SimpleXMLReader | |
43 | extends XMLFilterImpl | |
44 | { | |
45 | private SAXParser _parser; | |
46 | ||
47 | /** | |
48 | * Creates a new instance, using the default configuration of {@link | |
49 | * SAXParserFactory}. The checked exceptions thrown by the factory are | |
50 | * caught and rethrown as <code>XmlException</code>. | |
51 | */ | |
52 | public SimpleXMLReader() | |
53 | 1 | { |
54 | try | |
55 | { | |
56 | 1 | _parser = SAXParserFactory.newInstance().newSAXParser(); |
57 | } | |
58 | 0 | catch (Exception ee) |
59 | { | |
60 | 0 | throw new XmlException(ee); |
61 | 1 | } |
62 | 1 | } |
63 | ||
64 | ||
65 | /** | |
66 | * Creates a new instance, wrapping a pre-existing parser. | |
67 | */ | |
68 | public SimpleXMLReader(SAXParser parser) | |
69 | 0 | { |
70 | 0 | _parser = parser; |
71 | 0 | } |
72 | ||
73 | ||
74 | @Override | |
75 | public void parse(InputSource input) | |
76 | throws SAXException, IOException | |
77 | { | |
78 | 1 | _parser.parse(input, new XMLFilterImplBridge(this)); |
79 | 1 | } |
80 | ||
81 | ||
82 | @Override | |
83 | public void parse(String systemId) | |
84 | throws SAXException, IOException | |
85 | { | |
86 | 0 | _parser.parse(systemId, new XMLFilterImplBridge(this)); |
87 | 0 | } |
88 | } |