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
60
61
62
63
64
65
66 public class PatternHandler extends JaxenHandler
67 {
68 private Pattern pattern;
69
70 public PatternHandler()
71 {
72 }
73
74
75
76
77
78
79
80
81
82
83 public Pattern getPattern()
84 {
85 return getPattern( true );
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public Pattern getPattern(boolean shouldSimplify)
101 {
102 if ( shouldSimplify && ! this.simplified )
103 {
104
105 this.pattern.simplify();
106 this.simplified = true;
107 }
108
109 return this.pattern;
110 }
111
112
113
114
115 public void endXPath()
116 {
117 this.pattern = (Pattern) pop();
118
119 System.out.println( "stack is: " + stack );
120
121 popFrame();
122 }
123
124 public void endPathExpr()
125 {
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 LinkedList frame = popFrame();
142
143 System.out.println( "endPathExpr(): " + frame );
144
145 push( frame.removeFirst() );
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
174 public void startAbsoluteLocationPath()
175 {
176
177 pushFrame();
178
179 push( createAbsoluteLocationPath() );
180 }
181
182 public void endAbsoluteLocationPath() throws JaxenException
183 {
184
185 endLocationPath();
186 }
187
188 public void startRelativeLocationPath()
189 {
190
191 pushFrame();
192
193 push( createRelativeLocationPath() );
194 }
195
196 public void endRelativeLocationPath() throws JaxenException
197 {
198
199 endLocationPath();
200 }
201
202 protected void endLocationPath() throws JaxenException
203 {
204
205 LinkedList list = popFrame();
206
207 System.out.println( "endLocationPath: " + list );
208
209 LocationPathPattern locationPath = (LocationPathPattern) list.removeFirst();
210 push( locationPath );
211 boolean doneNodeTest = false;
212 while ( ! list.isEmpty() )
213 {
214 Object filter = list.removeFirst();
215 if ( filter instanceof NodeTest )
216 {
217 if ( doneNodeTest )
218 {
219 LocationPathPattern parent = new LocationPathPattern( (NodeTest) filter );
220 locationPath.setParentPattern( parent );
221 locationPath = parent;
222 doneNodeTest = false;
223 }
224 else
225 {
226 locationPath.setNodeTest( (NodeTest) filter );
227 }
228 }
229 else if ( filter instanceof FilterExpr )
230 {
231 locationPath.addFilter( (FilterExpr) filter );
232 }
233 else if ( filter instanceof LocationPathPattern )
234 {
235 LocationPathPattern parent = (LocationPathPattern) filter;
236 locationPath.setParentPattern( parent );
237 locationPath = parent;
238 doneNodeTest = false;
239 }
240 }
241 }
242
243
244 public void startNameStep(int axis,
245 String prefix,
246 String localName)
247 {
248
249 pushFrame();
250
251 short nodeType = Pattern.ELEMENT_NODE;
252 switch ( axis )
253 {
254 case Axis.ATTRIBUTE:
255 nodeType = Pattern.ATTRIBUTE_NODE;
256 break;
257 case Axis.NAMESPACE:
258 nodeType = Pattern.NAMESPACE_NODE;
259 break;
260 }
261
262 if ( prefix != null && prefix.length() > 0 && ! prefix.equals( "*" ) )
263 {
264 push( new NamespaceTest( prefix, nodeType ) );
265 }
266 if ( localName != null && localName.length() > 0 && ! localName.equals( "*" ) )
267 {
268 push( new NameTest( localName, nodeType ) );
269 }
270 }
271
272 public void startTextNodeStep(int axis)
273 {
274
275 pushFrame();
276
277 push( new NodeTypeTest( Pattern.TEXT_NODE ) );
278 }
279
280 public void startCommentNodeStep(int axis)
281 {
282
283 pushFrame();
284
285 push( new NodeTypeTest( Pattern.COMMENT_NODE ) );
286 }
287
288 public void startAllNodeStep(int axis)
289 {
290
291 pushFrame();
292
293 push( AnyNodeTest.getInstance() );
294 }
295
296 public void startProcessingInstructionNodeStep(int axis,
297 String name)
298 {
299
300 pushFrame();
301
302
303 push( new NodeTypeTest( Pattern.PROCESSING_INSTRUCTION_NODE ) );
304 }
305
306 protected void endStep()
307 {
308 LinkedList list = popFrame();
309 if ( ! list.isEmpty() )
310 {
311 push( list.removeFirst() );
312
313 if ( ! list.isEmpty() )
314 {
315 System.out.println( "List should now be empty!" + list );
316 }
317 }
318 }
319
320
321 public void startUnionExpr()
322 {
323
324 }
325
326 public void endUnionExpr(boolean create) throws JaxenException
327 {
328
329
330 if ( create )
331 {
332
333
334 Expr rhs = (Expr) pop();
335 Expr lhs = (Expr) pop();
336
337 push( getXPathFactory().createUnionExpr( lhs,
338 rhs ) );
339 }
340 }
341
342 protected Pattern createAbsoluteLocationPath()
343 {
344 return new LocationPathPattern( NodeTypeTest.DOCUMENT_TEST );
345 }
346
347 protected Pattern createRelativeLocationPath()
348 {
349 return new LocationPathPattern();
350 }
351
352 }