Refactoring to move all the XML parsing logic into a

common place.
This commit is contained in:
Oliver Burn 2002-12-07 13:02:52 +00:00
parent 9d4233b0b7
commit 76ae239351
4 changed files with 131 additions and 145 deletions

View File

@ -0,0 +1,120 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2002 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
/**
* Contains the common implementation of a loader, for loading a configuration
* from an XML file.
* <p>
* The error handling policy can be described as being austere, dead set,
* disciplinary, dour, draconian, exacting, firm, forbidding, grim, hard, hard-
* boiled, harsh, harsh, in line, iron-fisted, no-nonsense, oppressive,
* persnickety, picky, prudish, punctilious, puritanical, rigid, rigorous,
* scrupulous, set, severe, square, stern, stickler, straight, strait-laced,
* stringent, stuffy, stuffy, tough, unpermissive, unsparing and uptight.
*
* @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
*/
abstract class AbstractLoader
extends DefaultHandler
{
/** the public id to resolve */
private final String mPublicId;
/** the resource name for the DTD */
private final String mDtdResourceName;
/** parser to read XML files **/
private final XMLReader mParser;
/**
* Creates a new instance.
* @param aPublicId the public ID for the DTD to resolve
* @param aDtdResourceName the resource for the DTD
* @throws SAXException if an error occurs
* @throws ParserConfigurationException if an error occurs
*/
protected AbstractLoader(String aPublicId, String aDtdResourceName)
throws SAXException, ParserConfigurationException
{
mPublicId = aPublicId;
mDtdResourceName = aDtdResourceName;
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
mParser = factory.newSAXParser().getXMLReader();
mParser.setContentHandler(this);
mParser.setEntityResolver(this);
mParser.setErrorHandler(this);
}
/**
* Parses the specified input source.
* @param aInputSource the input source to parse.
* @throws IOException if an error occurs
* @throws SAXException in an error occurs
*/
protected void parseInputSource(InputSource aInputSource)
throws IOException, SAXException
{
mParser.parse(aInputSource);
}
/** @see org.xml.sax.EntityResolver */
public InputSource resolveEntity(String aPublicId, String aSystemId)
throws SAXException
{
if (mPublicId.equals(aPublicId)) {
final InputStream dtdIS = getClass().getClassLoader()
.getResourceAsStream(mDtdResourceName);
if (dtdIS == null) {
throw new SAXException(
"Unable to load internal dtd " + mDtdResourceName);
}
return new InputSource(dtdIS);
}
return super.resolveEntity(aPublicId, aSystemId);
}
/** @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException) */
public void warning(SAXParseException aEx) throws SAXException
{
throw aEx;
}
/** @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException) */
public void error(SAXParseException aEx) throws SAXException
{
throw aEx;
}
/** @see org.xml.sax.ErrorHandler#fatalError */
public void fatalError(SAXParseException aEx) throws SAXException
{
throw aEx;
}
}

View File

@ -24,16 +24,12 @@ import com.puppycrawl.tools.checkstyle.api.Utils;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
/**
* Loads a configuration from a configuration XML file.
@ -42,22 +38,20 @@ import org.xml.sax.helpers.DefaultHandler;
* @version 1.0
*/
class ConfigurationLoader
extends DefaultHandler
extends AbstractLoader
{
/** the public ID for the configuration dtd */
private static final String CONFIG_DTD_PUBLIC_ID =
private static final String DTD_PUBLIC_ID =
"-//Puppy Crawl//DTD Check Configuration 1.0//EN";
/** the resource for the configuration dtd */
private static final String CONFIG_DTD_RESOURCE =
private static final String DTD_RESOURCE_NAME =
"com/puppycrawl/tools/checkstyle/configuration_1_0.dtd";
/** overriding properties **/
private final Properties mOverrideProps;
/** parser to read XML files **/
private final XMLReader mParser;
/** the loaded configurations **/
private Stack mConfigStack = new Stack();
private final Stack mConfigStack = new Stack();
/** the Configuration that is being built */
private Configuration mConfiguration = null;
@ -70,13 +64,8 @@ class ConfigurationLoader
private ConfigurationLoader(Properties aOverrideProps)
throws ParserConfigurationException, SAXException
{
super(DTD_PUBLIC_ID, DTD_RESOURCE_NAME);
mOverrideProps = aOverrideProps;
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
mParser = factory.newSAXParser().getXMLReader();
mParser.setContentHandler(this);
mParser.setEntityResolver(this);
mParser.setErrorHandler(HardErrorHandler.INSTANCE);
}
/**
@ -89,31 +78,13 @@ class ConfigurationLoader
void parseFile(String aFilename)
throws FileNotFoundException, IOException, SAXException
{
mParser.parse(new InputSource(new FileReader(aFilename)));
parseInputSource(new InputSource(new FileReader(aFilename)));
}
///////////////////////////////////////////////////////////////////////////
// Document handler methods
///////////////////////////////////////////////////////////////////////////
/** @see org.xml.sax.EntityResolver */
public InputSource resolveEntity(String aPublicId, String aSystemId)
throws SAXException
{
if (CONFIG_DTD_PUBLIC_ID.equals(aPublicId)) {
final InputStream dtdIS = getClass().getClassLoader()
.getResourceAsStream(CONFIG_DTD_RESOURCE);
if (dtdIS == null) {
throw new SAXException(
"Unable to load internal dtd " + CONFIG_DTD_RESOURCE);
}
return new InputSource(dtdIS);
}
return super.resolveEntity(aPublicId, aSystemId);
}
/** @see org.xml.sax.helpers.DefaultHandler **/
public void startElement(String aNamespaceURI,
String aLocalName,

View File

@ -1,63 +0,0 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2002 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/**
* This class implements the error handling policy for Checkstyle. The policy
* can be summarised as being austere, dead set, disciplinary, dour, draconian,
* exacting, firm, forbidding, grim, hard, hard-boiled, harsh, harsh, in line,
* iron-fisted, no-nonsense, oppressive, persnickety, picky, prudish,
* punctilious, puritanical, rigid, rigorous, scrupulous, set, severe, square,
* stern, stickler, straight, strait-laced, stringent, stuffy, stuffy, tough,
* unpermissive, unsparing or uptight.
* @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
*/
class HardErrorHandler
implements ErrorHandler
{
/** the instance for use */
static final HardErrorHandler INSTANCE = new HardErrorHandler();
/** Prevent instances */
private HardErrorHandler()
{
}
/** @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException) */
public void warning(SAXParseException aEx) throws SAXException
{
throw aEx;
}
/** @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException) */
public void error(SAXParseException aEx) throws SAXException
{
throw aEx;
}
/** @see org.xml.sax.ErrorHandler#fatalError */
public void fatalError(SAXParseException aEx) throws SAXException
{
throw aEx;
}
}

View File

@ -28,12 +28,9 @@ import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
/**
* Loads a list of package names from a package name XML file.
@ -41,14 +38,14 @@ import org.xml.sax.helpers.DefaultHandler;
* @version 4-Dec-2002
*/
public class PackageNamesLoader
extends DefaultHandler
extends AbstractLoader
{
/** the public ID for the configuration dtd */
private static final String PACKAGE_DTD_PUBLIC_ID =
private static final String DTD_PUBLIC_ID =
"-//Puppy Crawl//DTD Package Names 1.0//EN";
/** the resource for the configuration dtd */
private static final String PACKAGE_DTD_RESOURCE =
private static final String DTD_RESOURCE_NAME =
"com/puppycrawl/tools/checkstyle/packages_1_0.dtd";
/** Name of default checkstyle package names resource file.
@ -62,9 +59,6 @@ public class PackageNamesLoader
/** The loaded package names */
private Stack mPackageStack = new Stack();
/** parser to read XML files **/
private XMLReader mParser;
/**
* Creates a new <code>PackageNameLoader</code> instance.
* @throws ParserConfigurationException if an error occurs
@ -73,12 +67,7 @@ public class PackageNamesLoader
private PackageNamesLoader()
throws ParserConfigurationException, SAXException
{
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
mParser = factory.newSAXParser().getXMLReader();
mParser.setContentHandler(this);
mParser.setEntityResolver(this);
mParser.setErrorHandler(HardErrorHandler.INSTANCE);
super(DTD_PUBLIC_ID, DTD_RESOURCE_NAME);
}
/**
@ -91,37 +80,6 @@ public class PackageNamesLoader
new String[mPackageNames.size()]);
}
/**
* Parses the specified source, loading its package names.
* @param aInputSource the source to parse
* @throws FileNotFoundException if an error occurs
* @throws IOException if an error occurs
* @throws SAXException if an error occurs
*/
private void parseInputSource(InputSource aInputSource)
throws FileNotFoundException, IOException, SAXException
{
mPackageStack.clear();
mParser.parse(aInputSource);
}
/** @see org.xml.sax.EntityResolver */
public InputSource resolveEntity(String aPublicId, String aSystemId)
throws SAXException
{
if (PACKAGE_DTD_PUBLIC_ID.equals(aPublicId)) {
final InputStream dtdIS = getClass().getClassLoader()
.getResourceAsStream(PACKAGE_DTD_RESOURCE);
if (dtdIS == null) {
throw new SAXException(
"Unable to load internal dtd " + PACKAGE_DTD_RESOURCE);
}
return new InputSource(dtdIS);
}
return super.resolveEntity(aPublicId, aSystemId);
}
/** @see org.xml.sax.helpers.DefaultHandler **/
public void startElement(String aNamespaceURI,
String aLocalName,
@ -217,7 +175,7 @@ public class PackageNamesLoader
* @throws CheckstyleException if an error occurs.
*/
private static String[] loadPackageNames(InputSource aSource,
String aSourceName)
String aSourceName)
throws CheckstyleException
{
try {