⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlparser.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* * Copyright (c) 1999-2000 by David Brownell.  All Rights Reserved. * * This program is open source software; you may use, copy, modify, and * redistribute it under the terms of the LICENSE with which it was * originally distributed. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * LICENSE for more details. *///// Copyright (c) 1997, 1998 by Microstar Software Ltd.// From Microstar's README (the entire original license)://// AElfred is free for both commercial and non-commercial use and// redistribution, provided that Microstar's copyright and disclaimer are// retained intact.  You are free to modify AElfred for your own use and// to redistribute AElfred with your modifications, provided that the// modifications are clearly documented.//// This program is distributed in the hope that it will be useful, but// WITHOUT ANY WARRANTY; without even the implied warranty of// merchantability or fitness for a particular purpose.  Please use it AT// YOUR OWN RISK.//package org.dom4j.io.aelfred;import org.xml.sax.SAXException;import java.io.*;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;// $Id: XmlParser.java,v 1.2 2003/06/10 16:18:34 per_nyfelt Exp $/** * Parse XML documents and return parse events through call-backs. * Use the <code>SAXDriver</code> class as your entry point, as the * internal parser interfaces are subject to change. * * @author Written by David Megginson &lt;dmeggins@microstar.com&gt; *  (version 1.2a with bugfixes) * @author Updated by David Brownell &lt;david-b@pacbell.net&gt; * @version $Date: 2003/06/10 16:18:34 $ * @see SAXDriver */final class XmlParser{    //    // Use special cheats that speed up the code by    // avoiding per-character readCh () method calls.    //    private final static boolean USE_CHEATS = true;    //////////////////////////////////////////////////////////////////////    // Constructors.    ////////////////////////////////////////////////////////////////////////    /**     * Construct a new parser with no associated handler.     * @see #setHandler     * @see #parse     */    // package private    XmlParser ()    {        cleanupVariables ();    }    /**     * Set the handler that will receive parsing events.     * @param handler The handler to receive callback events.     * @see #parse     */    // package private    void setHandler (SAXDriver handler)    {        this.handler = handler;    }    /**     * Parse an XML document from the character stream, byte stream, or URI     * that you provide (in that order of preference).  Any URI that you     * supply will become the base URI for resolving relative URI, and may     * be used to acquire a reader or byte stream.     *     * <p>You may parse more than one document, but that must be done     * sequentially.  Only one thread at a time may use this parser.     *     * @param systemId The URI of the document; should never be null,     *  but may be so iff a reader <em>or</em> a stream is provided.     * @param publicId The public identifier of the document, or null.     * @param reader A character stream; must be null if stream isn't.     * @param stream A byte input stream; must be null if reader isn't.     * @param encoding The suggested encoding, or null if unknown.     * @exception java.lang.Exception Basically SAXException or IOException     */    // package private     void doParse (    String      systemId,    String      publicId,    Reader      reader,    InputStream stream,    String      encoding    ) throws Exception    {    if (handler == null)        throw new IllegalStateException ("no callback handler");    basePublicId = publicId;    baseURI = systemId;    baseReader = reader;    baseInputStream = stream;    initializeVariables ();    // predeclare the built-in entities here (replacement texts)    // we don't need to intern(), since we're guaranteed literals    // are always (globally) interned.    setInternalEntity ("amp", "&#38;");    setInternalEntity ("lt", "&#60;");    setInternalEntity ("gt", "&#62;");    setInternalEntity ("apos", "&#39;");    setInternalEntity ("quot", "&#34;");    handler.startDocument ();    pushURL ("[document]", basePublicId, baseURI,        baseReader, baseInputStream, encoding);    try {        parseDocument ();        handler.endDocument ();    } finally {        if (baseReader != null)        try { baseReader.close ();        } catch (IOException e) { /* ignore */ }        if (baseInputStream != null)        try { baseInputStream.close ();        } catch (IOException e) { /* ignore */ }        if (is != null)        try { is.close ();        } catch (IOException e) { /* ignore */ }        if (reader != null)        try {            reader.close ();        } catch (IOException e) { /* ignore */        }        cleanupVariables ();    }    }    ////////////////////////////////////////////////////////////////////////    // Constants.    ////////////////////////////////////////////////////////////////////////    //    // Constants for element content type.    //    /**     * Constant: an element has not been declared.     * @see #getElementContentType     */    public final static int CONTENT_UNDECLARED = 0;    /**     * Constant: the element has a content model of ANY.     * @see #getElementContentType     */    public final static int CONTENT_ANY = 1;    /**     * Constant: the element has declared content of EMPTY.     * @see #getElementContentType     */    public final static int CONTENT_EMPTY = 2;    /**     * Constant: the element has mixed content.     * @see #getElementContentType     */    public final static int CONTENT_MIXED = 3;    /**     * Constant: the element has element content.     * @see #getElementContentType     */    public final static int CONTENT_ELEMENTS = 4;    //    // Constants for the entity type.    //    /**     * Constant: the entity has not been declared.     * @see #getEntityType     */    public final static int ENTITY_UNDECLARED = 0;    /**     * Constant: the entity is internal.     * @see #getEntityType     */    public final static int ENTITY_INTERNAL = 1;    /**     * Constant: the entity is external, non-XML data.     * @see #getEntityType     */    public final static int ENTITY_NDATA = 2;    /**     * Constant: the entity is external XML data.     * @see #getEntityType     */    public final static int ENTITY_TEXT = 3;    //    // Constants for attribute type.    //    /**     * Constant: the attribute has not been declared for this element type.     * @see #getAttributeType     */    public final static int ATTRIBUTE_UNDECLARED = 0;    /**     * Constant: the attribute value is a string value.     * @see #getAttributeType     */    public final static int ATTRIBUTE_CDATA = 1;    /**     * Constant: the attribute value is a unique identifier.     * @see #getAttributeType     */    public final static int ATTRIBUTE_ID = 2;    /**     * Constant: the attribute value is a reference to a unique identifier.     * @see #getAttributeType     */    public final static int ATTRIBUTE_IDREF = 3;    /**     * Constant: the attribute value is a list of ID references.     * @see #getAttributeType     */    public final static int ATTRIBUTE_IDREFS = 4;    /**     * Constant: the attribute value is the name of an entity.     * @see #getAttributeType     */    public final static int ATTRIBUTE_ENTITY = 5;    /**     * Constant: the attribute value is a list of entity names.     * @see #getAttributeType     */    public final static int ATTRIBUTE_ENTITIES = 6;    /**     * Constant: the attribute value is a name token.     * @see #getAttributeType     */    public final static int ATTRIBUTE_NMTOKEN = 7;    /**     * Constant: the attribute value is a list of name tokens.     * @see #getAttributeType     */    public final static int ATTRIBUTE_NMTOKENS = 8;    /**     * Constant: the attribute value is a token from an enumeration.     * @see #getAttributeType     */    public final static int ATTRIBUTE_ENUMERATED = 9;    /**     * Constant: the attribute is the name of a notation.     * @see #getAttributeType     */    public final static int ATTRIBUTE_NOTATION = 10;    //    // When the class is loaded, populate the hash table of    // attribute types.    //    /**     * Hash table of attribute types.     */    private static HashMap attributeTypeHash;    static {    attributeTypeHash = new HashMap (13);    attributeTypeHash.put ("CDATA", new Integer (ATTRIBUTE_CDATA));    attributeTypeHash.put ("ID", new Integer (ATTRIBUTE_ID));    attributeTypeHash.put ("IDREF", new Integer (ATTRIBUTE_IDREF));    attributeTypeHash.put ("IDREFS", new Integer (ATTRIBUTE_IDREFS));    attributeTypeHash.put ("ENTITY", new Integer (ATTRIBUTE_ENTITY));    attributeTypeHash.put ("ENTITIES", new Integer (ATTRIBUTE_ENTITIES));    attributeTypeHash.put ("NMTOKEN", new Integer (ATTRIBUTE_NMTOKEN));    attributeTypeHash.put ("NMTOKENS", new Integer (ATTRIBUTE_NMTOKENS));    attributeTypeHash.put ("NOTATION", new Integer (ATTRIBUTE_NOTATION));    }    //    // Constants for supported encodings.  "external" is just a flag.    //    private final static int ENCODING_EXTERNAL = 0;    private final static int ENCODING_UTF_8 = 1;    private final static int ENCODING_ISO_8859_1 = 2;    private final static int ENCODING_UCS_2_12 = 3;    private final static int ENCODING_UCS_2_21 = 4;    private final static int ENCODING_UCS_4_1234 = 5;    private final static int ENCODING_UCS_4_4321 = 6;    private final static int ENCODING_UCS_4_2143 = 7;    private final static int ENCODING_UCS_4_3412 = 8;    private final static int ENCODING_ASCII = 9;    //    // Constants for attribute default value.    //    /**     * Constant: the attribute is not declared.     * @see #getAttributeDefaultValueType     */    public final static int ATTRIBUTE_DEFAULT_UNDECLARED = 30;    /**     * Constant: the attribute has a literal default value specified.     * @see #getAttributeDefaultValueType     * @see #getAttributeDefaultValue     */    public final static int ATTRIBUTE_DEFAULT_SPECIFIED = 31;    /**     * Constant: the attribute was declared #IMPLIED.     * @see #getAttributeDefaultValueType     */    public final static int ATTRIBUTE_DEFAULT_IMPLIED = 32;    /**     * Constant: the attribute was declared #REQUIRED.     * @see #getAttributeDefaultValueType     */    public final static int ATTRIBUTE_DEFAULT_REQUIRED = 33;    /**     * Constant: the attribute was declared #FIXED.     * @see #getAttributeDefaultValueType     * @see #getAttributeDefaultValue     */    public final static int ATTRIBUTE_DEFAULT_FIXED = 34;    //    // Constants for input.    //    private final static int INPUT_NONE = 0;    private final static int INPUT_INTERNAL = 1;    private final static int INPUT_EXTERNAL = 2;    private final static int INPUT_STREAM = 3;    private final static int INPUT_BUFFER = 4;    private final static int INPUT_READER = 5;    //    // Flags for reading literals.    //    // expand general entity refs (attribute values in dtd and content)    private final static int LIT_ENTITY_REF = 2;    // normalize this value (whitespace etc) (attributes, public ids)    private final static int LIT_NORMALIZE = 4;    // literal is an attribute value     private final static int LIT_ATTRIBUTE = 8;    // don't expand parameter entities    private final static int LIT_DISABLE_PE = 16;    // don't expand [or parse] character refs    private final static int LIT_DISABLE_CREF = 32;    // don't parse general entity refs    private final static int LIT_DISABLE_EREF = 64;    // don't expand general entities, but make sure we _could_    private final static int LIT_ENTITY_CHECK = 128;    //    // Flags affecting PE handling in DTDs (if expandPE is true).    // PEs expand with space padding, except inside literals.    //    private final static int CONTEXT_NORMAL = 0;    private final static int CONTEXT_LITERAL = 1;    //////////////////////////////////////////////////////////////////////    // Error reporting.    //////////////////////////////////////////////////////////////////////    /**     * Report an error.     * @param message The error message.     * @param textFound The text that caused the error (or null).     * @see SAXDriver#error     * @see #line     */    private void error (String message, String textFound, String textExpected)    throws SAXException    {    if (textFound != null) {        message = message + " (found \"" + textFound + "\")";    }    if (textExpected != null) {        message = message + " (expected \"" + textExpected + "\")";    }    String uri = null;    if (externalEntity != null) {        uri = externalEntity.getURL ().toString ();    }    handler.error (message, uri, line, column);    // "can't happen"    throw new SAXException (message);    }    /**     * Report a serious error.     * @param message The error message.     * @param textFound The text that caused the error (or null).     */    private void error (String message, char textFound, String textExpected)    throws SAXException    {    error (message, new Character (textFound).toString (), textExpected);    }    /** Report typical case fatal errors. */    private void error (String message)    throws SAXException    {    error (message, null, null);    }    //////////////////////////////////////////////////////////////////////    // Major syntactic productions.

⌨️ 快捷键说明

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