validatorhandlerimpl.java

来自「JAVA 所有包」· Java 代码 · 共 1,050 行 · 第 1/3 页

JAVA
1,050
字号
/* * Copyright 2005 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.sun.org.apache.xerces.internal.jaxp.validation;import java.io.IOException;import java.io.InputStream;import java.io.Reader;import java.io.StringReader;import java.util.HashMap;import java.util.Locale;import javax.xml.parsers.FactoryConfigurationError;import javax.xml.parsers.SAXParserFactory;import javax.xml.transform.Result;import javax.xml.transform.Source;import javax.xml.transform.sax.SAXResult;import javax.xml.transform.sax.SAXSource;import javax.xml.validation.TypeInfoProvider;import javax.xml.validation.ValidatorHandler;import com.sun.org.apache.xerces.internal.impl.Constants;import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;import com.sun.org.apache.xerces.internal.impl.validation.EntityState;import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator;import com.sun.org.apache.xerces.internal.util.AttributesProxy;import com.sun.org.apache.xerces.internal.util.SAXLocatorWrapper;import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter;import com.sun.org.apache.xerces.internal.util.SymbolTable;import com.sun.org.apache.xerces.internal.util.URI;import com.sun.org.apache.xerces.internal.util.XMLAttributesImpl;import com.sun.org.apache.xerces.internal.util.XMLSymbols;import com.sun.org.apache.xerces.internal.xni.Augmentations;import com.sun.org.apache.xerces.internal.xni.NamespaceContext;import com.sun.org.apache.xerces.internal.xni.QName;import com.sun.org.apache.xerces.internal.xni.XMLAttributes;import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;import com.sun.org.apache.xerces.internal.xni.XMLLocator;import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;import com.sun.org.apache.xerces.internal.xni.XMLString;import com.sun.org.apache.xerces.internal.xni.XNIException;import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException;import com.sun.org.apache.xerces.internal.xs.AttributePSVI;import com.sun.org.apache.xerces.internal.xs.ElementPSVI;import com.sun.org.apache.xerces.internal.xs.ItemPSVI;import com.sun.org.apache.xerces.internal.xs.PSVIProvider;import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;import org.w3c.dom.TypeInfo;import org.w3c.dom.ls.LSInput;import org.w3c.dom.ls.LSResourceResolver;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.DTDHandler;import org.xml.sax.ErrorHandler;import org.xml.sax.InputSource;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.SAXNotSupportedException;import org.xml.sax.XMLReader;import org.xml.sax.ext.Attributes2;import org.xml.sax.ext.EntityResolver2;/** * <p>Implementation of ValidatorHandler for W3C XML Schemas and * also a validator helper for <code>SAXSource</code>s.</p> * * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) * @author Michael Glavassevich, IBM *  * @version $Id: ValidatorHandlerImpl.java,v 1.2.6.1 2005/09/05 09:17:30 sunithareddy Exp $ */final class ValidatorHandlerImpl extends ValidatorHandler implements    DTDHandler, EntityState, PSVIProvider, ValidatorHelper, XMLDocumentHandler {        // feature identifiers        /** Feature identifier: namespace prefixes. */    private static final String NAMESPACE_PREFIXES =        Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE;        /** Feature identifier: string interning. */    protected static final String STRING_INTERNING =        Constants.SAX_FEATURE_PREFIX + Constants.STRING_INTERNING_FEATURE;        // property identifiers        /** Property identifier: error reporter. */    private static final String ERROR_REPORTER =        Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;        /** Property identifier: namespace context. */    private static final String NAMESPACE_CONTEXT =        Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_CONTEXT_PROPERTY;        /** Property identifier: XML Schema validator. */    private static final String SCHEMA_VALIDATOR =        Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY;        /** Property identifier: security manager. */    private static final String SECURITY_MANAGER =        Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY;        /** Property identifier: symbol table. */    private static final String SYMBOL_TABLE =        Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;    /** Property identifier: validation manager. */    private static final String VALIDATION_MANAGER =        Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;     //    // Data    //        /** Error reporter. */    private XMLErrorReporter fErrorReporter;        /** The namespace context of this document: stores namespaces in scope */    private NamespaceContext fNamespaceContext;        /** Schema validator. **/    private XMLSchemaValidator fSchemaValidator;        /** Symbol table **/    private SymbolTable fSymbolTable;        /** Validation manager. */    private ValidationManager fValidationManager;        /** Component manager. **/    private XMLSchemaValidatorComponentManager fComponentManager;    /** XML Locator wrapper for SAX. **/    private final SAXLocatorWrapper fSAXLocatorWrapper = new SAXLocatorWrapper();        /** Flag used to track whether the namespace context needs to be pushed. */    private boolean fNeedPushNSContext = true;        /** Map for tracking unparsed entities. */    private HashMap fUnparsedEntities = null;        /** Flag used to track whether XML names and Namespace URIs have been internalized. */    private boolean fStringsInternalized = false;        /** Fields for start element, end element and characters. */    private final QName fElementQName = new QName();    private final QName fAttributeQName = new QName();    private final XMLAttributesImpl fAttributes = new XMLAttributesImpl();    private final AttributesProxy fAttrAdapter = new AttributesProxy(fAttributes);     private final XMLString fTempString = new XMLString();        //    // User Objects    //        private ContentHandler fContentHandler = null;        /*     * Constructors     */        public ValidatorHandlerImpl(XSGrammarPoolContainer grammarContainer) {        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);    }    /*     * ValidatorHandler methods     */        public void setContentHandler(ContentHandler receiver) {        fContentHandler = receiver;    }        public ContentHandler getContentHandler() {        return fContentHandler;    }    public void setErrorHandler(ErrorHandler errorHandler) {        fComponentManager.setErrorHandler(errorHandler);    }    public ErrorHandler getErrorHandler() {        return fComponentManager.getErrorHandler();    }    public void setResourceResolver(LSResourceResolver resourceResolver) {        fComponentManager.setResourceResolver(resourceResolver);    }    public LSResourceResolver getResourceResolver() {        return fComponentManager.getResourceResolver();    }    public TypeInfoProvider getTypeInfoProvider() {        return fTypeInfoProvider;    }        public boolean getFeature(String name)         throws SAXNotRecognizedException, SAXNotSupportedException {        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 void setFeature(String name, boolean value)        throws SAXNotRecognizedException, SAXNotSupportedException {        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 Object getProperty(String name)        throws SAXNotRecognizedException, SAXNotSupportedException {        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 void setProperty(String name, Object object)        throws SAXNotRecognizedException, SAXNotSupportedException {        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}));        }    }        /*     * EntityState methods     */        public boolean isEntityDeclared(String name) {        return false;    }    public boolean isEntityUnparsed(String name) {        if (fUnparsedEntities != null) {            return fUnparsedEntities.containsKey(name);        }        return false;    }        /*     * XMLDocumentHandler methods     */    public void startDocument(XMLLocator locator, String encoding,            NamespaceContext namespaceContext, Augmentations augs)            throws XNIException {        if (fContentHandler != null) {            try {                fContentHandler.startDocument();            }            catch (SAXException e) {                throw new XNIException(e);            }        }    }    public void xmlDecl(String version, String encoding, String standalone,            Augmentations augs) throws XNIException {}    public void doctypeDecl(String rootElement, String publicId,            String systemId, Augmentations augs) throws XNIException {}    public void comment(XMLString text, Augmentations augs) throws XNIException {}    public void processingInstruction(String target, XMLString data,            Augmentations augs) throws XNIException {        if (fContentHandler != null) {            try {                fContentHandler.processingInstruction(target, data.toString());            }            catch (SAXException e) {                throw new XNIException(e);            }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?