domnormalizer.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,354 行 · 第 1/5 页

JAVA
1,354
字号
            fPSVI = ((fConfiguration.features & DOMConfigurationImpl.PSVI) !=0)?true:false;                        // reset ID table            fDocument.clearIdentifiers();                        // reset schema validator            if(fValidationHandler!=null)                ((XMLComponent) fValidationHandler).reset(fConfiguration);                    }                fErrorHandler = (DOMErrorHandler) fConfiguration.getParameter(Constants.DOM_ERROR_HANDLER);        if (fValidationHandler != null) {            fValidationHandler.setDocumentHandler(this);            fValidationHandler.startDocument( new SimpleLocator(fDocument.fDocumentURI, fDocument.fDocumentURI, -1, -1 ), fDocument.encoding, fNamespaceContext, null);                    }        try {            Node kid, next;            for (kid = fDocument.getFirstChild(); kid != null; kid = next) {                next = kid.getNextSibling();                kid = normalizeNode(kid);                if (kid != null) { // don't advance                    next = kid;                }            }                        // release resources            if (fValidationHandler != null) {                fValidationHandler.endDocument(null);                CoreDOMImplementationImpl.singleton.releaseValidator(                XMLGrammarDescription.XML_SCHEMA, fValidationHandler);                fValidationHandler = null;            }        }        catch (RuntimeException e) {			if(DEBUG_ND) e.printStackTrace();            if( e==abort )                return; // processing aborted by the user            throw e;    // otherwise re-throw.        }            }            /**     *     * This method acts as if the document was going through a save     * and load cycle, putting the document in a "normal" form. The actual result     * depends on the features being set and governing what operations actually     * take place. See setNormalizationFeature for details. Noticeably this method     * normalizes Text nodes, makes the document "namespace wellformed",     * according to the algorithm described below in pseudo code, by adding missing     * namespace declaration attributes and adding or changing namespace prefixes, updates     * the replacement tree of EntityReference nodes,normalizes attribute values, etc.     *     * @param node   Modified node or null. If node is returned, we need     *               to normalize again starting on the node returned.     * @return  the normalized Node     */    protected Node normalizeNode(Node node){                int type = node.getNodeType();        boolean wellformed;        fLocator.fRelatedNode=node;                switch (type) {            case Node.DOCUMENT_TYPE_NODE: {                if (DEBUG_ND) {                    System.out.println("==>normalizeNode:{doctype}");                }            	fDTDValidator =(XMLDTDValidator) CoreDOMImplementationImpl.singleton.getDTDValidator();                DocumentTypeImpl docNode = (DocumentTypeImpl)node;                if(fDTDValidator != null){				//fix me :             	fConfiguration.setFeature(DOMConfigurationImpl.XERCES_VALIDATION, false);								fDTDValidator.startDocument( new SimpleLocator(fDocument.fDocumentURI, fDocument.fDocumentURI, -1, -1 ), fDocument.encoding, fNamespaceContext, null);            				                if (DEBUG_ND) 					System.out.println("Internal subset is "+docNode.getInternalSubset());                XMLGrammarPoolImpl grammarPool = getGrammarPool(docNode.getSystemId(),docNode.getInternalSubset());                fConfiguration.setProperty(GRAMMAR_POOL, grammarPool);                    	fDTDValidator.setDocumentHandler(this);                 ((XMLComponent) fDTDValidator).reset(fConfiguration);				fDTDValidator.doctypeDecl(docNode.getName(),docNode.getPublicId(),docNode.getSystemId(),null);				 docTypeFound = true;                }                //REVISIT: well-formness encoding info                break;            }                        case Node.ELEMENT_NODE: {                if (DEBUG_ND) {                    System.out.println("==>normalizeNode:{element} "+node.getNodeName());                }                                //do the name check only when version of the document was changed &                //application has set the value of well-formed features to true                if ( ((fConfiguration.features & DOMConfigurationImpl.WELLFORMED) != 0) &&                fDocument.isXMLVersionChanged()){                    if(fNamespaceValidation){                        wellformed = CoreDocumentImpl.isValidQName(node.getPrefix() , node.getLocalName(),  fDocument.isXML11Version()) ;                    }                    else{                        wellformed = CoreDocumentImpl.isXMLName(node.getNodeName() , fDocument.isXML11Version());                    }                    if (!wellformed){				            String msg = DOMMessageFormatter.formatMessage(				                DOMMessageFormatter.DOM_DOMAIN, 				                "wf-invalid-character-in-node-name", 				                new Object[]{"Element", node.getNodeName()});                            reportDOMError(fErrorHandler, fError, fLocator, msg, DOMError.SEVERITY_ERROR,                                 "wf-invalid-character-in-node-name");                                           }                }                // push namespace context                fNamespaceContext.pushContext();                fLocalNSBinder.reset();                                ElementImpl elem = (ElementImpl)node;                if (elem.needsSyncChildren()) {                    elem.synchronizeChildren();                }                AttributeMap attributes = (elem.hasAttributes()) ? (AttributeMap) elem.getAttributes() : null;                                // fix namespaces and remove default attributes                if ((fConfiguration.features & DOMConfigurationImpl.NAMESPACES) !=0) {                    // fix namespaces                    // normalize attribute values                    // remove default attributes                    namespaceFixUp(elem, attributes);                } else {                    if ( attributes!=null ) {                        for ( int i=0; i<attributes.getLength(); ++i ) {                            Attr attr = (Attr)attributes.item(i);                            //removeDefault(attr, attributes);                            attr.normalize();                            if ( ((fConfiguration.features & DOMConfigurationImpl.WELLFORMED) != 0)){                                isAttrValueWF(fErrorHandler, fError, fLocator, attributes, (AttrImpl)attr, attr.getValue(), fDocument.isXML11Version());                                if (fDocument.isXMLVersionChanged()){                                    wellformed=CoreDocumentImpl.isXMLName(node.getNodeName() , fDocument.isXML11Version());                                    if (!wellformed){				                            String msg = DOMMessageFormatter.formatMessage(				                              DOMMessageFormatter.DOM_DOMAIN, 				                              "wf-invalid-character-in-node-name", 				                               new Object[]{"Attr",node.getNodeName()});				                            reportDOMError(fErrorHandler, fError, fLocator, msg, DOMError.SEVERITY_ERROR, 				                                "wf-invalid-character-in-node-name");                                    }                                }                            }                        }                    }                }                                                if (fValidationHandler != null) {                    // REVISIT: possible solutions to discard default content are:                    //         either we pass some flag to XML Schema validator                    //         or rely on the PSVI information.                    fAttrProxy.setAttributes(attributes, fDocument, elem);                    updateQName(elem, fQName); // updates global qname                    // set error node in the dom error wrapper                    // so if error occurs we can report an error node                    fConfiguration.fErrorHandlerWrapper.fCurrentNode = node;                    fCurrentNode = node;                    // call re-validation handler                    fValidationHandler.startElement(fQName, fAttrProxy, null);                }                                if(fDTDValidator != null){					if(attributes!=null)                    fAttrProxy.setAttributes(attributes, fDocument, elem);                    updateQName(elem, fQName); // updates global qname                    fCurrentNode = node;                    fDTDValidator.startElement(fQName, fAttrProxy, null);                }                                // normalize children                Node kid, next;                for (kid = elem.getFirstChild(); kid != null; kid = next) {                    next = kid.getNextSibling();                    kid = normalizeNode(kid);                    if (kid !=null) {                        next = kid;  // don't advance                    }                }                if (DEBUG_ND) {                    // normalized subtree                    System.out.println("***The children of {"+node.getNodeName()+"} are normalized");                    for (kid = elem.getFirstChild(); kid != null; kid = next) {                        next = kid.getNextSibling();                        System.out.println(kid.getNodeName() +"["+kid.getNodeValue()+"]");                    }                                    }                                                if (fValidationHandler != null) {                    updateQName(elem, fQName); // updates global qname                    //                    // set error node in the dom error wrapper                    // so if error occurs we can report an error node                    fConfiguration.fErrorHandlerWrapper.fCurrentNode = node;                    fCurrentNode = node;                    fValidationHandler.endElement(fQName, null);                }                if(fDTDValidator != null ){                    updateQName(elem, fQName); // updates global qname                    fCurrentNode = node;                    fDTDValidator.endElement(fQName,null);                }                                // pop namespace context                fNamespaceContext.popContext();                                break;            }                        case Node.COMMENT_NODE: {                if (DEBUG_ND) {                    System.out.println("==>normalizeNode:{comments}");                }                                if ((fConfiguration.features & DOMConfigurationImpl.COMMENTS) == 0) {                    Node prevSibling = node.getPreviousSibling();                    Node parent = node.getParentNode();                    // remove the comment node                    parent.removeChild(node);                    if (prevSibling != null && prevSibling.getNodeType() == Node.TEXT_NODE) {                        Node nextSibling = prevSibling.getNextSibling();                        if (nextSibling != null && nextSibling.getNodeType() == Node.TEXT_NODE) {                            ((TextImpl)nextSibling).insertData(0, prevSibling.getNodeValue());                            parent.removeChild(prevSibling);                            return nextSibling;                        }                    }                }//if comment node need not be removed                else {                    if ( ((fConfiguration.features & DOMConfigurationImpl.WELLFORMED) != 0)){                        String commentdata = ((Comment)node).getData();                        // check comments for invalid xml chracter as per the version                        // of the document                        isCommentWF(fErrorHandler, fError, fLocator, commentdata, fDocument.isXML11Version());                    }                }//end-else if comment node is not to be removed.                break;            }            case Node.ENTITY_REFERENCE_NODE: {                if (DEBUG_ND) {                    System.out.println("==>normalizeNode:{entityRef} "+node.getNodeName());                }                                if ((fConfiguration.features & DOMConfigurationImpl.ENTITIES) == 0) {                    Node prevSibling = node.getPreviousSibling();                    Node parent = node.getParentNode();                    ((EntityReferenceImpl)node).setReadOnly(false, true);                    expandEntityRef(parent, node);                    parent.removeChild(node);                    Node next = (prevSibling != null)?prevSibling.getNextSibling():parent.getFirstChild();                    // The list of children #text -> &ent;                    // and entity has a first child as a text                    // we should not advance                    if (prevSibling != null && next != null && prevSibling.getNodeType() == Node.TEXT_NODE && next.getNodeType() == Node.TEXT_NODE) {                        return prevSibling;  // Don't advance                                              }                    return next;                } else {                    if ( ((fConfiguration.features & DOMConfigurationImpl.WELLFORMED) != 0) &&

⌨️ 快捷键说明

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