FileDocCategorySizeDatePackage
XPathResultImpl.javaAPI DocJava SE 6 API19684Tue Jun 10 00:23:14 BST 2008com.sun.org.apache.xpath.internal.domapi

XPathResultImpl

public class XPathResultImpl extends Object implements XPathResult, EventListener
The class provides an implementation XPathResult according to the DOM L3 XPath Specification, Working Group Note 26 February 2004.

See also the Document Object Model (DOM) Level 3 XPath Specification.

The XPathResult interface represents the result of the evaluation of an XPath expression within the context of a particular node. Since evaluation of an XPath expression can result in various result types, this object makes it possible to discover and manipulate the type and value of the result.

This implementation wraps an XObject.

see
com.sun.org.apache.xpath.internal.objects.XObject
see
org.w3c.dom.xpath.XPathResult
xsl.usage
internal

Fields Summary
private final XObject
m_resultObj
The wrapped XObject
private final XPath
m_xpath
The xpath object that wraps the expression used for this result.
private final short
m_resultType
This the type specified by the user during construction. Typically the constructor will be called by com.sun.org.apache.xpath.internal.XPath.evaluate().
private boolean
m_isInvalidIteratorState
private final Node
m_contextNode
Only used to attach a mutation event handler when specified type is an iterator type.
private NodeIterator
m_iterator
The iterator, if this is an iterator type.
private NodeList
m_list
The list, if this is a snapshot type.
Constructors Summary
XPathResultImpl(short type, XObject result, Node contextNode, XPath xpath)
Constructor for XPathResultImpl. For internal use only.

	

	        	 
	         
		// Check that the type is valid
		if (!isValidType(type)) {
            String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_INVALID_XPATH_TYPE, new Object[] {new Integer(type)});       
            throw new XPathException(XPathException.TYPE_ERR,fmsg); // Invalid XPath type argument: {0}           
		}

        // Result object should never be null!
        if (null == result) {
            String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_EMPTY_XPATH_RESULT, null);       
            throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,fmsg); // Empty XPath result object
        }
        
        this.m_resultObj = result;
        this.m_contextNode = contextNode;
        this.m_xpath = xpath;

        // If specified result was ANY_TYPE, determine XObject type
        if (type == ANY_TYPE) {
            this.m_resultType = getTypeFromXObject(result);
        } else {
            this.m_resultType = type;
        }
		
        // If the context node supports DOM Events and the type is one of the iterator
        // types register this result as an event listener
        if (((m_resultType == XPathResult.ORDERED_NODE_ITERATOR_TYPE) ||
            (m_resultType == XPathResult.UNORDERED_NODE_ITERATOR_TYPE))) {
            	addEventListener();
                
        }// else can we handle iterator types if contextNode doesn't support EventTarget??
    				
        // If this is an iterator type get the iterator
        if ((m_resultType == ORDERED_NODE_ITERATOR_TYPE) ||
            (m_resultType == UNORDERED_NODE_ITERATOR_TYPE) ||
            (m_resultType == ANY_UNORDERED_NODE_TYPE) ||
            (m_resultType == FIRST_ORDERED_NODE_TYPE))  {
    
            try {
                m_iterator = m_resultObj.nodeset();
            } catch (TransformerException te) {
                // probably not a node type
				String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_INCOMPATIBLE_TYPES, new Object[] {m_xpath.getPatternString(), getTypeString(getTypeFromXObject(m_resultObj)),getTypeString(m_resultType)});       
  			    throw new XPathException(XPathException.TYPE_ERR, fmsg);  // "The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be coerced into the specified XPathResultType of {2}."},
           }
    
                // If user requested ordered nodeset and result is unordered 
                // need to sort...TODO
    //            if ((m_resultType == ORDERED_NODE_ITERATOR_TYPE) &&
    //                (!(((DTMNodeIterator)m_iterator).getDTMIterator().isDocOrdered()))) {
    // 
    //            }
    
        // If it's a snapshot type, get the nodelist
        } else if ((m_resultType == UNORDERED_NODE_SNAPSHOT_TYPE) ||
                   (m_resultType == ORDERED_NODE_SNAPSHOT_TYPE)) {
            try {       	
        	   m_list = m_resultObj.nodelist();
            } catch (TransformerException te) {
        		// probably not a node type 
				String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_INCOMPATIBLE_TYPES, new Object[] {m_xpath.getPatternString(), getTypeString(getTypeFromXObject(m_resultObj)),getTypeString(m_resultType)});       
				throw new XPathException(XPathException.TYPE_ERR, fmsg); // "The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be coerced into the specified XPathResultType of {2}."},
            }
    	}	    	 
	
Methods Summary
private voidaddEventListener()
Add m_contextNode to Event Listner to listen for Mutations Events

  	if(m_contextNode instanceof EventTarget)
		((EventTarget)m_contextNode).addEventListener("DOMSubtreeModified",this,true);
  	
  
public booleangetBooleanValue()

see
org.w3c.dom.xpath.XPathResult#getBooleanValue()

		if (getResultType() != BOOLEAN_TYPE) {
			String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_TO_BOOLEAN, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});		
			throw new XPathException(XPathException.TYPE_ERR,fmsg);
//		"The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a boolean."			
		} else {
			try {
			   return m_resultObj.bool();
			} catch (TransformerException e) {
				// Type check above should prevent this exception from occurring.
				throw new XPathException(XPathException.TYPE_ERR,e.getMessage());
			}
		}
	
public booleangetInvalidIteratorState()

see
org.w3c.dom.xpath.XPathResult#getInvalidIteratorState()

		return m_isInvalidIteratorState;
	
public doublegetNumberValue()
The value of this number result.

exception
XPathException TYPE_ERR: raised if resultType is not NUMBER_TYPE.
see
org.w3c.dom.xpath.XPathResult#getNumberValue()

		if (getResultType() != NUMBER_TYPE) {
			String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_XPATHRESULTTYPE_TO_NUMBER, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});		
			throw new XPathException(XPathException.TYPE_ERR,fmsg);
//		"The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a number"
		} else {
			try {
			   return m_resultObj.num();
			} catch (Exception e) {
				// Type check above should prevent this exception from occurring.
				throw new XPathException(XPathException.TYPE_ERR,e.getMessage());
			}
		}		
	
public shortgetResultType()

see
org.w3c.dom.xpath.XPathResult#getResultType()

		return m_resultType;
	
public org.w3c.dom.NodegetSingleNodeValue()
The value of this single node result, which may be null.

exception
XPathException TYPE_ERR: raised if resultType is not ANY_UNORDERED_NODE_TYPE or FIRST_ORDERED_NODE_TYPE.
see
org.w3c.dom.xpath.XPathResult#getSingleNodeValue()

		
		if ((m_resultType != ANY_UNORDERED_NODE_TYPE) &&
		    (m_resultType != FIRST_ORDERED_NODE_TYPE)) {
				String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_TO_SINGLENODE, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});                        
				throw new XPathException(XPathException.TYPE_ERR,fmsg);
//				"The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a single node. 
//				 This method applies only to types ANY_UNORDERED_NODE_TYPE and FIRST_ORDERED_NODE_TYPE."
	    }
	
		NodeIterator result = null;
		try {
			result = m_resultObj.nodeset();
		} catch (TransformerException te) {
			throw new XPathException(XPathException.TYPE_ERR,te.getMessage());
		}
        
        if (null == result) return null;
        
        Node node = result.nextNode();
         
        // Wrap "namespace node" in an XPathNamespace 
        if (isNamespaceNode(node)) {
            return new XPathNamespaceImpl(node);
        } else {
            return node;
        }        
	
public intgetSnapshotLength()
The number of nodes in the result snapshot. Valid values for snapshotItem indices are 0 to snapshotLength-1 inclusive.

exception
XPathException TYPE_ERR: raised if resultType is not UNORDERED_NODE_SNAPSHOT_TYPE or ORDERED_NODE_SNAPSHOT_TYPE.
see
org.w3c.dom.xpath.XPathResult#getSnapshotLength()

	
		if ((m_resultType != UNORDERED_NODE_SNAPSHOT_TYPE) &&
		    (m_resultType != ORDERED_NODE_SNAPSHOT_TYPE)) {
				String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_GET_SNAPSHOT_LENGTH, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});                        
				throw new XPathException(XPathException.TYPE_ERR,fmsg); 
//				"The method getSnapshotLength cannot be called on the XPathResult of XPath expression {0} because its XPathResultType is {1}.
	    }
			
		return m_list.getLength();
	
public java.lang.StringgetStringValue()
The value of this string result.

exception
XPathException TYPE_ERR: raised if resultType is not STRING_TYPE.
see
org.w3c.dom.xpath.XPathResult#getStringValue()

		if (getResultType() != STRING_TYPE) {
			String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_TO_STRING, new Object[] {m_xpath.getPatternString(), m_resultObj.getTypeString()});		
			throw new XPathException(XPathException.TYPE_ERR,fmsg);
//		"The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a string."
		} else {
			try {
			   return m_resultObj.str();
			} catch (Exception e) {
				// Type check above should prevent this exception from occurring.
				throw new XPathException(XPathException.TYPE_ERR,e.getMessage());
			}
		}
	
private shortgetTypeFromXObject(com.sun.org.apache.xpath.internal.objects.XObject object)
Given an XObject, determine the corresponding DOM XPath type

return
type string

      switch (object.getType()) {
        case XObject.CLASS_BOOLEAN: return BOOLEAN_TYPE;
        case XObject.CLASS_NODESET: return UNORDERED_NODE_ITERATOR_TYPE; 
        case XObject.CLASS_NUMBER: return NUMBER_TYPE;
        case XObject.CLASS_STRING: return STRING_TYPE;
        // XPath 2.0 types                         
//          case XObject.CLASS_DATE: 
//          case XObject.CLASS_DATETIME:
//          case XObject.CLASS_DTDURATION:
//          case XObject.CLASS_GDAY:
//          case XObject.CLASS_GMONTH:
//          case XObject.CLASS_GMONTHDAY:
//          case XObject.CLASS_GYEAR:
//          case XObject.CLASS_GYEARMONTH: 
//          case XObject.CLASS_TIME:
//          case XObject.CLASS_YMDURATION: return STRING_TYPE; // treat all date types as strings?
      
        case XObject.CLASS_RTREEFRAG: return UNORDERED_NODE_ITERATOR_TYPE; 
        case XObject.CLASS_NULL: return ANY_TYPE; // throw exception ?
        default: return ANY_TYPE; // throw exception ?
    }     
    
  
private java.lang.StringgetTypeString(int type)
Given a request type, return the equivalent string. For diagnostic purposes.

return
type string

     switch (type) {
      case ANY_TYPE: return "ANY_TYPE";
      case ANY_UNORDERED_NODE_TYPE: return "ANY_UNORDERED_NODE_TYPE";
      case BOOLEAN_TYPE: return "BOOLEAN";
      case FIRST_ORDERED_NODE_TYPE: return "FIRST_ORDERED_NODE_TYPE";
      case NUMBER_TYPE: return "NUMBER_TYPE";
      case ORDERED_NODE_ITERATOR_TYPE: return "ORDERED_NODE_ITERATOR_TYPE";
      case ORDERED_NODE_SNAPSHOT_TYPE: return "ORDERED_NODE_SNAPSHOT_TYPE";
      case STRING_TYPE: return "STRING_TYPE";
      case UNORDERED_NODE_ITERATOR_TYPE: return "UNORDERED_NODE_ITERATOR_TYPE";
      case UNORDERED_NODE_SNAPSHOT_TYPE: return "UNORDERED_NODE_SNAPSHOT_TYPE";
      default: return "#UNKNOWN";
    }
  
public voidhandleEvent(org.w3c.dom.events.Event event)

see
org.w3c.dom.events.EventListener#handleEvent(Event)

		
		if (event.getType().equals("DOMSubtreeModified")) {
			// invalidate the iterator
			m_isInvalidIteratorState = true;
			
			// deregister as a listener to reduce computational load
			removeEventListener();
		}
	
private booleanisNamespaceNode(org.w3c.dom.Node node)
Given a node, determine if it is a namespace node.

param
node
return
boolean Returns true if this is a namespace node; otherwise, returns false.

    
     if ((null != node) && 
         (node.getNodeType() == Node.ATTRIBUTE_NODE) &&
         (node.getNodeName().startsWith("xmlns:") || node.getNodeName().equals("xmlns"))) {
        return true;   
     } else {
        return false;
     }
  
static booleanisValidType(short type)
Check if the specified type is one of the supported types.

param
type The specified type
return
true If the specified type is supported; otherwise, returns false.

		switch (type) {
			case ANY_TYPE:
			case NUMBER_TYPE:
			case STRING_TYPE:
			case BOOLEAN_TYPE:
			case UNORDERED_NODE_ITERATOR_TYPE:
			case ORDERED_NODE_ITERATOR_TYPE:
			case UNORDERED_NODE_SNAPSHOT_TYPE:
			case ORDERED_NODE_SNAPSHOT_TYPE:
			case ANY_UNORDERED_NODE_TYPE:
			case FIRST_ORDERED_NODE_TYPE: return true;
			default: return false;
		}
	
public org.w3c.dom.NodeiterateNext()
Iterates and returns the next node from the node set or nullif there are no more nodes.

return
Returns the next node.
exception
XPathException TYPE_ERR: raised if resultType is not UNORDERED_NODE_ITERATOR_TYPE or ORDERED_NODE_ITERATOR_TYPE.
exception
DOMException INVALID_STATE_ERR: The document has been mutated since the result was returned.
see
org.w3c.dom.xpath.XPathResult#iterateNext()

		if ((m_resultType != UNORDERED_NODE_ITERATOR_TYPE) &&
		    (m_resultType != ORDERED_NODE_ITERATOR_TYPE)) {
          String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_NON_ITERATOR_TYPE, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});                        
		  throw new XPathException(XPathException.TYPE_ERR, fmsg);  
//		  "The method iterateNext cannot be called on the XPathResult of XPath expression {0} because its XPathResultType is {1}. 
//		  This method applies only to types UNORDERED_NODE_ITERATOR_TYPE and ORDERED_NODE_ITERATOR_TYPE."},
	    }

		if (getInvalidIteratorState()) {
          String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_DOC_MUTATED, null);                        
		  throw new DOMException(DOMException.INVALID_STATE_ERR,fmsg);  // Document mutated since result was returned. Iterator is invalid.
		}			 

        Node node = m_iterator.nextNode();
        if(null == node)
        	removeEventListener(); // JIRA 1673
        // Wrap "namespace node" in an XPathNamespace 
        if (isNamespaceNode(node)) {
            return new XPathNamespaceImpl(node);
        } else {
            return node;
        }
	
private voidremoveEventListener()
Remove m_contextNode to Event Listner to listen for Mutations Events

	if(m_contextNode instanceof EventTarget)
		((EventTarget)m_contextNode).removeEventListener("DOMSubtreeModified",this,true);
public org.w3c.dom.NodesnapshotItem(int index)
Returns the indexth item in the snapshot collection. If index is greater than or equal to the number of nodes in the list, this method returns null. Unlike the iterator result, the snapshot does not become invalid, but may not correspond to the current document if it is mutated.

param
index Index into the snapshot collection.
return
The node at the indexth position in the NodeList, or null if that is not a valid index.
exception
XPathException TYPE_ERR: raised if resultType is not UNORDERED_NODE_SNAPSHOT_TYPE or ORDERED_NODE_SNAPSHOT_TYPE.
see
org.w3c.dom.xpath.XPathResult#snapshotItem(int)

		
		if ((m_resultType != UNORDERED_NODE_SNAPSHOT_TYPE) &&
		    (m_resultType != ORDERED_NODE_SNAPSHOT_TYPE)) {
           String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_NON_SNAPSHOT_TYPE, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});                        
           throw new XPathException(XPathException.TYPE_ERR, fmsg); 
//		"The method snapshotItem cannot be called on the XPathResult of XPath expression {0} because its XPathResultType is {1}. 
//		This method applies only to types UNORDERED_NODE_SNAPSHOT_TYPE and ORDERED_NODE_SNAPSHOT_TYPE."},
	    }		
        
        Node node = m_list.item(index);
        
        // Wrap "namespace node" in an XPathNamespace 
        if (isNamespaceNode(node)) {
            return new XPathNamespaceImpl(node);
        } else {
            return node;
        }