FileDocCategorySizeDatePackage
SAXParserImpl.javaAPI DocApache Xerces 3.0.127468Fri Sep 14 20:33:54 BST 2007org.apache.xerces.jaxp

SAXParserImpl

public class SAXParserImpl extends SAXParser implements org.apache.xerces.xs.PSVIProvider, JAXPConstants
This is the implementation specific class for the javax.xml.parsers.SAXParser.
author
Rajiv Mordani
author
Edwin Goei
version
$Id: SAXParserImpl.java 520058 2007-03-19 19:33:53Z mrglavas $

Fields Summary
private static final String
NAMESPACES_FEATURE
Feature identifier: namespaces.
private static final String
NAMESPACE_PREFIXES_FEATURE
Feature identifier: namespace prefixes.
private static final String
VALIDATION_FEATURE
Feature identifier: validation.
private static final String
XMLSCHEMA_VALIDATION_FEATURE
Feature identifier: XML Schema validation
private static final String
XINCLUDE_FEATURE
Feature identifier: XInclude processing
private static final String
SECURITY_MANAGER
Property identifier: security manager.
private JAXPSAXParser
xmlReader
private String
schemaLanguage
private final Schema
grammar
private org.apache.xerces.xni.parser.XMLComponent
fSchemaValidator
private org.apache.xerces.xni.parser.XMLComponentManager
fSchemaValidatorComponentManager
private org.apache.xerces.impl.validation.ValidationManager
fSchemaValidationManager
private UnparsedEntityHandler
fUnparsedEntityHandler
private final ErrorHandler
fInitErrorHandler
Initial ErrorHandler
private final EntityResolver
fInitEntityResolver
Initial EntityResolver
Constructors Summary
SAXParserImpl(SAXParserFactoryImpl spf, Hashtable features)
Create a SAX parser with the associated features

param
features Hashtable of SAX features, may be null

    
                          
        
          
        this(spf, features, false);
    
SAXParserImpl(SAXParserFactoryImpl spf, Hashtable features, boolean secureProcessing)
Create a SAX parser with the associated features

param
features Hashtable of SAX features, may be null

        // Instantiate a SAXParser directly and not through SAX so that we use the right ClassLoader
        xmlReader = new JAXPSAXParser(this);

        // JAXP "namespaceAware" == SAX Namespaces feature
        // Note: there is a compatibility problem here with default values:
        // JAXP default is false while SAX 2 default is true!
        xmlReader.setFeature0(NAMESPACES_FEATURE, spf.isNamespaceAware());

        // SAX "namespaces" and "namespace-prefixes" features should not
        // both be false.  We make them opposite for backward compatibility
        // since JAXP 1.0 apps may want to receive xmlns* attributes.
        xmlReader.setFeature0(NAMESPACE_PREFIXES_FEATURE, !spf.isNamespaceAware());
        
        // Avoid setting the XInclude processing feature if the value is false.
        // This will keep the configuration from throwing an exception if it
        // does not support XInclude.
        if (spf.isXIncludeAware()) {
            xmlReader.setFeature0(XINCLUDE_FEATURE, true);
        }
        
        // If the secure processing feature is on set a security manager.
        if (secureProcessing) {
            xmlReader.setProperty0(SECURITY_MANAGER, new SecurityManager());
        }
        
        // Set application's features, followed by validation features.
        setFeatures(features);
        
        // If validating, provide a default ErrorHandler that prints
        // validation errors with a warning telling the user to set an
        // ErrorHandler.
        if (spf.isValidating()) {
            fInitErrorHandler = new DefaultValidationErrorHandler();
            xmlReader.setErrorHandler(fInitErrorHandler);
        }
        else {
            fInitErrorHandler = xmlReader.getErrorHandler();
        }
        xmlReader.setFeature0(VALIDATION_FEATURE, spf.isValidating());
        
        // Get the Schema object from the factory
        this.grammar = spf.getSchema();
        if (grammar != null) {
            XMLParserConfiguration config = xmlReader.getXMLParserConfiguration();
            XMLComponent validatorComponent = null;
            /** For Xerces grammars, use built-in schema validator. **/
            if (grammar instanceof XSGrammarPoolContainer) {
                validatorComponent = new XMLSchemaValidator();
                fSchemaValidationManager = new ValidationManager();
                fUnparsedEntityHandler = new UnparsedEntityHandler(fSchemaValidationManager);
                config.setDTDHandler(fUnparsedEntityHandler);
                fUnparsedEntityHandler.setDTDHandler(xmlReader);
                xmlReader.setDTDSource(fUnparsedEntityHandler);
                fSchemaValidatorComponentManager = new SchemaValidatorConfiguration(config, 
                        (XSGrammarPoolContainer) grammar, fSchemaValidationManager);
            }
            /** For third party grammars, use the JAXP validator component. **/
            else {
                validatorComponent = new JAXPValidatorComponent(grammar.newValidatorHandler());
                fSchemaValidatorComponentManager = config;
            }
            config.addRecognizedFeatures(validatorComponent.getRecognizedFeatures());
            config.addRecognizedProperties(validatorComponent.getRecognizedProperties());
            config.setDocumentHandler((XMLDocumentHandler) validatorComponent);
            ((XMLDocumentSource)validatorComponent).setDocumentHandler(xmlReader);
            xmlReader.setDocumentSource((XMLDocumentSource) validatorComponent);
            fSchemaValidator = validatorComponent;
        }
        
        // Initial EntityResolver
        fInitEntityResolver = xmlReader.getEntityResolver();
    
Methods Summary
public org.apache.xerces.xs.AttributePSVIgetAttributePSVI(int index)

        return ((PSVIProvider)xmlReader).getAttributePSVI(index);
    
public org.apache.xerces.xs.AttributePSVIgetAttributePSVIByName(java.lang.String uri, java.lang.String localname)

        return ((PSVIProvider)xmlReader).getAttributePSVIByName(uri, localname);
    
public org.apache.xerces.xs.ElementPSVIgetElementPSVI()

        return ((PSVIProvider)xmlReader).getElementPSVI();
    
public org.xml.sax.ParsergetParser()

        // Xerces2 AbstractSAXParser implements SAX1 Parser
        // assert(xmlReader instanceof Parser);
        return (Parser) xmlReader;
    
public java.lang.ObjectgetProperty(java.lang.String name)
returns the particular property requested for in the underlying implementation of org.xml.sax.XMLReader.

        return xmlReader.getProperty(name);
    
public javax.xml.validation.SchemagetSchema()

        return grammar;
    
public org.xml.sax.XMLReadergetXMLReader()
Returns the XMLReader that is encapsulated by the implementation of this class.

        return xmlReader;
    
public booleanisNamespaceAware()

        try {
            return xmlReader.getFeature(NAMESPACES_FEATURE);
        } 
        catch (SAXException x) {
            throw new IllegalStateException(x.getMessage());
        }
    
public booleanisValidating()

        try {
            return xmlReader.getFeature(VALIDATION_FEATURE);
        } 
        catch (SAXException x) {
            throw new IllegalStateException(x.getMessage());
        }
    
public booleanisXIncludeAware()
Gets the XInclude processing mode for this parser

return
the state of XInclude processing mode

        try {
            return xmlReader.getFeature(XINCLUDE_FEATURE);
        }
        catch (SAXException exc) {
            return false;
        }
    
public voidparse(org.xml.sax.InputSource is, org.xml.sax.helpers.DefaultHandler dh)

        if (is == null) {
            throw new IllegalArgumentException();
        }
        if (dh != null) {
            xmlReader.setContentHandler(dh);
            xmlReader.setEntityResolver(dh);
            xmlReader.setErrorHandler(dh);
            xmlReader.setDTDHandler(dh);
            xmlReader.setDocumentHandler(null);
        }
        xmlReader.parse(is);
    
public voidparse(org.xml.sax.InputSource is, org.xml.sax.HandlerBase hb)

        if (is == null) {
            throw new IllegalArgumentException();
        }
        if (hb != null) {
            xmlReader.setDocumentHandler(hb);
            xmlReader.setEntityResolver(hb);
            xmlReader.setErrorHandler(hb);
            xmlReader.setDTDHandler(hb);
            xmlReader.setContentHandler(null);
        }
        xmlReader.parse(is);
    
public voidreset()

        try {
            /** Restore initial values of features and properties. **/
            xmlReader.restoreInitState();
        } 
        catch (SAXException exc) {
            // This should never happen. We only store recognized
            // features and properties in the hash maps. For now
            // just ignore it.
        }
        /** Restore various handlers. **/
        xmlReader.setContentHandler(null);
        xmlReader.setDTDHandler(null);
        if (xmlReader.getErrorHandler() != fInitErrorHandler) {
            xmlReader.setErrorHandler(fInitErrorHandler);
        }
        if (xmlReader.getEntityResolver() != fInitEntityResolver) {
            xmlReader.setEntityResolver(fInitEntityResolver);
        }
    
private voidsetFeatures(java.util.Hashtable features)
Set any features of our XMLReader based on any features set on the SAXParserFactory. XXX Does not handle possible conflicts between SAX feature names and JAXP specific feature names, eg. SAXParserFactory.isValidating()

        if (features != null) {
            for (Enumeration e = features.keys(); e.hasMoreElements();) {
                String feature = (String)e.nextElement();
                boolean value = ((Boolean)features.get(feature)).booleanValue();
                xmlReader.setFeature0(feature, value);
            }
        }
    
public voidsetProperty(java.lang.String name, java.lang.Object value)
Sets the particular property in the underlying implementation of org.xml.sax.XMLReader.

        xmlReader.setProperty(name, value);