KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > api > AbstractLoader


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 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 java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26 import javax.xml.parsers.SAXParserFactory JavaDoc;
27 import org.xml.sax.InputSource JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.SAXParseException JavaDoc;
30 import org.xml.sax.XMLReader JavaDoc;
31 import org.xml.sax.helpers.DefaultHandler JavaDoc;
32
33 /**
34  * Contains the common implementation of a loader, for loading a configuration
35  * from an XML file.
36  * <p>
37  * The error handling policy can be described as being austere, dead set,
38  * disciplinary, dour, draconian, exacting, firm, forbidding, grim, hard, hard-
39  * boiled, harsh, harsh, in line, iron-fisted, no-nonsense, oppressive,
40  * persnickety, picky, prudish, punctilious, puritanical, rigid, rigorous,
41  * scrupulous, set, severe, square, stern, stickler, straight, strait-laced,
42  * stringent, stuffy, stuffy, tough, unpermissive, unsparing and uptight.
43  *
44  * @author Oliver Burn
45  */

46 public abstract class AbstractLoader
47     extends DefaultHandler JavaDoc
48 {
49     /** maps public id to resolve to esource name for the DTD */
50     private final Map JavaDoc mPublicIdToResourceNameMap;
51     /** parser to read XML files **/
52     private final XMLReader JavaDoc mParser;
53
54     /**
55      * Creates a new instance.
56      * @param aPublicId the public ID for the DTD to resolve
57      * @param aDtdResourceName the resource for the DTD
58      * @throws SAXException if an error occurs
59      * @throws ParserConfigurationException if an error occurs
60      */

61     protected AbstractLoader(String JavaDoc aPublicId, String JavaDoc aDtdResourceName)
62         throws SAXException JavaDoc, ParserConfigurationException JavaDoc
63     {
64         this(new HashMap JavaDoc(1));
65         mPublicIdToResourceNameMap.put(aPublicId, aDtdResourceName);
66     }
67
68     /**
69      * Creates a new instance.
70      * @param aPublicIdToResourceNameMap maps public IDs to DTD resource names
71      * @throws SAXException if an error occurs
72      * @throws ParserConfigurationException if an error occurs
73      */

74     protected AbstractLoader(Map JavaDoc aPublicIdToResourceNameMap)
75         throws SAXException JavaDoc, ParserConfigurationException JavaDoc
76     {
77         mPublicIdToResourceNameMap = new HashMap JavaDoc(aPublicIdToResourceNameMap);
78         final SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
79         factory.setValidating(true);
80         factory.setNamespaceAware(true);
81         mParser = factory.newSAXParser().getXMLReader();
82         mParser.setContentHandler(this);
83         mParser.setEntityResolver(this);
84         mParser.setErrorHandler(this);
85     }
86
87     /**
88      * Parses the specified input source.
89      * @param aInputSource the input source to parse.
90      * @throws IOException if an error occurs
91      * @throws SAXException in an error occurs
92      */

93     public void parseInputSource(InputSource JavaDoc aInputSource)
94         throws IOException JavaDoc, SAXException JavaDoc
95     {
96         mParser.parse(aInputSource);
97     }
98
99     /** {@inheritDoc} */
100     public InputSource JavaDoc resolveEntity(String JavaDoc aPublicId, String JavaDoc aSystemId)
101         throws SAXException JavaDoc
102     {
103         if (mPublicIdToResourceNameMap.keySet().contains(aPublicId)) {
104             final String JavaDoc dtdResourceName =
105                     (String JavaDoc) mPublicIdToResourceNameMap.get(aPublicId);
106             final ClassLoader JavaDoc loader =
107                 Thread.currentThread().getContextClassLoader();
108             final InputStream JavaDoc dtdIS =
109                 loader.getResourceAsStream(dtdResourceName);
110             if (dtdIS == null) {
111                 throw new SAXException JavaDoc(
112                     "Unable to load internal dtd " + dtdResourceName);
113             }
114             return new InputSource JavaDoc(dtdIS);
115         }
116         // This is a hack to workaround problem with SAX
117
// DefaultHeader.resolveEntity():
118
// sometimes it throws SAX- and IO- exceptions
119
// sometime SAX only :(
120
try {
121             if (false) {
122                 throw new IOException JavaDoc("");
123             }
124             return super.resolveEntity(aPublicId, aSystemId);
125         }
126         catch (final IOException JavaDoc e) {
127             throw new SAXException JavaDoc("" + e, e);
128         }
129     }
130
131     /** {@inheritDoc} */
132     public void warning(SAXParseException JavaDoc aEx) throws SAXException JavaDoc
133     {
134         throw aEx;
135     }
136
137     /** {@inheritDoc} */
138     public void error(SAXParseException JavaDoc aEx) throws SAXException JavaDoc
139     {
140         throw aEx;
141     }
142
143     /** {@inheritDoc} */
144     public void fatalError(SAXParseException JavaDoc aEx) throws SAXException JavaDoc
145     {
146         throw aEx;
147     }
148 }
149
Popular Tags