Changeset 23410 for main/trunk/gli
- Timestamp:
- 2010-12-07T19:26:53+13:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/metadata/MetadataXMLFileManager.java
r17612 r23410 47 47 static private ArrayList modified_metadata_xml_files = new ArrayList(); 48 48 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 49 55 50 56 static public void addMetadata(CollectionTreeNode file_node, ArrayList metadata_values) … … 201 207 } 202 208 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); 203 212 // Return the metadata assigned to the specified file from the applicable metadata.xml files 204 213 return getMetadataAssignedToFile(file, applicable_metadata_xml_files); … … 388 397 } 389 398 } 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 390 448 }
Note:
See TracChangeset
for help on using the changeset viewer.