KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
39  * matches a node if it matches a filter expression. Filter implements
40  * the a[b] pattern.
41  */

42 public class FilterPattern extends AbstractPattern {
43   private Expr _expr;
44   
45   private AbstractPattern _position;
46
47   public FilterPattern(AbstractPattern parent, Expr expr)
48   {
49     super(parent);
50     
51     _expr = expr;
52
53     if (parent == null)
54       throw new RuntimeException JavaDoc();
55   }
56
57   public String JavaDoc getNodeName()
58   {
59     return _parent.getNodeName();
60   }
61
62   /**
63    * Returns the filter's expression.
64    */

65   public Expr getExpr()
66   {
67     return _expr;
68   }
69
70   /**
71    * Matches if the filter expression matches. When the filter expression
72    * returns a number, match if the position() equals the expression.
73    *
74    * @param node the current node to test
75    * @param env the variable environment
76    *
77    * @return true if the pattern matches.
78    */

79   /* Because match works from the bottom up, it must sometimes evaluate
80    * the expression several times. Take the match pattern
81    * 'a/preceding-sibling::b[2]'. Given the XML:
82    * <foo>
83    * <b id=1/>
84    * <b id=2/>
85    * <a id=3/>
86    * <b id=4/>
87    * <a id=5/>
88    * </foo>
89    * The node b[@id=1] has two valid positions: 2 and 3, corresponding to
90    * 'axis-contexts' a[@id=3] and a[@id=5].
91    *
92    * The position index iterates through the 'axis-contexts.' The axis,
93    * e.g. preceding-sibling, signals the filter that another axis-context
94    * is available through env.setMorePositions().
95    */

96   public boolean match(Node JavaDoc node, ExprEnvironment env)
97     throws XPathException
98   {
99     if (! _parent.match(node, env))
100       return false;
101     
102     // Select patterns use the first shortcut.
103
int envPosition = env.getContextPosition();
104
105     if (envPosition > 0) {
106       if (_expr.isBoolean()) {
107     return _expr.evalBoolean(node, env);
108       }
109       else if (_expr.isNumber()) {
110         double test = _expr.evalNumber(node, env);
111
112         return (envPosition == (int) test);
113       }
114       else {
115     Object JavaDoc value = _expr.evalObject(node, env);
116
117     if (value instanceof Number JavaDoc)
118       return (envPosition == ((Number JavaDoc) value).intValue());
119     
120     return Expr.toBoolean(value);
121       }
122     }
123
124     // Match patterns need to use a more complicated test.
125
if (! (env instanceof Env))
126       throw new RuntimeException JavaDoc(String.valueOf(env));
127     
128     Env globalEnv = (Env) env;
129     boolean oldMorePositions = globalEnv.setMorePositions(true);
130     int oldIndex = globalEnv.setPositionIndex(0);
131     try {
132       for (int i = 0; globalEnv.hasMorePositions(); i++) {
133         globalEnv.setPositionIndex(i);
134         globalEnv.setMorePositions(false);
135
136         if (_expr.isNumber()) {
137           double test = _expr.evalNumber(node, env);
138           double position = _parent.position(node, globalEnv,
139                          _parent.copyPosition());
140
141           if (position == test)
142             return true;
143         }
144     else if (_expr.isBoolean()) {
145       if (_expr.evalBoolean(node, env))
146         return true;
147     }
148     else {
149       Object JavaDoc value = _expr.evalObject(node, env);
150
151       if (value instanceof Number JavaDoc) {
152         double test = ((Number JavaDoc) value).doubleValue();
153         double position = _parent.position(node, globalEnv,
154                            _parent.copyPosition());
155
156         if (position == test)
157           return true;
158       }
159       else if (Expr.toBoolean(value))
160         return true;
161     }
162       }
163
164       return false;
165     } finally {
166       globalEnv.setPositionIndex(oldIndex);
167       globalEnv.setMorePositions(oldMorePositions);
168     }
169   }
170
171   /**
172    * Creates a new node iterator.
173    *
174    * @param node the starting node
175    * @param env the variable environment
176    * @param match the axis match pattern
177    *
178    * @return the node iterator
179    */

180   public NodeIterator createNodeIterator(Node JavaDoc node, ExprEnvironment env,
181                                          AbstractPattern match)
182     throws XPathException
183   {
184     NodeIterator parentIter;
185     parentIter = _parent.createNodeIterator(node, env, _parent.copyPosition());
186
187     return new FilterIterator(parentIter, _expr, env, node);
188   }
189
190   public AbstractPattern copyPosition()
191   {
192     return null;
193   }
194
195   public String JavaDoc toString()
196   {
197     return (_parent.toString() + "[" + _expr + "]");
198   }
199 }
200
Popular Tags