validatorhandlerimpl.java

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

JAVA
1,050
字号
                setContentHandler(null);            }            return;        }        throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),                 "SourceResultMismatch",                 new Object [] {source.getClass().getName(), result.getClass().getName()}));    }        /*     * PSVIProvider methods     */        public ElementPSVI getElementPSVI() {        return fTypeInfoProvider.getElementPSVI();    }        public AttributePSVI getAttributePSVI(int index) {        return fTypeInfoProvider.getAttributePSVI(index);    }        public AttributePSVI getAttributePSVIByName(String uri, String localname) {        return fTypeInfoProvider.getAttributePSVIByName(uri, localname);    }     //    //    // helper methods    //    //        /** Fills in a QName object. */    private void fillQName(QName toFill, String uri, String localpart, String raw) {        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);    }        /** Fills in the XMLAttributes object. */    private void fillXMLAttributes(Attributes att) {        fAttributes.removeAllAttributes();        final int len = att.getLength();        for (int i = 0; i < len; ++i) {            fillXMLAttribute(att, i);            fAttributes.setSpecified(i, true);        }    }        /** Fills in the XMLAttributes object. */    private void fillXMLAttributes2(Attributes2 att) {        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);            }        }    }        /** Adds an attribute to the XMLAttributes object. */    private void fillXMLAttribute(Attributes att, int index) {        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));    }        /**     * {@link TypeInfoProvider} implementation.     *     * REVISIT: I'm not sure if this code should belong here.     */    private final XMLSchemaTypeInfoProvider fTypeInfoProvider = new XMLSchemaTypeInfoProvider();    private static class XMLSchemaTypeInfoProvider extends TypeInfoProvider {                /** Element augmentations: contains ElementPSVI. **/        private Augmentations fElementAugs;                /** Attributes: augmentations for each attribute contain AttributePSVI. **/        private XMLAttributes fAttributes;                /** In start element. **/        private boolean fInStartElement = false;                /** Initializes the TypeInfoProvider with type information for the current element. **/        void beginStartElement(Augmentations elementAugs, XMLAttributes attributes) {            fInStartElement = true;            fElementAugs = elementAugs;            fAttributes = attributes;        }                /** Cleanup at the end of start element. **/        void finishStartElement() {            fInStartElement = false;            fElementAugs = null;            fAttributes = null;        }                /** Initializes the TypeInfoProvider with type information for the current element. **/        void beginEndElement(Augmentations elementAugs) {            fElementAugs = elementAugs;        }                /** Cleanup at the end of end element. **/        void finishEndElement() {            fElementAugs = null;        }                /**         * Throws a {@link IllegalStateException} if we are not in         * the startElement callback. the JAXP API requires this         * for most of the public methods.         */        private void checkState() {            if( !fInStartElement ) {                throw new IllegalStateException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),                         "TypeInfoProviderIllegalState", null));            }        }                public TypeInfo getAttributeTypeInfo(int index) {            checkState();            return getAttributeType(index);        }                private TypeInfo getAttributeType( int index ) {            checkState();            if( index<0 || fAttributes.getLength()<=index )                throw new IndexOutOfBoundsException(Integer.toString(index));            Augmentations augs = fAttributes.getAugmentations(index);            if (augs == null) return null;            AttributePSVI psvi = (AttributePSVI)augs.getItem(Constants.ATTRIBUTE_PSVI);            return getTypeInfoFromPSVI(psvi);        }                public TypeInfo getAttributeTypeInfo(String attributeUri, String attributeLocalName) {            checkState();            return getAttributeTypeInfo(fAttributes.getIndex(attributeUri,attributeLocalName));        }                public TypeInfo getAttributeTypeInfo(String attributeQName) {            checkState();            return getAttributeTypeInfo(fAttributes.getIndex(attributeQName));        }                public TypeInfo getElementTypeInfo() {            checkState();            if (fElementAugs == null) return null;            ElementPSVI psvi = (ElementPSVI)fElementAugs.getItem(Constants.ELEMENT_PSVI);            return getTypeInfoFromPSVI(psvi);        }                private TypeInfo getTypeInfoFromPSVI( ItemPSVI psvi ) {            if(psvi==null)  return null;                        // TODO: make sure if this is correct.            // TODO: since the number of types in a schema is quite limited,            // TypeInfoImpl should be pooled. Even better, it should be a part            // of the element decl.            if( psvi.getValidity()== ElementPSVI.VALIDITY_VALID ) {                XSTypeDefinition t = psvi.getMemberTypeDefinition();                if (t != null) {                    return (t instanceof TypeInfo) ? (TypeInfo) t : null;                }            }                        XSTypeDefinition t = psvi.getTypeDefinition();            // TODO: can t be null?            if (t != null) {                return (t instanceof TypeInfo) ? (TypeInfo) t : null;             }            return null;        }                public boolean isIdAttribute(int index) {            checkState();            XSSimpleType type = (XSSimpleType)getAttributeType(index);            if(type==null)  return false;            return type.isIDType();        }                public boolean isSpecified(int index) {            checkState();            return fAttributes.isSpecified(index);        }                /*         * Other methods         */                // PSVIProvider support        ElementPSVI getElementPSVI() {            return (fElementAugs != null) ? (ElementPSVI) fElementAugs.getItem(Constants.ELEMENT_PSVI) : null;        }                AttributePSVI getAttributePSVI(int index) {            if (fAttributes != null) {                Augmentations augs = fAttributes.getAugmentations(index);                if (augs != null) {                    return (AttributePSVI) augs.getItem(Constants.ATTRIBUTE_PSVI);                }            }            return null;        }                AttributePSVI getAttributePSVIByName(String uri, String localname) {            if (fAttributes != null) {                Augmentations augs = fAttributes.getAugmentations(uri, localname);                if (augs != null) {                    return (AttributePSVI) augs.getItem(Constants.ATTRIBUTE_PSVI);                }            }            return null;        }    }        /** SAX adapter for an LSResourceResolver. */    private final ResolutionForwarder fResolutionForwarder = new ResolutionForwarder(null);    static final class ResolutionForwarder         implements EntityResolver2 {                //        // Data        //        /** XML 1.0 type constant according to DOM L3 LS REC spec "http://www.w3.org/TR/2004/REC-DOM-Level-3-LS-20040407/" */        private static final String XML_TYPE = "http://www.w3.org/TR/REC-xml";        /** The DOM entity resolver. */        protected LSResourceResolver fEntityResolver;        //        // Constructors        //        /** Default constructor. */        public ResolutionForwarder() {}        /** Wraps the specified DOM entity resolver. */        public ResolutionForwarder(LSResourceResolver entityResolver) {            setEntityResolver(entityResolver);        }                //        // Public methods        //        /** Sets the DOM entity resolver. */        public void setEntityResolver(LSResourceResolver entityResolver) {            fEntityResolver = entityResolver;        } // setEntityResolver(LSResourceResolver)        /** Returns the DOM entity resolver. */        public LSResourceResolver getEntityResolver() {            return fEntityResolver;        } // getEntityResolver():LSResourceResolver        /**         * Always returns <code>null</code>. An LSResourceResolver has no corresponding method.         */        public InputSource getExternalSubset(String name, String baseURI)                throws SAXException, IOException {            return null;        }        /**         * Resolves the given resource and adapts the <code>LSInput</code>         * returned into an <code>InputSource</code>.         */        public InputSource resolveEntity(String name, String publicId,                 String baseURI, String systemId) throws SAXException, IOException {            if (fEntityResolver != null) {                LSInput lsInput = fEntityResolver.resolveResource(XML_TYPE, null, publicId, systemId, baseURI);                if (lsInput != null) {                    final String pubId = lsInput.getPublicId();                    final String sysId = lsInput.getSystemId();                    final String baseSystemId = lsInput.getBaseURI();                    final Reader charStream = lsInput.getCharacterStream();                    final InputStream byteStream = lsInput.getByteStream();                    final String data = lsInput.getStringData();                    final String encoding = lsInput.getEncoding();                    /**                     * An LSParser looks at inputs specified in LSInput in                     * the following order: characterStream, byteStream,                     * stringData, systemId, publicId. For consistency                     * with the DOM Level 3 Load and Save Recommendation                     * use the same lookup order here.                     */                    InputSource inputSource = new InputSource();                    inputSource.setPublicId(pubId);                    inputSource.setSystemId((baseSystemId != null) ? resolveSystemId(systemId, baseSystemId) : systemId);                                        if (charStream != null) {                        inputSource.setCharacterStream(charStream);                    }                    else if (byteStream != null) {                        inputSource.setByteStream(byteStream);                    }                    else if (data != null && data.length() != 0) {                        inputSource.setCharacterStream(new StringReader(data));                    }                    inputSource.setEncoding(encoding);                    return inputSource;                }            }            return null;        }                /** Delegates to EntityResolver2.resolveEntity(String, String, String, String). */        public InputSource resolveEntity(String publicId, String systemId)                throws SAXException, IOException {            return resolveEntity(null, publicId, null, systemId);        }                /** Resolves a system identifier against a base URI. */        private String resolveSystemId(String systemId, String baseURI) {            try {                return XMLEntityManager.expandSystemId(systemId, baseURI, false);            }            // In the event that resolution failed against the            // base URI, just return the system id as is. There's not            // much else we can do.            catch (URI.MalformedURIException ex) {                return systemId;            }        }    }}

⌨️ 快捷键说明

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