protected Context getContext(Object node) { if ( node instanceof Context ) { return (Context) node; } if ( node instanceof Element ) { return super.getContext( node ); } if ( node instanceof List ) { List newList = new ArrayList(); for ( Iterator listIter = ((List)node).iterator(); listIter.hasNext(); ) { newList.add( new Element( null, "root", listIter.next() ) ); } return super.getContext( newList ); } return super.getContext( new Element( null, "root", node ) ); }
/** Select all nodes that are selected by this XPath * expression. If multiple nodes match, multiple nodes * will be returned. Nodes will be returned * in document-order, as defined by the XPath * specification. If the expression selects a non-node-set * (i.e. a number, boolean, or string) then a List * containing just that one object is returned. * </p> * * @param node the node, node-set or Context object for evaluation. * This value can be null. * * @return the node-set of all items selected * by this XPath expression * @throws JaxenException if an XPath error occurs during expression evaluation * * @see #selectNodesForContext */ public List selectNodes(Object node) throws JaxenException { Context context = getContext( node ); return selectNodesForContext( context ); }
/** Retrieve a boolean-value interpretation of this XPath * expression when evaluated against a given context. * * <p> * The boolean-value of the expression is determined per * the <code>boolean(..)</code> function defined * in the XPath specification. This means that an expression * that selects zero nodes will return <code>false</code>, * while an expression that selects one or more nodes will * return <code>true</code>. * </p> * * @param node the node, node-set or Context object for evaluation. This value can be null. * * @return the boolean-value of the result of evaluating this expression with the specified context node * @throws JaxenException if an XPath error occurs during expression evaluation */ public boolean booleanValueOf(Object node) throws JaxenException { Context context = getContext( node ); List result = selectNodesForContext( context ); if ( result == null ) return false; return BooleanFunction.evaluate( result, context.getNavigator() ).booleanValue(); }
/** Retrieve a number-value interpretation of this XPath * expression when evaluated against a given context. * * <p> * The number-value of the expression is determined per * the <code>number(..)</code> core function as defined * in the XPath specification. This means that if this * expression selects multiple nodes, the number-value * of the first node is returned. * </p> * * @param node the node, node-set or Context object for evaluation. This value can be null. * * @return a <code>Double</code> indicating the numeric value of * evaluating this expression against the specified context * @throws JaxenException if an XPath error occurs during expression evaluation */ public Number numberValueOf(Object node) throws JaxenException { Context context = getContext( node ); Object result = selectSingleNodeForContext( context ); return NumberFunction.evaluate( result, context.getNavigator() ); }
/** Retrieves the string-value of the result of * evaluating this XPath expression when evaluated * against the specified context. * * <p> * The string-value of the expression is determined per * the <code>string(..)</code> core function defined * in the XPath specification. This means that an expression * that selects zero nodes will return the empty string, * while an expression that selects one-or-more nodes will * return the string-value of the first node. * </p> * * @param node the node, node-set or Context object for evaluation. This value can be null. * * @return the string-value of the result of evaluating this expression with the specified context node * @throws JaxenException if an XPath error occurs during expression evaluation */ public String stringValueOf(Object node) throws JaxenException { Context context = getContext( node ); Object result = selectSingleNodeForContext( context ); if ( result == null ) { return ""; } return StringFunction.evaluate( result, context.getNavigator() ); }
protected Context getContext(Object node) { if (node instanceof Context) { return (Context) node; } if (node instanceof LogicalComponent) { return super.getContext(node); } if (node instanceof List) { List<Object> newList = new ArrayList<>(); for (Object o : ((List) node)) { newList.add(o); } return super.getContext(newList); } return super.getContext(node); }