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

📄 xmlparser.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* XmlParser.java --    Copyright (C) 1999,2000,2001, 2006 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version.Partly derived from code which carried the following notice:  Copyright (c) 1997, 1998 by Microstar Software Ltd.  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 gnu.xml.aelfred2;import gnu.java.security.action.GetPropertyAction;import java.io.BufferedInputStream;import java.io.CharConversionException;import java.io.EOFException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.IOException;import java.io.Reader;import java.io.UnsupportedEncodingException;import java.net.URL;import java.net.URLConnection;import java.security.AccessController;import java.util.Iterator;import java.util.HashMap;import java.util.LinkedList;import org.xml.sax.InputSource;import org.xml.sax.SAXException;/** * Parse XML documents and return parse events through call-backs. * Use the <code>SAXDriver</code> class as your entry point, as all * 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;dbrownell@users.sourceforge.net&gt; * @see SAXDriver */final class XmlParser{  // avoid slow per-character readCh()  private final static boolean USE_CHEATS = true;  ////////////////////////////////////////////////////////////////////////  // 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-parsable 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;      //  // Attribute type constants are interned literal strings.  //    //  // 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_STREAM = 3;  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 (space chars) (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;  // literal is a public ID value   private final static int LIT_PUBID = 256;      //  // 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;    // Emit warnings for relative URIs with no base URI.  static boolean uriWarnings;  static  {    String key = "gnu.xml.aelfred2.XmlParser.uriWarnings";    GetPropertyAction a = new GetPropertyAction(key);    uriWarnings = "true".equals(AccessController.doPrivileged(a));        }      //  // The current XML handler interface.  //  private SAXDriver handler;    //  // I/O information.  //  private Reader reader;   // current reader  private InputStream is;     // current input stream  private int line;     // current line number  private int column;   // current column number  private int sourceType;   // type of input source  private LinkedList inputStack;   // stack of input soruces  private URLConnection externalEntity; // current external entity  private int encoding;   // current character encoding  private int currentByteCount; // bytes read from current source  private InputSource scratch;  // temporary    //  // Buffers for decoded but unparsed character input.  //  private char[] readBuffer;  private int readBufferPos;  private int readBufferLength;  private int readBufferOverflow;  // overflow from last data chunk.    //  // Buffer for undecoded raw byte input.  //  private final static int READ_BUFFER_MAX = 16384;  private byte[] rawReadBuffer;      //  // Buffer for attribute values, char refs, DTD stuff.  //  private static int DATA_BUFFER_INITIAL = 4096;  private char[] dataBuffer;  private int dataBufferPos;    //  // Buffer for parsed names.  //  private static int NAME_BUFFER_INITIAL = 1024;  private char[] nameBuffer;  private int nameBufferPos;    //  // Save any standalone flag  //  private boolean docIsStandalone;    //  // Hashtables for DTD information on elements, entities, and notations.  // Populated until we start ignoring decls (because of skipping a PE)  //  private HashMap elementInfo;  private HashMap entityInfo;  private HashMap notationInfo;  private boolean skippedPE;    //  // Element type currently in force.  //  private String currentElement;  private int currentElementContent;    //  // Stack of entity names, to detect recursion.  //  private LinkedList entityStack;    //  // PE expansion is enabled in most chunks of the DTD, not all.  // When it's enabled, literals are treated differently.  //  private boolean inLiteral;  private boolean expandPE;  private boolean peIsError;    //  // can't report entity expansion inside two constructs:  // - attribute expansions (internal entities only)  // - markup declarations (parameter entities only)  //  private boolean doReport;    //  // Symbol table, for caching interned names.  //  // These show up wherever XML names or nmtokens are used:  naming elements,  // attributes, PIs, notations, entities, and enumerated attribute values.  //  // NOTE:  This hashtable doesn't grow.  The default size is intended to be  // rather large for most documents.  Example:  one snapshot of the DocBook  // XML 4.1 DTD used only about 350 such names.  As a rule, only pathological  // documents (ones that don't reuse names) should ever see much collision.  //  // Be sure that SYMBOL_TABLE_LENGTH always stays prime, for best hashing.  // "2039" keeps the hash table size at about two memory pages on typical  // 32 bit hardware.  //  private final static int SYMBOL_TABLE_LENGTH = 2039;    private Object[][] symbolTable;    //  // Hash table of attributes found in current start tag.  //  private String[] tagAttributes;  private int tagAttributePos;    //  // Utility flag: have we noticed a CR while reading the last  // data chunk?  If so, we will have to go back and normalise  // CR or CR/LF line ends.  //  private boolean sawCR;    //  // Utility flag: are we in CDATA?  If so, whitespace isn't ignorable.  //   private boolean inCDATA;    //  // Xml version.  //    private static final int XML_10 = 0;   private static final int XML_11 = 1;   private int xmlVersion = XML_10;  //////////////////////////////////////////////////////////////////////  // Constructors.  ////////////////////////////////////////////////////////////////////////    /**   * Construct a new parser with no associated handler.   * @see #setHandler   * @see #parse   */  // package private  XmlParser()  {  }  /**   * 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> Only one thread at a time may use this parser; since it is   * private to this package, post-parse cleanup is done by the caller,   * which MUST NOT REUSE the parser (just null it).   *   * @param systemId Absolute 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");      }    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;");    try      {        // pushURL first to ensure locator is correct in startDocument        // ... it might report an IO or encoding exception.        handler.startDocument();        pushURL(false, "[document]",                // default baseURI: null                new ExternalIdentifiers(publicId, systemId, null),                reader, stream, encoding, false);                parseDocument();      }    catch (EOFException e)      {        //empty input        error("empty document, with no root element.");      }    finally      {        if (reader != null)          {            try              {                reader.close();              }            catch (IOException e)              {                /* ignore */              }          }        if (stream != null)          {            try              {                stream.close();

⌨️ 快捷键说明

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