FileDocCategorySizeDatePackage
NonValidatingConfiguration.javaAPI DocApache Xerces 3.0.128991Fri Sep 14 20:33:56 BST 2007org.apache.xerces.parsers

NonValidatingConfiguration

public class NonValidatingConfiguration extends BasicParserConfiguration implements org.apache.xerces.xni.parser.XMLPullParserConfiguration
This is the non validating parser configuration. It extends the basic configuration with the set of following parser components: Document scanner, DTD scanner, namespace binder, document handler.

Xerces parser that uses this configuration is not conformant non-validating XML processor, since conformant non-validating processor is required to process "all the declarations they read in the internal DTD subset ... must use the information in those declarations to normalize attribute values, include the replacement text of internal entities, and supply default attribute values".

author
Elena Litani, IBM
version
$Id: NonValidatingConfiguration.java 447239 2006-09-18 05:08:26Z mrglavas $

Fields Summary
protected static final String
WARN_ON_DUPLICATE_ATTDEF
Feature identifier: warn on duplicate attribute definition.
protected static final String
WARN_ON_DUPLICATE_ENTITYDEF
Feature identifier: warn on duplicate entity definition.
protected static final String
WARN_ON_UNDECLARED_ELEMDEF
Feature identifier: warn on undeclared element definition.
protected static final String
ALLOW_JAVA_ENCODINGS
Feature identifier: allow Java encodings.
protected static final String
CONTINUE_AFTER_FATAL_ERROR
Feature identifier: continue after fatal error.
protected static final String
LOAD_EXTERNAL_DTD
Feature identifier: load external DTD.
protected static final String
NOTIFY_BUILTIN_REFS
Feature identifier: notify built-in refereces.
protected static final String
NOTIFY_CHAR_REFS
Feature identifier: notify character refereces.
protected static final String
NORMALIZE_DATA
Feature identifier: expose schema normalized value
protected static final String
SCHEMA_ELEMENT_DEFAULT
Feature identifier: send element default value via characters()
protected static final String
ERROR_REPORTER
Property identifier: error reporter.
protected static final String
ENTITY_MANAGER
Property identifier: entity manager.
protected static final String
DOCUMENT_SCANNER
Property identifier document scanner:
protected static final String
DTD_SCANNER
Property identifier: DTD scanner.
protected static final String
XMLGRAMMAR_POOL
Property identifier: grammar pool.
protected static final String
DTD_VALIDATOR
Property identifier: DTD validator.
protected static final String
NAMESPACE_BINDER
Property identifier: namespace binder.
protected static final String
DATATYPE_VALIDATOR_FACTORY
Property identifier: datatype validator factory.
protected static final String
VALIDATION_MANAGER
protected static final String
SCHEMA_VALIDATOR
Property identifier: XML Schema validator.
private static final boolean
PRINT_EXCEPTION_STACK_TRACE
Set to true and recompile to print exception stack trace.
protected org.apache.xerces.xni.grammars.XMLGrammarPool
fGrammarPool
Grammar pool.
protected org.apache.xerces.impl.dv.DTDDVFactory
fDatatypeValidatorFactory
Datatype validator factory.
protected org.apache.xerces.impl.XMLErrorReporter
fErrorReporter
Error reporter.
protected org.apache.xerces.impl.XMLEntityManager
fEntityManager
Entity manager.
protected org.apache.xerces.xni.parser.XMLDocumentScanner
fScanner
Document scanner.
protected org.apache.xerces.xni.parser.XMLInputSource
fInputSource
Input Source
protected org.apache.xerces.xni.parser.XMLDTDScanner
fDTDScanner
DTD scanner.
protected org.apache.xerces.impl.validation.ValidationManager
fValidationManager
private org.apache.xerces.impl.XMLNSDocumentScannerImpl
fNamespaceScanner
Document scanner that does namespace binding.
private org.apache.xerces.impl.XMLDocumentScannerImpl
fNonNSScanner
Default Xerces implementation of scanner
protected boolean
fConfigUpdated
fConfigUpdated is set to true if there has been any change to the configuration settings, i.e a feature or a property was changed.
protected org.apache.xerces.xni.XMLLocator
fLocator
Locator
protected boolean
fParseInProgress
True if a parse is in progress. This state is needed because some features/properties cannot be set while parsing (e.g. validation and namespaces).
Constructors Summary
public NonValidatingConfiguration()
Default constructor.


    //
    // Constructors
    //

       
      
        this(null, null, null);
    
public NonValidatingConfiguration(org.apache.xerces.util.SymbolTable symbolTable)
Constructs a parser configuration using the specified symbol table.

param
symbolTable The symbol table to use.

        this(symbolTable, null, null);
    
public NonValidatingConfiguration(org.apache.xerces.util.SymbolTable symbolTable, org.apache.xerces.xni.grammars.XMLGrammarPool grammarPool)
Constructs a parser configuration using the specified symbol table and grammar pool.

REVISIT: Grammar pool will be updated when the new validation engine is implemented.

param
symbolTable The symbol table to use.
param
grammarPool The grammar pool to use.

        this(symbolTable, grammarPool, null);
    
public NonValidatingConfiguration(org.apache.xerces.util.SymbolTable symbolTable, org.apache.xerces.xni.grammars.XMLGrammarPool grammarPool, org.apache.xerces.xni.parser.XMLComponentManager parentSettings)
Constructs a parser configuration using the specified symbol table, grammar pool, and parent settings.

REVISIT: Grammar pool will be updated when the new validation engine is implemented.

param
symbolTable The symbol table to use.
param
grammarPool The grammar pool to use.
param
parentSettings The parent settings.

        super(symbolTable, parentSettings);

        // add default recognized features
        final String[] recognizedFeatures = {        	
        	PARSER_SETTINGS,
			NAMESPACES,
            //WARN_ON_DUPLICATE_ATTDEF,     // from XMLDTDScannerImpl
            //WARN_ON_UNDECLARED_ELEMDEF,   // from XMLDTDScannerImpl
            //ALLOW_JAVA_ENCODINGS,         // from XMLEntityManager
            CONTINUE_AFTER_FATAL_ERROR,
            //LOAD_EXTERNAL_DTD,    // from XMLDTDScannerImpl
            //NOTIFY_BUILTIN_REFS,  // from XMLDocumentFragmentScannerImpl
            //NOTIFY_CHAR_REFS,		// from XMLDocumentFragmentScannerImpl
            //WARN_ON_DUPLICATE_ENTITYDEF   // from XMLEntityManager
        };
        addRecognizedFeatures(recognizedFeatures);

        // set state for default features
        //setFeature(WARN_ON_DUPLICATE_ATTDEF, false);  // from XMLDTDScannerImpl
        //setFeature(WARN_ON_UNDECLARED_ELEMDEF, false);    // from XMLDTDScannerImpl
        //setFeature(ALLOW_JAVA_ENCODINGS, false);      // from XMLEntityManager
        fFeatures.put(CONTINUE_AFTER_FATAL_ERROR, Boolean.FALSE);
		fFeatures.put(PARSER_SETTINGS, Boolean.TRUE);
		fFeatures.put(NAMESPACES, Boolean.TRUE);
        //setFeature(LOAD_EXTERNAL_DTD, true);      // from XMLDTDScannerImpl
        //setFeature(NOTIFY_BUILTIN_REFS, false);   // from XMLDocumentFragmentScannerImpl
        //setFeature(NOTIFY_CHAR_REFS, false);      // from XMLDocumentFragmentScannerImpl
        //setFeature(WARN_ON_DUPLICATE_ENTITYDEF, false);   // from XMLEntityManager

        // add default recognized properties
        final String[] recognizedProperties = {
            ERROR_REPORTER,             
            ENTITY_MANAGER, 
            DOCUMENT_SCANNER,
            DTD_SCANNER,
            DTD_VALIDATOR,
            NAMESPACE_BINDER,
            XMLGRAMMAR_POOL,   
            DATATYPE_VALIDATOR_FACTORY,
            VALIDATION_MANAGER
        };
        addRecognizedProperties(recognizedProperties);
	
        fGrammarPool = grammarPool;
        if(fGrammarPool != null){
			fProperties.put(XMLGRAMMAR_POOL, fGrammarPool);
        }

        fEntityManager = createEntityManager();
		fProperties.put(ENTITY_MANAGER, fEntityManager);
        addComponent(fEntityManager);

        fErrorReporter = createErrorReporter();
        fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner());
		fProperties.put(ERROR_REPORTER, fErrorReporter);
        addComponent(fErrorReporter);

        // this configuration delays creation of the scanner
        // till it is known if namespace processing should be performed

        fDTDScanner = createDTDScanner();
        if (fDTDScanner != null) {
			fProperties.put(DTD_SCANNER, fDTDScanner);
            if (fDTDScanner instanceof XMLComponent) {
                addComponent((XMLComponent)fDTDScanner);
            }
        }

        fDatatypeValidatorFactory = createDatatypeValidatorFactory();
        if (fDatatypeValidatorFactory != null) {
			fProperties.put(DATATYPE_VALIDATOR_FACTORY,
                        fDatatypeValidatorFactory);
        }
        fValidationManager = createValidationManager();

        if (fValidationManager != null) {
			fProperties.put(VALIDATION_MANAGER, fValidationManager);
        }
        // add message formatters
        if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) {
            XMLMessageFormatter xmft = new XMLMessageFormatter();
            fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft);
            fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft);
        }
        
		fConfigUpdated = false;

        // set locale
        try {
            setLocale(Locale.getDefault());
        }
        catch (XNIException e) {
            // do nothing
            // REVISIT: What is the right thing to do? -Ac
        }

    
Methods Summary
protected voidcheckFeature(java.lang.String featureId)
Check a feature. If feature is know and supported, this method simply returns. Otherwise, the appropriate exception is thrown.

param
featureId The unique identifier (URI) of the feature.
throws
XMLConfigurationException Thrown for configuration error. In general, components should only throw this exception if it is really a critical error.


        //
        // Xerces Features
        //

        if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
            final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
        	
            //
            // http://apache.org/xml/features/validation/dynamic
            //   Allows the parser to validate a document only when it
            //   contains a grammar. Validation is turned on/off based
            //   on each document instance, automatically.
            //
            if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE.length() && 
                featureId.endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) {
                return;
            }
            //
            // http://apache.org/xml/features/validation/default-attribute-values
            //
            if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE.length() && 
                featureId.endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) {
                // REVISIT
                short type = XMLConfigurationException.NOT_SUPPORTED;
                throw new XMLConfigurationException(type, featureId);
            }
            //
            // http://apache.org/xml/features/validation/default-attribute-values
            //
            if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE.length() && 
                featureId.endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) {
                // REVISIT
                short type = XMLConfigurationException.NOT_SUPPORTED;
                throw new XMLConfigurationException(type, featureId);
            }
            //
            // http://apache.org/xml/features/validation/nonvalidating/load-dtd-grammar
            //
            if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE.length() && 
                featureId.endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) {
                return;
            }
            //
            // http://apache.org/xml/features/validation/nonvalidating/load-external-dtd
            //
            if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() && 
                featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) {
                return;
            }

            //
            // http://apache.org/xml/features/validation/default-attribute-values
            //
            if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE.length() && 
                featureId.endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) {
                short type = XMLConfigurationException.NOT_SUPPORTED;
                throw new XMLConfigurationException(type, featureId);
            }
        }

        //
        // Not recognized
        //

        super.checkFeature(featureId);

    
protected voidcheckProperty(java.lang.String propertyId)
Check a property. If the property is know and supported, this method simply returns. Otherwise, the appropriate exception is thrown.

param
propertyId The unique identifier (URI) of the property being set.
throws
XMLConfigurationException Thrown for configuration error. In general, components should only throw this exception if it is really a critical error.


        //
        // Xerces Properties
        //

        if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
            final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();
            
            if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() && 
                propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) {
                return;
            }
        }

        if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) {
            final int suffixLength = propertyId.length() - Constants.JAXP_PROPERTY_PREFIX.length();

            if (suffixLength == Constants.SCHEMA_SOURCE.length() && 
                propertyId.endsWith(Constants.SCHEMA_SOURCE)) {
                return;
            }
        }

        //
        // Not recognized
        //

        super.checkProperty(propertyId);

    
public voidcleanup()
If the application decides to terminate parsing before the xml document is fully parsed, the application should call this method to free any resource allocated during parsing. For example, close all opened streams.

        fEntityManager.closeReaders();
    
protected voidconfigurePipeline()
Configures the pipeline.

        // create appropriate scanner
        // and register it as one of the components.
        if (fFeatures.get(NAMESPACES) == Boolean.TRUE) {
            if (fNamespaceScanner == null) {
                fNamespaceScanner = new XMLNSDocumentScannerImpl();
                addComponent((XMLComponent)fNamespaceScanner);
            }
            fProperties.put(DOCUMENT_SCANNER, fNamespaceScanner);
            fNamespaceScanner.setDTDValidator(null);
            fScanner = fNamespaceScanner;
        } 
        else {
            if (fNonNSScanner == null) {
                fNonNSScanner = new XMLDocumentScannerImpl();
                addComponent((XMLComponent)fNonNSScanner);
            }
            fProperties.put(DOCUMENT_SCANNER, fNonNSScanner);
            fScanner = fNonNSScanner;
        }

        fScanner.setDocumentHandler(fDocumentHandler);
        fLastComponent = fScanner;
        // setup dtd pipeline
        if (fDTDScanner != null) {
                fDTDScanner.setDTDHandler(fDTDHandler);
                fDTDScanner.setDTDContentModelHandler(fDTDContentModelHandler);
        }


    
protected org.apache.xerces.xni.parser.XMLDTDScannercreateDTDScanner()
Create a DTD scanner.

        return new XMLDTDScannerImpl();
    
protected org.apache.xerces.impl.dv.DTDDVFactorycreateDatatypeValidatorFactory()
Create a datatype validator factory.

        return DTDDVFactory.getInstance();
    
protected org.apache.xerces.xni.parser.XMLDocumentScannercreateDocumentScanner()
Create a document scanner.

        return null;
    
protected org.apache.xerces.impl.XMLEntityManagercreateEntityManager()
Creates an entity manager.

        return new XMLEntityManager();
    
protected org.apache.xerces.impl.XMLErrorReportercreateErrorReporter()
Creates an error reporter.

        return new XMLErrorReporter();
    
protected org.apache.xerces.impl.validation.ValidationManagercreateValidationManager()

        return new ValidationManager();
    
public booleangetFeature(java.lang.String featureId)

			// make this feature special
		if (featureId.equals(PARSER_SETTINGS)){
			return fConfigUpdated;
		}
		return super.getFeature(featureId);

	
public booleanparse(boolean complete)
Parses the document in a pull parsing fashion.

param
complete True if the pull parser should parse the remaining document completely.
return
True if there is more document to parse.
exception
XNIException Any XNI exception, possibly wrapping another exception.
exception
IOException An IO exception from the parser, possibly from a byte stream or character stream supplied by the parser.
see
#setInputSource

        //
        // reset and configure pipeline and set InputSource.
        if (fInputSource !=null) {
            try {
                // resets and sets the pipeline.
                reset();
                fScanner.setInputSource(fInputSource);
                fInputSource = null;
            } 
            catch (XNIException ex) {
                if (PRINT_EXCEPTION_STACK_TRACE)
                    ex.printStackTrace();
                throw ex;
            } 
            catch (IOException ex) {
                if (PRINT_EXCEPTION_STACK_TRACE)
                    ex.printStackTrace();
                throw ex;
            } 
            catch (RuntimeException ex) {
                if (PRINT_EXCEPTION_STACK_TRACE)
                    ex.printStackTrace();
                throw ex;
            }
            catch (Exception ex) {
                if (PRINT_EXCEPTION_STACK_TRACE)
                    ex.printStackTrace();
                throw new XNIException(ex);
            }
        }

        try {
            return fScanner.scanDocument(complete);
        } 
        catch (XNIException ex) {
            if (PRINT_EXCEPTION_STACK_TRACE)
                ex.printStackTrace();
            throw ex;
        } 
        catch (IOException ex) {
            if (PRINT_EXCEPTION_STACK_TRACE)
                ex.printStackTrace();
            throw ex;
        } 
        catch (RuntimeException ex) {
            if (PRINT_EXCEPTION_STACK_TRACE)
                ex.printStackTrace();
            throw ex;
        }
        catch (Exception ex) {
            if (PRINT_EXCEPTION_STACK_TRACE)
                ex.printStackTrace();
            throw new XNIException(ex);
        }

    
public voidparse(org.apache.xerces.xni.parser.XMLInputSource source)
Parses the specified input source.

param
source The input source.
exception
XNIException Throws exception on XNI error.
exception
java.io.IOException Throws exception on i/o error.


        if (fParseInProgress) {
            // REVISIT - need to add new error message
            throw new XNIException("FWK005 parse may not be called while parsing.");
        }
        fParseInProgress = true;

        try {
            setInputSource(source);
            parse(true);
        } 
        catch (XNIException ex) {
            if (PRINT_EXCEPTION_STACK_TRACE)
                ex.printStackTrace();
            throw ex;
        } 
        catch (IOException ex) {
            if (PRINT_EXCEPTION_STACK_TRACE)
                ex.printStackTrace();
            throw ex;
        }
        catch (RuntimeException ex) {
            if (PRINT_EXCEPTION_STACK_TRACE)
                ex.printStackTrace();
            throw ex;
        }              
        catch (Exception ex) {
            if (PRINT_EXCEPTION_STACK_TRACE)
                ex.printStackTrace();
            throw new XNIException(ex);
        }
        finally {
            fParseInProgress = false;
            // close all streams opened by xerces
            this.cleanup();
        }

    
protected voidreset()
Reset all components before parsing.

throws
XNIException Thrown if an error occurs during initialization.


        if (fValidationManager != null)
            fValidationManager.reset();
        // configure the pipeline and initialize the components
        configurePipeline();
        super.reset();

    
public voidsetFeature(java.lang.String featureId, boolean state)

		fConfigUpdated = true;
		super.setFeature(featureId, state);
	
public voidsetInputSource(org.apache.xerces.xni.parser.XMLInputSource inputSource)
Sets the input source for the document to parse.

param
inputSource The document's input source.
exception
XMLConfigurationException Thrown if there is a configuration error when initializing the parser.
exception
IOException Thrown on I/O error.
see
#parse(boolean)

        
        // REVISIT: this method used to reset all the components and
        //          construct the pipeline. Now reset() is called
        //          in parse (boolean) just before we parse the document
        //          Should this method still throw exceptions..?

        fInputSource = inputSource;
        
    
public voidsetLocale(java.util.Locale locale)
Set the locale to use for messages.

param
locale The locale object to use for localization of messages.
exception
XNIException Thrown if the parser does not support the specified locale.

        super.setLocale(locale);
        fErrorReporter.setLocale(locale);
    
public voidsetProperty(java.lang.String propertyId, java.lang.Object value)

		fConfigUpdated = true;
		super.setProperty(propertyId, value);