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 | |
package org.jaxen.pattern; |
49 | |
|
50 | |
import java.util.ArrayList; |
51 | |
import java.util.Iterator; |
52 | |
import java.util.List; |
53 | |
|
54 | |
import org.jaxen.Context; |
55 | |
import org.jaxen.JaxenException; |
56 | |
import org.jaxen.Navigator; |
57 | |
import org.jaxen.expr.FilterExpr; |
58 | |
import org.jaxen.util.SingletonList; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public class LocationPathPattern extends Pattern { |
69 | |
|
70 | |
|
71 | 84 | private NodeTest nodeTest = AnyNodeTest.getInstance(); |
72 | |
|
73 | |
|
74 | |
private Pattern parentPattern; |
75 | |
|
76 | |
|
77 | |
private Pattern ancestorPattern; |
78 | |
|
79 | |
|
80 | |
private List filters; |
81 | |
|
82 | |
|
83 | |
private boolean absolute; |
84 | |
|
85 | |
|
86 | |
public LocationPathPattern() |
87 | 76 | { |
88 | 76 | } |
89 | |
|
90 | |
public LocationPathPattern(NodeTest nodeTest) |
91 | 8 | { |
92 | 8 | this.nodeTest = nodeTest; |
93 | 8 | } |
94 | |
|
95 | |
public Pattern simplify() |
96 | |
{ |
97 | 84 | if ( parentPattern != null ) |
98 | |
{ |
99 | 28 | parentPattern = parentPattern.simplify(); |
100 | |
} |
101 | 84 | if ( ancestorPattern != null ) |
102 | |
{ |
103 | 4 | ancestorPattern = ancestorPattern.simplify(); |
104 | |
} |
105 | 84 | if ( filters == null ) |
106 | |
{ |
107 | 74 | if ( parentPattern == null && ancestorPattern == null ) |
108 | |
{ |
109 | 44 | return nodeTest; |
110 | |
} |
111 | 30 | if ( parentPattern != null && ancestorPattern == null ) |
112 | |
{ |
113 | 26 | if ( nodeTest instanceof AnyNodeTest ) |
114 | |
{ |
115 | 4 | return parentPattern; |
116 | |
} |
117 | |
} |
118 | |
} |
119 | 36 | return this; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
public void addFilter(FilterExpr filter) |
125 | |
{ |
126 | 10 | if ( filters == null ) |
127 | |
{ |
128 | 10 | filters = new ArrayList(); |
129 | |
} |
130 | 10 | filters.add( filter ); |
131 | 10 | } |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public void setParentPattern(Pattern parentPattern) |
137 | |
{ |
138 | 28 | this.parentPattern = parentPattern; |
139 | 28 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public void setAncestorPattern(Pattern ancestorPattern) |
145 | |
{ |
146 | 4 | this.ancestorPattern = ancestorPattern; |
147 | 4 | } |
148 | |
|
149 | |
|
150 | |
|
151 | |
public void setNodeTest(NodeTest nodeTest) throws JaxenException |
152 | |
{ |
153 | 72 | if ( this.nodeTest instanceof AnyNodeTest ) |
154 | |
{ |
155 | 72 | this.nodeTest = nodeTest; |
156 | 72 | } |
157 | |
else |
158 | |
{ |
159 | 0 | throw new JaxenException( "Attempt to overwrite nodeTest: " + this.nodeTest + " with: " + nodeTest ); |
160 | |
} |
161 | 72 | } |
162 | |
|
163 | |
|
164 | |
|
165 | |
public boolean matches( Object node, Context context ) throws JaxenException |
166 | |
{ |
167 | 0 | Navigator navigator = context.getNavigator(); |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | 0 | if (! nodeTest.matches(node, context) ) |
176 | |
{ |
177 | 0 | return false; |
178 | |
} |
179 | |
|
180 | 0 | if (parentPattern != null) |
181 | |
{ |
182 | 0 | Object parent = navigator.getParentNode( node ); |
183 | 0 | if ( parent == null ) |
184 | |
{ |
185 | 0 | return false; |
186 | |
} |
187 | 0 | if ( ! parentPattern.matches( parent, context ) ) |
188 | |
{ |
189 | 0 | return false; |
190 | |
} |
191 | |
} |
192 | |
|
193 | 0 | if (ancestorPattern != null) { |
194 | 0 | Object ancestor = navigator.getParentNode( node ); |
195 | |
while (true) |
196 | |
{ |
197 | 0 | if ( ancestorPattern.matches( ancestor, context ) ) |
198 | |
{ |
199 | 0 | break; |
200 | |
} |
201 | 0 | if ( ancestor == null ) |
202 | |
{ |
203 | 0 | return false; |
204 | |
} |
205 | 0 | if ( navigator.isDocument( ancestor ) ) |
206 | |
{ |
207 | 0 | return false; |
208 | |
} |
209 | 0 | ancestor = navigator.getParentNode( ancestor ); |
210 | 0 | } |
211 | |
} |
212 | |
|
213 | 0 | if (filters != null) |
214 | |
{ |
215 | 0 | List list = new SingletonList(node); |
216 | |
|
217 | 0 | context.setNodeSet( list ); |
218 | |
|
219 | |
|
220 | |
|
221 | 0 | boolean answer = true; |
222 | |
|
223 | 0 | for (Iterator iter = filters.iterator(); iter.hasNext(); ) |
224 | |
{ |
225 | 0 | FilterExpr filter = (FilterExpr) iter.next(); |
226 | |
|
227 | 0 | if ( ! filter.asBoolean( context ) ) |
228 | |
{ |
229 | 0 | answer = false; |
230 | 0 | break; |
231 | |
} |
232 | 0 | } |
233 | |
|
234 | |
|
235 | 0 | context.setNodeSet( list ); |
236 | |
|
237 | 0 | return answer; |
238 | |
} |
239 | 0 | return true; |
240 | |
} |
241 | |
|
242 | |
public double getPriority() |
243 | |
{ |
244 | 2 | if ( filters != null ) |
245 | |
{ |
246 | 2 | return 0.5; |
247 | |
} |
248 | 0 | return nodeTest.getPriority(); |
249 | |
} |
250 | |
|
251 | |
|
252 | |
public short getMatchType() |
253 | |
{ |
254 | 18 | return nodeTest.getMatchType(); |
255 | |
} |
256 | |
|
257 | |
public String getText() |
258 | |
{ |
259 | 0 | StringBuffer buffer = new StringBuffer(); |
260 | 0 | if ( absolute ) |
261 | |
{ |
262 | 0 | buffer.append( "/" ); |
263 | |
} |
264 | 0 | if (ancestorPattern != null) |
265 | |
{ |
266 | 0 | String text = ancestorPattern.getText(); |
267 | 0 | if ( text.length() > 0 ) |
268 | |
{ |
269 | 0 | buffer.append( text ); |
270 | 0 | buffer.append( "//" ); |
271 | |
} |
272 | |
} |
273 | 0 | if (parentPattern != null) |
274 | |
{ |
275 | 0 | String text = parentPattern.getText(); |
276 | 0 | if ( text.length() > 0 ) |
277 | |
{ |
278 | 0 | buffer.append( text ); |
279 | 0 | buffer.append( "/" ); |
280 | |
} |
281 | |
} |
282 | 0 | buffer.append( nodeTest.getText() ); |
283 | |
|
284 | 0 | if ( filters != null ) |
285 | |
{ |
286 | 0 | buffer.append( "[" ); |
287 | 0 | for (Iterator iter = filters.iterator(); iter.hasNext(); ) |
288 | |
{ |
289 | 0 | FilterExpr filter = (FilterExpr) iter.next(); |
290 | 0 | buffer.append( filter.getText() ); |
291 | 0 | } |
292 | 0 | buffer.append( "]" ); |
293 | |
} |
294 | 0 | return buffer.toString(); |
295 | |
} |
296 | |
|
297 | |
public String toString() |
298 | |
{ |
299 | 0 | return super.toString() + "[ absolute: " + absolute + " parent: " + parentPattern + " ancestor: " |
300 | |
+ ancestorPattern + " filters: " + filters + " nodeTest: " |
301 | |
+ nodeTest + " ]"; |
302 | |
} |
303 | |
|
304 | |
public boolean isAbsolute() |
305 | |
{ |
306 | 0 | return absolute; |
307 | |
} |
308 | |
|
309 | |
public void setAbsolute(boolean absolute) |
310 | |
{ |
311 | 0 | this.absolute = absolute; |
312 | 0 | } |
313 | |
|
314 | |
public boolean hasAnyNodeTest() |
315 | |
{ |
316 | 0 | return nodeTest instanceof AnyNodeTest; |
317 | |
} |
318 | |
|
319 | |
} |