【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了XMLSAXParser.java提供大家参考,希望对大家有所帮助!
/*
* IBM Confidential OCO Source Material
* 5639-D57 (C) COPYRIGHT International Business Machines Corp. 2002.
* The source code for this program is not published or otherwise divested
* of its trade secrets, irrespective of what has been deposited with the
* U.S. Copyright Office.
*/
import java.io.IOException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/**
* This class is a parser wrapper and an extension of DefaultHandler to serve as an SAX parser
* with error handling.
*/
public class XMLSAXParser extends DefaultHandler {
// Standard XML Parser features.
public static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces";
public static final String NAMESPACE_PREFIXES_FEATURE_ID = "http://
xml.org/sax/features/namespace-prefixes";
public static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation";
public static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema";
public static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking";
public static final String DYNAMIC_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/dynamic";
// Standard XML Parser property.
public static final String SCHEMA_LOCATION_PROPERTY_KEY = "http://apache.org/xml/properties/schema/external-schemaLocation";
// Apache's implementation of SAX Parser.
protected static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";
// Indicate whether there is any parsing error?
protected static boolean parsingSuccess = true;
// Message types for printing parsing error messages.
protected static final String MSG_WARNING = "Warning";
protected static final String MSG_ERROR = "Error";
protected static final String MSG_FATAL_ERROR = "Fatal Error";
protected XMLReader parser;
/**
* Default constructor
*/
public XMLSAXParser() {
try {
parser = XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME);
}
catch (Exception e) {
System.err.println("error: Unable to instantiate parser ("+DEFAULT_PARSER_NAME+")");
}
parser.setErrorHandler(this);
} // Constructor
/**
* Parse the input XML file.
*
* @param The URI of the XML file.
*/
public void parse(String uri) throws IOException, SAXException {
parsingSuccess = true;
parser.parse(uri);
} // parse
/**
* Check whether XML parsing is successful.
*
* @return true/false
*/
public boolean getParsingSuccess() {
return parsingSuccess;
} // getParsingSuccess
/**
* Turn on/off XML Parser's feature.
*
* @param featureId - Parser's feature ID.
* @param state - true/false
*/
public void setFeature(String featureId, boolean state) throws SAXException {
if (parser == null) {
return;
}
parser.setFeature(featureId, state);
} // setFeature
/**
* Sets XML Parser's property.
*
* @param name - Property name.
* @param value - Property value.
*/
public void setProperty(String name, String value) throws SAXException {
if (parser == null) {
return;
}
parser.setProperty(name, value);
} // setProperty
//
// ErrorHandler methods
//
/**
* Called by the XML Parser when there is a parsing warning.
*
* @param ex - The warning information encoded as an exception.
*/
public void warning(SAXParseException ex) throws SAXException {
printError(MSG_WARNING, ex);
} // warning
/**
* Called by the XML Parser when there is a parsing error.
*
* @param ex - The error information encoded as an exception.
*/
public void error(SAXParseException ex) throws SA