1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 package org.jaxen.pattern;
50
51 import java.util.LinkedList;
52
53 import org.jaxen.JaxenException;
54 import org.jaxen.JaxenHandler;
55 import org.jaxen.expr.Expr;
56 import org.jaxen.expr.FilterExpr;
57 import org.jaxen.saxpath.Axis;
58
59 /*** SAXPath <code>XPathHandler</code> implementation capable
60 * of building Jaxen expression trees which can walk various
61 * different object models.
62 *
63 * @author bob mcwhirter (bob@werken.com)
64 */
65 public class PatternHandler extends JaxenHandler
66 {
67 private Pattern pattern;
68
69 public PatternHandler()
70 {
71 }
72
73 /*** Retrieve the simplified Jaxen Pattern expression tree.
74 *
75 * <p>
76 * This method is only valid once <code>XPathReader.parse(...)</code>
77 * successfully returned.
78 * </p>
79 *
80 * @return The Pattern expression tree.
81 */
82 public Pattern getPattern()
83 {
84 return getPattern( true );
85 }
86
87 /*** Retrieve the Jaxen Pattern expression tree, optionally
88 * simplified.
89 *
90 * <p>
91 * This method is only valid once <code>XPathReader.parse(...)</code>
92 * successfully returned.
93 * </p>
94 *
95 * @param shouldSimplify ????
96 *
97 * @return The Pattern expression tree.
98 */
99 public Pattern getPattern(boolean shouldSimplify)
100 {
101 if ( shouldSimplify && ! this.simplified )
102 {
103
104 this.pattern.simplify();
105 this.simplified = true;
106 }
107
108 return this.pattern;
109 }
110
111
112
113
114 public void endXPath()
115 {
116 this.pattern = (Pattern) pop();
117
118 System.out.println( "stack is: " + stack );
119
120 popFrame();
121 }
122
123 public void endPathExpr()
124 {
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 LinkedList frame = popFrame();
141
142 System.out.println( "endPathExpr(): " + frame );
143
144 push( frame.removeFirst() );
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 }
172
173 public void startAbsoluteLocationPath()
174 {
175
176 pushFrame();
177
178 push( createAbsoluteLocationPath() );
179 }
180
181 public void endAbsoluteLocationPath() throws JaxenException
182 {
183
184 endLocationPath();
185 }
186
187 public void startRelativeLocationPath()
188 {
189
190 pushFrame();
191
192 push( createRelativeLocationPath() );
193 }
194
195 public void endRelativeLocationPath() throws JaxenException
196 {
197
198 endLocationPath();
199 }
200
201 protected void endLocationPath() throws JaxenException
202 {
203
204 LinkedList list = popFrame();
205
206 System.out.println( "endLocationPath: " + list );
207
208 LocationPathPattern locationPath = (LocationPathPattern) list.removeFirst();
209 push( locationPath );
210 boolean doneNodeTest = false;
211 while ( ! list.isEmpty() )
212 {
213 Object filter = list.removeFirst();
214 if ( filter instanceof NodeTest )
215 {
216 if ( doneNodeTest )
217 {
218 LocationPathPattern parent = new LocationPathPattern( (NodeTest) filter );
219 locationPath.setParentPattern( parent );
220 locationPath = parent;
221 doneNodeTest = false;
222 }
223 else
224 {
225 locationPath.setNodeTest( (NodeTest) filter );
226 }
227 }
228 else if ( filter instanceof FilterExpr )
229 {
230 locationPath.addFilter( (FilterExpr) filter );
231 }
232 else if ( filter instanceof LocationPathPattern )
233 {
234 LocationPathPattern parent = (LocationPathPattern) filter;
235 locationPath.setParentPattern( parent );
236 locationPath = parent;
237 doneNodeTest = false;
238 }
239 }
240 }
241
242
243 public void startNameStep(int axis,
244 String prefix,
245 String localName)
246 {
247
248 pushFrame();
249
250 short nodeType = Pattern.ELEMENT_NODE;
251 switch ( axis )
252 {
253 case Axis.ATTRIBUTE:
254 nodeType = Pattern.ATTRIBUTE_NODE;
255 break;
256 case Axis.NAMESPACE:
257 nodeType = Pattern.NAMESPACE_NODE;
258 break;
259 }
260
261 if ( prefix != null && prefix.length() > 0 && ! prefix.equals( "*" ) )
262 {
263 push( new NamespaceTest( prefix, nodeType ) );
264 }
265 if ( localName != null && localName.length() > 0 && ! localName.equals( "*" ) )
266 {
267 push( new NameTest( localName, nodeType ) );
268 }
269 }
270
271 public void startTextNodeStep(int axis)
272 {
273
274 pushFrame();
275
276 push( new NodeTypeTest( Pattern.TEXT_NODE ) );
277 }
278
279 public void startCommentNodeStep(int axis)
280 {
281
282 pushFrame();
283
284 push( new NodeTypeTest( Pattern.COMMENT_NODE ) );
285 }
286
287 public void startAllNodeStep(int axis)
288 {
289
290 pushFrame();
291
292 push( AnyNodeTest.getInstance() );
293 }
294
295 public void startProcessingInstructionNodeStep(int axis,
296 String name)
297 {
298
299 pushFrame();
300
301
302 push( new NodeTypeTest( Pattern.PROCESSING_INSTRUCTION_NODE ) );
303 }
304
305 protected void endStep()
306 {
307 LinkedList list = popFrame();
308 if ( ! list.isEmpty() )
309 {
310 push( list.removeFirst() );
311
312 if ( ! list.isEmpty() )
313 {
314 System.out.println( "List should now be empty!" + list );
315 }
316 }
317 }
318
319
320 public void startUnionExpr()
321 {
322
323 }
324
325 public void endUnionExpr(boolean create) throws JaxenException
326 {
327
328
329 if ( create )
330 {
331
332
333 Expr rhs = (Expr) pop();
334 Expr lhs = (Expr) pop();
335
336 push( getXPathFactory().createUnionExpr( lhs,
337 rhs ) );
338 }
339 }
340
341 protected Pattern createAbsoluteLocationPath()
342 {
343 return new LocationPathPattern( NodeTypeTest.DOCUMENT_TEST );
344 }
345
346 protected Pattern createRelativeLocationPath()
347 {
348 return new LocationPathPattern();
349 }
350
351 }