1 17 package org.alfresco.repo.search.impl.lucene; 18 19 import java.util.ArrayList ; 20 21 import org.alfresco.repo.search.impl.lucene.analysis.PathTokenFilter; 22 import org.alfresco.repo.search.impl.lucene.query.AbsoluteStructuredFieldPosition; 23 import org.alfresco.repo.search.impl.lucene.query.DescendantAndSelfStructuredFieldPosition; 24 import org.alfresco.repo.search.impl.lucene.query.PathQuery; 25 import org.alfresco.repo.search.impl.lucene.query.RelativeStructuredFieldPosition; 26 import org.alfresco.repo.search.impl.lucene.query.SelfAxisStructuredFieldPosition; 27 import org.alfresco.repo.search.impl.lucene.query.StructuredFieldPosition; 28 import org.alfresco.service.cmr.dictionary.DictionaryService; 29 import org.alfresco.service.namespace.NamespacePrefixResolver; 30 import org.saxpath.Axis; 31 import org.saxpath.Operator; 32 import org.saxpath.SAXPathException; 33 import org.saxpath.XPathHandler; 34 35 public class LuceneXPathHandler implements XPathHandler 36 { 37 private PathQuery query; 38 39 private boolean isAbsolutePath = true; 40 41 int absolutePosition = 0; 42 43 private NamespacePrefixResolver namespacePrefixResolver; 44 45 private DictionaryService dictionaryService; 46 47 public LuceneXPathHandler() 48 { 49 super(); 50 } 51 52 public PathQuery getQuery() 53 { 54 return this.query; 55 } 56 57 public void endAbsoluteLocationPath() throws SAXPathException 58 { 59 } 61 62 public void endAdditiveExpr(int op) throws SAXPathException 63 { 64 switch (op) 65 { 66 case Operator.NO_OP: 67 break; 68 case Operator.ADD: 69 case Operator.SUBTRACT: 70 throw new UnsupportedOperationException (); 71 default: 72 throw new UnsupportedOperationException ("Unknown operation " + op); 73 } 74 } 75 76 public void endAllNodeStep() throws SAXPathException 77 { 78 } 81 82 public void endAndExpr(boolean create) throws SAXPathException 83 { 84 if (create) 85 { 86 throw new UnsupportedOperationException (); 87 } 88 } 89 90 public void endCommentNodeStep() throws SAXPathException 91 { 92 throw new UnsupportedOperationException (); 94 } 95 96 public void endEqualityExpr(int op) throws SAXPathException 97 { 98 switch (op) 99 { 100 case Operator.NO_OP: 101 break; 102 case Operator.EQUALS: 103 case Operator.NOT_EQUALS: 104 throw new UnsupportedOperationException (); 105 default: 106 throw new UnsupportedOperationException ("Unknown operation " + op); 107 } 108 } 109 110 public void endFilterExpr() throws SAXPathException 111 { 112 throw new UnsupportedOperationException (); 114 } 115 116 public void endFunction() throws SAXPathException 117 { 118 throw new UnsupportedOperationException (); 120 } 121 122 public void endMultiplicativeExpr(int op) throws SAXPathException 123 { 124 switch (op) 125 { 126 case Operator.NO_OP: 127 break; 128 case Operator.MULTIPLY: 129 case Operator.DIV: 130 case Operator.MOD: 131 throw new UnsupportedOperationException (); 132 default: 133 throw new UnsupportedOperationException ("Unknown operation " + op); 134 } 135 } 136 137 public void endNameStep() throws SAXPathException 138 { 139 } 142 143 public void endOrExpr(boolean create) throws SAXPathException 144 { 145 if (create) 146 { 147 throw new UnsupportedOperationException (); 148 } 149 } 150 151 public void endPathExpr() throws SAXPathException 152 { 153 } 155 156 public void endPredicate() throws SAXPathException 157 { 158 throw new UnsupportedOperationException (); 160 } 161 162 public void endProcessingInstructionNodeStep() throws SAXPathException 163 { 164 throw new UnsupportedOperationException (); 166 } 167 168 public void endRelationalExpr(int op) throws SAXPathException 169 { 170 switch (op) 171 { 172 case Operator.NO_OP: 173 break; 174 case Operator.GREATER_THAN: 175 case Operator.GREATER_THAN_EQUALS: 176 case Operator.LESS_THAN: 177 case Operator.LESS_THAN_EQUALS: 178 throw new UnsupportedOperationException (); 179 default: 180 throw new UnsupportedOperationException ("Unknown operation " + op); 181 } 182 } 183 184 public void endRelativeLocationPath() throws SAXPathException 185 { 186 } 188 189 public void endTextNodeStep() throws SAXPathException 190 { 191 throw new UnsupportedOperationException (); 193 } 194 195 public void endUnaryExpr(int op) throws SAXPathException 196 { 197 switch (op) 198 { 199 case Operator.NO_OP: 200 break; 201 case Operator.NEGATIVE: 202 throw new UnsupportedOperationException (); 203 default: 204 throw new UnsupportedOperationException ("Unknown operation " + op); 205 } 206 } 207 208 public void endUnionExpr(boolean create) throws SAXPathException 209 { 210 if (create) 211 { 212 throw new UnsupportedOperationException (); 213 } 214 } 215 216 public void endXPath() throws SAXPathException 217 { 218 } 220 221 public void literal(String arg0) throws SAXPathException 222 { 223 throw new UnsupportedOperationException (); 225 } 226 227 public void number(double arg0) throws SAXPathException 228 { 229 throw new UnsupportedOperationException (); 231 } 232 233 public void number(int arg0) throws SAXPathException 234 { 235 throw new UnsupportedOperationException (); 237 } 238 239 public void startAbsoluteLocationPath() throws SAXPathException 240 { 241 if (!isAbsolutePath) 242 { 243 throw new IllegalStateException (); 244 } 245 246 } 247 248 public void startAdditiveExpr() throws SAXPathException 249 { 250 } 252 253 public void startAllNodeStep(int axis) throws SAXPathException 254 { 255 switch (axis) 256 { 257 case Axis.CHILD: 258 if (isAbsolutePath) 259 { 260 addRelative(null, null); 263 } 264 else 265 { 266 addRelative(null, null); 267 } 268 break; 269 case Axis.DESCENDANT_OR_SELF: 270 query.appendQuery(getArrayList(new DescendantAndSelfStructuredFieldPosition(), new DescendantAndSelfStructuredFieldPosition())); 271 break; 272 case Axis.SELF: 273 query.appendQuery(getArrayList(new SelfAxisStructuredFieldPosition(), new SelfAxisStructuredFieldPosition())); 274 break; 275 default: 276 throw new UnsupportedOperationException (); 277 } 278 } 279 280 private ArrayList <StructuredFieldPosition> getArrayList(StructuredFieldPosition one, StructuredFieldPosition two) 281 { 282 ArrayList <StructuredFieldPosition> answer = new ArrayList <StructuredFieldPosition>(2); 283 answer.add(one); 284 answer.add(two); 285 return answer; 286 } 287 288 public void startAndExpr() throws SAXPathException 289 { 290 } 292 293 public void startCommentNodeStep(int arg0) throws SAXPathException 294 { 295 throw new UnsupportedOperationException (); 297 } 298 299 public void startEqualityExpr() throws SAXPathException 300 { 301 } 303 304 public void startFilterExpr() throws SAXPathException 305 { 306 throw new UnsupportedOperationException (); 308 } 309 310 public void startFunction(String arg0, String arg1) throws SAXPathException 311 { 312 throw new UnsupportedOperationException (); 314 } 315 316 public void startMultiplicativeExpr() throws SAXPathException 317 { 318 } 320 321 public void startNameStep(int axis, String nameSpace, String localName) throws SAXPathException 322 { 323 switch (axis) 324 { 325 case Axis.CHILD: 326 if (isAbsolutePath) 327 { 328 addRelative(nameSpace, localName); 331 } 332 else 333 { 334 addRelative(nameSpace, localName); 335 } 336 break; 337 default: 338 throw new UnsupportedOperationException (); 339 } 340 341 } 342 343 private void addAbsolute(String nameSpace, String localName) 344 { 345 ArrayList <StructuredFieldPosition> answer = new ArrayList <StructuredFieldPosition>(2); 346 absolutePosition++; 348 if ((nameSpace == null) || (nameSpace.length() == 0)) 349 { 350 351 if(localName.equals("*")) 352 { 353 answer.add(new RelativeStructuredFieldPosition("*")); 354 } 355 else if (namespacePrefixResolver.getNamespaceURI("") == null) 356 { 357 answer.add(new AbsoluteStructuredFieldPosition(PathTokenFilter.NO_NS_TOKEN_TEXT, absolutePosition)); 358 } 359 else 360 { 361 answer.add(new AbsoluteStructuredFieldPosition(namespacePrefixResolver.getNamespaceURI(""), absolutePosition)); 362 } 363 364 } 365 else 366 { 367 answer.add(new AbsoluteStructuredFieldPosition(namespacePrefixResolver.getNamespaceURI(nameSpace), absolutePosition)); 368 } 369 370 absolutePosition++; 371 if ((localName == null) || (localName.length() == 0)) 372 { 373 answer.add(new AbsoluteStructuredFieldPosition("*", absolutePosition)); 374 } 375 else 376 { 377 answer.add(new AbsoluteStructuredFieldPosition(localName, absolutePosition)); 378 } 379 query.appendQuery(answer); 380 381 } 382 383 private void addRelative(String nameSpace, String localName) 384 { 385 ArrayList <StructuredFieldPosition> answer = new ArrayList <StructuredFieldPosition>(2); 386 if ((nameSpace == null) || (nameSpace.length() == 0)) 387 { 388 if(localName.equals("*")) 389 { 390 answer.add(new RelativeStructuredFieldPosition("*")); 391 } 392 else if (namespacePrefixResolver.getNamespaceURI("") == null) 393 { 394 answer.add(new RelativeStructuredFieldPosition(PathTokenFilter.NO_NS_TOKEN_TEXT)); 395 } 396 else 397 { 398 answer.add(new RelativeStructuredFieldPosition(namespacePrefixResolver.getNamespaceURI(""))); 399 } 400 } 401 else 402 { 403 answer.add(new RelativeStructuredFieldPosition(namespacePrefixResolver.getNamespaceURI(nameSpace))); 404 } 405 406 if ((localName == null) || (localName.length() == 0)) 407 { 408 answer.add(new RelativeStructuredFieldPosition("*")); 409 } 410 else 411 { 412 answer.add(new RelativeStructuredFieldPosition(localName)); 413 } 414 query.appendQuery(answer); 415 } 416 417 public void startOrExpr() throws SAXPathException 418 { 419 } 421 422 public void startPathExpr() throws SAXPathException 423 { 424 } 426 427 public void startPredicate() throws SAXPathException 428 { 429 throw new UnsupportedOperationException (); 431 } 432 433 public void startProcessingInstructionNodeStep(int arg0, String arg1) throws SAXPathException 434 { 435 throw new UnsupportedOperationException (); 437 } 438 439 public void startRelationalExpr() throws SAXPathException 440 { 441 } 443 444 public void startRelativeLocationPath() throws SAXPathException 445 { 446 isAbsolutePath = false; 447 } 448 449 public void startTextNodeStep(int arg0) throws SAXPathException 450 { 451 throw new UnsupportedOperationException (); 453 } 454 455 public void startUnaryExpr() throws SAXPathException 456 { 457 } 459 460 public void startUnionExpr() throws SAXPathException 461 { 462 } 464 465 public void startXPath() throws SAXPathException 466 { 467 query = new PathQuery(dictionaryService); 468 } 469 470 public void variableReference(String uri, String localName) throws SAXPathException 471 { 472 473 } 474 475 public void setNamespacePrefixResolver(NamespacePrefixResolver namespacePrefixResolver) 476 { 477 this.namespacePrefixResolver = namespacePrefixResolver; 478 } 479 480 public void setDictionaryService(DictionaryService dictionaryService) 481 { 482 this.dictionaryService = dictionaryService; 483 } 484 485 486 487 488 } 489 | Popular Tags |