domvalidatorhelper.java

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

JAVA
593
字号
                    fSchemaValidator.endCDATA(null);                 }                break;            case Node.PROCESSING_INSTRUCTION_NODE:                /**                  * The validator does nothing with processing instructions so bypass it.                 * Send the ProcessingInstruction node directly to the result builder.                 */                if (fDOMValidatorHandler != null) {                    fDOMValidatorHandler.processingInstruction((ProcessingInstruction) node);                }                break;            case Node.COMMENT_NODE:                /**                  * The validator does nothing with comments so bypass it.                 * Send the Comment node directly to the result builder.                 */                if (fDOMValidatorHandler != null) {                    fDOMValidatorHandler.comment((Comment) node);                }                break;            case Node.DOCUMENT_TYPE_NODE:                /**                  * Send the DocumentType node directly to the result builder.                 */                if (fDOMValidatorHandler != null) {                    fDOMValidatorHandler.doctypeDecl((DocumentType) node);                }                break;            default: // Ignore other node types.                break;        }    }        /** Do processing for the end of a node. */    private void finishNode(Node node) {        if (node.getNodeType() == Node.ELEMENT_NODE) {            fCurrentElement = node;            // end element            fillQName(fElementQName, node);            fSchemaValidator.endElement(fElementQName, null);            // pop namespace context            fNamespaceContext.popContext();        }    }        /**     * Extracts NamedNodeMap of entities. We need this to validate     * elements and attributes of type xs:ENTITY, xs:ENTITIES or      * types dervied from them.     */    private void setupEntityMap(Document doc) {        if (doc != null) {            DocumentType docType = doc.getDoctype();            if (docType != null) {                fEntities = docType.getEntities();                return;            }        }        fEntities = null;    }        /**     * Sets up handler for <code>DOMResult</code>.     */    private void setupDOMResultHandler(DOMSource source, DOMResult result) throws SAXException {        // If there's no DOMResult, unset the validator handler        if (result == null) {            fDOMValidatorHandler = null;            fSchemaValidator.setDocumentHandler(null);            return;        }        final Node nodeResult = result.getNode();        // If the source node and result node are the same use the DOMResultAugmentor.        // Otherwise use the DOMResultBuilder.        if (source.getNode() == nodeResult) {            fDOMValidatorHandler = fDOMResultAugmentor;            fDOMResultAugmentor.setDOMResult(result);            fSchemaValidator.setDocumentHandler(fDOMResultAugmentor);            return;        }        if (result.getNode() == null) {            try {                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();                factory.setNamespaceAware(true);                DocumentBuilder builder = factory.newDocumentBuilder();                result.setNode(builder.newDocument());            }            catch (ParserConfigurationException e) {                throw new SAXException(e);            }        }        fDOMValidatorHandler = fDOMResultBuilder;        fDOMResultBuilder.setDOMResult(result);        fSchemaValidator.setDocumentHandler(fDOMResultBuilder);    }        private void fillQName(QName toFill, Node node) {        final String prefix = node.getPrefix();        final String localName = node.getLocalName();        final String rawName = node.getNodeName();        final String namespace = node.getNamespaceURI();        toFill.prefix = (prefix != null) ? fSymbolTable.addSymbol(prefix) : XMLSymbols.EMPTY_STRING;        toFill.localpart = (localName != null) ? fSymbolTable.addSymbol(localName) : XMLSymbols.EMPTY_STRING;        toFill.rawname = (rawName != null) ? fSymbolTable.addSymbol(rawName) : XMLSymbols.EMPTY_STRING;         toFill.uri = (namespace != null && namespace.length() > 0) ? fSymbolTable.addSymbol(namespace) : null;    }        private void processAttributes(NamedNodeMap attrMap) {        final int attrCount = attrMap.getLength();        fAttributes.removeAllAttributes();        for (int i = 0; i < attrCount; ++i) {            Attr attr = (Attr) attrMap.item(i);            String value = attr.getValue();            if (value == null) {                value = XMLSymbols.EMPTY_STRING;            }            fillQName(fAttributeQName, attr);            // REVISIT: Assuming all attributes are of type CDATA. The actual type may not matter. -- mrglavas            fAttributes.addAttributeNS(fAttributeQName, XMLSymbols.fCDATASymbol, value);            fAttributes.setSpecified(i, attr.getSpecified());            // REVISIT: Should we be looking at non-namespace attributes            // for additional mappings? Should we detect illegal namespace            // declarations and exclude them from the context? -- mrglavas            if (fAttributeQName.uri == NamespaceContext.XMLNS_URI) {                // process namespace attribute                if (fAttributeQName.prefix == XMLSymbols.PREFIX_XMLNS) {                    fNamespaceContext.declarePrefix(fAttributeQName.localpart, value.length() != 0 ? fSymbolTable.addSymbol(value) : null);                }                else {                    fNamespaceContext.declarePrefix(XMLSymbols.EMPTY_STRING, value.length() != 0 ? fSymbolTable.addSymbol(value) : null);                }            }        }    }        private void sendCharactersToValidator(String str) {        if (str != null) {            final int length = str.length();            final int remainder = length & CHUNK_MASK;            if (remainder > 0) {                str.getChars(0, remainder, fCharBuffer, 0);                fTempString.setValues(fCharBuffer, 0, remainder);                fSchemaValidator.characters(fTempString, null);            }            int i = remainder;            while (i < length) {                str.getChars(i, i += CHUNK_SIZE, fCharBuffer, 0);                fTempString.setValues(fCharBuffer, 0, CHUNK_SIZE);                fSchemaValidator.characters(fTempString, null);            }        }    }        Node getCurrentElement() {        return fCurrentElement;    }        /**     * NamespaceContext for the DOMSource, includes context for ancestor nodes.     */    final class DOMNamespaceContext implements NamespaceContext {                //        // Data        //        /**          * Namespace binding information. This array is composed of a         * series of tuples containing the namespace binding information:         * &lt;prefix, uri&gt;.         */        protected String[] fNamespace = new String[16 * 2];        /** The size of the namespace information array. */        protected int fNamespaceSize = 0;                /**          * Flag indicating whether the namespace context          * has been from the root node's ancestors.         */        protected boolean fDOMContextBuilt = false;                //        // Methods        //        public void pushContext() {            fNamespaceContext.pushContext();        }        public void popContext() {            fNamespaceContext.popContext();        }        public boolean declarePrefix(String prefix, String uri) {            return fNamespaceContext.declarePrefix(prefix, uri);        }        public String getURI(String prefix) {            String uri = fNamespaceContext.getURI(prefix);            if (uri == null) {                if (!fDOMContextBuilt) {                    fillNamespaceContext();                    fDOMContextBuilt = true;                }                if (fNamespaceSize > 0 &&                     !fNamespaceContext.containsPrefix(prefix)) {                    uri = getURI0(prefix);                }            }            return uri;        }        public String getPrefix(String uri) {            return fNamespaceContext.getPrefix(uri);        }        public int getDeclaredPrefixCount() {            return fNamespaceContext.getDeclaredPrefixCount();        }        public String getDeclaredPrefixAt(int index) {            return fNamespaceContext.getDeclaredPrefixAt(index);        }        public Enumeration getAllPrefixes() {            return fNamespaceContext.getAllPrefixes();        }        public void reset() {            fDOMContextBuilt = false;            fNamespaceSize = 0;         }                private void fillNamespaceContext() {            if (fRoot != null) {                Node currentNode = fRoot.getParentNode();                while (currentNode != null) {                    if (Node.ELEMENT_NODE == currentNode.getNodeType()) {                        NamedNodeMap attributes = currentNode.getAttributes();                        final int attrCount = attributes.getLength();                        for (int i = 0; i < attrCount; ++i) {                            Attr attr = (Attr) attributes.item(i);                            String value = attr.getValue();                            if (value == null) {                                value = XMLSymbols.EMPTY_STRING;                            }                            fillQName(fAttributeQName, attr);                            // REVISIT: Should we be looking at non-namespace attributes                            // for additional mappings? Should we detect illegal namespace                            // declarations and exclude them from the context? -- mrglavas                            if (fAttributeQName.uri == NamespaceContext.XMLNS_URI) {                                // process namespace attribute                                if (fAttributeQName.prefix == XMLSymbols.PREFIX_XMLNS) {                                    declarePrefix0(fAttributeQName.localpart, value.length() != 0 ? fSymbolTable.addSymbol(value) : null);                                }                                else {                                    declarePrefix0(XMLSymbols.EMPTY_STRING, value.length() != 0 ? fSymbolTable.addSymbol(value) : null);                                }                            }                        }                                            }                    currentNode = currentNode.getParentNode();                }            }        }                private void declarePrefix0(String prefix, String uri) {                       // resize array, if needed            if (fNamespaceSize == fNamespace.length) {                String[] namespacearray = new String[fNamespaceSize * 2];                System.arraycopy(fNamespace, 0, namespacearray, 0, fNamespaceSize);                fNamespace = namespacearray;            }            // bind prefix to uri in current context            fNamespace[fNamespaceSize++] = prefix;            fNamespace[fNamespaceSize++] = uri;        }                private String getURI0(String prefix) {            // find prefix in the DOM context            for (int i = 0; i < fNamespaceSize; i += 2) {                if (fNamespace[i] == prefix) {                    return fNamespace[i + 1];                }            }            // prefix not found            return null;        }    }    } // DOMValidatorHelper

⌨️ 快捷键说明

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