xmlserializer.java
来自「JAVA 所有包」· Java 代码 · 共 1,471 行 · 第 1/4 页
JAVA
1,471 行
/* * Copyright 1999-2002,2004,2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */// Sep 14, 2000:// Fixed problem with namespace handling. Contributed by// David Blondeau <blondeau@intalio.com>// Sep 14, 2000:// Fixed serializer to report IO exception directly, instead at// the end of document processing.// Reported by Patrick Higgins <phiggins@transzap.com>// Aug 21, 2000:// Fixed bug in startDocument not calling prepare.// Reported by Mikael Staldal <d96-mst-ingen-reklam@d.kth.se>// Aug 21, 2000:// Added ability to omit DOCTYPE declaration.package com.sun.org.apache.xml.internal.serialize;import java.io.IOException;import java.io.OutputStream;import java.io.Writer;import java.util.Enumeration;import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;import com.sun.org.apache.xerces.internal.util.NamespaceSupport;import com.sun.org.apache.xerces.internal.util.SymbolTable;import com.sun.org.apache.xerces.internal.util.XMLChar;import com.sun.org.apache.xerces.internal.util.XMLSymbols;import com.sun.org.apache.xerces.internal.xni.NamespaceContext;import org.w3c.dom.Attr;import org.w3c.dom.DOMError;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.traversal.NodeFilter;import org.xml.sax.AttributeList;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.AttributesImpl;/** * Implements an XML serializer supporting both DOM and SAX pretty * serializing. For usage instructions see {@link Serializer}. * <p> * If an output stream is used, the encoding is taken from the * output format (defaults to <tt>UTF-8</tt>). If a writer is * used, make sure the writer uses the same encoding (if applies) * as specified in the output format. * <p> * The serializer supports both DOM and SAX. SAX serializing is done by firing * SAX events and using the serializer as a document handler. DOM serializing is done * by calling {@link #serialize(Document)} or by using DOM Level 3 * {@link org.w3c.dom.ls.DOMSerializer} and * serializing with {@link org.w3c.dom.ls.DOMSerializer#write}, * {@link org.w3c.dom.ls.DOMSerializer#writeToString}. * <p> * If an I/O exception occurs while serializing, the serializer * will not throw an exception directly, but only throw it * at the end of serializing (either DOM or SAX's {@link * org.xml.sax.DocumentHandler#endDocument}. * <p> * For elements that are not specified as whitespace preserving, * the serializer will potentially break long text lines at space * boundaries, indent lines, and serialize elements on separate * lines. Line terminators will be regarded as spaces, and * spaces at beginning of line will be stripped. * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a> * @author <a href="mailto:rahul.srivastava@sun.com">Rahul Srivastava</a> * @author Elena Litani IBM * @version $Revision: 1.2.6.1 $ $Date: 2005/09/09 07:26:18 $ * @see Serializer */public class XMLSerializerextends BaseMarkupSerializer { // // constants // protected static final boolean DEBUG = false; // // data // // // DOM Level 3 implementation: variables intialized in DOMSerializerImpl // /** stores namespaces in scope */ protected NamespaceSupport fNSBinder; /** stores all namespace bindings on the current element */ protected NamespaceSupport fLocalNSBinder; /** symbol table for serialization */ protected SymbolTable fSymbolTable; protected final static String PREFIX = "NS"; /** * Controls whether namespace fixup should be performed during * the serialization. * NOTE: if this field is set to true the following * fields need to be initialized: fNSBinder, fLocalNSBinder, fSymbolTable, * XMLSymbols.EMPTY_STRING, fXmlSymbol, fXmlnsSymbol */ protected boolean fNamespaces = false; /** * Controls whether namespace prefixes will be printed out during serialization */ protected boolean fNamespacePrefixes = true; private boolean fPreserveSpace; /** * Constructs a new serializer. The serializer cannot be used without * calling {@link #setOutputCharStream} or {@link #setOutputByteStream} * first. */ public XMLSerializer() { super( new OutputFormat( Method.XML, null, false ) ); } /** * Constructs a new serializer. The serializer cannot be used without * calling {@link #setOutputCharStream} or {@link #setOutputByteStream} * first. */ public XMLSerializer( OutputFormat format ) { super( format != null ? format : new OutputFormat( Method.XML, null, false ) ); _format.setMethod( Method.XML ); } /** * Constructs a new serializer that writes to the specified writer * using the specified output format. If <tt>format</tt> is null, * will use a default output format. * * @param writer The writer to use * @param format The output format to use, null for the default */ public XMLSerializer( Writer writer, OutputFormat format ) { super( format != null ? format : new OutputFormat( Method.XML, null, false ) ); _format.setMethod( Method.XML ); setOutputCharStream( writer ); } /** * Constructs a new serializer that writes to the specified output * stream using the specified output format. If <tt>format</tt> * is null, will use a default output format. * * @param output The output stream to use * @param format The output format to use, null for the default */ public XMLSerializer( OutputStream output, OutputFormat format ) { super( format != null ? format : new OutputFormat( Method.XML, null, false ) ); _format.setMethod( Method.XML ); setOutputByteStream( output ); } public void setOutputFormat( OutputFormat format ) { super.setOutputFormat( format != null ? format : new OutputFormat( Method.XML, null, false ) ); } /** * This methods turns on namespace fixup algorithm during * DOM serialization. * @see org.w3c.dom.ls.DOMSerializer * * @param namespaces */ public void setNamespaces (boolean namespaces){ fNamespaces = namespaces; if (fNSBinder == null) { fNSBinder = new NamespaceSupport(); fLocalNSBinder = new NamespaceSupport(); fSymbolTable = new SymbolTable(); } } //-----------------------------------------// // SAX content handler serializing methods // //-----------------------------------------// public void startElement( String namespaceURI, String localName, String rawName, Attributes attrs ) throws SAXException { int i; boolean preserveSpace; ElementState state; String name; String value; boolean addNSAttr = false; if (DEBUG) { System.out.println("==>startElement("+namespaceURI+","+localName+ ","+rawName+")"); } 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( ( localName == null || localName.length() == 0 ) ? rawName : localName ); } 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 or a comment if (_indenting && ! state.preserveSpace && ( state.empty || state.afterElement || state.afterComment)) _printer.breakLine(); } preserveSpace = state.preserveSpace; //We remove the namespaces from the attributes list so that they will //be in _prefixes attrs = extractNamespaces(attrs); // Do not change the current element state yet. // This only happens in endElement(). if (rawName == null || rawName.length() == 0) { if (localName == null) { String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "NoName", null); throw new SAXException(msg); } if (namespaceURI != null && ! namespaceURI.equals( "" )) { String prefix; prefix = getPrefix( namespaceURI ); if (prefix != null && prefix.length() > 0) rawName = prefix + ":" + localName; else rawName = localName; } else rawName = localName; addNSAttr = true; } _printer.printText( '<' ); _printer.printText( rawName ); _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.getQName( i ); if (name != null && name.length() == 0) { String prefix; String attrURI; name = attrs.getLocalName( i ); attrURI = attrs.getURI( i ); if (( attrURI != null && attrURI.length() != 0 ) && ( namespaceURI == null || namespaceURI.length() == 0 || ! attrURI.equals( namespaceURI ) )) { prefix = getPrefix( attrURI ); if (prefix != null && prefix.length() > 0) name = prefix + ":" + name; } } value = attrs.getValue( i ); if (value == null) value = ""; _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(); } } } if (_prefixes != null) { Enumeration keys; keys = _prefixes.keys(); while (keys.hasMoreElements()) { _printer.printSpace(); value = (String) keys.nextElement(); name = (String) _prefixes.get( value ); if (name.length() == 0) { _printer.printText( "xmlns=\"" ); printEscaped( value ); _printer.printText( '"' ); } else { _printer.printText( "xmlns:" ); _printer.printText( name ); _printer.printText( "=\"" ); printEscaped( value ); _printer.printText( '"' ); } } } // 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( namespaceURI, localName, rawName, preserveSpace ); name = ( localName == null || localName.length() == 0 ) ? rawName : namespaceURI + "^" + localName; state.doCData = _format.isCDataElement( name ); state.unescaped = _format.isNonEscapingElement( name ); } catch (IOException except) { throw new SAXException( except ); } } public void endElement( String namespaceURI, String localName,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?