xmlschemafactory.java

来自「JAVA 所有包」· Java 代码 · 共 436 行 · 第 1/2 页

JAVA
436
字号
/* * 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.util.Locale;import javax.xml.XMLConstants;import javax.xml.transform.Source;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.sax.SAXSource;import javax.xml.transform.stream.StreamSource;import javax.xml.validation.Schema;import javax.xml.validation.SchemaFactory;import com.sun.org.apache.xerces.internal.impl.Constants;import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader;import com.sun.org.apache.xerces.internal.util.DOMEntityResolverWrapper;import com.sun.org.apache.xerces.internal.util.DOMInputSource;import com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper;import com.sun.org.apache.xerces.internal.util.SAXInputSource;import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter;import com.sun.org.apache.xerces.internal.util.SecurityManager;import com.sun.org.apache.xerces.internal.util.XMLGrammarPoolImpl;import com.sun.org.apache.xerces.internal.xni.XNIException;import com.sun.org.apache.xerces.internal.xni.grammars.Grammar;import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;import org.w3c.dom.Node;import org.w3c.dom.ls.LSResourceResolver;import org.xml.sax.ErrorHandler;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.SAXNotSupportedException;import org.xml.sax.SAXParseException;/** * {@link SchemaFactory} for XML Schema. * * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) * @version $Id: XMLSchemaFactory.java,v 1.1.4.1 2005/09/05 11:47:33 sunithareddy Exp $ */public final class XMLSchemaFactory extends SchemaFactory {        // property identifiers        /** Feature identifier: schema full checking. */    private static final String SCHEMA_FULL_CHECKING =        Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING;        /** Property identifier: grammar pool. */    private static final String XMLGRAMMAR_POOL =        Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;        /** Property identifier: SecurityManager. */    private static final String SECURITY_MANAGER =        Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY;        //    // Data    //        /** The XMLSchemaLoader */    private final XMLSchemaLoader fXMLSchemaLoader = new XMLSchemaLoader();        /** User-specified ErrorHandler; can be null. */    private ErrorHandler fErrorHandler;        /** The LSResrouceResolver */    private LSResourceResolver fLSResourceResolver;        /** The DOMEntityResolverWrapper */    private final DOMEntityResolverWrapper fDOMEntityResolverWrapper;        /** The ErrorHandlerWrapper */    private ErrorHandlerWrapper fErrorHandlerWrapper;        /** The SecurityManager. */    private SecurityManager fSecurityManager;        /** The container for the real grammar pool. */     private XMLGrammarPoolWrapper fXMLGrammarPoolWrapper;        public XMLSchemaFactory() {        fErrorHandlerWrapper = new ErrorHandlerWrapper(DraconianErrorHandler.getInstance());        fDOMEntityResolverWrapper = new DOMEntityResolverWrapper();        fXMLGrammarPoolWrapper = new XMLGrammarPoolWrapper();        fXMLSchemaLoader.setFeature(SCHEMA_FULL_CHECKING, true);        fXMLSchemaLoader.setProperty(XMLGRAMMAR_POOL, fXMLGrammarPoolWrapper);        fXMLSchemaLoader.setEntityResolver(fDOMEntityResolverWrapper);        fXMLSchemaLoader.setErrorHandler(fErrorHandlerWrapper);    }        /**     * <p>Is specified schema supported by this <code>SchemaFactory</code>?</p>     *     * @param schemaLanguage Specifies the schema language which the returned <code>SchemaFactory</code> will understand.     *    <code>schemaLanguage</code> must specify a <a href="#schemaLanguage">valid</a> schema language.     *     * @return <code>true</code> if <code>SchemaFactory</code> supports <code>schemaLanguage</code>, else <code>false</code>.     *     * @throws NullPointerException If <code>schemaLanguage</code> is <code>null</code>.     * @throws IllegalArgumentException If <code>schemaLanguage.length() == 0</code>     *   or <code>schemaLanguage</code> does not specify a <a href="#schemaLanguage">valid</a> schema language.     */    public boolean isSchemaLanguageSupported(String schemaLanguage) {        if (schemaLanguage == null) {            throw new NullPointerException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),                     "SchemaLanguageNull", null));        }        if (schemaLanguage.length() == 0) {            throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),                     "SchemaLanguageLengthZero", null));        }        // only W3C XML Schema 1.0 is supported         return schemaLanguage.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI);    }        public LSResourceResolver getResourceResolver() {        return fLSResourceResolver;    }        public void setResourceResolver(LSResourceResolver resourceResolver) {        fLSResourceResolver = resourceResolver;        fDOMEntityResolverWrapper.setEntityResolver(resourceResolver);        fXMLSchemaLoader.setEntityResolver(fDOMEntityResolverWrapper);    }        public ErrorHandler getErrorHandler() {        return fErrorHandler;    }        public void setErrorHandler(ErrorHandler errorHandler) {        fErrorHandler = errorHandler;        fErrorHandlerWrapper.setErrorHandler(errorHandler != null ? errorHandler : DraconianErrorHandler.getInstance());        fXMLSchemaLoader.setErrorHandler(fErrorHandlerWrapper);    }          public Schema newSchema( Source[] schemas ) throws SAXException {                // this will let the loader store parsed Grammars into the pool.        XMLGrammarPoolImplExtension pool = new XMLGrammarPoolImplExtension();        fXMLGrammarPoolWrapper.setGrammarPool(pool);                XMLInputSource[] xmlInputSources = new XMLInputSource[schemas.length];        InputStream inputStream;        Reader reader;        for( int i=0; i<schemas.length; i++ ) {            Source source = schemas[i];            if (source instanceof StreamSource) {                StreamSource streamSource = (StreamSource) source;                String publicId = streamSource.getPublicId();                String systemId = streamSource.getSystemId();                inputStream = streamSource.getInputStream();                reader = streamSource.getReader();                xmlInputSources[i] = new XMLInputSource(publicId, systemId, null);                xmlInputSources[i].setByteStream(inputStream);                xmlInputSources[i].setCharacterStream(reader);            }            else if (source instanceof SAXSource) {                SAXSource saxSource = (SAXSource) source;                InputSource inputSource = saxSource.getInputSource();                if (inputSource == null) {                    throw new SAXException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),                             "SAXSourceNullInputSource", null));                }                xmlInputSources[i] = new SAXInputSource(saxSource.getXMLReader(), inputSource);            }            else if (source instanceof DOMSource) {                DOMSource domSource = (DOMSource) source;                Node node = domSource.getNode();                String systemID = domSource.getSystemId();                          xmlInputSources[i] = new DOMInputSource(node, systemID);            }            else if (source == null) {                throw new NullPointerException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),                         "SchemaSourceArrayMemberNull", null));            }            else {                throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),                         "SchemaFactorySourceUnrecognized",                         new Object [] {source.getClass().getName()}));            }        }                try {            fXMLSchemaLoader.loadGrammar(xmlInputSources);        }         catch (XNIException e) {            // this should have been reported to users already.            throw Util.toSAXException(e);        }         catch (IOException e) {            // this hasn't been reported, so do so now.            SAXParseException se = new SAXParseException(e.getMessage(),null,e);            fErrorHandler.error(se);            throw se; // and we must throw it.        }        

⌨️ 快捷键说明

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