Changeset 6202


Ignore:
Timestamp:
2003-12-10T16:31:18+13:00 (20 years ago)
Author:
jmt12
Message:

As well as implementing a removeExtractedNodes method, also fixed error where not all metadata was removed when a file was

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/msm/GDMDocument.java

    r6172 r6202  
    170170                // 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.
    171171                if(sibling_metadata_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equals(metadata_element.getAttribute(StaticStrings.NAME_ATTRIBUTE))) {
    172                     // Check the values and return if they are the same.
     172                    // Check the values and return if they are the same.
    173173                   if(metadata.getAbsoluteValue().equals(MSMUtils.getValue(sibling_metadata_element))) {
    174174                      return;
     
    184184        sibling_description_elements = null;
    185185        if(will_accumulate || force_accumulate) { //mode.equals(ACCUMULATE)) {
    186         metadata_element.setAttribute(MODE_ATTRIBUTE, ACCUMULATE);
     186            metadata_element.setAttribute(MODE_ATTRIBUTE, ACCUMULATE);
    187187        }
    188188        // 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
     
    250250    return base_document;
    251251    }
     252
    252253    /** Get all of the metadata, including directory level, associated with this file. */
    253254    public ArrayList getMetadata(String filename, boolean remove, ArrayList metadatum_so_far, File file, boolean append_folder_level) {
     
    257258    public ArrayList getMetadata(String filename, boolean remove, ArrayList metadatum_so_far, File file, boolean append_folder_level, boolean purge) {
    258259    Gatherer.println("Get metadata for " + filename);
     260    Gatherer.println("append_folder_level: " + append_folder_level);
     261    Gatherer.println("purge: " + purge);
    259262    ArrayList metadatum = null;
     263    ArrayList queued_for_removal = new ArrayList();
    260264    if(metadatum_so_far == null) {
    261265        metadatum = new ArrayList();
     
    267271        // Retrieve the document element.
    268272        Element directorymetadata_element = base_document.getDocumentElement();
    269         // Iterate through the filesets, checking the FileName child element against    the target file's name using regular expression matching.
     273        // Iterate through the filesets, checking the FileName child element against the target file's name using regular expression matching.
    270274        NodeList fileset_elements = directorymetadata_element.getElementsByTagName(FILESET_ELEMENT);
    271275        for(int i = 0; i < fileset_elements.getLength(); i++) {
     
    368372                    // 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).
    369373                    // 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 + "?");
    370379                    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);
    377382                    }
    378383                    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.");
    381385                        String current_value = metadata.getValueNode().getFullPath(false);
    382386                        ///ystem.err.println("Checking the current mdv path: " + current_value);
     
    399403                metadata_elements = null;
    400404                }
     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                }
    401422                description_element = null;
    402423            }
     
    410431        if(description_elements.getLength() == 0) {
    411432            directorymetadata_element.removeChild(fileset_element);
     433            up_to_date = false;
    412434        }
    413435        description_elements = null;
     
    437459    return ((doctype_name.equals("GreenstoneDirectoryMetadata") && root_name.equals("GreenstoneDirectoryMetadata")) || (doctype_name.equals("DirectoryMetadata") && root_name.equals("DirectoryMetadata")));
    438460    }
     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
    439492    /** 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. */
    440493    public void removeMetadata(String filename, Metadata metadata) {
    441494    Gatherer.println("Remove metadata: " + metadata + "\nFrom filename: " + filename);
    442495    try {
    443         boolean found = false;
    444         boolean first_metadata_element_found = true;
    445         boolean make_next_metadata_element_overwrite = false;
    446         boolean remove_fileset = false;
    447         // Retrieve the document element.
    448         Element directorymetadata_element = base_document.getDocumentElement();
    449         // Iterate through the filesets looking for the directory level one.
    450         NodeList fileset_elements = directorymetadata_element.getElementsByTagName(FILESET_ELEMENT);
    451         for(int i = 0; !found && i < fileset_elements.getLength(); i++) {
     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++) {
    452505        Element fileset_element = (Element) fileset_elements.item(i);
    453506        NodeList filename_elements = fileset_element.getElementsByTagName(FILENAME_ELEMENT);
    454507        for(int j = 0; !found && j < filename_elements.getLength(); j++) {
    455             Element filename_element = (Element) filename_elements.item(j);
    456             String filename_text = MSMUtils.getValue(filename_element);
    457             if((filename != null && filename.matches(filename_text) && !filename.equals(DIRECTORY_FILENAME)) || (filename == null && filename_text.equals(DIRECTORY_FILENAME))) {
     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))) {
    458511            // Retrieve the Metadata Element for this fileset, and iterate through them looking for the one which we are to remove.
    459512            NodeList description_elements = fileset_element.getElementsByTagName("Description");
    460513            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;
    506549                }
    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;
    508561            }
    509562            description_elements = null;
    510             }
    511             filename_text = null;
    512             filename_element = null;
     563            }
     564            filename_text = null;
     565            filename_element = null;
    513566        }
    514567        filename_elements = null;
    515568        if(found && remove_fileset) {
    516             directorymetadata_element.removeChild(fileset_element);
     569            directorymetadata_element.removeChild(fileset_element);
    517570        }
    518571        fileset_element = null;
    519         }
    520         fileset_elements = null;
    521         directorymetadata_element = null;
    522         up_to_date = false;
     572        }
     573        fileset_elements = null;
     574        directorymetadata_element = null;
     575        up_to_date = false;
    523576    }
    524577    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) {
    531586    this.up_to_date = up_to_date;
    532     }
     587    }
    533588}
Note: See TracChangeset for help on using the changeset viewer.