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

📄 gnomexmlreader.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* GnomeXMLReader.java -    Copyright (C) 2004 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. */package gnu.xml.libxmlj.sax;import java.io.File;import java.io.FileNotFoundException;import java.io.InputStream;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import java.util.Arrays;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.DTDHandler;import org.xml.sax.EntityResolver;import org.xml.sax.ErrorHandler;import org.xml.sax.InputSource;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.SAXNotSupportedException;import org.xml.sax.SAXParseException;import org.xml.sax.XMLReader;import org.xml.sax.ext.DeclHandler;import org.xml.sax.ext.LexicalHandler;import gnu.xml.libxmlj.util.NamedInputStream;import gnu.xml.libxmlj.util.StandaloneLocator;import gnu.xml.libxmlj.util.XMLJ;/** * A SAX2 parser that uses libxml2. * * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> */public class GnomeXMLReaderimplements XMLReader{  static  {    XMLJ.init ();  }  private static final String FEATURES_PREFIX =    "http://xml.org/sax/features/";    private static final List RECOGNIZED_FEATURES =    Arrays.asList (new String[]                   {                   "external-general-entities",                   "external-parameter-entities",                   "is-standalone",                   "lexical-handler/parameter-entities",                   "namespaces",                   "namespace-prefixes",                   "resolve-dtd-uris",                   "string-interning",                   "use-attributes2",                   "use-locator2",                   "use-entity-resolver2",                   "validation"                   });    private static final String PROPERTIES_PREFIX =    "http://xml.org/sax/properties/";    private static final List RECOGNIZED_PROPERTIES =    Arrays.asList (new String[]                   {                   "declaration-handler",                   "dom-node",                   "lexical-handler",                   "xml-string"                   });  // Features  private transient boolean standalone;  private boolean namespaces;  private boolean namespacePrefixes;  private boolean validation;  // Callback handlers  private ContentHandler contentHandler;  private DTDHandler dtdHandler;  private EntityResolver entityResolver;  private ErrorHandler errorHandler;  private DeclHandler declarationHandler;  private LexicalHandler lexicalHandler;  private GnomeLocator locator;  // Namespace helper for handling callbacks  private transient Namespaces ns;  // If true, do not invoke callback methods except endDocument  private transient boolean seenFatalError;  private transient boolean seenStartDocument;  private transient String base;  public GnomeXMLReader ()  {    this (true, true);  }  public GnomeXMLReader (boolean namespaces, boolean validation)  {    this.namespaces = namespaces;    this.validation = validation;    ns = new Namespaces ();  }  public ContentHandler getContentHandler ()  {    return contentHandler;  }  public void setContentHandler (ContentHandler handler)  {    contentHandler = handler;  }  public DTDHandler getDTDHandler ()  {    return dtdHandler;  }  public void setDTDHandler (DTDHandler handler)  {    dtdHandler = handler;  }  public EntityResolver getEntityResolver ()  {    return entityResolver;  }  public void setEntityResolver (EntityResolver resolver)  {    entityResolver = resolver;  }  public ErrorHandler getErrorHandler ()  {    return errorHandler;  }  public void setErrorHandler (ErrorHandler handler)  {    errorHandler = handler;  }  // Features  public boolean getFeature (String name)    throws SAXNotRecognizedException, SAXNotSupportedException  {    checkFeatureName (name);    String key = name.substring (FEATURES_PREFIX.length ());    if ("external-general-entities".equals (key))      {        return validation; // TODO check this      }    else if ("external-parameter-entities".equals (key))      {        return validation; // TODO check this      }    else if ("is-standalone".equals (key))      {        return standalone;      }    else if ("namespaces".equals (key))      {        return namespaces;      }    else if ("namespace-prefixes".equals (key))      {        return namespacePrefixes;      }    else if ("resolve-dtd-uris".equals (key))      {        return true;      }    else if ("validation".equals (key))      {        return validation;      }    else      {        return false;      }  }  public void setFeature (String name, boolean value)    throws SAXNotRecognizedException, SAXNotSupportedException  {    checkFeatureName (name);    String key = name.substring (FEATURES_PREFIX.length ());    if ("namespaces".equals (key))      {        namespaces = value;      }    else if ("namespace-prefixes".equals (key))      {        namespacePrefixes = value;      }    else if ("validation".equals (key))      {        validation = value;      }  }  /**   * Check that the specified feature name is recognized.   */  static void checkFeatureName (String name)    throws SAXNotRecognizedException  {    if (name == null || !name.startsWith (FEATURES_PREFIX))      {        throw new SAXNotRecognizedException (name);      }    String key = name.substring (FEATURES_PREFIX.length ());    if (!RECOGNIZED_FEATURES.contains (key))      {        throw new SAXNotRecognizedException (name);      }  }  // Properties  public Object getProperty (String name)    throws SAXNotRecognizedException, SAXNotSupportedException  {    checkPropertyName (name);    String key = name.substring (PROPERTIES_PREFIX.length ());    if ("declaration-handler".equals (key))      {        return getDeclarationHandler ();      }    else if ("lexical-handler".equals (key))      {        return getLexicalHandler ();      }    else      {        throw new SAXNotSupportedException (name);      }  }  public void setProperty (String name, Object value)    throws SAXNotRecognizedException, SAXNotSupportedException  {    checkPropertyName (name);    String key = name.substring (PROPERTIES_PREFIX.length ());    if ("declaration-handler".equals (key))      {        setDeclarationHandler ((DeclHandler) value);      }    else if ("lexical-handler".equals (key))      {        setLexicalHandler ((LexicalHandler) value);      }  }  public DeclHandler getDeclarationHandler ()  {    return declarationHandler;  }  public void setDeclarationHandler (DeclHandler declarationHandler)  {    this.declarationHandler = declarationHandler;  }  public LexicalHandler getLexicalHandler ()  {    return lexicalHandler;  }  public void setLexicalHandler (LexicalHandler lexicalHandler)  {    this.lexicalHandler = lexicalHandler;  }  /**   * Check that the specified property name is recognized.   */  static void checkPropertyName (String name)    throws SAXNotRecognizedException  {    if (!name.startsWith (PROPERTIES_PREFIX))      {        throw new SAXNotRecognizedException (name);      }    String key = name.substring (PROPERTIES_PREFIX.length ());    if (!RECOGNIZED_PROPERTIES.contains (key))      {        throw new SAXNotRecognizedException (name);      }  }  // Parse  public void parse (String systemId)    throws IOException, SAXException  {    URL url = null;    try      {        url = new URL (systemId);      }    catch (MalformedURLException e)      {        File file = new File(systemId);        if (!file.exists ())          {            throw new FileNotFoundException (systemId);          }        String path  = file.getAbsolutePath();        if (File.separatorChar != '/')          {            path = path.replace (File.separatorChar, '/');          }        if (!path.startsWith ("/"))          {            path = "/" + path;          }        if (!path.endsWith ("/") && file.isDirectory ())          {            path = path + "/";          }        url = new URL ("file:" + path);      }    InputSource source = new InputSource(url.toString ());    source.setByteStream (url.openStream ());    parse (source);  }  public synchronized void parse (InputSource input)    throws IOException, SAXException  {    NamedInputStream in = XMLJ.getInputStream (input);    byte[] detectBuffer = in.getDetectBuffer ();    String publicId = input.getPublicId ();    String systemId = input.getSystemId ();    base = XMLJ.getBaseURI (systemId);    // Reset state    standalone = false;    seenFatalError = false;    seenStartDocument = false;    if (systemId != null)      {        int lsi = systemId.lastIndexOf ('/');        if (lsi != -1)          {            base = systemId.substring (0, lsi + 1);          }      }    // Handle zero-length document    if (detectBuffer == null)      {        startDocument (true);        fatalError ("No document element", 0, 0, publicId, systemId);        endDocument ();        return;      }    // Parse    parseStream(in,                detectBuffer,                publicId,                systemId,                base,                validation,                contentHandler != null,                dtdHandler != null,                entityResolver != null,                errorHandler != null,                declarationHandler != null,                lexicalHandler != null);    in.close ();  }  native void parseStream (InputStream in,                           byte[] detectBuffer,                           String publicId,                           String systemId,                           String base,                           boolean validate,                           boolean contentHandler,                           boolean dtdHandler,                           boolean entityResolver,                           boolean errorHandler,                           boolean declarationHandler,                           boolean lexicalHandler)    throws IOException, SAXException;  String getURI (String prefix)  {    if (!namespaces)      {        return null;      }    return ns.getURI (prefix);  }  // Callbacks from libxmlj  private void startDTD (String name, String publicId, String systemId)    throws SAXException  {    if (seenFatalError || lexicalHandler == null)      {        return;      }    try      {        systemId = XMLJ.getAbsoluteURI (base, systemId);        lexicalHandler.startDTD (name, publicId, systemId);      }    catch (Exception e)      {        if (e instanceof SAXException)          {            throw (SAXException) e;          }        else          {            throw new SAXException (e);          }      }  }  private void externalEntityDecl (String name, String publicId,                                   String systemId)    throws SAXException  {    if (seenFatalError || declarationHandler == null)      {        return;      }    try      {        systemId = XMLJ.getAbsoluteURI (base, systemId);        declarationHandler.externalEntityDecl (name, publicId, systemId);      }    catch (Exception e)      {        if (e instanceof SAXException)          {            throw (SAXException) e;          }        else          {            throw new SAXException (e);          }      }  }  private void internalEntityDecl (String name, String value)    throws SAXException  {    if (seenFatalError || declarationHandler == null)      {        return;      }    try      {        declarationHandler.internalEntityDecl (name, value);      }    catch (Exception e)      {        if (e instanceof SAXException)          {            throw (SAXException) e;          }        else          {            throw new SAXException (e);          }      }  }  private InputStream resolveEntity (String publicId, String systemId)    throws SAXException, IOException

⌨️ 快捷键说明

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