xmldocument.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,456 行 · 第 1/4 页

JAVA
1,456
字号
/* * $Id: XmlDocument.java,v 1.10 2001/11/09 08:44:06 edwingo Exp $ * * The Apache Software License, Version 1.1 * * * Copyright (c) 2000 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 "Crimson" 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) 1999, Sun Microsystems, Inc.,  * http://www.sun.com.  For more information on the Apache Software  * Foundation, please see <http://www.apache.org/>. */package org.apache.crimson.tree;import java.io.InputStream;import java.io.OutputStreamWriter;import java.io.OutputStream;import java.io.Writer;import java.io.IOException;import java.util.Dictionary;import java.util.Enumeration;import java.util.Hashtable;import java.util.Locale;import org.w3c.dom.*;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.helpers.XMLReaderFactory;import org.xml.sax.XMLReader;import org.apache.crimson.parser.Parser2;import org.apache.crimson.parser.Resolver;import org.apache.crimson.parser.ValidatingParser;import org.apache.crimson.util.MessageCatalog;import org.apache.crimson.util.XmlNames;/** * This class implements the DOM <em>Document</em> interface, and also * provides static factory methods to create document instances.  Instances * represent the top level of an XML 1.0 document, typically consisting * of processing instructions followed by one tree of XML data.  These * documents may be written out for transfer or storage using a variety * of text encodings. * * <P> The static factory methods do not offer any customization options. * in particular, they do not enforce XML Namespaces when parsing, do not * offer customizable element factories, and discard certain information * which is not intended to be significant to applications.  If your * application requires more sophisticated use of DOM, you may need * to use SAX directly with an <em>XmlDocumentBuilder</em>. * * <P> <b>Note: element factories are deprecated</b> because they are * non-standard.  They are only provided in this version for backwards * compatibility.  Instances are factories for their subsidiary nodes, but * applications may provide their own element factory to bind element tags * to particular DOM implementation classes (which must subclass * ElementNode).  For example, a factory may use a set of classes which * support the HTML DOM methods, or which support methods associated with * XML vocabularies for specialized problem domains as found within * Internet Commerce systems.  For example, an element tag * <code>&lt;PurchaseOrder&gt;</code> could be mapped to a * <code>com.startup.commerce.PurchaseOrder</code> class.  The factory can * also use XML Namespace information, if desired. * * <P> Since DOM requires nodes to be owned exclusively by one document, * they can't be moved from one document to another using DOM APIs.  This * class provides an <em>changeNodeOwner</em> functionality which may be * used to change the document associated with a node, and with any of its * children. * * <P> <em> Only the core DOM model is supported here, not the HTML support. * Such support basically adds a set of convenience element types, and so * can be implemented through element factories and document subclasses.</em> * * @see XmlDocumentBuilder * * @author David Brownell * @author Rajiv Mordani * @version $Revision: 1.10 $ */public class XmlDocument extends ParentNode implements DocumentEx{    // package private (with jdk 1.1 'javac' bug workaround)    static /* final */ String           eol;    static {        String  temp;        try { temp = System.getProperty ("line.separator", "\n"); }        catch (SecurityException e) { temp = "\n"; }        eol = temp;    }    static final MessageCatalog         catalog = new Catalog ();    private Locale              locale = Locale.getDefault ();    private String              systemId;    private ElementFactory      factory;    // package private    int                         mutationCount;    boolean replaceRootElement;    /**     * Constructs an empty document object.     */    public XmlDocument() {        // No-op    }    /**     * Construct an XML document from the data at the specified URI,     * optionally validating.  This uses validating parser if     * validation is requested, otherwise uses non-validating     * parser.  XML Namespace conformance is not tested when parsing.     *     * @param documentURI The URI (normally URL) of the document     * @param doValidate If true, validity errors are treated as fatal     *     * @exception IOException as appropriate     * @exception SAXException as appropriate     * @exception SAXParseException (with line number information)     *  for parsing errors     * @exception IllegalStateException at least when the parser     *  is configured incorrectly     * @deprecated Use JAXP javax.xml.parsers package instead     */    public static XmlDocument createXmlDocument (        String  documentURI,        boolean doValidate    ) throws IOException, SAXException    {        return createXmlDocument (new InputSource (documentURI), doValidate);    }    /**     * Construct an XML document from the data at the specified URI,     * using the nonvalidating parser.  XML Namespace conformance     * is not tested when parsing.     *     * @param documentURI The URI (normally URL) of the document     *     * @exception IOException as appropriate     * @exception SAXException as appropriate     * @exception SAXParseException (with line number information)     *  for parsing errors     * @exception IllegalStateException at least when the parser     *  is configured incorrectly     * @deprecated Use JAXP javax.xml.parsers package instead     */    public static XmlDocument createXmlDocument (String documentURI)    throws IOException, SAXException    {        return createXmlDocument (new InputSource (documentURI), false);    }    /**     * Construct an XML document from input stream, optionally validating.     * This document must not require interpretation of relative URLs,     * since the base URL is not known.  This uses the validating parser     * if validation is requested, otherwise uses the non-validating     * parser.  XML Namespace conformance is not tested when parsing.     *     * @param in Holds xml document     * @param doValidate If true, validity errors are treated as fatal     *     * @exception IOException as appropriate     * @exception SAXException as appropriate     * @exception SAXParseException (with line number information)     *  for parsing errors     * @exception IllegalStateException at least when the parser     *  is configured incorrectly     * @deprecated Use JAXP javax.xml.parsers package instead     */    public static XmlDocument createXmlDocument (        InputStream     in,        boolean         doValidate    ) throws IOException, SAXException    {        return createXmlDocument (new InputSource (in), doValidate);    }    /**     * Construct an XML document from the data in the specified input     * source, optionally validating.  This uses the validating parser     * if validation is requested, otherwise uses the non-validating     * parser.  XML Namespace conformance is not tested when parsing.     *     * @param in The input source of the document     * @param doValidate If true, validity errors are treated as fatal     *     * @exception IOException as appropriate     * @exception SAXException as appropriate     * @exception SAXParseException (with line number information)     *  for parsing errors     * @exception IllegalStateException at least when the parser     *  is configured incorrectly     * @deprecated Use JAXP javax.xml.parsers package instead     */    public static XmlDocument createXmlDocument(InputSource in,                                                boolean doValidate)        throws IOException, SAXException    {        // Create XMLReader allowing user to override using system property        // String defaultReader = "org.apache.xerces.parsers.SAXParser";        String defaultReader = "org.apache.crimson.parser.XMLReaderImpl";        String prop;        try {            prop = System.getProperty("org.xml.sax.driver", defaultReader);        } catch (SecurityException se) {            // This can happen if we are running as an applet            prop = defaultReader;        }        XMLReader xmlReader = XMLReaderFactory.createXMLReader(prop);        //        // Namespace related features needed for XmlDocumentBuilder        //        String namespaces = "http://xml.org/sax/features/namespaces";        xmlReader.setFeature(namespaces, true);        String nsPrefixes = "http://xml.org/sax/features/namespace-prefixes";        xmlReader.setFeature(nsPrefixes, true);        // Create XmlDocumentBuilder instance        XmlDocumentBuilder builder = new XmlDocumentBuilder();        // Use as the ContentHandler        xmlReader.setContentHandler(builder);                  // org.xml.sax.ext.LexicalHandler        String lexHandler = "http://xml.org/sax/properties/lexical-handler";        xmlReader.setProperty(lexHandler, builder);        // org.xml.sax.ext.DeclHandler        String declHandler            = "http://xml.org/sax/properties/declaration-handler";        xmlReader.setProperty(declHandler, builder);        // DTDHandler        xmlReader.setDTDHandler(builder);        // Validation        String validation = "http://xml.org/sax/features/validation";        xmlReader.setFeature(validation, doValidate);        // If validating, use an error handler that throws an exception for        // validation errors.        if (doValidate) {            xmlReader.setErrorHandler(new DefaultHandler() {                public void error(SAXParseException e) throws SAXException {                    throw e;                }            });        }        builder.setDisableNamespaces(true);        // Parse the input        xmlReader.parse(in);        return builder.getDocument();    }    /**     * Returns the locale to be used for diagnostic messages.     */    public Locale       getLocale ()        { return locale; }        /**     * Assigns the locale to be used for diagnostic messages.     * Multi-language applications, such as web servers dealing with     * clients from different locales, need the ability to interact     * with clients in languages other than the server's default.     * When an XmlDocument is created, its locale is the default     * locale for the virtual machine.     *     * @see #chooseLocale     */    public void setLocale (Locale locale)    {        if (locale == null)            locale = Locale.getDefault ();        this.locale = locale;    }    /**     * Chooses a client locale to use for diagnostics, using the first     * language specified in the list that is supported by this DOM     * implementation.  That locale is then automatically assigned using <a     * href="#setLocale(java.util.Locale)">setLocale()</a>.  Such a list     * could be provided by a variety of user preference mechanisms,     * including the HTTP <em>Accept-Language</em> header field.     *     * @see org.apache.crimson.util.MessageCatalog     *     * @param languages Array of language specifiers, ordered with the most     *  preferable one at the front.  For example, "en-ca" then "fr-ca",     *  followed by "zh_CN".  Both RFC 1766 and Java styles are supported.     * @return The chosen locale, or null.     */    public Locale chooseLocale (String languages [])    {        Locale  l = catalog.chooseLocale (languages);        if (l != null)            setLocale (l);        return l;    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?