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

ValidatorHandlerImpl

public final class ValidatorHandlerImpl extends ValidatorHandler implements org.apache.xerces.xs.PSVIProvider, ValidatorHelper, org.apache.xerces.impl.validation.EntityState, DTDHandler, org.apache.xerces.xni.XMLDocumentHandler

Implementation of ValidatorHandler for W3C XML Schemas and also a validator helper for SAXSources.

author
Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
author
Michael Glavassevich, IBM
version
$Id: ValidatorHandlerImpl.java 542519 2007-05-29 13:54:42Z mrglavas $

Fields Summary
private static final String
NAMESPACE_PREFIXES
Feature identifier: namespace prefixes.
protected static final String
STRING_INTERNING
Feature identifier: string interning.
private static final String
ERROR_REPORTER
Property identifier: error reporter.
private static final String
LEXICAL_HANDLER
Property identifier: lexical handler.
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 final org.apache.xerces.impl.XMLErrorReporter
fErrorReporter
Error reporter.
private final org.apache.xerces.xni.NamespaceContext
fNamespaceContext
The namespace context of this document: stores namespaces in scope
private final org.apache.xerces.impl.xs.XMLSchemaValidator
fSchemaValidator
Schema validator.
private final org.apache.xerces.util.SymbolTable
fSymbolTable
Symbol table
private final org.apache.xerces.impl.validation.ValidationManager
fValidationManager
Validation manager.
private final XMLSchemaValidatorComponentManager
fComponentManager
Component manager.
private final org.apache.xerces.util.SAXLocatorWrapper
fSAXLocatorWrapper
XML Locator wrapper for SAX.
private boolean
fNeedPushNSContext
Flag used to track whether the namespace context needs to be pushed.
private HashMap
fUnparsedEntities
Map for tracking unparsed entities.
private boolean
fStringsInternalized
Flag used to track whether XML names and Namespace URIs have been internalized.
private final org.apache.xerces.xni.QName
fElementQName
Fields for start element, end element and characters.
private final org.apache.xerces.xni.QName
fAttributeQName
private final org.apache.xerces.util.XMLAttributesImpl
fAttributes
private final org.apache.xerces.util.AttributesProxy
fAttrAdapter
private final org.apache.xerces.xni.XMLString
fTempString
private ContentHandler
fContentHandler
private final XMLSchemaTypeInfoProvider
fTypeInfoProvider
{@link TypeInfoProvider} implementation. REVISIT: I'm not sure if this code should belong here.
private final ResolutionForwarder
fResolutionForwarder
SAX adapter for an LSResourceResolver.
Constructors Summary
public ValidatorHandlerImpl(XSGrammarPoolContainer grammarContainer)

    
    /*
     * Constructors
     */
    
       
        this(new XMLSchemaValidatorComponentManager(grammarContainer));
        fComponentManager.addRecognizedFeatures(new String [] {NAMESPACE_PREFIXES});
        fComponentManager.setFeature(NAMESPACE_PREFIXES, false);
        setErrorHandler(null);
        setResourceResolver(null);
    
public ValidatorHandlerImpl(XMLSchemaValidatorComponentManager componentManager)

        fComponentManager = componentManager;
        fErrorReporter = (XMLErrorReporter) fComponentManager.getProperty(ERROR_REPORTER);
        fNamespaceContext = (NamespaceContext) fComponentManager.getProperty(NAMESPACE_CONTEXT);
        fSchemaValidator = (XMLSchemaValidator) fComponentManager.getProperty(SCHEMA_VALIDATOR);
        fSymbolTable = (SymbolTable) fComponentManager.getProperty(SYMBOL_TABLE);
        fValidationManager = (ValidationManager) fComponentManager.getProperty(VALIDATION_MANAGER);
    
Methods Summary
public voidcharacters(org.apache.xerces.xni.XMLString text, org.apache.xerces.xni.Augmentations augs)

        if (fContentHandler != null) {
            // if the type is union it is possible that we receive
            // a character call with empty data
            if (text.length == 0) {
                return;
            }
            try {
                fContentHandler.characters(text.ch, text.offset, text.length);
            }
            catch (SAXException e) {
                throw new XNIException(e);
            }
        }
    
public voidcharacters(char[] ch, int start, int length)

        try {
            fTempString.setValues(ch, start, length);
            fSchemaValidator.characters(fTempString, null);
        }
        catch (XMLParseException e) {
            throw Util.toSAXParseException(e);
        }
        catch (XNIException e) {
            throw Util.toSAXException(e);
        }
    
public voidcomment(org.apache.xerces.xni.XMLString text, org.apache.xerces.xni.Augmentations augs)

public voiddoctypeDecl(java.lang.String rootElement, java.lang.String publicId, java.lang.String systemId, org.apache.xerces.xni.Augmentations augs)

public voidemptyElement(org.apache.xerces.xni.QName element, org.apache.xerces.xni.XMLAttributes attributes, org.apache.xerces.xni.Augmentations augs)

        /** Split empty element event. **/
        startElement(element, attributes, augs);
        endElement(element, augs);
    
public voidendCDATA(org.apache.xerces.xni.Augmentations augs)

public voidendDocument(org.apache.xerces.xni.Augmentations augs)

        if (fContentHandler != null) {
            try {
                fContentHandler.endDocument();
            }
            catch (SAXException e) {
                throw new XNIException(e);
            }
        }
    
public voidendDocument()

        fSAXLocatorWrapper.setLocator(null);
        try {
            fSchemaValidator.endDocument(null);
        }
        catch (XMLParseException e) {
            throw Util.toSAXParseException(e);
        }
        catch (XNIException e) {
            throw Util.toSAXException(e);
        }
    
public voidendElement(org.apache.xerces.xni.QName element, org.apache.xerces.xni.Augmentations augs)

        if (fContentHandler != null) {
            try {
                fTypeInfoProvider.beginEndElement(augs);
                fContentHandler.endElement((element.uri != null) ? element.uri : XMLSymbols.EMPTY_STRING,
                        element.localpart, element.rawname);
            }
            catch (SAXException e) {
                throw new XNIException(e);
            }
            finally {
                fTypeInfoProvider.finishEndElement();
            }
        }
    
public voidendElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)

        fillQName(fElementQName, uri, localName, qName);
        try {
            fSchemaValidator.endElement(fElementQName, null);
        }
        catch (XMLParseException e) {
            throw Util.toSAXParseException(e);
        }
        catch (XNIException e) {
            throw Util.toSAXException(e);
        }
        finally {
            fNamespaceContext.popContext();
        }
    
public voidendGeneralEntity(java.lang.String name, org.apache.xerces.xni.Augmentations augs)

public voidendPrefixMapping(java.lang.String prefix)

        if (fContentHandler != null) {
            fContentHandler.endPrefixMapping(prefix);
        }
    
private voidfillQName(org.apache.xerces.xni.QName toFill, java.lang.String uri, java.lang.String localpart, java.lang.String raw)
Fills in a QName object.

        if (!fStringsInternalized) {
            uri = (uri != null && uri.length() > 0) ? fSymbolTable.addSymbol(uri) : null;
            localpart = (localpart != null) ? fSymbolTable.addSymbol(localpart) : XMLSymbols.EMPTY_STRING;
            raw = (raw != null) ? fSymbolTable.addSymbol(raw) : XMLSymbols.EMPTY_STRING;
        }
        else {
            if (uri != null && uri.length() == 0) {
                uri = null;
            }
            if (localpart == null) {
                localpart = XMLSymbols.EMPTY_STRING;
            }
            if (raw == null) {
                raw = XMLSymbols.EMPTY_STRING;
            }
        }
        String prefix = XMLSymbols.EMPTY_STRING;
        int prefixIdx = raw.indexOf(':");
        if (prefixIdx != -1) {
            prefix = fSymbolTable.addSymbol(raw.substring(0, prefixIdx));
        }
        toFill.setValues(prefix, localpart, raw, uri);
    
private voidfillXMLAttribute(org.xml.sax.Attributes att, int index)
Adds an attribute to the XMLAttributes object.

        fillQName(fAttributeQName, att.getURI(index), att.getLocalName(index), att.getQName(index));
        String type = att.getType(index);
        fAttributes.addAttributeNS(fAttributeQName, (type != null) ? type : XMLSymbols.fCDATASymbol, att.getValue(index));
    
private voidfillXMLAttributes(org.xml.sax.Attributes att)
Fills in the XMLAttributes object.

        fAttributes.removeAllAttributes();
        final int len = att.getLength();
        for (int i = 0; i < len; ++i) {
            fillXMLAttribute(att, i);
            fAttributes.setSpecified(i, true);
        }
    
private voidfillXMLAttributes2(org.xml.sax.ext.Attributes2 att)
Fills in the XMLAttributes object.

        fAttributes.removeAllAttributes();
        final int len = att.getLength();
        for (int i = 0; i < len; ++i) {
            fillXMLAttribute(att, i);
            fAttributes.setSpecified(i, att.isSpecified(i));
            if (att.isDeclared(i)) {
                fAttributes.getAugmentations(i).putItem(Constants.ATTRIBUTE_DECLARED, Boolean.TRUE);
            }
        }
    
public org.apache.xerces.xs.AttributePSVIgetAttributePSVI(int index)

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

        return fTypeInfoProvider.getAttributePSVIByName(uri, localname);
    
public org.xml.sax.ContentHandlergetContentHandler()

        return fContentHandler;
    
public org.apache.xerces.xni.parser.XMLDocumentSourcegetDocumentSource()

        return fSchemaValidator;
    
public org.apache.xerces.xs.ElementPSVIgetElementPSVI()

        return fTypeInfoProvider.getElementPSVI();
    
public org.xml.sax.ErrorHandlergetErrorHandler()

        return fComponentManager.getErrorHandler();
    
public booleangetFeature(java.lang.String name)

        if (name == null) {
            throw new NullPointerException();
        }
        try {
            return fComponentManager.getFeature(name);
        }
        catch (XMLConfigurationException e) {
            final String identifier = e.getIdentifier();
            final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ?
                    "feature-not-recognized" : "feature-not-supported";
            throw new SAXNotRecognizedException(
                    SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                    key, new Object [] {identifier}));
        }
    
public java.lang.ObjectgetProperty(java.lang.String name)

        if (name == null) {
            throw new NullPointerException();
        }
        try {
            return fComponentManager.getProperty(name);
        }
        catch (XMLConfigurationException e) {
            final String identifier = e.getIdentifier();
            final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ?
                    "property-not-recognized" : "property-not-supported";
            throw new SAXNotRecognizedException(
                    SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                    key, new Object [] {identifier}));
        }
    
public org.w3c.dom.ls.LSResourceResolvergetResourceResolver()

        return fComponentManager.getResourceResolver();
    
public javax.xml.validation.TypeInfoProvidergetTypeInfoProvider()

        return fTypeInfoProvider;
    
public voidignorableWhitespace(org.apache.xerces.xni.XMLString text, org.apache.xerces.xni.Augmentations augs)

        if (fContentHandler != null) {
            try {
                fContentHandler.ignorableWhitespace(text.ch, text.offset, text.length);
            }
            catch (SAXException e) {
                throw new XNIException(e);
            }
        }
    
public voidignorableWhitespace(char[] ch, int start, int length)

        try {
            fTempString.setValues(ch, start, length);
            fSchemaValidator.ignorableWhitespace(fTempString, null);
        }
        catch (XMLParseException e) {
            throw Util.toSAXParseException(e);
        }
        catch (XNIException e) {
            throw Util.toSAXException(e);
        }
    
public booleanisEntityDeclared(java.lang.String name)

        return false;
    
public booleanisEntityUnparsed(java.lang.String name)

        if (fUnparsedEntities != null) {
            return fUnparsedEntities.containsKey(name);
        }
        return false;
    
public voidnotationDecl(java.lang.String name, java.lang.String publicId, java.lang.String systemId)

public voidprocessingInstruction(java.lang.String target, org.apache.xerces.xni.XMLString data, org.apache.xerces.xni.Augmentations augs)

        if (fContentHandler != null) {
            try {
                fContentHandler.processingInstruction(target, data.toString());
            }
            catch (SAXException e) {
                throw new XNIException(e);
            }
        }
    
public voidprocessingInstruction(java.lang.String target, java.lang.String data)

        /** 
         * Processing instructions do not participate in schema validation,
         * so just forward the event to the application's content
         * handler. 
         */
        if (fContentHandler != null) {
            fContentHandler.processingInstruction(target, data);
        }
    
public voidsetContentHandler(org.xml.sax.ContentHandler receiver)

        fContentHandler = receiver;
    
public voidsetDocumentLocator(org.xml.sax.Locator locator)

        fSAXLocatorWrapper.setLocator(locator);
        if (fContentHandler != null) {
            fContentHandler.setDocumentLocator(locator);
        }
    
public voidsetDocumentSource(org.apache.xerces.xni.parser.XMLDocumentSource source)

public voidsetErrorHandler(org.xml.sax.ErrorHandler errorHandler)

        fComponentManager.setErrorHandler(errorHandler);
    
public voidsetFeature(java.lang.String name, boolean value)

        if (name == null) {
            throw new NullPointerException();
        }
        try {
            fComponentManager.setFeature(name, value);
        }
        catch (XMLConfigurationException e) {
            final String identifier = e.getIdentifier();
            final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ?
                    "feature-not-recognized" : "feature-not-supported";
            throw new SAXNotRecognizedException(
                    SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                    key, new Object [] {identifier}));
        }
    
public voidsetProperty(java.lang.String name, java.lang.Object object)

        if (name == null) {
            throw new NullPointerException();
        }
        try {
            fComponentManager.setProperty(name, object);
        }
        catch (XMLConfigurationException e) {
            final String identifier = e.getIdentifier();
            final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ?
                    "property-not-recognized" : "property-not-supported";
            throw new SAXNotRecognizedException(
                    SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                    key, new Object [] {identifier}));
        }
    
public voidsetResourceResolver(org.w3c.dom.ls.LSResourceResolver resourceResolver)

        fComponentManager.setResourceResolver(resourceResolver);
    
public voidskippedEntity(java.lang.String name)

        // there seems to be no corresponding method on XMLDocumentFilter.
        // just pass it down to the output, if any.
        if (fContentHandler != null) {
            fContentHandler.skippedEntity(name);
        }
    
public voidstartCDATA(org.apache.xerces.xni.Augmentations augs)

public voidstartDocument(org.apache.xerces.xni.XMLLocator locator, java.lang.String encoding, org.apache.xerces.xni.NamespaceContext namespaceContext, org.apache.xerces.xni.Augmentations augs)

        if (fContentHandler != null) {
            try {
                fContentHandler.startDocument();
            }
            catch (SAXException e) {
                throw new XNIException(e);
            }
        }
    
public voidstartDocument()

        fComponentManager.reset();
        fSchemaValidator.setDocumentHandler(this);
        fValidationManager.setEntityState(this);
        fTypeInfoProvider.finishStartElement(); // cleans up TypeInfoProvider
        fNeedPushNSContext = true;
        if (fUnparsedEntities != null && !fUnparsedEntities.isEmpty()) {
            // should only clear this if the last document contained unparsed entities
            fUnparsedEntities.clear();
        }
        fErrorReporter.setDocumentLocator(fSAXLocatorWrapper);
        try {
            fSchemaValidator.startDocument(fSAXLocatorWrapper, fSAXLocatorWrapper.getEncoding(), fNamespaceContext, null);
        }
        catch (XMLParseException e) {
            throw Util.toSAXParseException(e);
        }
        catch (XNIException e) {
            throw Util.toSAXException(e);
        }
    
public voidstartElement(org.apache.xerces.xni.QName element, org.apache.xerces.xni.XMLAttributes attributes, org.apache.xerces.xni.Augmentations augs)

        if (fContentHandler != null) {
            try {
                fTypeInfoProvider.beginStartElement(augs, attributes);
                fContentHandler.startElement((element.uri != null) ? element.uri : XMLSymbols.EMPTY_STRING, 
                        element.localpart, element.rawname, fAttrAdapter);
            }
            catch (SAXException e) {
                throw new XNIException(e);
            }
            finally {
                fTypeInfoProvider.finishStartElement();
            }
        }
    
public voidstartElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)

        if (fNeedPushNSContext) {
            fNamespaceContext.pushContext();
        }
        fNeedPushNSContext = true;
        
        // Fill element QName
        fillQName(fElementQName, uri, localName, qName);
        
        // Fill XMLAttributes
        if (atts instanceof Attributes2) {
            fillXMLAttributes2((Attributes2) atts);
        }
        else {
            fillXMLAttributes(atts);
        }
        
        try {
            fSchemaValidator.startElement(fElementQName, fAttributes, null);
        }
        catch (XMLParseException e) {
            throw Util.toSAXParseException(e);
        }
        catch (XNIException e) {
            throw Util.toSAXException(e);
        }
    
public voidstartGeneralEntity(java.lang.String name, org.apache.xerces.xni.XMLResourceIdentifier identifier, java.lang.String encoding, org.apache.xerces.xni.Augmentations augs)

public voidstartPrefixMapping(java.lang.String prefix, java.lang.String uri)

        String prefixSymbol;
        String uriSymbol;
        if (!fStringsInternalized) {
            prefixSymbol = (prefix != null) ? fSymbolTable.addSymbol(prefix) : XMLSymbols.EMPTY_STRING;
            uriSymbol = (uri != null && uri.length() > 0) ? fSymbolTable.addSymbol(uri) : null;
        }
        else {
            prefixSymbol = (prefix != null) ? prefix : XMLSymbols.EMPTY_STRING;
            uriSymbol = (uri != null && uri.length() > 0) ? uri : null;
        }
        if (fNeedPushNSContext) {
            fNeedPushNSContext = false;
            fNamespaceContext.pushContext();
        }
        fNamespaceContext.declarePrefix(prefixSymbol, uriSymbol);
        if (fContentHandler != null) {
            fContentHandler.startPrefixMapping(prefix, uri);
        }
    
public voidtextDecl(java.lang.String version, java.lang.String encoding, org.apache.xerces.xni.Augmentations augs)

public voidunparsedEntityDecl(java.lang.String name, java.lang.String publicId, java.lang.String systemId, java.lang.String notationName)

        if (fUnparsedEntities == null) {
            fUnparsedEntities = new HashMap();
        }
        fUnparsedEntities.put(name, name);
    
public voidvalidate(javax.xml.transform.Source source, javax.xml.transform.Result result)

        if (result instanceof SAXResult || result == null) {
            final SAXSource saxSource = (SAXSource) source;
            final SAXResult saxResult = (SAXResult) result;
            
            LexicalHandler lh = null;
            if (result != null) {
                ContentHandler ch = saxResult.getHandler();
                lh = saxResult.getLexicalHandler();
                /** If the lexical handler is not set try casting the ContentHandler. **/
                if (lh == null && ch instanceof LexicalHandler) {
                    lh = (LexicalHandler) ch;
                }
                setContentHandler(ch);
            }
            
            try {
                XMLReader reader = saxSource.getXMLReader();
                if( reader==null ) {
                    // create one now
                    SAXParserFactory spf = SAXParserFactory.newInstance();
                    spf.setNamespaceAware(true);
                    try {
                        reader = spf.newSAXParser().getXMLReader();
                        // If this is a Xerces SAX parser, set the security manager if there is one
                        if (reader instanceof org.apache.xerces.parsers.SAXParser) {
                           SecurityManager securityManager = (SecurityManager) fComponentManager.getProperty(SECURITY_MANAGER);
                           if (securityManager != null) {
                               try {
                                   reader.setProperty(SECURITY_MANAGER, securityManager);
                               }
                               // Ignore the exception if the security manager cannot be set.
                               catch (SAXException exc) {}
                           }
                        }
                    } catch( Exception e ) {
                        // this is impossible, but better safe than sorry
                        throw new FactoryConfigurationError(e);
                    }
                }
                
                // If XML names and Namespace URIs are already internalized we
                // can avoid running them through the SymbolTable.
                try {
                    fStringsInternalized = reader.getFeature(STRING_INTERNING);
                }
                catch (SAXException exc) {
                    // The feature isn't recognized or getting it is not supported.
                    // In either case, assume that strings are not internalized.
                    fStringsInternalized = false;
                }
                
                ErrorHandler errorHandler = fComponentManager.getErrorHandler();
                reader.setErrorHandler(errorHandler != null ? errorHandler : DraconianErrorHandler.getInstance());
                reader.setEntityResolver(fResolutionForwarder);
                fResolutionForwarder.setEntityResolver(fComponentManager.getResourceResolver());
                reader.setContentHandler(this);
                reader.setDTDHandler(this);
                try {
                    reader.setProperty(LEXICAL_HANDLER, lh);
                }
                // Ignore the exception if the lexical handler cannot be set.
                catch (SAXException exc) {}
                
                InputSource is = saxSource.getInputSource();
                reader.parse(is);
            } 
            finally {
                // release the reference to user's handler ASAP
                setContentHandler(null);
            }
            return;
        }
        throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
                "SourceResultMismatch", 
                new Object [] {source.getClass().getName(), result.getClass().getName()}));
    
public voidxmlDecl(java.lang.String version, java.lang.String encoding, java.lang.String standalone, org.apache.xerces.xni.Augmentations augs)