- Timestamp:
- 2003-12-10T16:31:18+13:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/msm/GDMDocument.java
r6172 r6202 170 170 // It appears that its possible that we can be asked to add the same metadata twice (especially after a copy action is cancelled then repeated). So we check if we have been asked to add exactly the same value twice. 171 171 if(sibling_metadata_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equals(metadata_element.getAttribute(StaticStrings.NAME_ATTRIBUTE))) { 172 172 // Check the values and return if they are the same. 173 173 if(metadata.getAbsoluteValue().equals(MSMUtils.getValue(sibling_metadata_element))) { 174 174 return; … … 184 184 sibling_description_elements = null; 185 185 if(will_accumulate || force_accumulate) { //mode.equals(ACCUMULATE)) { 186 metadata_element.setAttribute(MODE_ATTRIBUTE, ACCUMULATE);186 metadata_element.setAttribute(MODE_ATTRIBUTE, ACCUMULATE); 187 187 } 188 188 // As we can't possibly store all the metadata in memory, nor can we ensure that the indexes written to file remain the same until the new time we look at this file, and to avoid having to open a rewrite every collection document whenever any value tree changes, I'm writing the value out as a full path string … … 250 250 return base_document; 251 251 } 252 252 253 /** Get all of the metadata, including directory level, associated with this file. */ 253 254 public ArrayList getMetadata(String filename, boolean remove, ArrayList metadatum_so_far, File file, boolean append_folder_level) { … … 257 258 public ArrayList getMetadata(String filename, boolean remove, ArrayList metadatum_so_far, File file, boolean append_folder_level, boolean purge) { 258 259 Gatherer.println("Get metadata for " + filename); 260 Gatherer.println("append_folder_level: " + append_folder_level); 261 Gatherer.println("purge: " + purge); 259 262 ArrayList metadatum = null; 263 ArrayList queued_for_removal = new ArrayList(); 260 264 if(metadatum_so_far == null) { 261 265 metadatum = new ArrayList(); … … 267 271 // Retrieve the document element. 268 272 Element directorymetadata_element = base_document.getDocumentElement(); 269 // Iterate through the filesets, checking the FileName child element against 273 // Iterate through the filesets, checking the FileName child element against the target file's name using regular expression matching. 270 274 NodeList fileset_elements = directorymetadata_element.getElementsByTagName(FILESET_ELEMENT); 271 275 for(int i = 0; i < fileset_elements.getLength(); i++) { … … 368 372 // Having found our metadata check if the value from the xml matches the one from the gvaluenode. If not update it. This happens whenever hierarchy information is involved (indexes rapidly become obsolete). 369 373 // If remove was set, remove it. We can only remove pure file level metadata, or folder level iff we were asked for folder level. 374 ///atherer.println("Have we been asked to remove the metadata: " + metadata); 375 ///atherer.println("Given:"); 376 ///atherer.println("\tremove = " + remove); 377 ///atherer.println("\tfilename = " + filename); 378 ///atherer.println("\tfilename_text = " + filename_text + "?"); 370 379 if(remove && ((filename != null && filename.matches(filename_text) && !filename_text.equals(DIRECTORY_FILENAME)) || (filename == null && filename_text.equals(DIRECTORY_FILENAME)))) { 371 ///ystem.err.println("Removing " + metadata + " from " + file); 372 description_element.removeChild(metadata_element); 373 // Remove the description element if empty. 374 if(!description_element.hasChildNodes()) { 375 fileset_element.removeChild(description_element); 376 } 380 ///atherer.println("Yes! Queuing for Removal."); 381 queued_for_removal.add(metadata_element); 377 382 } 378 383 else { 379 //String current_value = metadata.getAbsoluteValue(); 380 //String current_value = Codec.transform(metadata.getValueNode().getFullPath(false), Codec.TEXT_TO_DOM); 384 ///atherer.println("No. Updating."); 381 385 String current_value = metadata.getValueNode().getFullPath(false); 382 386 ///ystem.err.println("Checking the current mdv path: " + current_value); … … 399 403 metadata_elements = null; 400 404 } 405 406 // Now we remove any elements that have been queued for deletion 407 for(int a = 0; a < queued_for_removal.size(); a++) { 408 Element metadata_element = (Element) queued_for_removal.get(a); 409 description_element.removeChild(metadata_element); 410 up_to_date = false; 411 } 412 queued_for_removal.clear(); 413 queued_for_removal = null; 414 415 // If the description_element no longer has any children remove it 416 NodeList metadata_elements = description_element.getElementsByTagName(StaticStrings.METADATA_ELEMENT); 417 NodeList extracted_elements = description_element.getElementsByTagName(StaticStrings.EXTRACTED_METADATA_ELEMENT); 418 if(metadata_elements.getLength() == 0 && extracted_elements.getLength() == 0) { 419 fileset_element.removeChild(description_element); 420 up_to_date = false; 421 } 401 422 description_element = null; 402 423 } … … 410 431 if(description_elements.getLength() == 0) { 411 432 directorymetadata_element.removeChild(fileset_element); 433 up_to_date = false; 412 434 } 413 435 description_elements = null; … … 437 459 return ((doctype_name.equals("GreenstoneDirectoryMetadata") && root_name.equals("GreenstoneDirectoryMetadata")) || (doctype_name.equals("DirectoryMetadata") && root_name.equals("DirectoryMetadata"))); 438 460 } 461 462 /** Remove all of the extracted metadata (XMetadata) from this document. */ 463 public void removeExtractedMetadata() { 464 try { 465 Element document_element = base_document.getDocumentElement(); 466 NodeList extracted_metadata_elements = document_element.getElementsByTagName(StaticStrings.EXTRACTED_METADATA_ELEMENT); 467 document_element = null; 468 for(int i = extracted_metadata_elements.getLength(); i != 0; i--) { 469 Element extracted_metadata_element = (Element) extracted_metadata_elements.item(i - 1); 470 String element_name = extracted_metadata_element.getAttribute(StaticStrings.NAME_ATTRIBUTE); 471 ElementWrapper element = Gatherer.c_man.getCollection().msm.getElement(element_name); 472 if(element != null) { 473 element.dec(); 474 } 475 element = null; 476 ///ystem.err.println("Removing extracted metadata: " + element_name + "=" + MSMUtils.getValue(extracted_metadata_element)); 477 element_name = null; 478 Node parent_node = extracted_metadata_element.getParentNode(); 479 parent_node.removeChild(extracted_metadata_element); 480 parent_node = null; 481 extracted_metadata_element = null; 482 } 483 extracted_metadata_elements = null; 484 up_to_date = false; 485 } 486 catch(Exception exception) { 487 Gatherer.println("Exception in GDMDocument.removeExtractedMetadata() - unexpected"); 488 Gatherer.printStackTrace(exception); 489 } 490 } 491 439 492 /** Remove the given directory level metadata from this document. All directory level metadata is available under the FileSet with filename '.*'. There is at least one nasty case to consider, where the first overwriting metadata entry, of several with the same element, is removed. In this case the next entry must become overwrite to ensure proper inheritance. */ 440 493 public void removeMetadata(String filename, Metadata metadata) { 441 494 Gatherer.println("Remove metadata: " + metadata + "\nFrom filename: " + filename); 442 495 try { 443 444 445 446 447 448 449 450 451 496 boolean found = false; 497 boolean first_metadata_element_found = true; 498 boolean make_next_metadata_element_overwrite = false; 499 boolean remove_fileset = false; 500 // Retrieve the document element. 501 Element directorymetadata_element = base_document.getDocumentElement(); 502 // Iterate through the filesets looking for the directory level one. 503 NodeList fileset_elements = directorymetadata_element.getElementsByTagName(FILESET_ELEMENT); 504 for(int i = 0; !found && i < fileset_elements.getLength(); i++) { 452 505 Element fileset_element = (Element) fileset_elements.item(i); 453 506 NodeList filename_elements = fileset_element.getElementsByTagName(FILENAME_ELEMENT); 454 507 for(int j = 0; !found && j < filename_elements.getLength(); j++) { 455 456 457 508 Element filename_element = (Element) filename_elements.item(j); 509 String filename_text = MSMUtils.getValue(filename_element); 510 if((filename != null && filename.matches(filename_text) && !filename.equals(DIRECTORY_FILENAME)) || (filename == null && filename_text.equals(DIRECTORY_FILENAME))) { 458 511 // Retrieve the Metadata Element for this fileset, and iterate through them looking for the one which we are to remove. 459 512 NodeList description_elements = fileset_element.getElementsByTagName("Description"); 460 513 for(int k = 0; !found && k < description_elements.getLength(); k++) { 461 Element description_element = (Element) description_elements.item(k); 462 // We have to do this for each type of metadata 463 for(int z = 0; z < ALL_METADATA_TYPES.length; z++) { 464 NodeList metadata_elements = description_element.getElementsByTagName(ALL_METADATA_TYPES[z]); 465 for(int l = 0; (!found || !make_next_metadata_element_overwrite) && l < metadata_elements.getLength(); l++) { 466 Element metadata_element = (Element) metadata_elements.item(l); 467 String element = metadata_element.getAttribute("name"); 468 String value = MSMUtils.getValue(metadata_element); 469 // See if this is the metadata we wish to remove 470 if(element.equals(metadata.getElement().getName())) { 471 if(value.equals(metadata.getAbsoluteValue())) { 472 // Remove it 473 ///ystem.err.println("Remove " + element + "-" + value); 474 description_element.removeChild(metadata_element); 475 found = true; 476 // If this was the first metadata with this element found, and it was set to overwrite, then we have to ensure that the next metadata with this element found (if any) is changed to be overwrite now. 477 if(first_metadata_element_found && !metadata.accumulates()) { 478 ///ystem.err.println("First of this element found!"); 479 make_next_metadata_element_overwrite = true; 480 } 481 } 482 // If this was the first metadata we've found with the element of the one to be removed set first found to false. 483 else if(first_metadata_element_found) { 484 ///ystem.err.println("Found a matching element: " + element + "=" + value); 485 first_metadata_element_found = false; 486 } 487 // Otherwise we should make this metadata overwrite as requested. 488 else if(make_next_metadata_element_overwrite) { 489 ///ystem.err.println("Changing to overwrite: " + element + "=" + value); 490 metadata_element.setAttribute(MODE_ATTRIBUTE, ""); 491 } 492 } 493 value = null; 494 element = null; 495 metadata_element = null; 496 } 497 NodeList normal_metadata_elements = description_element.getElementsByTagName(ALL_METADATA_TYPES[0]); 498 NodeList extracted_metadata_elements = description_element.getElementsByTagName(ALL_METADATA_TYPES[1]); 499 // If we found it, removed it, and now the description tag has no children, mark the fileset for removal 500 if(normal_metadata_elements.getLength() == 0 && extracted_metadata_elements.getLength() == 0) { 501 remove_fileset = true; 502 } 503 extracted_metadata_elements = null; 504 normal_metadata_elements = null; 505 metadata_elements = null; 514 Element description_element = (Element) description_elements.item(k); 515 // We have to do this for each type of metadata 516 for(int z = 0; z < ALL_METADATA_TYPES.length; z++) { 517 NodeList metadata_elements = description_element.getElementsByTagName(ALL_METADATA_TYPES[z]); 518 for(int l = 0; (!found || !make_next_metadata_element_overwrite) && l < metadata_elements.getLength(); l++) { 519 Element metadata_element = (Element) metadata_elements.item(l); 520 String element = metadata_element.getAttribute("name"); 521 String value = MSMUtils.getValue(metadata_element); 522 // See if this is the metadata we wish to remove 523 if(element.equals(metadata.getElement().getName())) { 524 if(value.equals(metadata.getValueNode().getFullPath(false))) { 525 // Remove it 526 ///ystem.err.println("Remove " + element + "-" + value); 527 description_element.removeChild(metadata_element); 528 found = true; 529 // If this was the first metadata with this element found, and it was set to overwrite, then we have to ensure that the next metadata with this element found (if any) is changed to be overwrite now. 530 if(first_metadata_element_found && !metadata.accumulates()) { 531 ///ystem.err.println("First of this element found!"); 532 make_next_metadata_element_overwrite = true; 533 } 534 } 535 // If this was the first metadata we've found with the element of the one to be removed set first found to false. 536 else if(first_metadata_element_found) { 537 ///ystem.err.println("Found a matching element: " + element + "=" + value); 538 first_metadata_element_found = false; 539 } 540 // Otherwise we should make this metadata overwrite as requested. 541 else if(make_next_metadata_element_overwrite) { 542 ///ystem.err.println("Changing to overwrite: " + element + "=" + value); 543 metadata_element.setAttribute(MODE_ATTRIBUTE, ""); 544 } 545 } 546 value = null; 547 element = null; 548 metadata_element = null; 506 549 } 507 description_element = null; 550 NodeList normal_metadata_elements = description_element.getElementsByTagName(ALL_METADATA_TYPES[0]); 551 NodeList extracted_metadata_elements = description_element.getElementsByTagName(ALL_METADATA_TYPES[1]); 552 // If we found it, removed it, and now the description tag has no children, mark the fileset for removal 553 if(normal_metadata_elements.getLength() == 0 && extracted_metadata_elements.getLength() == 0) { 554 remove_fileset = true; 555 } 556 extracted_metadata_elements = null; 557 normal_metadata_elements = null; 558 metadata_elements = null; 559 } 560 description_element = null; 508 561 } 509 562 description_elements = null; 510 511 512 563 } 564 filename_text = null; 565 filename_element = null; 513 566 } 514 567 filename_elements = null; 515 568 if(found && remove_fileset) { 516 569 directorymetadata_element.removeChild(fileset_element); 517 570 } 518 571 fileset_element = null; 519 520 521 522 572 } 573 fileset_elements = null; 574 directorymetadata_element = null; 575 up_to_date = false; 523 576 } 524 577 catch (Exception error) { 525 Gatherer.printStackTrace(error); 526 } 527 } 528 529 /** Change the up to date flag. */ 530 public void setUpToDate(boolean up_to_date) { 578 Gatherer.printStackTrace(error); 579 } 580 } 581 582 /** Change the up to date flag. 583 * @param up_to_date true if the document on the filesystem is the same as the one in memory, false otherwise 584 */ 585 public void setUpToDate(boolean up_to_date) { 531 586 this.up_to_date = up_to_date; 532 587 } 533 588 }
Note:
See TracChangeset
for help on using the changeset viewer.