FileDocCategorySizeDatePackage
XMLSchemaValidatorComponentManager.javaAPI DocApache Xerces 3.0.121303Fri Sep 14 20:33:52 BST 2007org.apache.xerces.jaxp.validation

XMLSchemaValidatorComponentManager

public final class XMLSchemaValidatorComponentManager extends org.apache.xerces.util.ParserConfigurationSettings implements org.apache.xerces.xni.parser.XMLComponentManager

An implementation of XMLComponentManager for a schema validator.

author
Michael Glavassevich, IBM
version
$Id: XMLSchemaValidatorComponentManager.java 447235 2006-09-18 05:01:44Z mrglavas $

Fields Summary
private static final String
SCHEMA_VALIDATION
Feature identifier: schema validation.
private static final String
VALIDATION
Feature identifier: validation.
private static final String
USE_GRAMMAR_POOL_ONLY
Feature identifier: use grammar pool only.
protected static final String
IGNORE_XSI_TYPE
Feature identifier: whether to ignore xsi:type attributes until a global element declaration is encountered
protected static final String
ID_IDREF_CHECKING
Feature identifier: whether to ignore ID/IDREF errors
protected static final String
UNPARSED_ENTITY_CHECKING
Feature identifier: whether to ignore unparsed entity errors
protected static final String
IDENTITY_CONSTRAINT_CHECKING
Feature identifier: whether to ignore identity constraint errors
private static final String
ENTITY_MANAGER
Property identifier: entity manager.
private static final String
ENTITY_RESOLVER
Property identifier: entity resolver.
private static final String
ERROR_HANDLER
Property identifier: error handler.
private static final String
ERROR_REPORTER
Property identifier: error reporter.
private static final String
NAMESPACE_CONTEXT
Property identifier: namespace context.
private static final String
SCHEMA_VALIDATOR
Property identifier: XML Schema validator.
private static final String
SECURITY_MANAGER
Property identifier: security manager.
private static final String
SYMBOL_TABLE
Property identifier: symbol table.
private static final String
VALIDATION_MANAGER
Property identifier: validation manager.
private static final String
XMLGRAMMAR_POOL
Property identifier: grammar pool.
private 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.
private boolean
fUseGrammarPoolOnly
Tracks whether the validator should use components from the grammar pool to the exclusion of all others.
private final HashMap
fComponents
Lookup map for components required for validation.
private org.apache.xerces.impl.XMLEntityManager
fEntityManager
Entity manager.
private org.apache.xerces.impl.XMLErrorReporter
fErrorReporter
Error reporter.
private org.apache.xerces.xni.NamespaceContext
fNamespaceContext
Namespace context.
private org.apache.xerces.impl.xs.XMLSchemaValidator
fSchemaValidator
XML Schema validator.
private org.apache.xerces.impl.validation.ValidationManager
fValidationManager
Validation manager.
private HashMap
fInitFeatures
Stores initial feature values for validator reset.
private HashMap
fInitProperties
Stores initial property values for validator reset.
private org.apache.xerces.util.SecurityManager
fInitSecurityManager
Stores the initial security manager.
private ErrorHandler
fErrorHandler
Application's ErrorHandler.
private LSResourceResolver
fResourceResolver
Application's LSResourceResolver.
Constructors Summary
public XMLSchemaValidatorComponentManager(XSGrammarPoolContainer grammarContainer)
Constructs a component manager suitable for Xerces' schema validator.

    
              
       
        
        // setup components 
        fEntityManager = new XMLEntityManager();
        fComponents.put(ENTITY_MANAGER, fEntityManager);
        
        fErrorReporter = new XMLErrorReporter();
        fComponents.put(ERROR_REPORTER, fErrorReporter);
        
        fNamespaceContext = new NamespaceSupport();
        fComponents.put(NAMESPACE_CONTEXT, fNamespaceContext);
        
        fSchemaValidator = new XMLSchemaValidator();
        fComponents.put(SCHEMA_VALIDATOR, fSchemaValidator);
        
        fValidationManager = new ValidationManager();
        fComponents.put(VALIDATION_MANAGER, fValidationManager);
        
        // setup other properties
        fComponents.put(ENTITY_RESOLVER, null);
        fComponents.put(ERROR_HANDLER, null);
        fComponents.put(SECURITY_MANAGER, null);
        fComponents.put(SYMBOL_TABLE, new SymbolTable());
        
        // setup grammar pool
        fComponents.put(XMLGRAMMAR_POOL, grammarContainer.getGrammarPool());
        fUseGrammarPoolOnly = grammarContainer.isFullyComposed();
        
        // add schema message formatter to error reporter
        fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter());
        
        // add all recognized features and properties and apply their defaults
        addRecognizedParamsAndSetDefaults(fEntityManager, grammarContainer);
        addRecognizedParamsAndSetDefaults(fErrorReporter, grammarContainer);
        addRecognizedParamsAndSetDefaults(fSchemaValidator, grammarContainer);
        
        // if the secure processing feature is set to true, add a security manager to the configuration
        Boolean secureProcessing = grammarContainer.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
        if (Boolean.TRUE.equals(secureProcessing)) {
            fInitSecurityManager = new SecurityManager();
        }
        fComponents.put(SECURITY_MANAGER, fInitSecurityManager);
        
        /* TODO: are other XMLSchemaValidator default values never set?
         * Initial investigation indicates that they aren't set, but
         * that they all have default values of false, so it works out
         * anyway -PM
         */
        fFeatures.put(IGNORE_XSI_TYPE, Boolean.FALSE);
        fFeatures.put(ID_IDREF_CHECKING, Boolean.TRUE);
        fFeatures.put(IDENTITY_CONSTRAINT_CHECKING, Boolean.TRUE);
        fFeatures.put(UNPARSED_ENTITY_CHECKING, Boolean.TRUE);
    
Methods Summary
public voidaddRecognizedParamsAndSetDefaults(org.apache.xerces.xni.parser.XMLComponent component, XSGrammarPoolContainer grammarContainer)
Adds all of the component's recognized features and properties to the list of default recognized features and properties, and sets default values on the configuration for features and properties which were previously absent from the configuration.

param
component The component whose recognized features and properties will be added to the configuration

        
        // register component's recognized features
        final String[] recognizedFeatures = component.getRecognizedFeatures();
        addRecognizedFeatures(recognizedFeatures);
        
        // register component's recognized properties
        final String[] recognizedProperties = component.getRecognizedProperties();
        addRecognizedProperties(recognizedProperties);

        // set default values
        setFeatureDefaults(component, recognizedFeatures, grammarContainer);
        setPropertyDefaults(component, recognizedProperties);
    
org.xml.sax.ErrorHandlergetErrorHandler()

        return fErrorHandler;
    
public booleangetFeature(java.lang.String featureId)
Returns the state of a feature.

param
featureId The feature identifier.
return
true if the feature is supported
throws
XMLConfigurationException Thrown for configuration error. In general, components should only throw this exception if it is really a critical error.

        if (PARSER_SETTINGS.equals(featureId)) {
            return fConfigUpdated;
        }
        else if (VALIDATION.equals(featureId) || SCHEMA_VALIDATION.equals(featureId)) {
            return true;
        }
        else if (USE_GRAMMAR_POOL_ONLY.equals(featureId)) {
            return fUseGrammarPoolOnly;
        }
        else if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(featureId)) {
            return getProperty(SECURITY_MANAGER) != null;
        }
        return super.getFeature(featureId);
    
public java.lang.ObjectgetProperty(java.lang.String propertyId)
Returns the value of a property.

param
propertyId The property identifier.
return
the value of the property
throws
XMLConfigurationException Thrown for configuration error. In general, components should only throw this exception if it is really a critical error.

        final Object component = fComponents.get(propertyId);
        if (component != null) {
            return component;
        }
        else if (fComponents.containsKey(propertyId)) {
            return null;
        }
        return super.getProperty(propertyId);
    
public org.w3c.dom.ls.LSResourceResolvergetResourceResolver()

        return fResourceResolver;
    
public voidreset()
Calls reset on each of the components owned by this component manager.

        fNamespaceContext.reset();
        fValidationManager.reset();
        fEntityManager.reset(this);
        fErrorReporter.reset(this);
        fSchemaValidator.reset(this);
        // Mark configuration as fixed.
        fConfigUpdated = false;
    
voidrestoreInitialState()
Cleans out configuration, restoring it to its initial state.

        fConfigUpdated = true;
        
        // Remove error resolver and error handler
        fComponents.put(ENTITY_RESOLVER, null);
        fComponents.put(ERROR_HANDLER, null);
        
        // Restore initial security manager
        fComponents.put(SECURITY_MANAGER, fInitSecurityManager);
        
        // Reset feature and property values to their initial values
        if (!fInitFeatures.isEmpty()) {
            Iterator iter = fInitFeatures.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                String name = (String) entry.getKey();
                boolean value = ((Boolean) entry.getValue()).booleanValue();
                super.setFeature(name, value);
            }
            fInitFeatures.clear();
        }
        if (!fInitProperties.isEmpty()) {
            Iterator iter = fInitProperties.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                String name = (String) entry.getKey();
                Object value = entry.getValue();
                super.setProperty(name, value);
            }
            fInitProperties.clear();
        }
    
voidsetErrorHandler(org.xml.sax.ErrorHandler errorHandler)

        fErrorHandler = errorHandler;
        setProperty(ERROR_HANDLER, (errorHandler != null) ? new ErrorHandlerWrapper(errorHandler) : 
                new ErrorHandlerWrapper(DraconianErrorHandler.getInstance()));
    
public voidsetFeature(java.lang.String featureId, boolean value)
Set the state of a feature.

param
featureId The unique identifier (URI) of the feature.
param
state The requested state of the feature (true or false).
exception
XMLConfigurationException If the requested feature is not known.

        if (PARSER_SETTINGS.equals(featureId)) {
            throw new XMLConfigurationException(XMLConfigurationException.NOT_SUPPORTED, featureId);
        }
        else if (value == false && (VALIDATION.equals(featureId) || SCHEMA_VALIDATION.equals(featureId))) {
            throw new XMLConfigurationException(XMLConfigurationException.NOT_SUPPORTED, featureId);
        }
        else if (USE_GRAMMAR_POOL_ONLY.equals(featureId) && value != fUseGrammarPoolOnly) {
            throw new XMLConfigurationException(XMLConfigurationException.NOT_SUPPORTED, featureId);
        }
        if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(featureId)) {
            setProperty(SECURITY_MANAGER, value ? new SecurityManager() : null);
            return;
        }
        fConfigUpdated = true;
        fEntityManager.setFeature(featureId, value);
        fErrorReporter.setFeature(featureId, value);
        fSchemaValidator.setFeature(featureId, value);
        if (!fInitFeatures.containsKey(featureId)) {
            boolean current = super.getFeature(featureId);
            fInitFeatures.put(featureId, current ? Boolean.TRUE : Boolean.FALSE); 
        }
        super.setFeature(featureId, value);
    
private voidsetFeatureDefaults(org.apache.xerces.xni.parser.XMLComponent component, java.lang.String[] recognizedFeatures, XSGrammarPoolContainer grammarContainer)
Sets feature defaults for the given component on this configuration.

        if (recognizedFeatures != null) {
            for (int i = 0; i < recognizedFeatures.length; ++i) {
                String featureId = recognizedFeatures[i];
                Boolean state = grammarContainer.getFeature(featureId);
                if (state == null) {
                    state = component.getFeatureDefault(featureId);
                }
                if (state != null) {
                    // Do not overwrite values already set on the configuration.
                    if (!fFeatures.containsKey(featureId)) {
                        fFeatures.put(featureId, state);
                        // For newly added components who recognize this feature
                        // but did not offer a default value, we need to make
                        // sure these components will get an opportunity to read
                        // the value before parsing begins.
                        fConfigUpdated = true;
                    }
                }
            }
        }
    
public voidsetProperty(java.lang.String propertyId, java.lang.Object value)
Sets the state of a property.

param
propertyId The unique identifier (URI) of the property.
param
value The requested state of the property.
exception
XMLConfigurationException If the requested property is not known.

        if ( ENTITY_MANAGER.equals(propertyId) || ERROR_REPORTER.equals(propertyId) ||
             NAMESPACE_CONTEXT.equals(propertyId) || SCHEMA_VALIDATOR.equals(propertyId) ||
             SYMBOL_TABLE.equals(propertyId) || VALIDATION_MANAGER.equals(propertyId) ||
             XMLGRAMMAR_POOL.equals(propertyId)) {
            throw new XMLConfigurationException(XMLConfigurationException.NOT_SUPPORTED, propertyId);
        }
        fConfigUpdated = true;
        fEntityManager.setProperty(propertyId, value);
        fErrorReporter.setProperty(propertyId, value);
        fSchemaValidator.setProperty(propertyId, value);
        if (ENTITY_RESOLVER.equals(propertyId) || ERROR_HANDLER.equals(propertyId) || 
                SECURITY_MANAGER.equals(propertyId)) {
            fComponents.put(propertyId, value);
            return;
        }
        if (!fInitProperties.containsKey(propertyId)) {
            fInitProperties.put(propertyId, super.getProperty(propertyId));
        }
        super.setProperty(propertyId, value);
    
private voidsetPropertyDefaults(org.apache.xerces.xni.parser.XMLComponent component, java.lang.String[] recognizedProperties)
Sets property defaults for the given component on this configuration.

        if (recognizedProperties != null) {
            for (int i = 0; i < recognizedProperties.length; ++i) {
                String propertyId = recognizedProperties[i];
                Object value = component.getPropertyDefault(propertyId);
                if (value != null) {
                    // Do not overwrite values already set on the configuration.
                    if (!fProperties.containsKey(propertyId)) {
                        fProperties.put(propertyId, value);
                        // For newly added components who recognize this property
                        // but did not offer a default value, we need to make
                        // sure these components will get an opportunity to read
                        // the value before parsing begins.
                        fConfigUpdated = true;
                    }
                }
            }
        }
    
voidsetResourceResolver(org.w3c.dom.ls.LSResourceResolver resourceResolver)

        fResourceResolver = resourceResolver;
        setProperty(ENTITY_RESOLVER, new DOMEntityResolverWrapper(resourceResolver));