001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xpath.pattern;
030:
031: import com.caucho.xpath.Env;
032: import com.caucho.xpath.Expr;
033: import com.caucho.xpath.ExprEnvironment;
034: import com.caucho.xpath.XPathException;
035:
036: import org.w3c.dom.Node;
037:
038: import java.util.Iterator;
039:
040: /**
041: * matches if the expression returns a node set and the test-node is
042: * contained in that node set.
043: *
044: * <p>The code interprets Iterators, ArrayLists, and Nodes as node sets.
045: */
046: public class FromExpr extends AbstractPattern {
047: private Expr _expr;
048:
049: public FromExpr(AbstractPattern parent, Expr expr) {
050: super (parent);
051:
052: _expr = expr;
053: }
054:
055: /**
056: * matches if the expression returns a node set and the test-node is
057: * contained in that node set.
058: *
059: * @param node the node to test
060: * @param env the variable environment.
061: *
062: * @return true if the node matches the pattern.
063: */
064: public boolean match(Node node, ExprEnvironment env)
065: throws XPathException {
066: NodeIterator iter = _expr.evalNodeSet(node, env);
067:
068: while (iter.hasNext()) {
069: Node subnode = iter.nextNode();
070:
071: if (subnode == node)
072: return true;
073: }
074:
075: return false;
076: }
077:
078: /**
079: * Creates a new node iterator.
080: *
081: * @param node the starting node
082: * @param env the variable environment
083: * @param match the axis match pattern
084: *
085: * @return the node iterator
086: */
087: public NodeIterator createNodeIterator(Node node,
088: ExprEnvironment env, AbstractPattern match)
089: throws XPathException {
090: return _expr.evalNodeSet(node, env);
091: }
092:
093: /**
094: * The position is the position in the expression node-set.
095: */
096: public int position(Node node, Env env, AbstractPattern pattern)
097: throws XPathException {
098: Iterator iter = _expr.evalNodeSet(node, env);
099:
100: int i = 1;
101: while (iter.hasNext()) {
102: if (iter.next() == node)
103: return i;
104: i++;
105: }
106:
107: return 0;
108: }
109:
110: /**
111: * The count is the size of the expression node-set.
112: */
113: public int count(Node node, Env env, AbstractPattern pattern)
114: throws XPathException {
115: Iterator iter = _expr.evalNodeSet(node, env);
116: int count = 0;
117:
118: while (iter.hasNext()) {
119: iter.next();
120: count++;
121: }
122:
123: return count;
124: }
125:
126: public String toString() {
127: return getPrefix() + "(" + _expr + ")";
128: }
129: }
|