domserializerimpl.java
来自「JAVA 所有包」· Java 代码 · 共 1,212 行 · 第 1/4 页
JAVA
1,212 行
/* * Copyright 1999-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. */package com.sun.org.apache.xml.internal.serialize;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.StringWriter;import java.io.UnsupportedEncodingException;import java.io.Writer;import java.lang.reflect.Method;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection;import java.util.StringTokenizer;import java.util.Vector;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 org.w3c.dom.DOMConfiguration;import org.w3c.dom.DOMError;import org.w3c.dom.DOMErrorHandler;import org.w3c.dom.DOMStringList;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 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.Document;import org.w3c.dom.DocumentFragment;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;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 * @author Sunitha Reddy, Sun Microsystems * @version $Id: DOMSerializerImpl.java,v 1.5 2006/01/23 06:47:25 sunithareddy 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; protected final static short FORMAT_PRETTY_PRINT = 0x1<<11; // 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 |= NSDECL; 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_FORMAT_PRETTY_PRINT)){ features = (short) (state ? features | FORMAT_PRETTY_PRINT : features & ~FORMAT_PRETTY_PRINT); } 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_NORMALIZE_CHARACTERS)) { // 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)) { //namespace-declaration has effect only if namespaces is true features = (short) (state ? features | NSDECL : features & ~NSDECL); serializer.fNamespacePrefixes = state; } else if (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 { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "TYPE_MISMATCH_ERR", new Object[] { name }); throw new DOMException(DOMException.TYPE_MISMATCH_ERR, msg); } } else if ( name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER) || name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION) || name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE) || name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS) && value != null) { 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_FOUND_ERR, msg); } } /** DOM L3-EXPERIMENTAL: * Check if parameter can be set */ public boolean canSetParameter(String name, Object state) { if (state == null) { return true; } if (state instanceof Boolean) { boolean value = ((Boolean) state).booleanValue(); if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES) || name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA) || name.equalsIgnoreCase(Constants.DOM_DISCARD_DEFAULT_CONTENT)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?