domnormalizer.java

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

JAVA
1,336
字号
                if (fDTDValidator != 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;                    fDTDValidator.startCDATA(null);                    fDTDValidator.characterData(node.getNodeValue(), null);                    fDTDValidator.endCDATA(null);                }                String value = node.getNodeValue();                                if ((fConfiguration.features & DOMConfigurationImpl.SPLITCDATA) != 0) {                    int index;                    Node parent = node.getParentNode();                    if (fDocument.errorChecking) {                        isXMLCharWF(fErrorHandler, fError, fLocator, node.getNodeValue(), fDocument.isXML11Version());                    }                    while ( (index=value.indexOf("]]>")) >= 0 ) {                        node.setNodeValue(value.substring(0, index+2));                        value = value.substring(index +2);                                                Node firstSplitNode = node;                        Node newChild = fDocument.createCDATASection(value);                        parent.insertBefore(newChild, node.getNextSibling());                        node = newChild;                                              // issue warning                        fLocator.fRelatedNode = firstSplitNode;                        String msg = DOMMessageFormatter.formatMessage(                            DOMMessageFormatter.DOM_DOMAIN,                             "cdata-sections-splitted",                              null);                        reportDOMError(fErrorHandler, fError, fLocator, msg, DOMError.SEVERITY_WARNING,                             "cdata-sections-splitted");                    }                }                else if (fDocument.errorChecking) {                    // check well-formedness                    isCDataWF(fErrorHandler, fError, fLocator, value, fDocument.isXML11Version());                }                break;            }        case Node.TEXT_NODE: {                 if (DEBUG_ND) {                    System.out.println("==>normalizeNode(text):{"+node.getNodeValue()+"}");                }                // If node is a text node, we need to check for one of two                // conditions:                //   1) There is an adjacent text node                //   2) There is no adjacent text node, but node is                //      an empty text node.                Node next = node.getNextSibling();                // If an adjacent text node, merge it with this node                if ( next!=null && next.getNodeType() == Node.TEXT_NODE ) {                    ((Text)node).appendData(next.getNodeValue());                    node.getParentNode().removeChild( next );                    // We don't need to check well-formness here since we are not yet                    // done with this node.                                        return node; // Don't advance;                                   } else if (node.getNodeValue().length()==0) {                    // If kid is empty, remove it                    node.getParentNode().removeChild( node );                } else {                                        // validator.characters() call and well-formness                    // Don't send characters or check well-formness in the following cases:                    // 1. entities is false, next child is entity reference: expand tree first                    // 2. comments is false, and next child is comment                    // 3. cdata is false, and next child is cdata                                      short nextType = (next != null)?next.getNodeType():-1;                    if (nextType == -1 || !(((fConfiguration.features & DOMConfigurationImpl.ENTITIES) == 0 &&                           nextType == Node.ENTITY_NODE) ||                          ((fConfiguration.features & DOMConfigurationImpl.COMMENTS) == 0 &&                           nextType == Node.COMMENT_NODE) ||                          ((fConfiguration.features & DOMConfigurationImpl.CDATA) == 0) &&                          nextType == Node.CDATA_SECTION_NODE)) {                              if (fDocument.errorChecking && ((fConfiguration.features & DOMConfigurationImpl.WELLFORMED) != 0) ){                                  isXMLCharWF(fErrorHandler, fError, fLocator, node.getNodeValue(), fDocument.isXML11Version());                              }                                                            if (fValidationHandler != null) {                                     fConfiguration.fErrorHandlerWrapper.fCurrentNode = node;                                     fCurrentNode = node;                                     fValidationHandler.characterData(node.getNodeValue(), null);                                     if (DEBUG_ND) {                                         System.out.println("=====>characterData(),"+nextType);                                     }                              }                                 if (fDTDValidator != null) {                                  fConfiguration.fErrorHandlerWrapper.fCurrentNode = node;                                  fCurrentNode = node;                                  fDTDValidator.characterData(node.getNodeValue(), null);                                  if (DEBUG_ND) {                                      System.out.println("=====>characterData(),"+nextType);                                  }                                  if(allWhitespace) {                                      allWhitespace = false;                                      ((TextImpl)node).setIgnorableWhitespace(true);                                  }                              }                       }                    else {                            if (DEBUG_ND) {                                System.out.println("=====>don't send characters(),"+nextType);                            }                    }                }                break;            }                case Node.PROCESSING_INSTRUCTION_NODE: {                        //do the well-formed valid PI target name , data check when application has set the value of well-formed feature to true            if (fDocument.errorChecking && (fConfiguration.features & DOMConfigurationImpl.WELLFORMED) != 0 ) {                ProcessingInstruction pinode = (ProcessingInstruction)node ;                String target = pinode.getTarget();                //1.check PI target name                if(fDocument.isXML11Version()){                    wellformed = XML11Char.isXML11ValidName(target);                }                else{                                    wellformed = XMLChar.isValidName(target);                }				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");                }                                                        //2. check PI data                //processing isntruction data may have certain characters                //which may not be valid XML character                               isXMLCharWF(fErrorHandler, fError, fLocator, pinode.getData(), fDocument.isXML11Version());            }        }//end case Node.PROCESSING_INSTRUCTION_NODE                }//end of switch        return null;    }//normalizeNode    private XMLGrammarPool createGrammarPool(DocumentTypeImpl docType) {        XMLGrammarPoolImpl pool = new XMLGrammarPoolImpl();                XMLGrammarPreparser preParser = new XMLGrammarPreparser(fSymbolTable);        preParser.registerPreparser(XMLGrammarDescription.XML_DTD, null);        preParser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);        preParser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, true);        preParser.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY, pool);                String internalSubset = docType.getInternalSubset();        XMLInputSource is = new XMLInputSource(docType.getPublicId(), docType.getSystemId(), null);                if(internalSubset != null)            is.setCharacterStream(new StringReader(internalSubset));        try {            DTDGrammar g = (DTDGrammar)preParser.preparseGrammar(XMLGrammarDescription.XML_DTD, is);            ((XMLDTDDescription)g.getGrammarDescription()).setRootName(docType.getName());            is.setCharacterStream(null);            g = (DTDGrammar)preParser.preparseGrammar(XMLGrammarDescription.XML_DTD, is);            ((XMLDTDDescription)g.getGrammarDescription()).setRootName(docType.getName());                    } catch (XNIException e) {        } catch (IOException e) {        }                return pool;    }    protected final void expandEntityRef (Node parent, Node reference){        Node kid, next;        for (kid = reference.getFirstChild(); kid != null; kid = next) {            next = kid.getNextSibling();            parent.insertBefore(kid, reference);        }    }    // fix namespaces    // normalize attribute values    // remove default attributes    // check attribute names if the version of the document changed.        protected final void namespaceFixUp (ElementImpl element, AttributeMap attributes){        if (DEBUG) {            System.out.println("[ns-fixup] element:" +element.getNodeName()+                               " uri: "+element.getNamespaceURI());        }        // ------------------------------------        // pick up local namespace declarations        // <xsl:stylesheet xmlns:xsl="http://xslt">        //   <!-- add the following via DOM         //          body is bound to http://xslt        //    -->        //   <xsl:body xmlns:xsl="http://bound"/>        //        // ------------------------------------        String value, name, uri, prefix;        if (attributes != null) {            // Record all valid local declarations            for (int k = 0; k < attributes.getLength(); ++k) {                Attr attr = (Attr)attributes.getItem(k);                               //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 && ((fConfiguration.features & DOMConfigurationImpl.WELLFORMED) != 0) &&                     fDocument.isXMLVersionChanged()) {                    //checkQName does checking based on the version of the document                    fDocument.checkQName(attr.getPrefix() , attr.getLocalName()) ;                }                                uri = attr.getNamespaceURI();                if (uri != null && uri.equals(NamespaceContext.XMLNS_URI)) {                    // namespace attribute                    value = attr.getNodeValue();                    if (value == null) {                        value=XMLSymbols.EMPTY_STRING;                    }                    // Check for invalid namespace declaration:                    if (fDocument.errorChecking && value.equals(NamespaceContext.XMLNS_URI)) {                    	//A null value for locale is passed to formatMessage,                     	//which means that the default locale will be used                        fLocator.fRelatedNode = attr;                        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.XML_DOMAIN,"CantBindXMLNS",null );                        reportDOMError(fErrorHandler, fError, fLocator, msg, DOMError.SEVERITY_ERROR, "CantBindXMLNS");                    } else {                        // XML 1.0 Attribute value normalization                        // value = normalizeAttributeValue(value, attr);                        prefix = attr.getPrefix();                        prefix = (prefix == null ||                                   prefix.length() == 0) ? XMLSymbols.EMPTY_STRING :fSymbolTable.addSymbol(prefix);                        String localpart = fSymbolTable.addSymbol( attr.getLocalName());                        if (prefix == XMLSymbols.PREFIX_XMLNS) { //xmlns:prefix                            value = fSymbolTable.addSymbol(value);                            if (value.length() != 0) {                                fNamespaceContext.declarePrefix(localpart, value);                            } else {                                // REVISIT: issue error on invalid declarations                                //          xmlns:foo = ""                            }                            //removeDefault (attr, attributes);                            continue;                        } else { // (localpart == fXmlnsSymbol && prefix == fEmptySymbol)  -- xmlns                            // empty prefix is always bound ("" or some string)                            value = fSymbolTable.addSymbol(value);                            fNamespaceContext.declarePrefix(XMLSymbols.EMPTY_STRING, value);                            //removeDefault (attr, attributes);                            continue;                        }                    }  // end-else: valid declaration                } // end-if: namespace attribute            }        }

⌨️ 快捷键说明

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