domnormalizer.java

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

JAVA
1,354
字号
        //version of the document is XML 1.1        if(isXML11Version){            //we need to check all chracters as per production rules of XML11            int i = 0 ;            while(i < datalength){                char c = dataarray[i++];                if(XML11Char.isXML11Invalid(c)){                    String msg =                    DOMMessageFormatter.formatMessage(                    DOMMessageFormatter.XML_DOMAIN,                    "InvalidCharInCDSect",                    new Object[] { Integer.toString(c, 16)});                    reportDOMError(                        errorHandler,                        error,                        locator,                        msg,                        DOMError.SEVERITY_ERROR,                        "wf-invalid-character");                }                else if (c==']'){                    int count = i;                    if (count<datalength && dataarray[count]==']'){                        while (++count <datalength && dataarray[count]==']'){                            // do nothing                        }                        if (count <datalength && dataarray[count]=='>'){                            //CDEndInContent		  					String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.XML_DOMAIN,							    "CDEndInContent", null);							reportDOMError(errorHandler, error, locator,msg, DOMError.SEVERITY_ERROR, "wf-invalid-character");                        }                    }                                    }            }        }//version of the document is XML 1.0        else{            //we need to check all chracters as per production rules of XML 1.0            int i = 0 ;            while(i < datalength){                char c = dataarray[i++];                if( XMLChar.isInvalid(c) ){                	//Note:  The key InvalidCharInCDSect from XMLMessages.properties                	//is being used to obtain the message and DOM error type                	//"wf-invalid-character" is used.  Also per DOM it is error but                 	//as per XML spec. it is fatal error					String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.XML_DOMAIN, "InvalidCharInCDSect", new Object[]{Integer.toString(c, 16)});					reportDOMError(errorHandler, error, locator, msg, DOMError.SEVERITY_ERROR, 					    "wf-invalid-character");                }                else if (c==']'){                    int count = i;                    if (count<datalength && dataarray[count]==']'){                        while (++count <datalength && dataarray[count]==']'){                            // do nothing                        }                        if (count <datalength && dataarray[count]=='>'){		  					String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.XML_DOMAIN,"CDEndInContent", null);							reportDOMError(errorHandler, error, locator,  msg, DOMError.SEVERITY_ERROR, "wf-invalid-character");                        }                    }                                    }            }        }//end-else fDocument.isXMLVersion()            }//isCDataWF        /**     * NON-DOM: check for valid XML characters as per the XML version     * @param datavalue     * @param isXML11Version = true if XML 1.1     */    public static final void isXMLCharWF(DOMErrorHandler errorHandler, DOMErrorImpl error, DOMLocatorImpl locator,    String datavalue, boolean isXML11Version) {        if(datavalue == null || (datavalue.length() == 0) ) return ;        char [] dataarray = datavalue.toCharArray();        int datalength = dataarray.length ;                //version of the document is XML 1.1        if(isXML11Version){                                //we need to check all characters as per production rules of XML11            int i = 0 ;            while(i < datalength){                if(XML11Char.isXML11Invalid(dataarray[i++])){					String msg = DOMMessageFormatter.formatMessage(                        DOMMessageFormatter.DOM_DOMAIN, "InvalidXMLCharInDOM",                         new Object[]{Integer.toString(dataarray[i-1], 16)});					reportDOMError(errorHandler, error, locator, msg, DOMError.SEVERITY_ERROR, 					    "wf-invalid-character");                };            }        }//version of the document is XML 1.0        else{                                //we need to check all characters as per production rules of XML 1.0            int i = 0 ;            while(i < datalength){                if( XMLChar.isInvalid(dataarray[i++]) ){					String msg = DOMMessageFormatter.formatMessage(                        DOMMessageFormatter.DOM_DOMAIN, "InvalidXMLCharInDOM",                         new Object[]{Integer.toString(dataarray[i-1], 16)});					reportDOMError(errorHandler, error, locator, msg, DOMError.SEVERITY_ERROR, 					    "wf-invalid-character");                };            }        }//end-else fDocument.isXMLVersion()            }//isXMLCharWF        /**     * NON-DOM: check if value of the comment is well-formed     * @param datavalue     * @param isXML11Version = true if XML 1.1     */    public static final void isCommentWF(DOMErrorHandler errorHandler, DOMErrorImpl error, DOMLocatorImpl locator,    String datavalue, boolean isXML11Version) {        if(datavalue == null || (datavalue.length() == 0) ) return ;                char [] dataarray = datavalue.toCharArray();        int datalength = dataarray.length ;                //version of the document is XML 1.1        if(isXML11Version){            //we need to check all chracters as per production rules of XML11            int i = 0 ;            while(i < datalength){                char c = dataarray[i++];                                if(XML11Char.isXML11Invalid(c)){  					String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.XML_DOMAIN, 					    "InvalidCharInComment", 					    new Object [] {Integer.toString(dataarray[i-1], 16)});					reportDOMError(errorHandler, error, locator, msg, DOMError.SEVERITY_ERROR, "wf-invalid-character");                }                else if (c == '-' && i<datalength && dataarray[i]=='-'){  					String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.XML_DOMAIN,					    "DashDashInComment", null);					// invalid: '--' in comment                   					reportDOMError(errorHandler, error, locator, msg, DOMError.SEVERITY_ERROR, "wf-invalid-character");                }            }        }//version of the document is XML 1.0        else{            //we need to check all chracters as per production rules of XML 1.0            int i = 0 ;            while(i < datalength){                char c = dataarray[i++];                if( XMLChar.isInvalid(c) ){  					String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.XML_DOMAIN,					    "InvalidCharInComment", new Object [] {Integer.toString(dataarray[i-1], 16)});					reportDOMError(errorHandler, error, locator, msg, DOMError.SEVERITY_ERROR, "wf-invalid-character");                }                  else if (c == '-' && i<datalength && dataarray[i]=='-'){  					String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.XML_DOMAIN,					    "DashDashInComment", null);					// invalid: '--' in comment                   					reportDOMError(errorHandler, error, locator, msg, DOMError.SEVERITY_ERROR, "wf-invalid-character");                }                                                  }                    }//end-else fDocument.isXMLVersion()            }//isCommentWF        /** NON-DOM: check if attribute value is well-formed     * @param attributes     * @param a     * @param value     */    public static final void isAttrValueWF(DOMErrorHandler errorHandler, DOMErrorImpl error,    DOMLocatorImpl locator, AttributeMap attributes, AttrImpl a, String value, boolean xml11Version) {        if (a.hasStringValue()) {            isXMLCharWF(errorHandler, error, locator, value, xml11Version);        } else {            NodeList children = a.getChildNodes();            //check each child node of the attribute's value            for (int j = 0; j < children.getLength(); j++) {                Node child = children.item(j);                //If the attribute's child is an entity refernce                if (child.getNodeType() == Node.ENTITY_REFERENCE_NODE) {                    Document owner = a.getOwnerDocument();                    Entity ent = null;                    //search for the entity in the docType                    //of the attribute's ownerDocument                    if (owner != null) {                        DocumentType docType = owner.getDoctype();                        if (docType != null) {                            NamedNodeMap entities = docType.getEntities();                            ent = (Entity) entities.getNamedItemNS(                            "*",                            child.getNodeName());                        }                    }                    //If the entity was not found issue a fatal error                    if (ent == null) {                        String msg = DOMMessageFormatter.formatMessage(                            DOMMessageFormatter.DOM_DOMAIN, "UndeclaredEntRefInAttrValue",                             new Object[]{a.getNodeName()});                        reportDOMError(errorHandler, error, locator, msg, DOMError.SEVERITY_ERROR,                             "UndeclaredEntRefInAttrValue");                    }                }                else {                    // Text node                    isXMLCharWF(errorHandler, error, locator, child.getNodeValue(), xml11Version);                }            }        }    }                /**     * Reports a DOM error to the user handler.     *     * If the error is fatal, the processing will be always aborted.     */    public static final void reportDOMError(DOMErrorHandler errorHandler, DOMErrorImpl error, DOMLocatorImpl locator, String message, short severity, String type ) {        if( errorHandler!=null ) {            error.reset();            error.fMessage = message;            error.fSeverity = severity;            error.fLocator = locator;            error.fType = type;            error.fRelatedData = locator.fRelatedNode;                if(!errorHandler.handleError(error))                throw abort;        }        if( severity==DOMError.SEVERITY_FATAL_ERROR )            throw abort;    }        protected final void updateQName(Node node, QName qname){                String prefix    = node.getPrefix();        String namespace = node.getNamespaceURI();        String localName = node.getLocalName();        // REVISIT: the symbols are added too often: start/endElement        //          and in the namespaceFixup. Should reduce number of calls to symbol table.        qname.prefix = (prefix!=null && prefix.length()!=0)?fSymbolTable.addSymbol(prefix):null;        qname.localpart = (localName != null)?fSymbolTable.addSymbol(localName):null;        qname.rawname = fSymbolTable.addSymbol(node.getNodeName());        qname.uri =  (namespace != null)?fSymbolTable.addSymbol(namespace):null;    }                /* REVISIT: remove this method if DOM does not change spec.     * Performs partial XML 1.0 attribute value normalization and replaces     * attribute value if the value is changed after the normalization.     * DOM defines that normalizeDocument acts as if the document was going     * through a save and load cycle, given that serializer will not escape     * any '\n' or '\r' characters on load those will be normalized.     * Thus during normalize document we need to do the following:     * - perform "2.11 End-of-Line Handling"     * - replace #xD, #xA, #x9 with #x20 (white space).     * Note: This alg. won't attempt to resolve entity references or character entity     * references, since '&' will be escaped during serialization and during loading     * this won't be recognized as entity reference, i.e. attribute value "&foo;" will     * be serialized as "&amp;foo;" and thus after loading will be "&foo;" again.     * @param value current attribute value     * @param attr current attribute     * @return String the value (could be original if normalization did not change     * the string)     */    final String normalizeAttributeValue(String value, Attr attr) {        if (!attr.getSpecified()){            // sp

⌨️ 快捷键说明

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