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.Iterator; |
52 | |
import java.util.List; |
53 | |
import java.util.ListIterator; |
54 | |
|
55 | |
import org.jaxen.JaxenException; |
56 | |
import org.jaxen.JaxenHandler; |
57 | |
import org.jaxen.expr.DefaultAllNodeStep; |
58 | |
import org.jaxen.expr.DefaultCommentNodeStep; |
59 | |
import org.jaxen.expr.DefaultFilterExpr; |
60 | |
import org.jaxen.expr.DefaultNameStep; |
61 | |
import org.jaxen.expr.DefaultProcessingInstructionNodeStep; |
62 | |
import org.jaxen.expr.DefaultStep; |
63 | |
import org.jaxen.expr.DefaultTextNodeStep; |
64 | |
import org.jaxen.expr.DefaultXPathFactory; |
65 | |
import org.jaxen.expr.Expr; |
66 | |
import org.jaxen.expr.FilterExpr; |
67 | |
import org.jaxen.expr.LocationPath; |
68 | |
import org.jaxen.expr.Predicate; |
69 | |
import org.jaxen.expr.PredicateSet; |
70 | |
import org.jaxen.expr.Step; |
71 | |
import org.jaxen.expr.UnionExpr; |
72 | |
import org.jaxen.saxpath.Axis; |
73 | |
import org.jaxen.saxpath.XPathReader; |
74 | |
import org.jaxen.saxpath.helpers.XPathReaderFactory; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | 0 | public class PatternParser |
83 | |
{ |
84 | |
private static final boolean TRACE = false; |
85 | |
private static final boolean USE_HANDLER = false; |
86 | |
public static Pattern parse(String text) throws JaxenException, org.jaxen.saxpath.SAXPathException |
87 | |
{ |
88 | |
if ( USE_HANDLER ) |
89 | |
{ |
90 | |
XPathReader reader = XPathReaderFactory.createReader(); |
91 | |
PatternHandler handler = new PatternHandler(); |
92 | |
|
93 | |
handler.setXPathFactory( new DefaultXPathFactory() ); |
94 | |
reader.setXPathHandler( handler ); |
95 | |
reader.parse( text ); |
96 | |
|
97 | |
return handler.getPattern(); |
98 | |
} |
99 | |
else |
100 | |
{ |
101 | 46 | XPathReader reader = XPathReaderFactory.createReader(); |
102 | 46 | JaxenHandler handler = new JaxenHandler(); |
103 | |
|
104 | 46 | handler.setXPathFactory( new DefaultXPathFactory() ); |
105 | 46 | reader.setXPathHandler( handler ); |
106 | 46 | reader.parse( text ); |
107 | |
|
108 | 46 | Pattern pattern = convertExpr( handler.getXPathExpr().getRootExpr() ); |
109 | 46 | return pattern.simplify(); |
110 | |
} |
111 | |
} |
112 | |
|
113 | |
protected static Pattern convertExpr(Expr expr) throws JaxenException |
114 | |
{ |
115 | |
if ( TRACE ) |
116 | |
{ |
117 | |
System.out.println( "Converting: " + expr + " into a pattern." ); |
118 | |
} |
119 | |
|
120 | 58 | if ( expr instanceof LocationPath ) |
121 | |
{ |
122 | 52 | return convertExpr( (LocationPath) expr ); |
123 | |
} |
124 | 6 | else if ( expr instanceof FilterExpr ) |
125 | |
{ |
126 | 0 | LocationPathPattern answer = new LocationPathPattern(); |
127 | 0 | answer.addFilter( (FilterExpr) expr ); |
128 | 0 | return answer; |
129 | |
} |
130 | 6 | else if ( expr instanceof UnionExpr ) |
131 | |
{ |
132 | 6 | UnionExpr unionExpr = (UnionExpr) expr; |
133 | 6 | Pattern lhs = convertExpr( unionExpr.getLHS() ); |
134 | 6 | Pattern rhs = convertExpr( unionExpr.getRHS() ); |
135 | 6 | return new UnionPattern( lhs, rhs ); |
136 | |
} |
137 | |
else |
138 | |
{ |
139 | 0 | LocationPathPattern answer = new LocationPathPattern(); |
140 | 0 | answer.addFilter( new DefaultFilterExpr( expr, |
141 | |
new PredicateSet()) ); |
142 | 0 | return answer; |
143 | |
} |
144 | |
} |
145 | |
|
146 | |
protected static LocationPathPattern convertExpr(LocationPath locationPath) throws JaxenException |
147 | |
{ |
148 | 52 | LocationPathPattern answer = new LocationPathPattern(); |
149 | |
|
150 | 52 | List steps = locationPath.getSteps(); |
151 | |
|
152 | |
|
153 | 52 | LocationPathPattern path = answer; |
154 | 52 | boolean first = true; |
155 | 52 | for ( ListIterator iter = steps.listIterator( steps.size() ); iter.hasPrevious(); ) |
156 | |
{ |
157 | 72 | Step step = (Step) iter.previous(); |
158 | 72 | if ( first ) |
159 | |
{ |
160 | 48 | first = false; |
161 | 48 | path = convertStep( path, step ); |
162 | 48 | } |
163 | |
else |
164 | |
{ |
165 | 24 | if ( navigationStep( step ) ) |
166 | |
{ |
167 | 24 | LocationPathPattern parent = new LocationPathPattern(); |
168 | 24 | int axis = step.getAxis(); |
169 | 24 | if ( axis == Axis.DESCENDANT || axis == Axis.DESCENDANT_OR_SELF ) |
170 | |
{ |
171 | 4 | path.setAncestorPattern( parent ); |
172 | 4 | } |
173 | |
else |
174 | |
{ |
175 | 20 | path.setParentPattern( parent ); |
176 | |
} |
177 | 24 | path = parent; |
178 | |
} |
179 | 24 | path = convertStep( path, step ); |
180 | |
} |
181 | 72 | } |
182 | 52 | if ( locationPath.isAbsolute() ) |
183 | |
{ |
184 | 8 | LocationPathPattern parent = new LocationPathPattern( NodeTypeTest.DOCUMENT_TEST ); |
185 | 8 | path.setParentPattern( parent ); |
186 | |
} |
187 | 52 | return answer; |
188 | |
} |
189 | |
|
190 | |
protected static LocationPathPattern convertStep(LocationPathPattern path, Step step) throws JaxenException |
191 | |
{ |
192 | 72 | if ( step instanceof DefaultAllNodeStep ) |
193 | |
{ |
194 | 4 | int axis = step.getAxis(); |
195 | 4 | if ( axis == Axis.ATTRIBUTE ) |
196 | |
{ |
197 | 0 | path.setNodeTest( NodeTypeTest.ATTRIBUTE_TEST ); |
198 | 0 | } |
199 | |
else |
200 | |
{ |
201 | 4 | path.setNodeTest( NodeTypeTest.ELEMENT_TEST ); |
202 | |
} |
203 | 4 | } |
204 | 68 | else if ( step instanceof DefaultCommentNodeStep ) |
205 | |
{ |
206 | 0 | path.setNodeTest( NodeTypeTest.COMMENT_TEST ); |
207 | 0 | } |
208 | 68 | else if ( step instanceof DefaultProcessingInstructionNodeStep ) |
209 | |
{ |
210 | 0 | path.setNodeTest( NodeTypeTest.PROCESSING_INSTRUCTION_TEST ); |
211 | 0 | } |
212 | 68 | else if ( step instanceof DefaultTextNodeStep ) |
213 | |
{ |
214 | 4 | path.setNodeTest( TextNodeTest.SINGLETON ); |
215 | 4 | } |
216 | 64 | else if ( step instanceof DefaultCommentNodeStep ) |
217 | |
{ |
218 | 0 | path.setNodeTest( NodeTypeTest.COMMENT_TEST ); |
219 | 0 | } |
220 | 64 | else if ( step instanceof DefaultNameStep ) |
221 | |
{ |
222 | 64 | DefaultNameStep nameStep = (DefaultNameStep) step; |
223 | 64 | String localName = nameStep.getLocalName(); |
224 | 64 | String prefix = nameStep.getPrefix(); |
225 | 64 | int axis = nameStep.getAxis(); |
226 | 64 | short nodeType = Pattern.ELEMENT_NODE; |
227 | 64 | if ( axis == Axis.ATTRIBUTE ) |
228 | |
{ |
229 | 4 | nodeType = Pattern.ATTRIBUTE_NODE; |
230 | |
} |
231 | 64 | if ( nameStep.isMatchesAnyName() ) |
232 | |
{ |
233 | 16 | if ( prefix.length() == 0 || prefix.equals( "*" ) ) |
234 | |
{ |
235 | 14 | if ( axis == Axis.ATTRIBUTE ) |
236 | |
{ |
237 | 4 | path.setNodeTest( NodeTypeTest.ATTRIBUTE_TEST ); |
238 | 4 | } |
239 | |
else |
240 | |
{ |
241 | 10 | path.setNodeTest( NodeTypeTest.ELEMENT_TEST ); |
242 | |
} |
243 | 10 | } |
244 | |
else |
245 | |
{ |
246 | 2 | path.setNodeTest( new NamespaceTest( prefix, nodeType ) ); |
247 | |
} |
248 | 2 | } |
249 | |
else |
250 | |
{ |
251 | 48 | path.setNodeTest( new NameTest( localName, nodeType ) ); |
252 | |
|
253 | |
} |
254 | 64 | return convertDefaultStep(path, nameStep); |
255 | |
} |
256 | 0 | else if ( step instanceof DefaultStep ) |
257 | |
{ |
258 | 0 | return convertDefaultStep(path, (DefaultStep) step); |
259 | |
} |
260 | |
else |
261 | |
{ |
262 | 0 | throw new JaxenException( "Cannot convert: " + step + " to a Pattern" ); |
263 | |
} |
264 | 8 | return path; |
265 | |
} |
266 | |
|
267 | |
protected static LocationPathPattern convertDefaultStep(LocationPathPattern path, DefaultStep step) throws JaxenException |
268 | |
{ |
269 | 64 | List predicates = step.getPredicates(); |
270 | 64 | if ( ! predicates.isEmpty() ) |
271 | |
{ |
272 | 10 | FilterExpr filter = new DefaultFilterExpr(new PredicateSet()); |
273 | 10 | for ( Iterator iter = predicates.iterator(); iter.hasNext(); ) |
274 | |
{ |
275 | 10 | filter.addPredicate( (Predicate) iter.next() ); |
276 | 10 | } |
277 | 10 | path.addFilter( filter ); |
278 | |
} |
279 | 64 | return path; |
280 | |
} |
281 | |
|
282 | |
protected static boolean navigationStep( Step step ) |
283 | |
{ |
284 | 24 | if ( step instanceof DefaultNameStep ) |
285 | |
{ |
286 | 20 | return true; |
287 | |
} |
288 | |
else |
289 | 6 | if ( step.getClass().equals( DefaultStep.class ) ) |
290 | |
{ |
291 | 0 | return ! step.getPredicates().isEmpty(); |
292 | |
} |
293 | |
else |
294 | |
{ |
295 | 4 | return true; |
296 | |
} |
297 | |
} |
298 | |
|
299 | |
} |
300 | |
|