FileDocCategorySizeDatePackage
SAXParserImpl.javaAPI DocJava SE 5 API17743Fri Aug 26 14:55:54 BST 2005com.sun.org.apache.xerces.internal.jaxp

SAXParserImpl

public class SAXParserImpl extends SAXParser implements JAXPConstants
This is the implementation specific class for the javax.xml.parsers.SAXParser.
author
Rajiv Mordani
author
Edwin Goei
version
$Id: SAXParserImpl.java,v 1.23 2004/02/24 23:15:58 mrglavas Exp $

Fields Summary
private final com.sun.org.apache.xerces.internal.parsers.JAXPConfiguration
parserConfiguration
private final AbstractSAXParser
xmlReader
private String
schemaLanguage
private final Schema
grammar
private final SecurityManager
secureProcessing
private boolean
isXIncludeAware
private boolean
enableSecureProcessing
private Hashtable
features
private SAXParserFactory
spf
private Hashtable
parserFeatures
Constructors Summary
SAXParserImpl(SAXParserFactory spfactory, Hashtable factoryfeatures)
Create a SAX parser with the associated features

param
features Hashtable of SAX features, may be null

        this.spf = spfactory ;
        this.features = factoryfeatures;
        // inherit 
		secureProcessing = new SecurityManager();
        this.grammar = spf.getSchema();        
        parserConfiguration = new JAXPConfiguration(grammar);
        xmlReader = new com.sun.org.apache.xerces.internal.parsers.SAXParser(parserConfiguration);
        
        //initialize the feature as per factory settings..
        init();
    
Methods Summary
public org.xml.sax.ParsergetParser()

deprecated

        return xmlReader;
    
public java.lang.ObjectgetProperty(java.lang.String name)
returns the particular property requested for in the underlying implementation of org.xml.sax.XMLReader.

        // TODO: reroute those properties to use new JAXP1.3 API. -KK
        if (JAXP_SCHEMA_LANGUAGE.equals(name)) {
            // JAXP 1.2 support
            return schemaLanguage;
        } else {
            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;
    
voidinit()

                
        schemaLanguage = null ;
        this.isXIncludeAware = spf.isXIncludeAware();
		if(features != null ){
       		Object tmpValue = features.get(Constants.FEATURE_SECURE_PROCESSING);
			if( tmpValue != null ) 
				enableSecureProcessing = ((Boolean)tmpValue).booleanValue();
		}
		
		if(enableSecureProcessing){
        	try {
            	setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY, secureProcessing);
            } catch (SAXNotRecognizedException sex) {
                sex.printStackTrace();
            } catch (SAXNotSupportedException se) {
                se.printStackTrace();
            }
        }
        
		xmlReader.setFeature(
                Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_AWARE,
                isXIncludeAware);
        
        // If validating, provide a default ErrorHandler that prints
        // validation errors with a warning telling the user to set an
        // ErrorHandler.
        if (spf.isValidating()) {
            xmlReader.setErrorHandler(new DefaultValidationErrorHandler());
        }

        xmlReader.setFeature(Constants.SAX_FEATURE_PREFIX +
                             Constants.VALIDATION_FEATURE, spf.isValidating());

        // 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.setFeature(Constants.SAX_FEATURE_PREFIX +
                             Constants.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.setFeature(Constants.SAX_FEATURE_PREFIX +
                             Constants.NAMESPACE_PREFIXES_FEATURE,
                             !spf.isNamespaceAware());
        setFeatures(features);
        
    
public booleanisNamespaceAware()

        try {
            return xmlReader.getFeature(Constants.SAX_FEATURE_PREFIX +
                                        Constants.NAMESPACES_FEATURE);
        } catch (SAXException x) {
            throw new IllegalStateException(x.getMessage());
        }
    
public booleanisSecureProcessing()

Return True if secure processing in effect.

Defaults to false.

return
state of Schema Caching

        return secureProcessing!=null;
    
public booleanisValidating()

        try {
            return xmlReader.getFeature(Constants.SAX_FEATURE_PREFIX +
                                        Constants.VALIDATION_FEATURE);
        } catch (SAXException x) {
            throw new IllegalStateException(x.getMessage());
        }
    
public booleanisXIncludeAware()

    
       
        return isXIncludeAware;
    
public voidreset()

Reset this DocumentBuilder to its original configuration.

DocumentBuilder is reset to the same state as when it was created with {@link DocumentBuilderFactory#newDocumentBuilder()}. reset() is designed to allow the reuse of existing DocumentBuilders thus saving resources associated with the creation of new DocumentBuilders.

The reset DocumentBuilder is not guaranteed to have the same {@link EntityResolver} or {@link ErrorHandler} Objects, e.g. {@link Object#equals(Object obj)}. It is guaranteed to have a functionally equal EntityResolver and ErrorHandler.

since
1.5

        if(xmlReader != null){
            try{
                xmlReader.reset();
                //set the object back to its factory settings
                resetSettings();
                
            }
            //xxx: underlying implementation reset throws XNIException what should we do in this case ?
            //if there was any propery that is not being supported
            //exception would have been thrown when setting it on the underlying implementation.
            catch(XNIException ex){
                //coninue.
            }
            catch(SAXException sax){}
        }
    
voidresetSettings()
Reset this instance back to factory settings

        Enumeration keys  = parserFeatures.keys();        
        
        while(keys.hasMoreElements()){
            String propertyId = (String)keys.nextElement();
            Object value = parserFeatures.get(propertyId);
            if(value instanceof Boolean){
                //System.out.println("Remvoing feature = " + propertyId + " with value = " + parserConfiguration.getFeatureDefaultValue(propertyId));
                //this means it is a feature, we have to get default value from the configuration
                xmlReader.setFeature(propertyId, parserConfiguration.getFeatureDefaultValue(propertyId));
            }
            else{//it's a property
                //System.out.println("Remvoing property = " + propertyId);
                //null value should delete the property from underlying implementation.
                xmlReader.setProperty(propertyId, null);
            }
        }
        //clear the hashtable once we have removed all the properties.
        parserFeatures.clear();
        
    
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();
				if(!feature.equals(Constants.FEATURE_SECURE_PROCESSING))
                	xmlReader.setFeature(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.

        // the spec says if a schema is given via SAXParserFactory
        // the JAXP 1.2 properties shouldn't be allowed. So
        // reject them first.
        if(grammar!=null) {
            if (JAXP_SCHEMA_LANGUAGE.equals(name)
            ||  JAXP_SCHEMA_SOURCE.equals(name)) {
                throw new SAXNotSupportedException(
                    SAXMessageFormatter.formatMessage(null, "schema-already-specified", null));
            }
        }
        
        if (JAXP_SCHEMA_LANGUAGE.equals(name)) {
            // JAXP 1.2 support            
            if ( W3C_XML_SCHEMA.equals(value) ) {
                //None of the properties will take effect till the setValidating(true) has been called                                                        
                if( isValidating() ) {
                    schemaLanguage = W3C_XML_SCHEMA;
                    xmlReader.setFeature(Constants.XERCES_FEATURE_PREFIX +
                                     Constants.SCHEMA_VALIDATION_FEATURE,
                                     true);
                    //also set the schema full checking to true.
                    xmlReader.setFeature(Constants.XERCES_FEATURE_PREFIX +
                                     Constants.SCHEMA_FULL_CHECKING,
                                     true);
                    //add this among the list of parser  features since this is added 
                    //on the parser instance and should be set to default value during 
                    //reset.
                    parserFeatures.put(Constants.XERCES_FEATURE_PREFIX +
                                     Constants.SCHEMA_VALIDATION_FEATURE, new Boolean(true));
                    parserFeatures.put(Constants.XERCES_FEATURE_PREFIX +
                                     Constants.SCHEMA_FULL_CHECKING, new Boolean(true));
                                     
                    // this will allow the parser not to emit DTD-related
                    // errors, as the spec demands
                    xmlReader.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
                    parserFeatures.put(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
                    
                }
                else{
                    //System.out.println("Property = " + name + "is not set");
                }
                
            } else if (value == null) {
                schemaLanguage = null;
                xmlReader.setFeature(Constants.XERCES_FEATURE_PREFIX +
                                     Constants.SCHEMA_VALIDATION_FEATURE,
                                     false);
                parserFeatures.put(Constants.XERCES_FEATURE_PREFIX +
                                     Constants.SCHEMA_VALIDATION_FEATURE,
                                     new Boolean(false));
                
            } else {
                // REVISIT: It would be nice if we could format this message
                // using a user specified locale as we do in the underlying
                // XMLReader -- mrglavas
                throw new SAXNotSupportedException(
                    SAXMessageFormatter.formatMessage(null, "schema-not-supported", null));
            }
        } 
        else if(JAXP_SCHEMA_SOURCE.equals(name)) {
            //If we are not validating then don't check for  JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCED            
            if ( isValidating() ) {
                String val = (String)getProperty(JAXP_SCHEMA_LANGUAGE);
                if ( val != null && W3C_XML_SCHEMA.equals(val) ) {
                    xmlReader.setProperty(name, value);
                    parserFeatures.put(name, value);
                }
                else {
                    throw new SAXNotSupportedException(
                        SAXMessageFormatter.formatMessage(null, 
                        "jaxp-order-not-supported", 
                        new Object[] {JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCE}));
                }
            }
		}
		/*else if(name.equlas(Constants.ENTITY_EXPANSION_LIMIT)){
			String elimit = (String)value;
			if(elimit != null && elimit != ""){
				int val = Integer.parseInt(elimit);
				secureProcessing.setEntityExpansionLimit(val);
			}
		}else if(name.equals(Constants.MAX_OCCUR_LIMIT)) {
			String mlimit = (String)value;
			if(mlimit != null && mlimit != ""){
				int val = Integer.parseInt(mlimit);
				secureProcessing.setMaxOccurNodeLimit(val);
			}
		}*/
        else if(value instanceof Boolean){
            //assume feature
            xmlReader.setFeature(name,((Boolean)value).booleanValue());
            parserFeatures.put(name, value);
        }else{
            xmlReader.setProperty(name, value);
            parserFeatures.put(name, value);
        }