KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xpath > Pattern


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;
30
31 import com.caucho.log.Log;
32 import com.caucho.xpath.pattern.AbstractPattern;
33 import com.caucho.xpath.pattern.NodeIterator;
34
35 import org.w3c.dom.Node JavaDoc;
36
37 import java.util.Iterator JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * A node selection pattern. Patterns represent compiled XPath node selectors.
42  * They can be used to find nodes, select nodes, and test if a node matches
43  * a pattern.
44  *
45  * <p>There are two types of patterns: select patterns and match patterns.
46  * <p>Select patterns match a node relative to another node.
47  * <code>find</code> and <code>select</code> use select patterns.
48  * <p>Match patterns match a node in isolation. <code>isMatch</code> uses
49  * match patterns.
50  */

51 public class Pattern {
52   protected final static Logger JavaDoc log = Log.open(Pattern.class);
53   
54   private AbstractPattern pattern;
55
56   Pattern(AbstractPattern pattern)
57   {
58     this.pattern = pattern;
59   }
60
61   /**
62    * Returns the first node matching the pattern. The pattern should
63    * be a select pattern.
64    *
65    * @param node node represented by '.' and start of match.
66    *
67    * @return first matching node
68    */

69   public Node JavaDoc find(Node JavaDoc node)
70     throws XPathException
71   {
72     if (node == null)
73       throw new NullPointerException JavaDoc();
74
75     Env env = XPath.createEnv();
76     // XXX: doesn't make sense for a match to have a context?
77
//env.setCurrentNode(node);
78
//env.setContextNode(node);
79

80     Iterator iter = pattern.select(node, env);
81
82     Node JavaDoc value = null;
83     if (iter.hasNext())
84       value = (Node JavaDoc) iter.next();
85
86     XPath.freeEnv(env);
87     
88     return value;
89   }
90
91   /**
92    * Returns the first node matching the pattern. The pattern should
93    * be a select pattern.
94    *
95    * @param node node represented by '.' and start of match.
96    * @param env variable environment.
97    *
98    * @return first matching node
99    */

100   public Node JavaDoc find(Node JavaDoc node, ExprEnvironment env)
101     throws XPathException
102   {
103     if (node == null)
104       throw new NullPointerException JavaDoc();
105
106     if (env instanceof Env) {
107       Env globalEnv = (Env) env;
108       
109       // XXX: doesn't make sense for a match to have a context?
110
//globalEnv.setCurrentNode(node);
111
//globalEnv.setContextNode(node);
112
}
113     
114     Iterator iter = pattern.select(node, env);
115
116     Node JavaDoc value = null;
117     if (iter.hasNext())
118       value = (Node JavaDoc) iter.next();
119
120     return value;
121   }
122
123   /**
124    * Selects all nodes matching the pattern. The pattern should be a
125    * select pattern.
126    *
127    * @param node node represented by '.' and start of match.
128    *
129    * @return iterator of matching nodes
130    */

131   public NodeIterator select(Node JavaDoc node)
132     throws XPathException
133   {
134     if (node == null)
135       throw new NullPointerException JavaDoc();
136
137     Env env = XPath.createEnv();
138
139     env.setCurrentNode(node);
140     
141     NodeIterator iter = pattern.select(node, env);
142
143     XPath.freeEnv(env);
144
145     return iter;
146   }
147
148   /**
149    * Selects all nodes matching the pattern. The pattern should be a
150    * select pattern.
151    *
152    * @param context node represented by '.' and start of match.
153    * @param env variable environment.
154    *
155    * @return iterator of matching nodes
156    */

157   public NodeIterator select(Node JavaDoc node, ExprEnvironment env)
158     throws XPathException
159   {
160     if (node == null)
161       throw new NullPointerException JavaDoc();
162
163     if (env instanceof Env) {
164       Env globalEnv = (Env) env;
165       globalEnv.setCurrentNode(node);
166       globalEnv.setContextNode(node);
167     }
168     
169     return pattern.select(node, env);
170   }
171   
172   /**
173    * Test if the node matches the pattern. The pattern should be a
174    * match pattern.
175    *
176    * @param node node to test
177    *
178    * @return true if the pattern matches.
179    */

180   public boolean isMatch(Node JavaDoc node)
181     throws XPathException
182   {
183     Env env = XPath.createEnv();
184
185     // XXX: doesn't make sense for a match to have a context?
186
//env.setCurrentNode(node);
187
//env.setContextNode(node);
188

189     boolean value = pattern.match(node, env);
190
191     XPath.freeEnv(env);
192
193     return value;
194   }
195
196   /**
197    * Test if the node matches the pattern. The pattern should be a
198    * match pattern.
199    *
200    * @param node node to test
201    * @param env variable environment.
202    *
203    * @return true if the pattern matches.
204    */

205   public boolean isMatch(Node JavaDoc node, ExprEnvironment env)
206     throws XPathException
207   {
208     return pattern.match(node, env);
209   }
210
211   /**
212    * Returns the underlying pattern implementation.
213    */

214   public AbstractPattern getPattern()
215   {
216     return pattern;
217   }
218
219   public String JavaDoc toString()
220   {
221     return pattern.toString();
222   }
223 }
224
Popular Tags