Changeset 4260
- Timestamp:
- 2003-05-08T16:21:21+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2Retrieve.java
r4017 r4260 33 33 import java.util.StringTokenizer; 34 34 import java.util.Vector; 35 35 import java.util.Set; 36 import java.util.Iterator; 36 37 37 38 /** Implements the generic retrieval and classifier services for GS2 … … 57 58 private static final String CLASSIFIER_METADATA_SERVICE = "ClassifierBrowseMetadataRetrieve"; 58 59 60 protected static final String STRUCT_PARAM = "structure"; 61 protected static final String INFO_PARAM = "info"; 62 63 protected static final String STRUCT_ANCESTORS = "ancestors"; 64 protected static final String STRUCT_PARENT = "parent"; 65 protected static final String STRUCT_SIBS = "siblings"; 66 protected static final String STRUCT_CHILDREN = "children"; 67 protected static final String STRUCT_DESCENDS = "descendants"; 68 69 protected static final String INFO_NUM_SIBS = "numSiblings"; 70 protected static final String INFO_NUM_CHILDREN = "numChildren"; 71 protected static final String INFO_SIB_POS = "siblingPosition"; 72 59 73 protected static final int DOCUMENT=1; 60 74 protected static final int CLASSIFIER=2; … … 297 311 } else { 298 312 node = doc_.createElement(GSXML.DOC_NODE_ELEM); 299 if ( !child &&OID.isTop(doc_id)) {313 if (/*!child &&*/ OID.isTop(doc_id)) { 300 314 node.setAttribute(GSXML.NODE_TYPE_ATT, GSXML.NODE_TYPE_ROOT); 301 315 } else if (!parent && isLeafNode(doc_id)) { … … 417 431 } 418 432 433 // the type of info required 434 boolean want_structure = false; 435 boolean want_info = false; 436 437 Vector info_types=new Vector(); 419 438 // The document structure information desired 420 boolean want Ancestors = false;421 boolean want Parent = false;422 boolean want Siblings = false;423 boolean want Children = false;424 boolean want Descendants = false;439 boolean want_ancestors = false; 440 boolean want_parent = false; 441 boolean want_siblings = false; 442 boolean want_children = false; 443 boolean want_descendants = false; 425 444 426 445 // Process the request parameters 427 Element param = (Element) param_list.getFirstChild();428 while (param != null) {429 if (!param.getNodeName().equals(GSXML.PARAM_ELEM)) {430 System.err.println("Warning: Non-param in paramList (ignored).");431 }432 else {433 434 if (param.getAttribute(GSXML.NAME_ATT).equals("structure")) {435 String structure = GSXML.getValue(param);436 437 438 if (structure.compareToIgnoreCase("ancestors") == 0)439 wantAncestors = true;440 else if (structure.compareToIgnoreCase("parent") == 0)441 wantParent = true;442 else if (structure.compareToIgnoreCase("siblings") == 0)443 wantSiblings = true;444 else if (structure.compareToIgnoreCase("children") == 0)445 wantChildren = true;446 else if (structure.compareToIgnoreCase("descendants") == 0)447 wantDescendants = true;448 449 System.err.println("Warning: Unknown value \"" + structure + "\".");450 }451 }452 453 param = (Element) param.getNextSibling();446 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM); 447 for (int i=0; i<params.getLength();i++) { 448 449 Element param = (Element)params.item(i); 450 String p_name = param.getAttribute(GSXML.NAME_ATT); 451 String p_value = GSXML.getValue(param); 452 // Identify the structure information desired 453 if (p_name.equals(STRUCT_PARAM)) { 454 want_structure = true; 455 456 // This is NOT locale sensitive 457 if (p_value.equals(STRUCT_ANCESTORS)) 458 want_ancestors = true; 459 else if (p_value.equals(STRUCT_PARENT)) 460 want_parent = true; 461 else if (p_value.equals(STRUCT_SIBS)) 462 want_siblings = true; 463 else if (p_value.equals(STRUCT_CHILDREN)) 464 want_children = true; 465 else if (p_value.equals(STRUCT_DESCENDS)) 466 want_descendants = true; 467 else 468 System.err.println("Warning: Unknown value \"" + p_value + "\"."); 469 } else if (p_name.equals(INFO_PARAM)) { 470 want_info = true; 471 info_types.add(p_value); 472 } 454 473 } 455 474 456 475 // Make sure there is no repeated information 457 if (want Ancestors)458 want Parent = false;459 if (want Descendants)460 want Children = false;476 if (want_ancestors) 477 want_parent = false; 478 if (want_descendants) 479 want_children = false; 461 480 462 481 … … 476 495 String doc_id = doc_ids[i]; 477 496 478 497 System.out.println("doc_id = "+doc_id); 479 498 if (OID.needsTranslating(doc_id)) { 480 499 doc_id = gdbm_src_.translateOID(doc_id); 500 System.out.println("translated doc_id = "+doc_id); 481 501 } 482 502 … … 485 505 doc_list.appendChild(doc); 486 506 doc.setAttribute(GSXML.NODE_ID_ATT, doc_id); 487 // all structure info goes into a docNodestructure elem 488 // classifier elem should be something else??? 489 Element structure_elem = doc_.createElement(GSXML.NODE_STRUCTURE_ELEM); 490 doc.appendChild(structure_elem); 491 492 // Add the requested structure information 493 Element current = createDocNode(doc_id, false, false); 494 495 //Ancestors: continually add parent nodes until the root is reached 496 Element top_node = current; // the top node so far 497 if (wantAncestors) { 498 String current_id = doc_id; 499 while (true) { 500 Element parent = getParent(current_id); 501 if (parent == null) 502 break; 503 504 parent.appendChild(top_node); 505 current_id = parent.getAttribute(GSXML.NODE_ID_ATT); 506 top_node = parent; 507 } 508 } 509 // Parent: get the parent of the selected node 510 if (wantParent) { 511 Element parent = getParent(doc_id); 512 if (parent != null) { 513 parent.appendChild(current); 514 top_node = parent; 515 } 516 } 517 518 // now the top node is the root of the structure 519 structure_elem.appendChild(top_node); 520 521 //Siblings: get the other descendants of the selected node's parent 522 if (wantSiblings) { 523 Element parent = (Element)current.getParentNode(); // this may be the structure element if there has been no request for parents or ancestors 524 String parent_id = OID.getParent(doc_id); 525 526 // add siblings, - returns a pointer to the new current node 527 current = addSiblings(parent, parent_id, doc_id); 528 } 529 507 508 509 if (want_info) { 510 511 Element info_elem = doc_.createElement("nodeStructureInfo"); 512 doc.appendChild(info_elem); 513 514 for (int j=0; j<info_types.size(); j++) { 515 String info_type = (String)info_types.get(j); 516 Element inf = getInfo(doc_id, info_type); 517 if (inf != null) { 518 info_elem.appendChild(inf); 519 } 520 } 521 } 522 if (want_structure) { 523 // all structure info goes into a nodeStructure elem 524 Element structure_elem = doc_.createElement(GSXML.NODE_STRUCTURE_ELEM); 525 doc.appendChild(structure_elem); 526 527 // Add the requested structure information 528 Element current = createDocNode(doc_id, false, false); 529 530 //Ancestors: continually add parent nodes until the root is reached 531 Element top_node = current; // the top node so far 532 if (want_ancestors) { 533 String current_id = doc_id; 534 while (true) { 535 Element parent = getParent(current_id); 536 if (parent == null) 537 break; 538 539 parent.appendChild(top_node); 540 current_id = parent.getAttribute(GSXML.NODE_ID_ATT); 541 top_node = parent; 542 } 543 } 544 // Parent: get the parent of the selected node 545 if (want_parent) { 546 Element parent = getParent(doc_id); 547 if (parent != null) { 548 parent.appendChild(current); 549 top_node = parent; 550 } 551 } 552 553 554 // now the top node is the root of the structure 555 structure_elem.appendChild(top_node); 556 557 //Siblings: get the other descendants of the selected node's parent 558 if (want_siblings) { 559 Element parent = (Element)current.getParentNode(); // this may be the structure element if there has been no request for parents or ancestors 560 String parent_id = OID.getParent(doc_id); 561 562 // add siblings, - returns a pointer to the new current node 563 current = addSiblings(parent, parent_id, doc_id); 564 } 565 530 566 // Children: get the descendants, but only one level deep 531 if (wantChildren) 532 addDescendants(current, doc_id, false); 533 // Descendants: recursively get every descendant of the selected node 534 if (wantDescendants) 535 addDescendants(current, doc_id, true); 536 } 567 if (want_children) 568 addDescendants(current, doc_id, false); 569 // Descendants: recursively get every descendant of the selected node 570 if (want_descendants) 571 addDescendants(current, doc_id, true); 572 } // if want structure 573 } // for each doc 537 574 return result; 538 575 } … … 576 613 // The metadata information required 577 614 Vector metadata_list = new Vector(); 578 615 boolean all_metadata = false; 579 616 // Process the request parameters 580 617 Element param = (Element) param_list.getFirstChild(); … … 583 620 if (param.getAttribute(GSXML.NAME_ATT).equals("metadata")) { 584 621 String metadata = GSXML.getValue(param); 622 if (metadata.equals("all")) { 623 all_metadata = true; 624 break; 625 } 585 626 metadata_list.add(metadata); 586 627 } … … 590 631 Element node_list = doc_.createElement(node_name+GSXML.LIST_MODIFIER); 591 632 result.appendChild(node_list); 592 633 593 634 // Get the documents 594 635 Element request_node_list = (Element) GSXML.getChildByTagName(request, node_name+GSXML.LIST_MODIFIER); … … 615 656 new_node.appendChild(node_meta_list); 616 657 DBInfo info = gdbm_src_.getInfo(node_id); 617 for (int m = 0; m < metadata_list.size(); m++) { 618 String metadata = (String) metadata_list.get(m); 619 String value = info.getInfo(metadata); 620 GSXML.addMetadata(doc_, node_meta_list, metadata, value); 658 if (info == null) {// I have had a case where it is null! 659 continue; 660 } 661 System.out.println("found the info"); 662 if (all_metadata) { 663 System.out.println("trying to get all the metadata"); 664 // return everything out of the database 665 Set keys = info.getKeys(); 666 Iterator it = keys.iterator(); 667 while(it.hasNext()) { 668 String key = (String)it.next(); 669 System.out.println("getting metadata "+key); 670 String value = info.getInfo(key); 671 GSXML.addMetadata(doc_, node_meta_list, key, value); 672 } 673 } else { // just get the selected ones 674 675 for (int m = 0; m < metadata_list.size(); m++) { 676 String metadata = (String) metadata_list.get(m); 677 String value = info.getInfo(metadata); 678 GSXML.addMetadata(doc_, node_meta_list, metadata, value); 679 } 621 680 } 622 681 } … … 641 700 return doc_content; 642 701 } 702 703 protected Element getInfo(String doc_id, String info_type) { 704 705 String value=""; 706 if (info_type.equals(INFO_NUM_SIBS)) { 707 String parent_id = OID.getParent(doc_id); 708 if (parent_id.equals(doc_id)) { 709 value="0"; 710 } else { 711 value = String.valueOf(getNumChildren(parent_id)); 712 } 713 } else if (info_type.equals(INFO_NUM_CHILDREN)) { 714 value = String.valueOf(getNumChildren(doc_id)); 715 } else if (info_type.equals(INFO_SIB_POS)) { 716 String parent_id = OID.getParent(doc_id); 717 if (parent_id.equals(doc_id)) { 718 value="-1"; 719 } else { 720 DBInfo info = gdbm_src_.getInfo(parent_id); 721 String contains = info.getInfo("contains"); 722 contains = contains.replaceAll("\"", parent_id); 723 String [] children = contains.split(";"); 724 for (int i=0;i<children.length;i++) { 725 String child_id = children[i]; 726 System.out.println("child="+child_id+" doc="+doc_id); 727 if (child_id.equals(doc_id)) { 728 value = String.valueOf(i+1); // make it from 1 to length 729 break; 730 } 731 } 732 } 733 } else { 734 return null; 735 } 736 Element info_elem = doc_.createElement("info"); 737 info_elem.setAttribute(GSXML.NAME_ATT, info_type); 738 info_elem.setAttribute(GSXML.VALUE_ATT, value); 739 return info_elem; 740 } 741 742 protected int getNumChildren(String doc_id) { 743 DBInfo info = gdbm_src_.getInfo(doc_id); 744 String contains = info.getInfo("contains"); 745 if (contains.equals("")) { 746 return 0; 747 } 748 String [] children = contains.split(";"); 749 return children.length; 750 } 751 643 752 }
Note:
See TracChangeset
for help on using the changeset viewer.