domnormalizer.java
来自「JAVA 所有包」· Java 代码 · 共 1,336 行 · 第 1/5 页
JAVA
1,336 行
int type = node.getNodeType(); boolean wellformed; fLocator.fRelatedNode=node; switch (type) { case Node.DOCUMENT_TYPE_NODE: { if (DEBUG_ND) { System.out.println("==>normalizeNode:{doctype}"); } DocumentTypeImpl docType = (DocumentTypeImpl)node; fDTDValidator = (XMLDTDValidator)CoreDOMImplementationImpl.singleton.getValidator(XMLGrammarDescription.XML_DTD); fDTDValidator.setDocumentHandler(this); fConfiguration.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY, createGrammarPool(docType)); fDTDValidator.reset(fConfiguration); fDTDValidator.startDocument( new SimpleLocator(fDocument.fDocumentURI, fDocument.fDocumentURI, -1, -1 ), fDocument.encoding, fNamespaceContext, null); fDTDValidator.doctypeDecl(docType.getName(), docType.getPublicId(), docType.getSystemId(), null); //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 (fDocument.errorChecking) { 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); if ((fConfiguration.features & DOMConfigurationImpl.NSDECL) == 0 && attributes != null ) { for (int i = 0; i < attributes.getLength(); ++i) { Attr att = (Attr)attributes.getItem(i); if (XMLSymbols.PREFIX_XMLNS.equals(att.getPrefix()) || XMLSymbols.PREFIX_XMLNS.equals(att.getName())) { elem.removeAttributeNode(att); --i; } } } } else { if ( attributes!=null ) { for ( int i=0; i<attributes.getLength(); ++i ) { Attr attr = (Attr)attributes.item(i); //removeDefault(attr, attributes); attr.normalize(); if (fDocument.errorChecking && ((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) { // 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 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 // // set error node in the dom error wrapper // so if error occurs we can report an error node fConfiguration.fErrorHandlerWrapper.fCurrentNode = node; 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 (fDocument.errorChecking && ((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 (fDocument.errorChecking && ((fConfiguration.features & DOMConfigurationImpl.WELLFORMED) != 0) && fDocument.isXMLVersionChanged()){ CoreDocumentImpl.isXMLName(node.getNodeName() , fDocument.isXML11Version()); } // REVISIT: traverse entity reference and send appropriate calls to the validator // (no normalization should be performed for the children). } break; } case Node.CDATA_SECTION_NODE: { if (DEBUG_ND) { System.out.println("==>normalizeNode:{cdata}"); } if ((fConfiguration.features & DOMConfigurationImpl.CDATA) == 0) { // convert CDATA to TEXT nodes Node prevSibling = node.getPreviousSibling(); if (prevSibling != null && prevSibling.getNodeType() == Node.TEXT_NODE){ ((Text)prevSibling).appendData(node.getNodeValue()); node.getParentNode().removeChild(node); return prevSibling; //don't advance } else { Text text = fDocument.createTextNode(node.getNodeValue()); Node parent = node.getParentNode(); node = parent.replaceChild(text, node); return text; //don't advance } } // send characters call for CDATA if (fValidationHandler != null) { // 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.startCDATA(null); fValidationHandler.characterData(node.getNodeValue(), null); fValidationHandler.endCDATA(null); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?