xmlserializer.java
来自「JAVA 所有包」· Java 代码 · 共 1,471 行 · 第 1/4 页
JAVA
1,471 行
String rawName ) throws SAXException { try { endElementIO( namespaceURI, localName, rawName ); } catch (IOException except) { throw new SAXException( except ); } } public void endElementIO( String namespaceURI, String localName, String rawName ) throws IOException { ElementState state; if (DEBUG) { System.out.println("==>endElement: " +rawName); } // Works much like content() with additions for closing // an element. Note the different checks for the closed // element's state and the parent element's state. _printer.unindent(); state = getElementState(); if (state.empty) { _printer.printText( "/>" ); } else { // Must leave CData section first if (state.inCData) _printer.printText( "]]>" ); // This element is not empty and that last content was // another element, so print a line break before that // last element and this element's closing tag. if (_indenting && ! state.preserveSpace && (state.afterElement || state.afterComment)) _printer.breakLine(); _printer.printText( "</" ); _printer.printText( state.rawName ); _printer.printText( '>' ); } // Leave the element state and update that of the parent // (if we're not root) to not empty and after element. state = leaveElementState(); state.afterElement = true; state.afterComment = false; state.empty = false; if (isDocumentState()) _printer.flush(); } //------------------------------------------// // SAX document handler serializing methods // //------------------------------------------// public void startElement( String tagName, AttributeList attrs ) throws SAXException { int i; boolean preserveSpace; ElementState state; String name; String value; if (DEBUG) { System.out.println("==>startElement("+tagName+")"); } try { if (_printer == null) { String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "NoWriterSupplied", null); throw new IllegalStateException(msg); } state = getElementState(); if (isDocumentState()) { // If this is the root element handle it differently. // If the first root element in the document, serialize // the document's DOCTYPE. Space preserving defaults // to that of the output format. if (! _started) startDocument( tagName ); } else { // For any other element, if first in parent, then // close parent's opening tag and use the parnet's // space preserving. if (state.empty) _printer.printText( '>' ); // Must leave CData section first if (state.inCData) { _printer.printText( "]]>" ); state.inCData = false; } // Indent this element on a new line if the first // content of the parent element or immediately // following an element. if (_indenting && ! state.preserveSpace && ( state.empty || state.afterElement || state.afterComment)) _printer.breakLine(); } preserveSpace = state.preserveSpace; // Do not change the current element state yet. // This only happens in endElement(). _printer.printText( '<' ); _printer.printText( tagName ); _printer.indent(); // For each attribute print it's name and value as one part, // separated with a space so the element can be broken on // multiple lines. if (attrs != null) { for (i = 0 ; i < attrs.getLength() ; ++i) { _printer.printSpace(); name = attrs.getName( i ); value = attrs.getValue( i ); if (value != null) { _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" )) preserveSpace = true; else preserveSpace = _format.getPreserveSpace(); } } } // Now it's time to enter a new element state // with the tag name and space preserving. // We still do not change the curent element state. state = enterElementState( null, null, tagName, preserveSpace ); state.doCData = _format.isCDataElement( tagName ); state.unescaped = _format.isNonEscapingElement( tagName ); } catch (IOException except) { throw new SAXException( except ); } } public void endElement( String tagName ) throws SAXException { endElement( null, null, tagName ); } //------------------------------------------// // Generic node serializing methods methods // //------------------------------------------// /** * Called to serialize the document's DOCTYPE by the root element. * The document type declaration must name the root element, * but the root element is only known when that element is serialized, * and not at the start of the document. * <p> * This method will check if it has not been called before ({@link #_started}), * will serialize the document type declaration, and will serialize all * pre-root comments and PIs that were accumulated in the document * (see {@link #serializePreRoot}). Pre-root will be serialized even if * this is not the first root element of the document. */ protected void startDocument( String rootTagName ) throws IOException { int i; String dtd; dtd = _printer.leaveDTD(); if (! _started) { if (! _format.getOmitXMLDeclaration()) { StringBuffer buffer; // Serialize the document declaration appreaing at the head // of very XML document (unless asked not to). buffer = new StringBuffer( "<?xml version=\"" ); if (_format.getVersion() != null) buffer.append( _format.getVersion() ); else buffer.append( "1.0" ); buffer.append( '"' ); String format_encoding = _format.getEncoding(); if (format_encoding != null) { buffer.append( " encoding=\"" ); buffer.append( format_encoding ); buffer.append( '"' ); } if (_format.getStandalone() && _docTypeSystemId == null && _docTypePublicId == null) buffer.append( " standalone=\"yes\"" ); buffer.append( "?>" ); _printer.printText( buffer ); _printer.breakLine(); } if (! _format.getOmitDocumentType()) { if (_docTypeSystemId != null) { // System identifier must be specified to print DOCTYPE. // If public identifier is specified print 'PUBLIC // <public> <system>', if not, print 'SYSTEM <system>'. _printer.printText( "<!DOCTYPE " ); _printer.printText( rootTagName ); if (_docTypePublicId != null) { _printer.printText( " PUBLIC " ); printDoctypeURL( _docTypePublicId ); if (_indenting) { _printer.breakLine(); for (i = 0 ; i < 18 + rootTagName.length() ; ++i) _printer.printText( " " ); } else _printer.printText( " " ); printDoctypeURL( _docTypeSystemId ); } else { _printer.printText( " SYSTEM " ); printDoctypeURL( _docTypeSystemId ); } // If we accumulated any DTD contents while printing. // this would be the place to print it. if (dtd != null && dtd.length() > 0) { _printer.printText( " [" ); printText( dtd, true, true ); _printer.printText( ']' ); } _printer.printText( ">" ); _printer.breakLine(); } else if (dtd != null && dtd.length() > 0) { _printer.printText( "<!DOCTYPE " ); _printer.printText( rootTagName ); _printer.printText( " [" ); printText( dtd, true, true ); _printer.printText( "]>" ); _printer.breakLine(); } } } _started = true; // Always serialize these, even if not te first root element. serializePreRoot(); } /** * Called to serialize a DOM element. Equivalent to calling {@link * #startElement}, {@link #endElement} and serializing everything * inbetween, but better optimized. */ protected void serializeElement( Element elem ) throws IOException { Attr attr; NamedNodeMap attrMap; int i; Node child; ElementState state; String name; String value; String tagName; String prefix, localUri; String uri; if (fNamespaces) { // local binder stores namespace declaration // that has been printed out during namespace fixup of // the current element fLocalNSBinder.reset(); // add new namespace context fNSBinder.pushContext(); } if (DEBUG) { System.out.println("==>startElement: " +elem.getNodeName() +" ns="+elem.getNamespaceURI()); } tagName = elem.getTagName(); state = getElementState(); if (isDocumentState()) { // If this is the root element handle it differently. // If the first root element in the document, serialize // the document's DOCTYPE. Space preserving defaults // to that of the output format. if (! _started) { startDocument( tagName); } } else { // For any other element, if first in parent, then // close parent's opening tag and use the parent's // space preserving. if (state.empty) _printer.printText( '>' ); // Must leave CData section first if (state.inCData) { _printer.printText( "]]>" ); state.inCData = false; } // Indent this element on a new line if the first // content of the parent element or immediately // following an element. if (_indenting && ! state.preserveSpace && ( state.empty || state.afterElement || state.afterComment)) _printer.breakLine(); } // Do not change the current element state yet. // This only happens in endElement(). fPreserveSpace = state.preserveSpace; int length = 0; attrMap = null; // retrieve attributes if (elem.hasAttributes()) { attrMap = elem.getAttributes(); length = attrMap.getLength(); } if (!fNamespaces) { // no namespace fixup should be performed // serialize element name _printer.printText( '<' ); _printer.printText( tagName ); _printer.indent(); // For each attribute print it's name and value as one part, // separated with a space so the element can be broken on // multiple lines. for ( i = 0 ; i < length ; ++i ) { attr = (Attr) attrMap.item( i ); name = attr.getName(); value = attr.getValue(); if ( value == null ) value = ""; printAttribute (name, value, attr.getSpecified(), attr); } } else { // do namespace fixup // REVISIT: some optimization could probably be done to avoid traversing // attributes twice. // // --------------------------------------- // record all valid namespace declarations // before attempting to fix element's namespace // --------------------------------------- for (i = 0;i < length;i++) { attr = (Attr) attrMap.item( i ); uri = attr.getNamespaceURI(); // check if attribute is a namespace decl if (uri != null && uri.equals(NamespaceContext.XMLNS_URI)) { value = attr.getNodeValue();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?