xmlserializer.java
来自「JAVA 所有包」· Java 代码 · 共 1,471 行 · 第 1/4 页
JAVA
1,471 行
_printer.printSpace(); if (prefix == XMLSymbols.EMPTY_STRING) { if (DEBUG) { System.out.println("=>add xmlns=\""+uri+"\" declaration"); } _printer.printText( XMLSymbols.PREFIX_XMLNS ); } else { if (DEBUG) { System.out.println("=>add xmlns:"+prefix+"=\""+uri+"\" declaration"); } _printer.printText( "xmlns:"+prefix ); } _printer.printText( "=\"" ); printEscaped( uri ); _printer.printText( '"' ); } /** * Prints attribute. * NOTE: xml:space attribute modifies output format * * @param name * @param value * @param isSpecified * @exception IOException */ private void printAttribute (String name, String value, boolean isSpecified, Attr attr) throws IOException{ if (isSpecified || (features & DOMSerializerImpl.DISCARDDEFAULT) == 0) { if (fDOMFilter !=null && (fDOMFilter.getWhatToShow() & NodeFilter.SHOW_ATTRIBUTE)!= 0) { short code = fDOMFilter.acceptNode(attr); switch (code) { case NodeFilter.FILTER_REJECT: case NodeFilter.FILTER_SKIP: { return; } default: { // fall through } } } _printer.printSpace(); _printer.printText( name ); _printer.printText( "=\"" ); printEscaped( value ); _printer.printText( '"' ); } // If the attribute xml:space exists, determine whether // to preserve spaces in this and child nodes based on // its value. if (name.equals( "xml:space" )) { if (value.equals( "preserve" )) fPreserveSpace = true; else fPreserveSpace = _format.getPreserveSpace(); } } protected String getEntityRef( int ch ) { // Encode special XML characters into the equivalent character references. // These five are defined by default for all XML documents. switch (ch) { case '<': return "lt"; case '>': return "gt"; case '"': return "quot"; case '\'': return "apos"; case '&': return "amp"; } return null; } /** Retrieve and remove the namespaces declarations from the list of attributes. * */ private Attributes extractNamespaces( Attributes attrs ) throws SAXException { AttributesImpl attrsOnly; String rawName; int i; int indexColon; String prefix; int length; if (attrs == null) { return null; } length = attrs.getLength(); attrsOnly = new AttributesImpl( attrs ); for (i = length - 1 ; i >= 0 ; --i) { rawName = attrsOnly.getQName( i ); //We have to exclude the namespaces declarations from the attributes //Append only when the feature http://xml.org/sax/features/namespace-prefixes" //is TRUE if (rawName.startsWith( "xmlns" )) { if (rawName.length() == 5) { startPrefixMapping( "", attrs.getValue( i ) ); attrsOnly.removeAttribute( i ); } else if (rawName.charAt(5) == ':') { startPrefixMapping(rawName.substring(6), attrs.getValue(i)); attrsOnly.removeAttribute( i ); } } } return attrsOnly; } // // Printing attribute value // protected void printEscaped(String source) throws IOException { int length = source.length(); for (int i = 0; i < length; ++i) { int ch = source.charAt(i); if (!XMLChar.isValid(ch)) { if (++i < length) { surrogates(ch, source.charAt(i)); } else { fatalError("The character '" + (char) ch + "' is an invalid XML character"); } continue; } // escape NL, CR, TAB if (ch == '\n' || ch == '\r' || ch == '\t') { printHex(ch); } else if (ch == '<') { _printer.printText("<"); } else if (ch == '&') { _printer.printText("&"); } else if (ch == '"') { _printer.printText("""); } else if ((ch >= ' ' && _encodingInfo.isPrintable((char) ch))) { _printer.printText((char) ch); } else { printHex(ch); } } } /** print text data */ protected void printXMLChar( int ch) throws IOException { if (ch == '\r') { printHex(ch); } else if ( ch == '<') { _printer.printText("<"); } else if (ch == '&') { _printer.printText("&"); } else if (ch == '>'){ // character sequence "]]>" can't appear in content, therefore // we should escape '>' _printer.printText(">"); } else if ( ch == '\n' || ch == '\t' || ( ch >= ' ' && _encodingInfo.isPrintable((char)ch))) { _printer.printText((char)ch); } else { printHex(ch); } } protected void printText( String text, boolean preserveSpace, boolean unescaped ) throws IOException { int index; char ch; int length = text.length(); if ( preserveSpace ) { // Preserving spaces: the text must print exactly as it is, // without breaking when spaces appear in the text and without // consolidating spaces. If a line terminator is used, a line // break will occur. for ( index = 0 ; index < length ; ++index ) { ch = text.charAt( index ); if (!XMLChar.isValid(ch)) { // check if it is surrogate if (++index <length) { surrogates(ch, text.charAt(index)); } else { fatalError("The character '"+(char)ch+"' is an invalid XML character"); } continue; } if ( unescaped ) { _printer.printText( ch ); } else printXMLChar( ch ); } } else { // Not preserving spaces: print one part at a time, and // use spaces between parts to break them into different // lines. Spaces at beginning of line will be stripped // by printing mechanism. Line terminator is treated // no different than other text part. for ( index = 0 ; index < length ; ++index ) { ch = text.charAt( index ); if (!XMLChar.isValid(ch)) { // check if it is surrogate if (++index <length) { surrogates(ch, text.charAt(index)); } else { fatalError("The character '"+(char)ch+"' is an invalid XML character"); } continue; } if ( unescaped ) _printer.printText( ch ); else printXMLChar( ch); } } } protected void printText( char[] chars, int start, int length, boolean preserveSpace, boolean unescaped ) throws IOException { int index; char ch; if ( preserveSpace ) { // Preserving spaces: the text must print exactly as it is, // without breaking when spaces appear in the text and without // consolidating spaces. If a line terminator is used, a line // break will occur. while ( length-- > 0 ) { ch = chars[start++]; if (!XMLChar.isValid(ch)) { // check if it is surrogate if ( length-- > 0 ) { surrogates(ch, chars[start++]); } else { fatalError("The character '"+(char)ch+"' is an invalid XML character"); } continue; } if ( unescaped ) _printer.printText( ch ); else printXMLChar( ch ); } } else { // Not preserving spaces: print one part at a time, and // use spaces between parts to break them into different // lines. Spaces at beginning of line will be stripped // by printing mechanism. Line terminator is treated // no different than other text part. while ( length-- > 0 ) { ch = chars[start++]; if (!XMLChar.isValid(ch)) { // check if it is surrogate if ( length-- > 0 ) { surrogates(ch, chars[start++]); } else { fatalError("The character '"+(char)ch+"' is an invalid XML character"); } continue; } if ( unescaped ) _printer.printText( ch ); else printXMLChar( ch ); } } } /** * DOM Level 3: * Check a node to determine if it contains unbound namespace prefixes. * * @param node The node to check for unbound namespace prefices */ protected void checkUnboundNamespacePrefixedNode (Node node) throws IOException{ if (fNamespaces) { if (DEBUG) { System.out.println("==>serializeNode("+node.getNodeName()+") [Entity Reference - Namespaces on]"); System.out.println("==>Declared Prefix Count: " + fNSBinder.getDeclaredPrefixCount()); System.out.println("==>Node Name: " + node.getNodeName()); System.out.println("==>First Child Node Name: " + node.getFirstChild().getNodeName()); System.out.println("==>First Child Node Prefix: " + node.getFirstChild().getPrefix()); System.out.println("==>First Child Node NamespaceURI: " + node.getFirstChild().getNamespaceURI()); } Node child, next; for (child = node.getFirstChild(); child != null; child = next) { next = child.getNextSibling(); if (DEBUG) { System.out.println("==>serializeNode("+child.getNodeName()+") [Child Node]"); System.out.println("==>serializeNode("+child.getPrefix()+") [Child Node Prefix]"); } //If a NamespaceURI is not declared for the current //node's prefix, raise a fatal error. String prefix = child.getPrefix(); prefix = (prefix == null || prefix.length() == 0) ? XMLSymbols.EMPTY_STRING : fSymbolTable.addSymbol(prefix); if (fNSBinder.getURI(prefix) == null && prefix != null) { fatalError("The replacement text of the entity node '" + node.getNodeName() + "' contains an element node '" + child.getNodeName() + "' with an undeclared prefix '" + prefix + "'."); } if (child.getNodeType() == Node.ELEMENT_NODE) { NamedNodeMap attrs = child.getAttributes(); for (int i = 0; i< attrs.getLength(); i++ ) { String attrPrefix = attrs.item(i).getPrefix(); attrPrefix = (attrPrefix == null || attrPrefix.length() == 0) ? XMLSymbols.EMPTY_STRING : fSymbolTable.addSymbol(attrPrefix); if (fNSBinder.getURI(attrPrefix) == null && attrPrefix != null) { fatalError("The replacement text of the entity node '" + node.getNodeName() + "' contains an element node '" + child.getNodeName() + "' with an attribute '" + attrs.item(i).getNodeName() + "' an undeclared prefix '" + attrPrefix + "'."); } } } if (child.hasChildNodes()) { checkUnboundNamespacePrefixedNode(child); } } } } public boolean reset() { super.reset(); if (fNSBinder != null){ fNSBinder.reset(); // during serialization always have a mapping to empty string // so we assume there is a declaration. fNSBinder.declarePrefix(XMLSymbols.EMPTY_STRING, XMLSymbols.EMPTY_STRING); } return true; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?