1 22 23 24 package org.xquark.xquery.metadata.resolver; 25 26 import java.util.*; 27 28 import org.xquark.xpath.Axis; 29 import org.xquark.xpath.TypedXTreeNode; 30 import org.xquark.xpath.XTree; 31 import org.xquark.xquery.parser.*; 32 import org.xquark.xquery.parser.primitivefunctions.fnfunctions.FunctionCOLLECTION; 33 import org.xquark.xquery.parser.primitivefunctions.fnfunctions.FunctionDOC; 34 35 39 89 public class PathResolver { 90 private static final String RCSRevision = "$Revision: 1.10 $"; 91 private static final String RCSName = "$Name: $"; 92 public final static String COLON = ":"; 100 public final static String STAR = "*"; 101 102 108 private MetadataAccess metadata = null; 109 110 113 private ArrayList resolvers = new ArrayList(); 114 115 private String path = null; 117 private ArrayList restrictedCollection = new ArrayList(1); 118 private ArrayList restrictedSource = new ArrayList(1); 119 private String restrictedDocument = null; 120 121 private Set sourceNames = new HashSet(); 122 private Set urls = new HashSet(); 123 private ArrayList nodes = new ArrayList(1); 124 128 public PathResolver(MetadataAccess metadata) { 129 this.metadata = metadata; 130 } 131 132 private void clear() { 133 resolvers.clear(); 134 sourceNames = new HashSet(); 135 urls = new HashSet(); 136 nodes = new ArrayList(1); 137 } 138 139 public void resolveSteps(InputFunctionCall expr) throws XQueryException { 141 if (expr == null) 142 return; 143 clear(); 144 extractRestriction(expr); 145 Path p = null; 146 p = new Path(expr, metadata, restrictedSource, restrictedCollection, restrictedDocument); 150 p.init(); 151 resolvers.add(p); 152 } 153 154 public void resolveSteps(ArrayList steps) throws XQueryException { 155 if (steps == null || steps.isEmpty()) 156 return; 157 clear(); 158 extractRestriction((org.xquark.xquery.parser.Step) steps.get(0)); 159 Path p = null; 160 p = new Path(steps, metadata, restrictedSource, restrictedCollection, restrictedDocument); 164 p.init(); 165 resolvers.add(p); 166 } 167 168 public void resolveStepsList(ArrayList stepslist) throws XQueryException { 169 if (stepslist == null || stepslist.isEmpty()) 170 return; 171 clear(); 172 for (int i = 0; i < stepslist.size(); i++) { 173 ArrayList steps = (ArrayList) stepslist.get(i); 174 if (steps == null || steps.isEmpty()) 175 continue; 176 extractRestriction((org.xquark.xquery.parser.Step) steps.get(0)); 177 Path p = null; 178 p = new Path(steps, metadata, restrictedSource, restrictedCollection, restrictedDocument); 182 p.init(); 183 resolvers.add(p); 184 } 185 } 186 187 private void extractRestriction(org.xquark.xquery.parser.XQueryExpression expr) { 189 restrictedCollection.clear(); 190 restrictedSource.clear(); 191 restrictedDocument = null;; 192 if (expr instanceof FunctionCOLLECTION) { 193 FunctionCOLLECTION funCol = (FunctionCOLLECTION)expr; 194 restrictedCollection.add(funCol.getCollectionName()); 195 restrictedSource.add(funCol.getSourceName()); 196 } else if (expr instanceof FunctionDOC) { 197 restrictedDocument = ((FunctionCall) expr).getArgument(0).getStringValue(); 198 } 199 } 200 201 private void extractRestriction(org.xquark.xquery.parser.Step step) { 202 extractRestriction(step.getExpression()); 203 } 204 205 208 212 227 232 public Set getSourceNames() { 233 if (!sourceNames.isEmpty()) return sourceNames; 234 for (int i = 0; i < resolvers.size(); i++) { 235 ArrayList sources = ((Path) resolvers.get(i)).getSourceNames(); 236 if (sources == null) 237 continue; 238 for (int j = 0; j < sources.size(); j++) { 239 String sourcename = (String ) sources.get(j); 240 if (!sourceNames.contains(sourcename)) 241 sourceNames.add(sourcename); 242 } 243 } 244 return sourceNames; 245 } 246 247 public Set getUrls() { 248 if (!urls.isEmpty()) return urls; 249 for (int i = 0; i < resolvers.size(); i++) { 250 String tmpstr = ((Path) resolvers.get(i)).getUrl(); 251 if (tmpstr == null) 252 continue; 253 if (!urls.contains(tmpstr)) 254 urls.add(tmpstr); 255 } 256 return urls; 257 } 258 273 287 297 304 private Path getPathByPathName(String pathname) { 305 for (int i = 0; i < resolvers.size(); i++) { 306 Path pi = (Path) resolvers.get(i); 307 if (pathname.equals(pi.getPathName())) 308 return pi; 309 } 310 return null; 311 } 312 313 318 public ArrayList getNodes() { 319 if (!nodes.isEmpty()) return nodes; 320 for (int i = 0; i < resolvers.size(); i++) { 321 ArrayList elementnodes = ((Path) resolvers.get(i)).getNodes(); 322 if (elementnodes != null) 323 for (int j = 0; j < elementnodes.size(); j++) { 324 TypedXTreeNode nodej = (TypedXTreeNode) elementnodes.get(j); 325 nodes.add(nodej); 329 } 330 } 331 return nodes; 332 } 333 334 338 342 protected void finalize() throws Throwable { 343 super.finalize(); 344 metadata = null; 345 resolvers = null; 346 } 347 } 348 349 350 351 357 class Path { 358 private static final String RCSRevision = "$Revision: 1.10 $"; 359 private static final String RCSName = "$Name: $"; 360 private String pathname = null; 361 private String fullpathname = null; 362 private ArrayList sources = null; 363 private MetadataAccess metadata = null; 364 private ArrayList steps = null; 366 367 private ArrayList restrictedSource = null; 368 private ArrayList restrictedCollection = null; 369 private String restrictedDocument = null; 370 private ArrayList nodes = null; 371 372 380 381 public Path(ArrayList steps, MetadataAccess metadata, ArrayList restrictedSource, ArrayList restrictedCollection, String restrictedDocument) throws XQueryException { 382 this.steps = (ArrayList)steps.clone(); 384 fullpathname = printSteps(steps, 0); 385 if (((org.xquark.xquery.parser.Step) steps.get(0)).getExpression() instanceof FunctionCOLLECTION) { 386 pathname = printSteps(steps, 1); 387 if (this.steps.size() == 1) 388 this.steps = null; 389 else 390 this.steps.remove(0); 391 } 392 else { 393 pathname = fullpathname; 394 } 395 this.metadata = metadata; 398 if (restrictedSource.isEmpty()) 399 restrictedSource = null; 400 this.restrictedSource = restrictedSource; 401 if (restrictedCollection.isEmpty()) 402 restrictedCollection = null; 403 this.restrictedCollection = restrictedCollection; 404 this.restrictedDocument = restrictedDocument; 405 } 408 409 public Path(InputFunctionCall expr, MetadataAccess metadata, ArrayList restrictedSource, ArrayList restrictedCollection, String restrictedDocument) throws XQueryException { 410 fullpathname = expr.toString(); 412 pathname = ""; 413 this.metadata = metadata; 416 if (restrictedSource.isEmpty()) 417 restrictedSource = null; 418 this.restrictedSource = restrictedSource; 419 if (restrictedCollection.isEmpty()) 420 restrictedCollection = null; 421 this.restrictedCollection = restrictedCollection; 422 this.restrictedDocument = restrictedDocument; 423 } 425 426 427 private String printSteps(ArrayList steps, int minIndex) { 428 StringBuffer buf = new StringBuffer (); 429 for (int i = minIndex; i < steps.size(); i++) { 430 org.xquark.xquery.parser.Step stepi = (org.xquark.xquery.parser.Step) steps.get(i); 431 boolean separator = stepi.hasSeparator(); 432 int axis = stepi.getAxis(); 433 if (separator) 434 buf.append('/'); 435 if (axis != -1) 436 buf.append(Axis.AXISSTRINGS[axis]); 437 buf.append(stepi.getExpression().toString()); 438 } 439 return buf.toString(); 440 } 441 442 446 public void init() { 447 if (restrictedDocument != null) 448 return; 449 450 Collection sourceCollection = metadata.getSources(); 451 Iterator sourceIterator = sourceCollection.iterator(); 452 while (sourceIterator.hasNext()) { 453 SourceMetadata sourceMetadata = (SourceMetadata) sourceIterator.next(); 454 String sourcename = sourceMetadata.getSourceName(); 455 456 if (restrictedSource != null) 457 if ((!restrictedSource.contains(PathResolver.STAR)) && (!restrictedSource.contains(sourcename))) { 458 continue; 459 } 460 461 Collection collections = sourceMetadata.getCollectionsMetadata(); 462 Iterator collectionsIterator = collections.iterator(); 463 464 Source source = new Source(sourcename); 465 boolean sourceIsValid = false; 466 while (collectionsIterator.hasNext()) { 467 CollectionMetadata collectionMetadata = (CollectionMetadata) collectionsIterator.next(); 468 String colname = collectionMetadata.getCollectionName(); 469 470 if (restrictedCollection != null) { 471 boolean found = false; 472 for (int r = 0; r < restrictedCollection.size(); r++) { 473 String str = (String ) restrictedCollection.get(r); 474 if (!str.equals(PathResolver.STAR)) { 475 if ((sourceMetadata.isCaseSensitive() && !str.equals(colname) && !colname.endsWith("." + str))) { 476 continue; 477 } 478 if (!sourceMetadata.isCaseSensitive()) { 479 if (str.equalsIgnoreCase(colname)) { 480 481 } else { 482 int startIndex = colname.length() - str.length() - 1; 483 if (0 > startIndex) { 484 continue; 485 } 486 String suffix = colname.substring(startIndex, colname.length()); 487 if (!suffix.equalsIgnoreCase("." + str)) { 488 continue; 489 } 490 } 491 } 492 } 493 494 String sname = (String ) restrictedSource.get(r); 495 if ((!sname.equals(PathResolver.STAR)) && ((!sname.equals(sourcename)))) 496 continue; 497 498 found = true; 499 break; 500 } 501 if (!found) 502 continue; 503 } 504 505 XTree schemaTree = collectionMetadata.getXTree(); 506 507 if (pathname.equals("")) { 508 Collection c = schemaTree.getRoot().getChildren(); 510 if (c == null) 511 continue; 512 Object [] sn = c.toArray(); 513 TypedXTreeNode tmpNode = null; 514 for (int l = 0; l < sn.length; l++) { 515 tmpNode = (TypedXTreeNode) sn[l]; 516 source.add(colname, tmpNode); 518 } 519 sourceIsValid = true; 520 } else { 521 525 if (steps == null) 526 continue; 527 Collection set = schemaTree.prune(steps); 529 if (set.size() > 0) { 530 source.add(colname, set); 531 sourceIsValid = true; 532 } 533 } 534 535 } 536 if (sourceIsValid == true) { 537 if (sources == null) 538 sources = new ArrayList(); 539 sources.add(source); 540 } 541 } 542 } 543 544 549 public String getPathName() { 550 return pathname; 551 } 552 553 558 public ArrayList getSourceNames() { 559 if (sources == null) 560 return null; 561 ArrayList v = new ArrayList(sources.size()); 562 for (int i = 0; i < sources.size(); i++) { 563 String sourcename = ((Source) sources.get(i)).getSourceName(); 564 v.add(sourcename); 565 } 566 return v; 567 } 568 569 572 public String getUrl() { 573 return restrictedDocument; 574 } 575 576 581 public ArrayList getNodes() { 582 if (nodes != null) return nodes; 583 if (sources == null) 584 return null; 585 nodes = new ArrayList(); 586 for (int i = 0; i < sources.size(); i++) { 587 ArrayList elementpaths = ((Source) sources.get(i)).getNodes(); 588 if (elementpaths != null) 589 for (int j = 0; j < elementpaths.size(); j++) { 590 TypedXTreeNode nodej = (TypedXTreeNode) elementpaths.get(j); 591 nodes.add(nodej); 592 } 593 } 594 return nodes; 595 } 596 597 611 633 649 671 } 687 688 689 690 694 class Source { 695 private static final String RCSRevision = "$Revision: 1.10 $"; 696 private static final String RCSName = "$Name: $"; 697 private String sourcename; 698 699 703 private Hashtable elementpaths; 704 705 710 public Source(String sourcename) { 711 this.sourcename = sourcename; 712 elementpaths = new Hashtable(); 713 } 714 715 718 public Hashtable getElementPaths() { 719 return elementpaths; 720 } 721 722 727 public void add(String colname, Collection nodes 728 ) { 729 for (Iterator iterator = nodes.iterator(); iterator.hasNext();) { 730 TypedXTreeNode node = (TypedXTreeNode) iterator.next(); 731 732 ArrayList roots = (ArrayList) elementpaths.get(colname); 733 if (roots == null) { 734 roots = new ArrayList(); 735 elementpaths.put(colname, roots); 736 } 737 roots.add(node); 739 } 740 } 741 742 public void add(String colname, TypedXTreeNode node 743 ) { 744 ArrayList roots = null; 745 if ((roots = (ArrayList) elementpaths.get(colname)) == null) { 746 roots = new ArrayList(); 747 elementpaths.put(colname, roots); 748 } 749 roots.add(node); 751 } 752 753 758 public String getSourceName() { 759 return sourcename; 760 } 761 762 769 public ArrayList getNodes() { 770 ArrayList v = new ArrayList(); 771 for (Enumeration e = elementpaths.elements(); e.hasMoreElements();) { 772 ArrayList vep = (ArrayList) e.nextElement(); 773 774 for (int i = 0; i < vep.size(); i++) { 775 v.add(vep.get(i)); 778 } 779 } 780 return v; 781 } 782 783 788 public ArrayList getNodesByCollection(String colname) { 789 ArrayList v = new ArrayList(); 790 ArrayList vep = (ArrayList) elementpaths.get(colname); 791 if (vep == null) 792 return v; 793 794 for (int i = 0; i < vep.size(); i++) { 795 v.add(vep.get(i)); 798 } 799 return v; 800 } 801 802 807 public ArrayList getColname() { 808 ArrayList v = new ArrayList(); 809 for (Enumeration e = elementpaths.keys(); e.hasMoreElements();) { 810 String colname = (String ) e.nextElement(); 811 v.add(colname); 812 } 813 return v; 814 } 815 } 816 817 | Popular Tags |