KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xpath > pattern > FromExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xpath.pattern;
30
31 import com.caucho.xpath.Env;
32 import com.caucho.xpath.Expr;
33 import com.caucho.xpath.ExprEnvironment;
34 import com.caucho.xpath.XPathException;
35
36 import org.w3c.dom.Node JavaDoc;
37
38 import java.util.Iterator JavaDoc;
39
40 /**
41  * matches if the expression returns a node set and the test-node is
42  * contained in that node set.
43  *
44  * <p>The code interprets Iterators, ArrayLists, and Nodes as node sets.
45  */

46 public class FromExpr extends AbstractPattern {
47   private Expr _expr;
48
49   public FromExpr(AbstractPattern parent, Expr expr)
50   {
51     super(parent);
52     
53     _expr = expr;
54   }
55
56   /**
57    * matches if the expression returns a node set and the test-node is
58    * contained in that node set.
59    *
60    * @param node the node to test
61    * @param env the variable environment.
62    *
63    * @return true if the node matches the pattern.
64    */

65   public boolean match(Node JavaDoc node, ExprEnvironment env)
66     throws XPathException
67   {
68     NodeIterator iter = _expr.evalNodeSet(node, env);
69
70     while (iter.hasNext()) {
71       Node JavaDoc subnode = iter.nextNode();
72       if (subnode == node)
73     return true;
74     }
75
76     return false;
77   }
78
79   /**
80    * Creates a new node iterator.
81    *
82    * @param node the starting node
83    * @param env the variable environment
84    * @param match the axis match pattern
85    *
86    * @return the node iterator
87    */

88   public NodeIterator createNodeIterator(Node JavaDoc node, ExprEnvironment env,
89                                          AbstractPattern match)
90     throws XPathException
91   {
92     return _expr.evalNodeSet(node, env);
93   }
94
95   /**
96    * The position is the position in the expression node-set.
97    */

98   public int position(Node JavaDoc node, Env env, AbstractPattern pattern)
99     throws XPathException
100   {
101     Iterator JavaDoc iter = _expr.evalNodeSet(node, env);
102
103     int i = 1;
104     while (iter.hasNext()) {
105       if (iter.next() == node)
106     return i;
107       i++;
108     }
109
110     return 0;
111   }
112
113   /**
114    * The count is the size of the expression node-set.
115    */

116   public int count(Node JavaDoc node, Env env, AbstractPattern pattern)
117     throws XPathException
118   {
119     Iterator JavaDoc iter = _expr.evalNodeSet(node, env);
120     int count = 0;
121
122     while (iter.hasNext()) {
123       iter.next();
124       count++;
125     }
126
127     return count;
128   }
129
130   public String JavaDoc toString()
131   {
132     return getPrefix() + "(" + _expr + ")";
133   }
134 }
135
Popular Tags