xnodeset.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,001 行 · 第 1/2 页
JAVA
1,001 行
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xalan" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, Lotus * Development Corporation., http://www.lotus.com. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.xpath.objects;//import org.w3c.dom.Node;//import org.w3c.dom.Text;//import org.w3c.dom.DocumentFragment;import org.w3c.dom.traversal.NodeIterator;import org.w3c.dom.NodeList;import org.apache.xml.dtm.DTM;import org.apache.xml.dtm.DTMIterator;import org.apache.xml.dtm.DTMManager;import org.apache.xpath.DOMHelper;import org.apache.xpath.XPathContext;import org.apache.xpath.NodeSetDTM;import org.apache.xpath.axes.ContextNodeList;import org.apache.xpath.axes.NodeSequence;import org.apache.xml.utils.StringVector;import org.apache.xml.utils.XMLString;/** * <meta name="usage" content="general"/> * This class represents an XPath nodeset object, and is capable of * converting the nodeset to other types, such as a string. */public class XNodeSet extends NodeSequence{ /** * Default constructor for derived objects. */ protected XNodeSet() { } /** * Construct a XNodeSet object. * * @param val Value of the XNodeSet object */ public XNodeSet(DTMIterator val) { super(); if(val instanceof XNodeSet) { setIter(((XNodeSet)val).m_iter); m_dtmMgr = ((XNodeSet)val).m_dtmMgr; m_last = ((XNodeSet)val).m_last; if(!((XNodeSet)val).hasCache()) ((XNodeSet)val).setShouldCacheNodes(true); m_obj = ((XNodeSet)val).m_obj; } else setIter(val); } /** * Construct a XNodeSet object. * * @param val Value of the XNodeSet object */ public XNodeSet(XNodeSet val) { super(); setIter(val.m_iter); m_dtmMgr = val.m_dtmMgr; m_last = val.m_last; if(!val.hasCache()) val.setShouldCacheNodes(true); m_obj = val.m_obj; } /** * Construct an empty XNodeSet object. This is used to create a mutable * nodeset to which random nodes may be added. */ public XNodeSet(DTMManager dtmMgr) { super(dtmMgr); } /** * Construct a XNodeSet object for one node. * * @param n Node to add to the new XNodeSet object */ public XNodeSet(int n, DTMManager dtmMgr) { super(new NodeSetDTM(dtmMgr)); m_dtmMgr = dtmMgr; if (DTM.NULL != n) { ((NodeSetDTM) m_obj).addNode(n); m_last = 1; } else m_last = 0; } /** * Tell that this is a CLASS_NODESET. * * @return type CLASS_NODESET */ public int getType() { return CLASS_NODESET; } /** * Given a request type, return the equivalent string. * For diagnostic purposes. * * @return type string "#NODESET" */ public String getTypeString() { return "#NODESET"; } /** * Get numeric value of the string conversion from a single node. * * @param n Node to convert * * @return numeric value of the string conversion from a single node. */ public double getNumberFromNode(int n) { XMLString xstr = m_dtmMgr.getDTM(n).getStringValue(n); return xstr.toDouble(); } /** * Cast result object to a number. * * @return numeric value of the string conversion from the * next node in the NodeSetDTM, or NAN if no node was found */ public double num() { int node = item(0); return (node != DTM.NULL) ? getNumberFromNode(node) : Double.NaN; } /** * Cast result object to a number, but allow side effects, such as the * incrementing of an iterator. * * @return numeric value of the string conversion from the * next node in the NodeSetDTM, or NAN if no node was found */ public double numWithSideEffects() { int node = nextNode(); return (node != DTM.NULL) ? getNumberFromNode(node) : Double.NaN; } /** * Cast result object to a boolean. * * @return True if there is a next node in the nodeset */ public boolean bool() { return (item(0) != DTM.NULL); } /** * Cast result object to a boolean, but allow side effects, such as the * incrementing of an iterator. * * @return True if there is a next node in the nodeset */ public boolean boolWithSideEffects() { return (nextNode() != DTM.NULL); } /** * Get the string conversion from a single node. * * @param n Node to convert * * @return the string conversion from a single node. */ public XMLString getStringFromNode(int n) { // %OPT% // I guess we'll have to get a static instance of the DTM manager... if(DTM.NULL != n) { return m_dtmMgr.getDTM(n).getStringValue(n); } else { return org.apache.xpath.objects.XString.EMPTYSTRING; } } /** * Directly call the * characters method on the passed ContentHandler for the * string-value. Multiple calls to the * ContentHandler's characters methods may well occur for a single call to * this method. * * @param ch A non-null reference to a ContentHandler. * * @throws org.xml.sax.SAXException */ public void dispatchCharactersEvents(org.xml.sax.ContentHandler ch) throws org.xml.sax.SAXException { int node = item(0); if(node != DTM.NULL) { m_dtmMgr.getDTM(node).dispatchCharactersEvents(node, ch, false); } } /** * Cast result object to an XMLString. * * @return The document fragment node data or the empty string. */ public XMLString xstr() { int node = item(0); return (node != DTM.NULL) ? getStringFromNode(node) : XString.EMPTYSTRING; } /** * Cast result object to a string. * * @return The string this wraps or the empty string if null */ public void appendToFsb(org.apache.xml.utils.FastStringBuffer fsb) { XString xstring = (XString)xstr(); xstring.appendToFsb(fsb); } /** * Cast result object to a string. * * @return the string conversion from the next node in the nodeset * or "" if there is no next node */ public String str() { int node = item(0); return (node != DTM.NULL) ? getStringFromNode(node).toString() : ""; } /** * Return a java object that's closest to the representation * that should be handed to an extension. * * @return The object that this class wraps */ public Object object() { if(null == m_obj) return this; else return m_obj; } // %REVIEW% // hmmm...// /**// * Cast result object to a result tree fragment.// *// * @param support The XPath context to use for the conversion // *// * @return the nodeset as a result tree fragment.// */// public DocumentFragment rtree(XPathContext support)// {// DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();// DocumentBuilder db = dbf.newDocumentBuilder();// Document myDoc = db.newDocument();// // DocumentFragment docFrag = myDoc.createDocumentFragment();//// DTMIterator nl = iter();// int node;//// while (DTM.NULL != (node = nl.nextNode()))// {// frag.appendChild(node, true, true);// }//// return frag.getDocument();// } /** * Cast result object to a nodelist. * * @return a NodeIterator. * * @throws javax.xml.transform.TransformerException */ public NodeIterator nodeset() throws javax.xml.transform.TransformerException { return new org.apache.xml.dtm.ref.DTMNodeIterator(iter()); } /** * Cast result object to a nodelist. * * @return a NodeList. * * @throws javax.xml.transform.TransformerException */ public NodeList nodelist() throws javax.xml.transform.TransformerException { return new org.apache.xml.dtm.ref.DTMNodeList(iter()); } // /**// * Return a java object that's closest to the representation// * that should be handed to an extension.// *// * @return The object that this class wraps// */// public Object object()// {// return new org.apache.xml.dtm.ref.DTMNodeList(iter());// } /** * Return the iterator without cloning, etc. */ public DTMIterator iterRaw() { return this; } public void release(DTMIterator iter) { } /** * Cast result object to a nodelist. * * @return The nodeset as a nodelist */ public DTMIterator iter() { try { if(hasCache()) return cloneWithReset(); else return this; // don't bother to clone... won't do any good! } catch (CloneNotSupportedException cnse) { throw new RuntimeException(cnse.getMessage()); } } /** * Get a fresh copy of the object. For use with variables. * * @return A fresh nodelist. */ public XObject getFresh() { try { if(hasCache()) return (XObject)cloneWithReset(); else return this; // don't bother to clone... won't do any good! } catch (CloneNotSupportedException cnse) { throw new RuntimeException(cnse.getMessage()); } } /** * Cast result object to a mutableNodeset. * * @return The nodeset as a mutableNodeset */ public NodeSetDTM mutableNodeset() { NodeSetDTM mnl; if(m_obj instanceof NodeSetDTM) { mnl = (NodeSetDTM) m_obj; } else { mnl = new NodeSetDTM(iter()); m_obj = mnl; setCurrentPos(0); } return mnl; } /** Less than comparator */ static LessThanComparator S_LT = new LessThanComparator(); /** Less than or equal comparator */ static LessThanOrEqualComparator S_LTE = new LessThanOrEqualComparator(); /** Greater than comparator */ static GreaterThanComparator S_GT = new GreaterThanComparator(); /** Greater than or equal comparator */ static GreaterThanOrEqualComparator S_GTE = new GreaterThanOrEqualComparator(); /** Equal comparator */ static EqualComparator S_EQ = new EqualComparator(); /** Not equal comparator */ static NotEqualComparator S_NEQ = new NotEqualComparator(); /** * Tell if one object is less than the other. * * @param obj2 Object to compare this nodeset to * @param comparator Comparator to use * * @return See the comments below for each object type comparison * * @throws javax.xml.transform.TransformerException
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?