/** Creates an expr, handling some special cases. */ public static Expr create(AbstractPattern pattern) { if (pattern instanceof NodeTypePattern && pattern.getParent() instanceof FromSelf && pattern.toString().equals(".")) return new ObjectExpr(SELF, "."); else if (pattern instanceof FromContext && ((FromContext) pattern).getCount() == 0 && pattern.getParent() == null) return new ObjectExpr(SELF, "."); else if (pattern instanceof NodePattern && pattern.getParent() instanceof FromAttributes && pattern.getParent().getParent() instanceof FromContext && ((FromContext) pattern.getParent().getParent()).getCount() == 0) return new ObjectExpr(ATTRIBUTE, ((NodePattern) pattern).getNodeName()); else return new NodeSetExpr(pattern); }
/** * Returns the value of the expression as a number. * * @param node the current node * @param env the variable environment. */ public double evalNumber(Node node, ExprEnvironment env) throws XPathException { Node value = _pattern.findAny(node, env); if (value == null) return Double.NaN; String string = XmlUtil.textValue(value); return stringToNumber(string); }
/** * Evaluate a node-set object, returning an ArrayList of the node set. * * @param node the current node * @param env the variable environment * @return an array list of the nodes */ public Object evalObject(Node node, ExprEnvironment env) throws XPathException { NodeListImpl list = new NodeListImpl(); NodeIterator iter = _pattern.select(node, env); Node value = null; while ((value = iter.nextNode()) != null) list.add(value); return list; }
/** * Returns true if there are any patterns matching the pattern. * * @param node the current node * @param env the variable environment. */ public boolean evalBoolean(Node node, ExprEnvironment env) throws XPathException { return _pattern.findAny(node, env) != null; }
public String toString() { return _pattern.toString(); }
public boolean equals(Object b) { if (!(b instanceof NodeSetExpr)) return false; return _pattern.equals(((NodeSetExpr) b)._pattern); }
/** * Evaluate a node-set object, returning an iterator of the node set. * * @param node the current node * @param env the variable environment * @return an iterator of the nodes */ public NodeIterator evalNodeSet(Node node, ExprEnvironment env) throws XPathException { return _pattern.select(node, env); }
/** * Returns the value of the node set expression as a string. The value is the text value of the * first node. * * @param node the current node * @param env the variable environment * @return the combined text value of the node. */ public String evalString(Node node, ExprEnvironment env) throws XPathException { Node value = _pattern.findAny(node, env); if (value == null) return ""; else return XmlUtil.textValue(value); }