FileDocCategorySizeDatePackage
XNodeSet.javaAPI DocJava SE 5 API22946Fri Aug 26 14:56:10 BST 2005com.sun.org.apache.xpath.internal.objects

XNodeSet

public class XNodeSet extends NodeSequence
This class represents an XPath nodeset object, and is capable of converting the nodeset to other types, such as a string.
xsl.usage
general

Fields Summary
static LessThanComparator
S_LT
Less than comparator
static LessThanOrEqualComparator
S_LTE
Less than or equal comparator
static GreaterThanComparator
S_GT
Greater than comparator
static GreaterThanOrEqualComparator
S_GTE
Greater than or equal comparator
static EqualComparator
S_EQ
Equal comparator
static NotEqualComparator
S_NEQ
Not equal comparator
Constructors Summary
protected XNodeSet()
Default constructor for derived objects.

  
public XNodeSet(DTMIterator val)
Construct a XNodeSet object.

param
val Value of the XNodeSet object

  	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);
  
public XNodeSet(XNodeSet val)
Construct a XNodeSet object.

param
val Value of the XNodeSet object

  	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;
  
public XNodeSet(DTMManager dtmMgr)
Construct an empty XNodeSet object. This is used to create a mutable nodeset to which random nodes may be added.

     this(DTM.NULL,dtmMgr);
  
public XNodeSet(int n, DTMManager dtmMgr)
Construct a XNodeSet object for one node.

param
n Node to add to the new XNodeSet object


    super(new NodeSetDTM(dtmMgr));
    m_dtmMgr = dtmMgr;

    if (DTM.NULL != n)
    {
      ((NodeSetDTM) m_obj).addNode(n);
      m_last = 1;
    }
    else
    	m_last = 0;
  
Methods Summary
public voidappendToFsb(com.sun.org.apache.xml.internal.utils.FastStringBuffer fsb)
Cast result object to a string.

return
The string this wraps or the empty string if null

    XString xstring = (XString)xstr();
    xstring.appendToFsb(fsb);
  
public booleanbool()
Cast result object to a boolean.

return
True if there is a next node in the nodeset

    return (item(0) != DTM.NULL);
  
public booleanboolWithSideEffects()
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

    return (nextNode() != DTM.NULL);
  
public booleancompare(com.sun.org.apache.xpath.internal.objects.XObject obj2, com.sun.org.apache.xpath.internal.objects.Comparator comparator)
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


                                        
       
           
  

    boolean result = false;
    int type = obj2.getType();

    if (XObject.CLASS_NODESET == type)
    {
      // %OPT% This should be XMLString based instead of string based...

      // From http://www.w3.org/TR/xpath: 
      // If both objects to be compared are node-sets, then the comparison 
      // will be true if and only if there is a node in the first node-set 
      // and a node in the second node-set such that the result of performing 
      // the comparison on the string-values of the two nodes is true.
      // Note this little gem from the draft:
      // NOTE: If $x is bound to a node-set, then $x="foo" 
      // does not mean the same as not($x!="foo"): the former 
      // is true if and only if some node in $x has the string-value 
      // foo; the latter is true if and only if all nodes in $x have 
      // the string-value foo.
      DTMIterator list1 = iterRaw();
      DTMIterator list2 = ((XNodeSet) obj2).iterRaw();
      int node1;
      java.util.Vector node2Strings = null;

      while (DTM.NULL != (node1 = list1.nextNode()))
      {
        XMLString s1 = getStringFromNode(node1);

        if (null == node2Strings)
        {
          int node2;

          while (DTM.NULL != (node2 = list2.nextNode()))
          {
            XMLString s2 = getStringFromNode(node2);

            if (comparator.compareStrings(s1, s2))
            {
              result = true;

              break;
            }

            if (null == node2Strings)
              node2Strings = new java.util.Vector();

            node2Strings.addElement(s2);
          }
        }
        else
        {
          int n = node2Strings.size();

          for (int i = 0; i < n; i++)
          {
            if (comparator.compareStrings(s1, (XMLString)node2Strings.elementAt(i)))
            {
              result = true;

              break;
            }
          }
        }
      }
      list1.reset();
      list2.reset();
    }
    else if (XObject.CLASS_BOOLEAN == type)
    {

      // From http://www.w3.org/TR/xpath: 
      // If one object to be compared is a node-set and the other is a boolean, 
      // then the comparison will be true if and only if the result of 
      // performing the comparison on the boolean and on the result of 
      // converting the node-set to a boolean using the boolean function 
      // is true.
      double num1 = bool() ? 1.0 : 0.0;
      double num2 = obj2.num();

      result = comparator.compareNumbers(num1, num2);
    }
    else if (XObject.CLASS_NUMBER == type)
    {

      // From http://www.w3.org/TR/xpath: 
      // If one object to be compared is a node-set and the other is a number, 
      // then the comparison will be true if and only if there is a 
      // node in the node-set such that the result of performing the 
      // comparison on the number to be compared and on the result of 
      // converting the string-value of that node to a number using 
      // the number function is true. 
      DTMIterator list1 = iterRaw();
      double num2 = obj2.num();
      int node;

      while (DTM.NULL != (node = list1.nextNode()))
      {
        double num1 = getNumberFromNode(node);

        if (comparator.compareNumbers(num1, num2))
        {
          result = true;

          break;
        }
      }
      list1.reset();
    }
    else if (XObject.CLASS_RTREEFRAG == type)
    {
      XMLString s2 = obj2.xstr();
      DTMIterator list1 = iterRaw();
      int node;

      while (DTM.NULL != (node = list1.nextNode()))
      {
        XMLString s1 = getStringFromNode(node);

        if (comparator.compareStrings(s1, s2))
        {
          result = true;

          break;
        }
      }
      list1.reset();
    }
    else if (XObject.CLASS_STRING == type)
    {

      // From http://www.w3.org/TR/xpath: 
      // If one object to be compared is a node-set and the other is a 
      // string, then the comparison will be true if and only if there 
      // is a node in the node-set such that the result of performing 
      // the comparison on the string-value of the node and the other 
      // string is true. 
      XMLString s2 = obj2.xstr();
      DTMIterator list1 = iterRaw();
      int node;

      while (DTM.NULL != (node = list1.nextNode()))
      {
        XMLString s1 = getStringFromNode(node);
        if (comparator.compareStrings(s1, s2))
        {
          result = true;

          break;
        }
      }
      list1.reset();
    }
    else
    {
      result = comparator.compareNumbers(this.num(), obj2.num());
    }

    return result;
  
public voiddispatchCharactersEvents(org.xml.sax.ContentHandler ch)
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

    int node = item(0);
	
    if(node != DTM.NULL)
    {
      m_dtmMgr.getDTM(node).dispatchCharactersEvents(node, ch, false);
    }
    
  
public booleanequals(com.sun.org.apache.xpath.internal.objects.XObject obj2)
Tell if two objects are functionally equal.

param
obj2 object to compare this nodeset to
return
see this.compare(...)
throws
javax.xml.transform.TransformerException

    try
    {
      return compare(obj2, S_EQ);
    }
    catch(javax.xml.transform.TransformerException te)
    {
      throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(te);
    }
  
public com.sun.org.apache.xpath.internal.objects.XObjectgetFresh()
Get a fresh copy of the object. For use with variables.

return
A fresh nodelist.

    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());
    }
  
public doublegetNumberFromNode(int n)
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.

    XMLString xstr = m_dtmMgr.getDTM(n).getStringValue(n);
    return xstr.toDouble();
  
public com.sun.org.apache.xml.internal.utils.XMLStringgetStringFromNode(int n)
Get the string conversion from a single node.

param
n Node to convert
return
the string conversion from a single node.

    // %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 com.sun.org.apache.xpath.internal.objects.XString.EMPTYSTRING;
    }
  
public intgetType()
Tell that this is a CLASS_NODESET.

return
type CLASS_NODESET

    return CLASS_NODESET;
  
public java.lang.StringgetTypeString()
Given a request type, return the equivalent string. For diagnostic purposes.

return
type string "#NODESET"

    return "#NODESET";
  
public booleangreaterThan(com.sun.org.apache.xpath.internal.objects.XObject obj2)
Tell if one object is less than the other.

param
obj2 object to compare this nodeset to
return
see this.compare(...)
throws
javax.xml.transform.TransformerException

    return compare(obj2, S_GT);
  
public booleangreaterThanOrEqual(com.sun.org.apache.xpath.internal.objects.XObject obj2)
Tell if one object is less than the other.

param
obj2 object to compare this nodeset to
return
see this.compare(...)
throws
javax.xml.transform.TransformerException

    return compare(obj2, S_GTE);
  
public com.sun.org.apache.xml.internal.dtm.DTMIteratoriter()
Cast result object to a nodelist.

return
The nodeset as a nodelist

    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());
    }
  
public com.sun.org.apache.xml.internal.dtm.DTMIteratoriterRaw()
Return the iterator without cloning, etc.

    return this;
  
public booleanlessThan(com.sun.org.apache.xpath.internal.objects.XObject obj2)
Tell if one object is less than the other.

param
obj2 object to compare this nodeset to
return
see this.compare(...)
throws
javax.xml.transform.TransformerException

    return compare(obj2, S_LT);
  
public booleanlessThanOrEqual(com.sun.org.apache.xpath.internal.objects.XObject obj2)
Tell if one object is less than or equal to the other.

param
obj2 object to compare this nodeset to
return
see this.compare(...)
throws
javax.xml.transform.TransformerException

    return compare(obj2, S_LTE);
  
public com.sun.org.apache.xpath.internal.NodeSetDTMmutableNodeset()
Cast result object to a mutableNodeset.

return
The nodeset as a mutableNodeset

    NodeSetDTM mnl;

    if(m_obj instanceof NodeSetDTM)
    {
      mnl = (NodeSetDTM) m_obj;
    }
    else
    {
      mnl = new NodeSetDTM(iter());
      m_obj = mnl;
      setCurrentPos(0);
    }

    return mnl;
  
public org.w3c.dom.NodeListnodelist()
Cast result object to a nodelist.

return
a NodeList.
throws
javax.xml.transform.TransformerException

    com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList nodelist = new com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList(this);
    // Creating a DTMNodeList has the side-effect that it will create a clone
    // XNodeSet with cache and run m_iter to the end. You cannot get any node
    // from m_iter after this call. As a fix, we call SetVector() on the clone's 
    // cache. See Bugzilla 14406.
    XNodeSet clone = (XNodeSet)nodelist.getDTMIterator();
    SetVector(clone.getVector());
    return nodelist;
  
public org.w3c.dom.traversal.NodeIteratornodeset()
Cast result object to a nodelist.

return
a NodeIterator.
throws
javax.xml.transform.TransformerException

    return new com.sun.org.apache.xml.internal.dtm.ref.DTMNodeIterator(iter());
  
public booleannotEquals(com.sun.org.apache.xpath.internal.objects.XObject obj2)
Tell if two objects are functionally not equal.

param
obj2 object to compare this nodeset to
return
see this.compare(...)
throws
javax.xml.transform.TransformerException

    return compare(obj2, S_NEQ);
  
public doublenum()
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


    int node = item(0);
    return (node != DTM.NULL) ? getNumberFromNode(node) : Double.NaN;
  
public doublenumWithSideEffects()
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

    int node = nextNode();

    return (node != DTM.NULL) ? getNumberFromNode(node) : Double.NaN;
  
public java.lang.Objectobject()
Return a java object that's closest to the representation that should be handed to an extension.

return
The object that this class wraps

    if(null == m_obj)
    	return this;
    else
    	return m_obj;
  
public voidrelease(com.sun.org.apache.xml.internal.dtm.DTMIterator iter)

  
public java.lang.Stringstr()
Cast result object to a string.

return
the string conversion from the next node in the nodeset or "" if there is no next node

    int node = item(0);
    return (node != DTM.NULL) ? getStringFromNode(node).toString() : "";   
  
public com.sun.org.apache.xml.internal.utils.XMLStringxstr()
Cast result object to an XMLString.

return
The document fragment node data or the empty string.

    int node = item(0);
    return (node != DTM.NULL) ? getStringFromNode(node) : XString.EMPTYSTRING;