FileDocCategorySizeDatePackage
StreamValidatorHelper.javaAPI DocApache Xerces 3.0.17080Fri Sep 14 20:33:56 BST 2007org.apache.xerces.jaxp.validation

StreamValidatorHelper

public final class StreamValidatorHelper extends Object implements ValidatorHelper

A validator helper for StreamSources.

author
Michael Glavassevich, IBM
version
$Id: StreamValidatorHelper.java 542521 2007-05-29 13:58:42Z mrglavas $

Fields Summary
private static final String
PARSER_SETTINGS
Feature identifier: parser settings.
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
SCHEMA_VALIDATOR
Property identifier: XML Schema validator.
private static final String
SYMBOL_TABLE
Property identifier: symbol table.
private static final String
VALIDATION_MANAGER
Property identifier: validation manager.
private SoftReference
fConfiguration
SoftReference to parser configuration.
private final org.apache.xerces.impl.xs.XMLSchemaValidator
fSchemaValidator
Schema validator.
private final XMLSchemaValidatorComponentManager
fComponentManager
Component manager.
Constructors Summary
public StreamValidatorHelper(XMLSchemaValidatorComponentManager componentManager)


       
        fComponentManager = componentManager;
        fSchemaValidator = (XMLSchemaValidator) fComponentManager.getProperty(SCHEMA_VALIDATOR);
    
Methods Summary
private org.apache.xerces.xni.parser.XMLParserConfigurationinitialize()

        XML11Configuration config = new XML11Configuration();
        config.setProperty(ENTITY_RESOLVER, fComponentManager.getProperty(ENTITY_RESOLVER));
        config.setProperty(ERROR_HANDLER, fComponentManager.getProperty(ERROR_HANDLER));
        XMLErrorReporter errorReporter = (XMLErrorReporter) fComponentManager.getProperty(ERROR_REPORTER);
        config.setProperty(ERROR_REPORTER, errorReporter);
        // add message formatters
        if (errorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) {
            XMLMessageFormatter xmft = new XMLMessageFormatter();
            errorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft);
            errorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft);
        }
        config.setProperty(SYMBOL_TABLE, fComponentManager.getProperty(SYMBOL_TABLE));
        config.setProperty(VALIDATION_MANAGER, fComponentManager.getProperty(VALIDATION_MANAGER));
        config.setDocumentHandler(fSchemaValidator);
        config.setDTDHandler(null);
        config.setDTDContentModelHandler(null);
        fConfiguration = new SoftReference(config);
        return config;
    
public voidvalidate(javax.xml.transform.Source source, javax.xml.transform.Result result)

        if (result == null) {
            final StreamSource streamSource = (StreamSource) source;
            XMLInputSource input = new XMLInputSource(streamSource.getPublicId(), streamSource.getSystemId(), null);
            input.setByteStream(streamSource.getInputStream());
            input.setCharacterStream(streamSource.getReader());
            
            // Gets the parser configuration. We'll create and initialize a new one, if we 
            // haven't created one before or if the previous one was garbage collected.
            XMLParserConfiguration config = (XMLParserConfiguration) fConfiguration.get();
            if (config == null) {
                config = initialize();
            }
            // If settings have changed on the component manager, refresh the error handler and entity resolver.
            else if (fComponentManager.getFeature(PARSER_SETTINGS)) {
                config.setProperty(ENTITY_RESOLVER, fComponentManager.getProperty(ENTITY_RESOLVER));
                config.setProperty(ERROR_HANDLER, fComponentManager.getProperty(ERROR_HANDLER));
            }
            
            // prepare for parse
            fComponentManager.reset();
            fSchemaValidator.setDocumentHandler(null);
            
            try {
                config.parse(input);
            }
            catch (XMLParseException e) {
                throw Util.toSAXParseException(e);
            }
            catch (XNIException e) {
                throw Util.toSAXException(e);
            }
            return;
        }
        throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
                "SourceResultMismatch", 
                new Object [] {source.getClass().getName(), result.getClass().getName()}));