domsourcexmlstreamreaderimpl.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 716 行 · 第 1/2 页
JAVA
716 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source 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, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Emil Ong */package com.caucho.xml.stream;import com.caucho.util.CharBuffer;import com.caucho.util.L10N;import com.caucho.vfs.*;import static javax.xml.XMLConstants.*;import javax.xml.namespace.NamespaceContext;import javax.xml.namespace.QName;import javax.xml.stream.Location;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;import javax.xml.transform.dom.DOMSource;import java.io.IOException;import java.io.InputStream;import java.io.Reader;import java.util.NoSuchElementException;import java.util.logging.Logger;import org.w3c.dom.*;import static org.w3c.dom.Node.*;/** * XML pull-parser interface. */public class DOMSourceXMLStreamReaderImpl implements XMLStreamReader { private static final Logger log = Logger.getLogger(DOMSourceXMLStreamReaderImpl.class.getName()); private static final L10N L = new L10N(DOMSourceXMLStreamReaderImpl.class); private NamespaceReaderContext _namespaceTracker; private Node _current; private boolean _ending = false; private String _systemId; private String _publicId; private String _version; private String _encoding; private boolean _standalone = false; private boolean _first = true; public DOMSourceXMLStreamReaderImpl(DOMSource source) throws XMLStreamException { this(source.getNode()); } public DOMSourceXMLStreamReaderImpl(Node node) throws XMLStreamException { _current = node; if (node.getNodeType() == Node.ELEMENT_NODE) _first = false; init(); } private void init() throws XMLStreamException { _namespaceTracker = new NamespaceReaderContext(); declareNamespaces(); if (_current instanceof Document) { Document document = (Document) _current; _encoding = document.getXmlEncoding(); _version = document.getXmlVersion(); _standalone = document.getXmlStandalone(); } } private void declareNamespaces() { if (isStartElement()) { _namespaceTracker.push(); Element element = (Element) _current; NamedNodeMap map = element.getAttributes(); if (map == null) return; for (int i = 0; i < map.getLength(); i++) { Attr attr = (Attr) map.item(i); if (attr.getName().startsWith(XMLNS_ATTRIBUTE)) { int colon = attr.getName().indexOf(':'); if (colon < 0) _namespaceTracker.declare(DEFAULT_NS_PREFIX, attr.getNodeValue()); else { String prefix = attr.getName().substring(0, colon); _namespaceTracker.declare(prefix, attr.getNodeValue()); } } } } } public int getAttributeCount() { if (isStartElement()) { Element element = (Element) _current; NamedNodeMap map = element.getAttributes(); if (map == null) return 0; int attributeCount = 0; // count the non-namespace prefix attributes for (int i = 0; i < map.getLength(); i++) { Attr attr = (Attr) map.item(i); if (! attr.getName().startsWith(XMLNS_ATTRIBUTE)) attributeCount++; } return attributeCount; } else { throw new IllegalStateException(L.l("Current event not a start or end element")); } } private Attr getAttribute(int index) { if (isStartElement()) { Element element = (Element) _current; NamedNodeMap map = element.getAttributes(); if (map == null) throw new NoSuchElementException(); int attributeCount = 0; for (int i = 0; i < map.getLength(); i++) { Attr attr = (Attr) map.item(i); if (! attr.getName().startsWith(XMLNS_ATTRIBUTE)) { if (attributeCount == index) return attr; attributeCount++; } } throw new NoSuchElementException(); } else { throw new IllegalStateException(L.l("Current event not a start or end element")); } } public String getAttributeLocalName(int index) { Attr attr = getAttribute(index); return attr.getLocalName(); } public QName getAttributeName(int index) { Attr attr = getAttribute(index); int colon = attr.getName().indexOf(':'); if (colon < 0) { if (attr.getNamespaceURI() == null || "".equals(attr.getNamespaceURI())) return new QName(attr.getNamespaceURI(), attr.getLocalName()); else return new QName(attr.getLocalName()); } String prefix = attr.getName().substring(0, colon); return new QName(attr.getNamespaceURI(), attr.getLocalName(), prefix); } public String getAttributeNamespace(int index) { Attr attr = getAttribute(index); return attr.getNamespaceURI(); } public String getAttributePrefix(int index) { Attr attr = getAttribute(index); int colon = attr.getName().indexOf(':'); if (colon < 0) return DEFAULT_NS_PREFIX; String prefix = attr.getName().substring(0, colon); return prefix; } public String getAttributeType(int index) { return "CDATA"; } public String getAttributeValue(int index) { Attr attr = getAttribute(index); return attr.getNodeValue(); } public boolean isAttributeSpecified(int index) { return index < getAttributeCount(); } public String getAttributeValue(String namespaceURI, String localName) { if (isStartElement()) { Element element = (Element) _current; NamedNodeMap map = element.getAttributes(); Attr attr = (Attr) map.getNamedItemNS(namespaceURI, localName); if (attr == null) return null; return attr.getNodeValue(); } else { throw new IllegalStateException(L.l("Current event not a start or end element")); } } public String getCharacterEncodingScheme() { return _encoding; } public String getElementText() throws XMLStreamException { return _current.getTextContent(); } public String getEncoding() { return _encoding; } public int getEventType() { if (_current == null) return END_DOCUMENT; switch (_current.getNodeType()) { case ATTRIBUTE_NODE: return ATTRIBUTE; case CDATA_SECTION_NODE: return CDATA; case COMMENT_NODE: return COMMENT; case DOCUMENT_FRAGMENT_NODE: throw new IllegalStateException(); case DOCUMENT_NODE: return _ending ? END_DOCUMENT : START_DOCUMENT; case DOCUMENT_TYPE_NODE: return DTD; case ELEMENT_NODE: return _ending ? END_ELEMENT : START_ELEMENT; case ENTITY_NODE: return ENTITY_DECLARATION; case ENTITY_REFERENCE_NODE: return ENTITY_REFERENCE; case NOTATION_NODE: return NOTATION_DECLARATION; case PROCESSING_INSTRUCTION_NODE: return PROCESSING_INSTRUCTION; case TEXT_NODE: return CHARACTERS; } return -1; } public Location getLocation() { return new UnknownLocation(); } public String getLocalName() { if (_current instanceof Element) { Element element = (Element) _current; return element.getLocalName(); } else { throw new IllegalStateException(L.l("Current event not a start or end element")); } } public String getNamespaceURI() { if (isStartElement()) { Element element = (Element) _current; return element.getNamespaceURI(); } else { throw new IllegalStateException(L.l("Current event not a start or end element")); } } public QName getName() { if (_current.getNodeType() == ELEMENT_NODE) { Element element = (Element) _current; String name = element.getNodeName(); int colon = name.indexOf(':');
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?