domserializerimpl.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,175 行 · 第 1/4 页
JAVA
1,175 行
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999-2004 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xerces" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 2002, International * Business Machines, Inc., http://www.apache.org. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package com.sun.org.apache.xml.internal.serialize;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.StringWriter;import java.io.UnsupportedEncodingException;import java.io.Writer;import java.lang.reflect.Method;import java.util.Enumeration;import java.util.Hashtable;import java.net.URI;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection;import java.util.Vector;import com.sun.org.apache.xerces.internal.dom.AttrImpl;import com.sun.org.apache.xerces.internal.dom.AttributeMap;import com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl;import com.sun.org.apache.xerces.internal.dom.DOMErrorImpl;import com.sun.org.apache.xerces.internal.dom.DOMLocatorImpl;import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;import com.sun.org.apache.xerces.internal.dom.DOMNormalizer;import com.sun.org.apache.xerces.internal.dom.DOMStringListImpl;import com.sun.org.apache.xerces.internal.impl.Constants;import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;import com.sun.org.apache.xerces.internal.util.NamespaceSupport;import com.sun.org.apache.xerces.internal.util.SymbolTable;import org.w3c.dom.DOMConfiguration;import org.w3c.dom.DOMError;import org.w3c.dom.DOMErrorHandler;import com.sun.org.apache.xerces.internal.util.XML11Char;import com.sun.org.apache.xerces.internal.util.XMLChar;import org.w3c.dom.Attr;import org.w3c.dom.Comment;import org.w3c.dom.DOMException;import org.w3c.dom.DOMStringList;import org.w3c.dom.Document;import org.w3c.dom.DocumentFragment;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.ProcessingInstruction;import org.w3c.dom.ls.LSException;import org.w3c.dom.ls.LSOutput;import org.w3c.dom.ls.LSSerializer;import org.w3c.dom.ls.LSSerializerFilter;/** * EXPERIMENTAL: Implemenatation of DOM Level 3 org.w3c.ls.LSSerializer by delegating serialization * calls to <CODE>XMLSerializer</CODE>. * LSSerializer provides an API for serializing (writing) a DOM document out in an * XML document. The XML data is written to an output stream. * During serialization of XML data, namespace fixup is done when possible as * defined in DOM Level 3 Core, Appendix B. * * @author Elena Litani, IBM * @author Gopal Sharma, Sun Microsystems * @author Arun Yadav, Sun Microsystems * @version $Id: DOMSerializerImpl.java,v 1.21 2004/04/22 20:39:04 mrglavas Exp $ */public class DOMSerializerImpl implements LSSerializer, DOMConfiguration { // TODO: When DOM Level 3 goes to REC replace method calls using // reflection for: getXmlEncoding, getInputEncoding and getXmlEncoding // with regular static calls on the Document object. // data // serializer private XMLSerializer serializer; // XML 1.1 serializer private XML11Serializer xml11Serializer; //Recognized parameters private DOMStringList fRecognizedParameters; /** REVISIT: Currently we handle 3 different configurations, would be nice just have one configuration * that has different recognized parameters depending if it is used in Core/LS. */ protected short features = 0; protected final static short NAMESPACES = 0x1<<0; protected final static short WELLFORMED = 0x1<<1; protected final static short ENTITIES = 0x1<<2; protected final static short CDATA = 0x1<<3; protected final static short SPLITCDATA = 0x1<<4; protected final static short COMMENTS = 0x1<<5; protected final static short DISCARDDEFAULT = 0x1<<6; protected final static short INFOSET = 0x1<<7; protected final static short XMLDECL = 0x1<<8; protected final static short NSDECL = 0x1<<9; protected final static short DOM_ELEMENT_CONTENT_WHITESPACE = 0x1<<10; // well-formness checking private DOMErrorHandler fErrorHandler = null; private final DOMErrorImpl fError = new DOMErrorImpl(); private final DOMLocatorImpl fLocator = new DOMLocatorImpl(); private static final RuntimeException abort = new RuntimeException(); /** * Constructs a new LSSerializer. * The constructor turns on the namespace support in <code>XMLSerializer</code> and * initializes the following fields: fNSBinder, fLocalNSBinder, fSymbolTable, * fEmptySymbol, fXmlSymbol, fXmlnsSymbol, fNamespaceCounter, fFeatures. */ public DOMSerializerImpl() { // set default features features |= NAMESPACES; features |= ENTITIES; features |= COMMENTS; features |= CDATA; features |= SPLITCDATA; features |= WELLFORMED; features |= NSDECL; features |= DOM_ELEMENT_CONTENT_WHITESPACE; features |= DISCARDDEFAULT; features |= XMLDECL; serializer = new XMLSerializer(); initSerializer(serializer); } // // LSSerializer methods // public DOMConfiguration getDomConfig(){ return this; } /** DOM L3-EXPERIMENTAL: * Setter for boolean and object parameters */ public void setParameter(String name, Object value) throws DOMException { if (value instanceof Boolean) { boolean state = ((Boolean) value).booleanValue(); if (name.equalsIgnoreCase(Constants.DOM_INFOSET)){ if (state){ features &=~ENTITIES; features &=~CDATA; features |=NAMESPACES; features |=WELLFORMED; features |=COMMENTS; } // false does not have any effect } else if (name.equalsIgnoreCase(Constants.DOM_XMLDECL)) { features = (short) (state ? features | XMLDECL : features & ~XMLDECL); } else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)) { features = (short) (state ? features | NAMESPACES : features & ~NAMESPACES); serializer.fNamespaces = state; } else if (name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)) { features = (short) (state ? features | SPLITCDATA : features & ~SPLITCDATA); } else if (name.equalsIgnoreCase(Constants.DOM_DISCARD_DEFAULT_CONTENT)) { features = (short) (state ? features | DISCARDDEFAULT : features & ~DISCARDDEFAULT); } else if (name.equalsIgnoreCase(Constants.DOM_WELLFORMED)) { features = (short) (state ? features | WELLFORMED : features & ~WELLFORMED); } else if (name.equalsIgnoreCase(Constants.DOM_ENTITIES)){ features = (short) (state ? features | ENTITIES : features & ~ENTITIES); } else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)){ features = (short) (state ? features | CDATA : features & ~CDATA); } else if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)){ features = (short) (state ? features | COMMENTS : features & ~COMMENTS); } else if (name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM) || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA) || name.equalsIgnoreCase(Constants.DOM_VALIDATE) || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION) || name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION) || name.equalsIgnoreCase(Constants.DOM_FORMAT_PRETTY_PRINT)){ // true is not supported if (state) { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "FEATURE_NOT_SUPPORTED", new Object[] { name }); throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg); } } else if ( name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS) || name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE) || name.equalsIgnoreCase(Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) { // false is not supported if (!state) { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "FEATURE_NOT_SUPPORTED", new Object[] { name }); throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg); } } else { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "FEATURE_NOT_FOUND", new Object[] { name }); throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg); } } else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) { if (value == null || value instanceof DOMErrorHandler) { fErrorHandler = (DOMErrorHandler)value; } else { // REVISIT: modify error exception to TYPE_MISMATCH
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?