Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractLoader |
|
| 2.0;2 |
1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2014 Oliver Burn | |
4 | // | |
5 | // This library is free software; you can redistribute it and/or | |
6 | // modify it under the terms of the GNU Lesser General Public | |
7 | // License as published by the Free Software Foundation; either | |
8 | // version 2.1 of the License, or (at your option) any later version. | |
9 | // | |
10 | // This library 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 library; if not, write to the Free Software | |
17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | //////////////////////////////////////////////////////////////////////////////// | |
19 | package com.puppycrawl.tools.checkstyle.api; | |
20 | ||
21 | import com.google.common.collect.Maps; | |
22 | import java.io.IOException; | |
23 | import java.io.InputStream; | |
24 | import java.util.HashMap; | |
25 | import java.util.Map; | |
26 | import javax.xml.parsers.ParserConfigurationException; | |
27 | import javax.xml.parsers.SAXParserFactory; | |
28 | import org.xml.sax.InputSource; | |
29 | import org.xml.sax.SAXException; | |
30 | import org.xml.sax.SAXParseException; | |
31 | import org.xml.sax.XMLReader; | |
32 | import org.xml.sax.helpers.DefaultHandler; | |
33 | ||
34 | /** | |
35 | * Contains the common implementation of a loader, for loading a configuration | |
36 | * from an XML file. | |
37 | * <p> | |
38 | * The error handling policy can be described as being austere, dead set, | |
39 | * disciplinary, dour, draconian, exacting, firm, forbidding, grim, hard, hard- | |
40 | * boiled, harsh, harsh, in line, iron-fisted, no-nonsense, oppressive, | |
41 | * persnickety, picky, prudish, punctilious, puritanical, rigid, rigorous, | |
42 | * scrupulous, set, severe, square, stern, stickler, straight, strait-laced, | |
43 | * stringent, stuffy, stuffy, tough, unpermissive, unsparing and uptight. | |
44 | * | |
45 | * @author Oliver Burn | |
46 | */ | |
47 | public abstract class AbstractLoader | |
48 | extends DefaultHandler | |
49 | { | |
50 | /** maps public id to resolve to esource name for the DTD */ | |
51 | private final Map<String, String> mPublicIdToResourceNameMap; | |
52 | /** parser to read XML files **/ | |
53 | private final XMLReader mParser; | |
54 | ||
55 | /** | |
56 | * Creates a new instance. | |
57 | * @param aPublicId the public ID for the DTD to resolve | |
58 | * @param aDtdResourceName the resource for the DTD | |
59 | * @throws SAXException if an error occurs | |
60 | * @throws ParserConfigurationException if an error occurs | |
61 | */ | |
62 | protected AbstractLoader(String aPublicId, String aDtdResourceName) | |
63 | throws SAXException, ParserConfigurationException | |
64 | { | |
65 | 594 | this(new HashMap<String, String>(1)); |
66 | 594 | mPublicIdToResourceNameMap.put(aPublicId, aDtdResourceName); |
67 | 594 | } |
68 | ||
69 | /** | |
70 | * Creates a new instance. | |
71 | * @param aPublicIdToResourceNameMap maps public IDs to DTD resource names | |
72 | * @throws SAXException if an error occurs | |
73 | * @throws ParserConfigurationException if an error occurs | |
74 | */ | |
75 | protected AbstractLoader(Map<String, String> aPublicIdToResourceNameMap) | |
76 | throws SAXException, ParserConfigurationException | |
77 | 619 | { |
78 | 619 | mPublicIdToResourceNameMap = |
79 | Maps.newHashMap(aPublicIdToResourceNameMap); | |
80 | 619 | final SAXParserFactory factory = SAXParserFactory.newInstance(); |
81 | 619 | factory.setValidating(true); |
82 | 619 | factory.setNamespaceAware(true); |
83 | 619 | mParser = factory.newSAXParser().getXMLReader(); |
84 | 619 | mParser.setContentHandler(this); |
85 | 619 | mParser.setEntityResolver(this); |
86 | 619 | mParser.setErrorHandler(this); |
87 | 619 | } |
88 | ||
89 | /** | |
90 | * Parses the specified input source. | |
91 | * @param aInputSource the input source to parse. | |
92 | * @throws IOException if an error occurs | |
93 | * @throws SAXException in an error occurs | |
94 | */ | |
95 | public void parseInputSource(InputSource aInputSource) | |
96 | throws IOException, SAXException | |
97 | { | |
98 | 619 | mParser.parse(aInputSource); |
99 | 611 | } |
100 | ||
101 | @Override | |
102 | public InputSource resolveEntity(String aPublicId, String aSystemId) | |
103 | throws SAXException, IOException | |
104 | { | |
105 | 622 | if (mPublicIdToResourceNameMap.keySet().contains(aPublicId)) { |
106 | 619 | final String dtdResourceName = |
107 | mPublicIdToResourceNameMap.get(aPublicId); | |
108 | 619 | final ClassLoader loader = |
109 | this.getClass().getClassLoader(); | |
110 | 619 | final InputStream dtdIS = |
111 | loader.getResourceAsStream(dtdResourceName); | |
112 | 619 | if (dtdIS == null) { |
113 | 0 | throw new SAXException( |
114 | "Unable to load internal dtd " + dtdResourceName); | |
115 | } | |
116 | 619 | return new InputSource(dtdIS); |
117 | } | |
118 | 3 | return super.resolveEntity(aPublicId, aSystemId); |
119 | } | |
120 | ||
121 | @Override | |
122 | public void warning(SAXParseException aEx) throws SAXException | |
123 | { | |
124 | 0 | throw aEx; |
125 | } | |
126 | ||
127 | @Override | |
128 | public void error(SAXParseException aEx) throws SAXException | |
129 | { | |
130 | 7 | throw aEx; |
131 | } | |
132 | ||
133 | @Override | |
134 | public void fatalError(SAXParseException aEx) throws SAXException | |
135 | { | |
136 | 0 | throw aEx; |
137 | } | |
138 | } |