1 61 62 63 package org.jaxen.pattern; 64 65 import java.util.LinkedList ; 66 67 import org.jaxen.JaxenException; 68 import org.jaxen.JaxenHandler; 69 import org.jaxen.expr.Expr; 70 import org.jaxen.expr.FilterExpr; 71 import org.jaxen.saxpath.Axis; 72 73 79 public class PatternHandler extends JaxenHandler 80 { 81 private Pattern pattern; 82 83 public PatternHandler() 84 { 85 } 86 87 96 public Pattern getPattern() 97 { 98 return getPattern( true ); 99 } 100 101 111 public Pattern getPattern(boolean shouldSimplify) 112 { 113 if ( shouldSimplify && ! this.simplified ) 114 { 115 this.pattern.simplify(); 117 this.simplified = true; 118 } 119 120 return this.pattern; 121 } 122 123 124 125 126 public void endXPath() throws JaxenException 127 { 128 this.pattern = (Pattern) pop(); 129 130 System.out.println( "stack is: " + stack ); 131 132 popFrame(); 133 } 134 135 public void endPathExpr() throws JaxenException 136 { 137 139 152 LinkedList frame = popFrame(); 153 154 System.out.println( "endPathExpr(): " + frame ); 155 156 push( frame.removeFirst() ); 157 183 } 184 185 public void startAbsoluteLocationPath() throws JaxenException 186 { 187 pushFrame(); 189 190 push( createAbsoluteLocationPath() ); 191 } 192 193 public void endAbsoluteLocationPath() throws JaxenException 194 { 195 endLocationPath(); 197 } 198 199 public void startRelativeLocationPath() throws JaxenException 200 { 201 pushFrame(); 203 204 push( createRelativeLocationPath() ); 205 } 206 207 public void endRelativeLocationPath() throws JaxenException 208 { 209 endLocationPath(); 211 } 212 213 protected void endLocationPath() throws JaxenException 214 { 215 LinkedList list = popFrame(); 217 218 System.out.println( "endLocationPath: " + list ); 219 220 LocationPathPattern locationPath = (LocationPathPattern) list.removeFirst(); 221 push( locationPath ); 222 boolean doneNodeTest = false; 223 while ( ! list.isEmpty() ) 224 { 225 Object filter = list.removeFirst(); 226 if ( filter instanceof NodeTest ) 227 { 228 if ( doneNodeTest ) 229 { 230 LocationPathPattern parent = new LocationPathPattern( (NodeTest) filter ); 231 locationPath.setParentPattern( parent ); 232 locationPath = parent; 233 doneNodeTest = false; 234 } 235 else 236 { 237 locationPath.setNodeTest( (NodeTest) filter ); 238 } 239 } 240 else if ( filter instanceof FilterExpr ) 241 { 242 locationPath.addFilter( (FilterExpr) filter ); 243 } 244 else if ( filter instanceof LocationPathPattern ) 245 { 246 LocationPathPattern parent = (LocationPathPattern) filter; 247 locationPath.setParentPattern( parent ); 248 locationPath = parent; 249 doneNodeTest = false; 250 } 251 } 252 } 253 254 255 public void startNameStep(int axis, 256 String prefix, 257 String localName) throws JaxenException 258 { 259 pushFrame(); 261 262 short nodeType = Pattern.ELEMENT_NODE; 263 switch ( axis ) 264 { 265 case Axis.ATTRIBUTE: 266 nodeType = Pattern.ATTRIBUTE_NODE; 267 break; 268 case Axis.NAMESPACE: 269 nodeType = Pattern.NAMESPACE_NODE; 270 break; 271 } 272 273 if ( prefix != null && prefix.length() > 0 && ! prefix.equals( "*" ) ) 274 { 275 push( new NamespaceTest( prefix, nodeType ) ); 276 } 277 if ( localName != null && localName.length() > 0 && ! localName.equals( "*" ) ) 278 { 279 push( new NameTest( localName, nodeType ) ); 280 } 281 } 282 283 public void startTextNodeStep(int axis) throws JaxenException 284 { 285 pushFrame(); 287 288 push( new NodeTypeTest( Pattern.TEXT_NODE ) ); 289 } 290 291 public void startCommentNodeStep(int axis) throws JaxenException 292 { 293 pushFrame(); 295 296 push( new NodeTypeTest( Pattern.COMMENT_NODE ) ); 297 } 298 299 public void startAllNodeStep(int axis) throws JaxenException 300 { 301 pushFrame(); 303 304 push( AnyNodeTest.getInstance() ); 305 } 306 307 public void startProcessingInstructionNodeStep(int axis, 308 String name) throws JaxenException 309 { 310 pushFrame(); 312 313 push( new NodeTypeTest( Pattern.PROCESSING_INSTRUCTION_NODE ) ); 315 } 316 317 protected void endStep() 318 { 319 LinkedList list = popFrame(); 320 if ( ! list.isEmpty() ) 321 { 322 push( list.removeFirst() ); 323 324 if ( ! list.isEmpty() ) 325 { 326 System.out.println( "List should now be empty!" + list ); 327 } 328 } 329 } 330 331 332 public void startUnionExpr() throws JaxenException 333 { 334 } 336 337 public void endUnionExpr(boolean create) throws JaxenException 338 { 339 341 if ( create ) 342 { 343 345 Expr rhs = (Expr) pop(); 346 Expr lhs = (Expr) pop(); 347 348 push( getXPathFactory().createUnionExpr( lhs, 349 rhs ) ); 350 } 351 } 352 353 protected Pattern createAbsoluteLocationPath() 354 { 355 return new LocationPathPattern( NodeTypeTest.DOCUMENT_TEST ); 356 } 357 358 protected Pattern createRelativeLocationPath() 359 { 360 return new LocationPathPattern(); 361 } 362 363 } 364 | Popular Tags |