Changeset 23410


Ignore:
Timestamp:
12/07/10 19:26:53 (11 years ago)
Author:
kjdon
Message:

Katherine fixed the reason why non-accumulating metadata (gs.FilenameEncoding) was glitchy and at times did not override meta assigned at higher levels with those assigned at lower levels. The applicable_metadata_xml_files needed to be inspected in a guaranteed top-down order for the override to work.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/metadata/MetadataXMLFileManager.java

    r17612 r23410  
    4747    static private ArrayList modified_metadata_xml_files = new ArrayList();
    4848
     49  /** For non-accumulating metadata (gs.FilenameEncoding), need the metadata.xml files
     50   * sorted in top-down folder level order, which is achieved through this Comparator.
     51   * By declaring a static class member here, we avoid recreating this object for every
     52   * comparison, which would otherwise have resulted in a constructor call each time. */
     53  static private MetadataXMLFileComparator metadataXMLFileComparator = new MetadataXMLFileComparator();
     54
    4955
    5056    static public void addMetadata(CollectionTreeNode file_node, ArrayList metadata_values)
     
    201207    }
    202208
     209    // sort the metadataxml files in order starting from those in the
     210    // topmost folders down to the one in the lowest level folder.
     211    Collections.sort(applicable_metadata_xml_files, metadataXMLFileComparator);
    203212    // Return the metadata assigned to the specified file from the applicable metadata.xml files
    204213    return getMetadataAssignedToFile(file, applicable_metadata_xml_files);
     
    388397    }
    389398    }
     399
     400 
     401  /**
     402  * Comparator to order MetadataXMLFiles in ascending order from
     403  * those in a higher level folder to those in a lower level folder
     404  * It is based on the assumption that all MetadataXMLFiles sent to
     405  * it to compare will be linear descendants of one toplevel folder
     406  * E.g. /A/metadata.xml, /A/B/metadata.xml, /A/B/C/D/metadata.xml.
     407  * In other words, that each is a substring of one other until we
     408  * get to the toplevel folder.
     409  */
     410  private static class MetadataXMLFileComparator implements Comparator {
     411   
     412    public int compare(Object o1, Object o2) {
     413      if(!(o1 instanceof MetadataXMLFile)) {
     414    return -1;
     415      } else if (!(o2 instanceof MetadataXMLFile)) {
     416    return 1;
     417      }
     418
     419      // Both are MetadataXMLFiles objects. Remove the terminating
     420      // "metadata.xml" from their filenames to get their containing folder
     421      String filename1 = ((MetadataXMLFile)o1).getParentFile().getAbsolutePath();
     422      String filename2 = ((MetadataXMLFile)o2).getParentFile().getAbsolutePath();
     423
     424      // if 1 is a prefix 2, then 1 < 2 in the ordering (1 comes before 2)
     425      if(filename2.startsWith(filename1)) {
     426    return -1;
     427      } else if(filename1.startsWith(filename2)) {
     428    return 1;
     429      } else {
     430    // unlikely that the metadata.xml files will be the same
     431    // or that neither is a prefix of the other
     432    return filename1.compareTo(filename2); // sorts in ascending order
     433      }     
     434     
     435    }
     436
     437    public boolean equals(Object obj) {
     438      if(!(obj instanceof MetadataXMLFileComparator)) {
     439    return false;
     440      }
     441     
     442      // else it is the same sort of comparator
     443      return true;
     444    }
     445
     446  }
     447
    390448}
Note: See TracChangeset for help on using the changeset viewer.