📄 xincludefilter.java
字号:
/* XIncludeFilter.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.InputStreamReader;import java.io.IOException;import java.io.Reader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.HashSet;import java.util.NoSuchElementException;import java.util.StringTokenizer;import javax.xml.namespace.QName;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLStreamConstants;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;import javax.xml.stream.util.ReaderDelegate;import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.DOMImplementation;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.ProcessingInstruction;import org.w3c.dom.TypeInfo;import org.w3c.dom.traversal.DocumentTraversal;import org.w3c.dom.traversal.NodeFilter;import org.w3c.dom.traversal.TreeWalker;import org.w3c.dom.xpath.XPathEvaluator;import org.w3c.dom.xpath.XPathNSResolver;import org.w3c.dom.xpath.XPathResult;import org.xml.sax.SAXException;/** * StAX filter for performing XInclude processing. * * @see http://www.w3.org/TR/xinclude/ * @see http://www.w3.org/TR/xptr-framework/ * @see http://www.w3.org/TR/xptr-element/ * * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> */class XIncludeFilter extends ReaderDelegate{ static final String XINCLUDE_NS_URI = "http://www.w3.org/2001/XInclude"; static final int SHOW_FLAGS = NodeFilter.SHOW_CDATA_SECTION | NodeFilter.SHOW_COMMENT | NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_ENTITY_REFERENCE | NodeFilter.SHOW_PROCESSING_INSTRUCTION | NodeFilter.SHOW_TEXT; final String systemId; final boolean namespaceAware; final boolean validating; final boolean expandERefs; String href; int event; boolean included; XPathResult result; int snapshotIndex; Node current; TreeWalker walker; HashSet seen = new HashSet(); boolean backtracking; boolean lookahead; Reader includedText; char[] buf; int len = -1; boolean inInclude, inFallback, seenFallback; DocumentBuilder builder; XIncludeFilter(XMLStreamReader reader, String systemId, boolean namespaceAware, boolean validating, boolean expandERefs) { super(reader); try { this.systemId = XMLParser.absolutize(null, systemId); } catch (MalformedURLException e) { RuntimeException e2 = new RuntimeException("unsupported URL: " + systemId); e2.initCause(e); throw e2; } this.namespaceAware = namespaceAware; this.validating = validating; this.expandERefs = expandERefs; } public int getAttributeCount() { if (current != null) { NamedNodeMap attrs = current.getAttributes(); return (attrs == null) ? 0 : attrs.getLength(); } return super.getAttributeCount(); } public String getAttributeLocalName(int index) { if (current != null) { NamedNodeMap attrs = current.getAttributes(); if (attrs == null) return null; Node attr = attrs.item(index); return attr.getLocalName(); } return super.getAttributeLocalName(index); } public String getAttributeNamespace(int index) { if (current != null) { NamedNodeMap attrs = current.getAttributes(); if (attrs == null) return null; Node attr = attrs.item(index); return attr.getNamespaceURI(); } return super.getAttributeNamespace(index); } public String getAttributePrefix(int index) { if (current != null) { NamedNodeMap attrs = current.getAttributes(); if (attrs == null) return null; Node attr = attrs.item(index); return attr.getPrefix(); } return super.getAttributePrefix(index); } public QName getAttributeName(int index) { if (current != null) { NamedNodeMap attrs = current.getAttributes(); if (attrs == null) return null; Node attr = attrs.item(index); String localName = attr.getLocalName(); String uri = attr.getNamespaceURI(); String prefix = attr.getPrefix(); return new QName(uri, localName, prefix); } return super.getAttributeName(index); } public String getAttributeType(int index) { if (current != null) { NamedNodeMap attrs = current.getAttributes(); if (attrs == null) return null; Attr attr = (Attr) attrs.item(index); TypeInfo ti = attr.getSchemaTypeInfo(); return (ti == null) ? "CDATA" : ti.getTypeName(); } return super.getAttributeType(index); } public boolean isAttributeSpecified(int index) { if (current != null) { NamedNodeMap attrs = current.getAttributes(); if (attrs == null) return false; Attr attr = (Attr) attrs.item(index); return attr.getSpecified(); } return super.isAttributeSpecified(index); } public String getAttributeValue(int index) { if (current != null) { NamedNodeMap attrs = current.getAttributes(); if (attrs == null) return null; Node attr = attrs.item(index); return attr.getNodeValue(); } return super.getAttributeValue(index); } public String getAttributeValue(String uri, String localName) { if (current != null) { NamedNodeMap attrs = current.getAttributes(); if (attrs == null) return null; Node attr = attrs.getNamedItemNS(uri, localName); return (attr == null) ? null : attr.getNodeValue(); } return super.getAttributeValue(uri, localName); } public String getElementText() throws XMLStreamException { if (current != null) return current.getTextContent(); return super.getElementText(); } public int getEventType() { return event; } public String getLocalName() { if (current != null) return current.getLocalName(); return super.getLocalName(); } public QName getName() { if (current != null) { String localName = current.getLocalName(); String uri = current.getNamespaceURI(); String prefix = current.getPrefix(); return new QName(uri, localName, prefix); } return super.getName(); } public String getNamespaceURI() { if (current != null) return current.getNamespaceURI(); return super.getNamespaceURI(); } // TODO namespaces public String getPIData() { if (current != null) return ((ProcessingInstruction) current).getData(); return super.getPIData(); } public String getPITarget() { if (current != null) return ((ProcessingInstruction) current).getTarget(); return super.getPITarget(); } public String getPrefix() { if (current != null) return current.getPrefix(); return super.getPrefix(); } public String getText() { if (current != null) return current.getNodeValue(); if (walker != null) { Node n = walker.getCurrentNode(); if (n != null) return n.getTextContent(); } if (buf != null) return new String(buf, 0, len); return super.getText(); } public char[] getTextCharacters() { if (current != null) { buf = current.getNodeValue().toCharArray(); len = buf.length; } if (buf != null) return buf; return super.getTextCharacters(); } public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) throws XMLStreamException { if (current != null) { buf = current.getNodeValue().toCharArray(); len = buf.length; } if (buf != null) { int max = Math.min(len - sourceStart, length); if (max > 0) System.arraycopy(buf, sourceStart, target, targetStart, max); return max; } return super.getTextCharacters(sourceStart, target, targetStart, length); } public int getTextLength() { if (current != null) { buf = current.getNodeValue().toCharArray(); len = buf.length; } if (buf != null) return len; return super.getTextLength(); } public int getTextStart() { if (current != null) { buf = current.getNodeValue().toCharArray(); len = buf.length; } if (buf != null) return 0; return super.getTextStart(); } public boolean hasNext() throws XMLStreamException { if (!lookahead) { try { next(); } catch (NoSuchElementException e) { event = -1; } lookahead = true; } return (event != -1); } public int next() throws XMLStreamException { if (lookahead) { lookahead = false; return event; } buf = null; len = 0; if (walker != null) { Node c = walker.getCurrentNode(); Node n = null; if (c.getNodeType() == Node.ELEMENT_NODE) { boolean isStartElement = !seen.contains(c); if (isStartElement) { seen.add(c); current = c; event = XMLStreamConstants.START_ELEMENT; return event; } else if (backtracking) { n = walker.nextSibling(); if (n != null) backtracking = false; } else { n = walker.firstChild(); if (n == null) n = walker.nextSibling(); } } else { n = walker.firstChild(); if (n == null) n = walker.nextSibling(); } if (n == null) { current = walker.parentNode(); if (current != null && current.getNodeType() == Node.ELEMENT_NODE) { // end-element backtracking = true; event = XMLStreamConstants.END_ELEMENT; return event; } else { walker = null; current = null; } } else { current = n; switch (n.getNodeType()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -