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

📄 xmlstreamreaderimpl.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* XMLStreamReaderImpl.java --    Copyright (C) 2005  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.stream;import java.io.InputStream;import java.io.IOException;import java.io.Reader;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.Map;import javax.xml.namespace.NamespaceContext;import javax.xml.namespace.QName;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import javax.xml.stream.Location;import javax.xml.stream.XMLResolver;import javax.xml.stream.XMLReporter;import javax.xml.stream.XMLStreamConstants;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;import javax.xml.stream.events.Attribute;import javax.xml.stream.events.Characters;import javax.xml.stream.events.Comment;import javax.xml.stream.events.DTD;import javax.xml.stream.events.EndDocument;import javax.xml.stream.events.EndElement;import javax.xml.stream.events.EndEntity;import javax.xml.stream.events.EntityDeclaration;import javax.xml.stream.events.EntityReference;import javax.xml.stream.events.Namespace;import javax.xml.stream.events.NotationDeclaration;import javax.xml.stream.events.ProcessingInstruction;import javax.xml.stream.events.StartDocument;import javax.xml.stream.events.StartElement;import javax.xml.stream.events.StartEntity;import javax.xml.stream.events.XMLEvent;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.SAXParseException;import org.xml.sax.XMLReader;import org.xml.sax.ext.Attributes2;import org.xml.sax.ext.DeclHandler;import org.xml.sax.ext.LexicalHandler;import org.xml.sax.ext.Locator2;import org.xml.sax.helpers.NamespaceSupport;/** * An XML parser. * * This implementation uses SAX to create a series of events in memory, * and then iterates over this series. This has the advantage of being simple * and unifying the existing XML parsing code. However, it is quite * memory-inefficient and obviously won't cope with streams of arbitrary * length. * * A future task could be to write a real, progressive/incremental * implementation of this class. In that case we should consider making that * the default XML parser implementation and using a SAX wrapper to it to * provide the GNU SAX implementation. * * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> */public class XMLStreamReaderImpl  implements XMLStreamReader, NamespaceContext{  private LinkedList events;  private XMLEvent currentEvent;  private int eventType;  private NamespaceSupport namespaces;  protected String publicId;  protected String systemId;  protected XMLResolver resolver;  protected XMLReporter reporter;  protected boolean validating;  protected boolean namespaceAware;  protected boolean coalescing;  protected boolean replacingEntityReferences;  protected boolean externalEntities;  protected boolean supportDTD;  protected XMLStreamReaderImpl(InputStream in,                                String publicId,                                String systemId,                                XMLResolver resolver,                                XMLReporter reporter,                                boolean validating,                                boolean namespaceAware,                                boolean coalescing,                                boolean replacingEntityReferences,                                boolean externalEntities,                                boolean supportDTD)    throws XMLStreamException  {    //this.in = in;    this.publicId = publicId;    this.systemId = systemId;    this.resolver = resolver;    this.reporter = reporter;    this.validating = validating;    this.namespaceAware = namespaceAware;    this.coalescing = coalescing;    this.replacingEntityReferences = replacingEntityReferences;    this.externalEntities = externalEntities;    this.supportDTD = supportDTD;    namespaces = new NamespaceSupport();    events = new LinkedList();        // Configure the SAX parser and perform the parse    try      {        SAXParserFactory f = SAXParserFactory.newInstance();        f.setNamespaceAware(namespaceAware);        f.setValidating(validating);        SAXParser p = f.newSAXParser();        XMLReader r = p.getXMLReader();        CallbackHandler ch = this.new CallbackHandler(r);        r.setFeature("http://xml.org/sax/features/external-general-entities",                      externalEntities);        r.setFeature("http://xml.org/sax/features/namespaces",                      namespaceAware);        r.setContentHandler(ch);        r.setDTDHandler(ch);        r.setEntityResolver(ch);        r.setErrorHandler(ch);        r.setProperty("http://xml.org/sax/properties/lexical-handler",                      ch);        InputSource source = new InputSource(in);        source.setSystemId(systemId);        r.parse(source);      }    catch (SAXException e)      {        events.add(e);      }    catch (IOException e)      {        events.add(e);      }    catch (ParserConfigurationException e)      {        XMLStreamException e2 = new XMLStreamException(e);        e2.initCause(e);        throw e2;      }  }  protected XMLStreamReaderImpl(Reader reader,                                String publicId,                                String systemId,                                XMLResolver resolver,                                XMLReporter reporter,                                boolean validating,                                boolean namespaceAware,                                boolean coalescing,                                boolean replacingEntityReferences,                                boolean externalEntities,                                boolean supportDTD)    throws XMLStreamException  {    //this.reader = reader;    this.publicId = publicId;    this.systemId = systemId;    this.resolver = resolver;    this.reporter = reporter;    this.validating = validating;    this.namespaceAware = namespaceAware;    this.coalescing = coalescing;    this.replacingEntityReferences = replacingEntityReferences;    this.externalEntities = externalEntities;    this.supportDTD = supportDTD;    namespaces = new NamespaceSupport();    events = new LinkedList();        // Configure the SAX parser and perform the parse    try      {        SAXParserFactory f = SAXParserFactory.newInstance();        f.setNamespaceAware(namespaceAware);        f.setValidating(validating);        SAXParser p = f.newSAXParser();        XMLReader r = p.getXMLReader();        CallbackHandler ch = this.new CallbackHandler(r);        r.setFeature("http://xml.org/sax/features/external-general-entities",                      externalEntities);        r.setFeature("http://xml.org/sax/features/namespaces",                      namespaceAware);        r.setContentHandler(ch);        r.setDTDHandler(ch);        r.setEntityResolver(ch);        r.setErrorHandler(ch);        r.setProperty("http://xml.org/sax/properties/lexical-handler",                      ch);        InputSource source = new InputSource(reader);        source.setSystemId(systemId);        r.parse(source);      }    catch (SAXException e)      {        events.add(e);      }    catch (IOException e)      {        events.add(e);      }    catch (ParserConfigurationException e)      {        XMLStreamException e2 = new XMLStreamException(e);        e2.initCause(e);        throw e2;      }  }  public Object getProperty(String name)    throws IllegalArgumentException  {    throw new IllegalArgumentException(name);  }  public int next()    throws XMLStreamException  {    if (events.isEmpty())      throw new XMLStreamException("EOF");    Object event = events.removeFirst();    if (event instanceof Exception)      {        Exception e = (Exception) event;        XMLStreamException e2 = new XMLStreamException(e);        e2.initCause(e);        throw e2;      }    currentEvent = (XMLEvent) event;    eventType = currentEvent.getEventType();    return eventType;  }  public void require(int type, String namespaceURI, String localName)    throws XMLStreamException  {    // TODO    throw new UnsupportedOperationException();  }  public String getElementText()    throws XMLStreamException  {    // TODO    throw new UnsupportedOperationException();  }  public int nextTag()    throws XMLStreamException  {    int ret;    do      {        ret = next();      }    while (ret != XMLStreamConstants.START_ELEMENT &&           ret != XMLStreamConstants.END_ELEMENT);    return ret;  }  public boolean hasNext()    throws XMLStreamException  {    return !events.isEmpty();  }  public void close()    throws XMLStreamException  {  }  public String getNamespaceURI(String prefix)  {    return namespaces.getURI(prefix);  }  public String getPrefix(String namespaceURI)  {    return namespaces.getPrefix(namespaceURI);  }  public Iterator getPrefixes(String namespaceURI)  {    LinkedList acc = new LinkedList();    for (Enumeration e = namespaces.getPrefixes(namespaceURI);         e.hasMoreElements(); )      acc.add(e.nextElement());    return acc.iterator();  }  public boolean isStartElement()  {    return eventType == START_ELEMENT;  }  public boolean isEndElement()  {    return eventType == END_ELEMENT;  }  public boolean isCharacters()  {    return eventType == CHARACTERS || eventType == CDATA;  }  public boolean isWhiteSpace()  {    return eventType == SPACE;  }  public String getAttributeValue(String namespaceURI, String localName)  {    StartElement se = (StartElement) currentEvent;    for (Iterator i = se.getAttributes(); i.hasNext(); )      {        Attribute attr = (Attribute) i.next();        QName name = attr.getName();        if (namespaceURI != null &&            !namespaceURI.equals(name.getNamespaceURI()))          continue;        if (!localName.equals(name.getLocalPart()))          continue;        return attr.getValue();      }    return null;  }  public int getAttributeCount()  {    StartElement se = (StartElement) currentEvent;    int count = 0;    for (Iterator i = se.getAttributes(); i.hasNext(); )      {        i.next();        count++;      }    return count;  }  public QName getAttributeQName(int index)  {    StartElement se = (StartElement) currentEvent;    int count = 0;    for (Iterator i = se.getAttributes(); i.hasNext(); )      {        Attribute attr = (Attribute) i.next();        if (index == count)          return attr.getName();        count++;      }    return null;  }  public String getAttributeNamespace(int index)  {    QName name = getAttributeQName(index);    return (name == null) ? null : name.getNamespaceURI();  }  public String getAttributeName(int index)  {    QName name = getAttributeQName(index);    return (name == null) ? null : name.getLocalPart();  }  public String getAttributePrefix(int index)  {    QName name = getAttributeQName(index);    return (name == null) ? null : name.getPrefix();  }  public String getAttributeType(int index)  {    StartElement se = (StartElement) currentEvent;    int count = 0;    for (Iterator i = se.getAttributes(); i.hasNext(); )      {        Attribute attr = (Attribute) i.next();        if (index == count)          {            QName type = attr.getDTDType();            return (type == null) ? "CDATA" : type.toString();          }        count++;      }    return null;  }  public String getAttributeValue(int index)  {    StartElement se = (StartElement) currentEvent;    int count = 0;    for (Iterator i = se.getAttributes(); i.hasNext(); )      {        Attribute attr = (Attribute) i.next();        if (index == count)          return attr.getValue();        count++;      }    return null;  }  public boolean isAttributeSpecified(int index)  {    StartElement se = (StartElement) currentEvent;    int count = 0;    for (Iterator i = se.getAttributes(); i.hasNext(); )      {        Attribute attr = (Attribute) i.next();        if (index == count)          return attr.isSpecified();        count++;      }    return false;  }  public int getNamespaceCount()  {    Iterator i = null;    switch (eventType)      {      case XMLStreamConstants.START_ELEMENT:        i = ((StartElement) currentEvent).getNamespaces();        break;      case XMLStreamConstants.END_ELEMENT:        i = ((EndElement) currentEvent).getNamespaces();        break;      default:        throw new IllegalStateException();      }    int count = 0;    while (i.hasNext())      {        i.next();        count++;      }    return count;  }  public String getNamespacePrefix(int index)  {    Iterator i = null;    switch (eventType)      {      case XMLStreamConstants.START_ELEMENT:        i = ((StartElement) currentEvent).getNamespaces();        break;      case XMLStreamConstants.END_ELEMENT:        i = ((EndElement) currentEvent).getNamespaces();        break;      default:        throw new IllegalStateException();      }    int count = 0;    while (i.hasNext())      {        Namespace ns = (Namespace) i.next();        if (index == count)          return ns.getPrefix();        count++;      }    return null;  }  public String getNamespaceURI(int index)  {    Iterator i = null;

⌨️ 快捷键说明

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