Ignore:
Timestamp:
2010-12-09T22:27:33+13:00 (13 years ago)
Author:
ak19
Message:

GLI now has a gs.FilenameEncoding metadata field which appears like all the others in GLI's EnrichPane, but is unique in that this metadata (once set, changed or removed) must be applied to the affected filenames in the Collection Tree. More importantly, the changes made for this are to allow GLI's java code to interact with the recent changes to Perl where strings were made unicode-aware (for proper regex matching) but which required other changes elsewhere. To still support filenames with different encodings Perl used URL encoded versions of filenames representing characters' code point values in URL encoding. This required that GLI write out URL encoded filenames to the metadata.xml files that are associated with each folder level of a collection, so that Perl can read them. In this way, they can both speak of the same filenames. Only works on unicode 16 (such as latin-1), non-UTF8 systems. The latter is a requirement since Java uses the filesystem encoding from startup. If it is UTF8, non-recognised characters are replaced by the invalid char for UTF8. This process being destructive, we can't get the original filenames' bytecodes back. The changes made to GLI will work on Windows which is UTF-16 (windows codepage 1252), presumably also Macs (some kind of UTF-16) and also works on Native Latin 1 Linux systems. UTF-8 Linux systems need to be reconfigured to Native Latin-1, or if not installed, an administrator can install it easily.

File:
1 edited

Legend:

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

    r23410 r23433  
    9090        if (current_file_directory_path.equals(metadata_xml_file.getParentFile().getAbsolutePath())) {
    9191            applicable_metadata_xml_file_found = true;
    92             metadata_xml_file.addMetadata(current_file, metadata_values);
     92            metadata_xml_file.addMetadata(file_nodes[i], metadata_values);
    9393            if (!modified_metadata_xml_files.contains(metadata_xml_file)) {
    9494            modified_metadata_xml_files.add(metadata_xml_file);
     
    107107
    108108        // ...and add the metadata
    109         new_metadata_xml_file.addMetadata(current_file, metadata_values);
     109        new_metadata_xml_file.addMetadata(file_nodes[i], metadata_values);
    110110        if (!modified_metadata_xml_files.contains(new_metadata_xml_file)) {
    111111            modified_metadata_xml_files.add(new_metadata_xml_file);
     
    160160
    161161    // Get the metadata assigned to the specified file from the applicable metadata.xml files
    162     ArrayList assigned_metadata = getMetadataAssignedToFile(file, applicable_metadata_xml_files);
     162    ArrayList assigned_metadata = getMetadataAssignedToFile(file, applicable_metadata_xml_files, false);
    163163
    164164    // Remove any folder-level metadata
     
    173173
    174174
     175    /** Returns the metadata assigned to a file inside the collection, excluding folder-level/inherited metadata. */
     176    static public ArrayList getMetadataAssignedDirectlyToFile(File file)
     177    {
     178        return getMetadataAssignedDirectlyToFile(file, false);
     179    }
     180   
    175181    /** Returns the metadata assigned to a file inside the collection, excluding folder-level/inherited metadata. */
    176     static public ArrayList getMetadataAssignedDirectlyToFile(File file)
    177     {
    178     // Get all the metadata assigned to the specified file...
    179     ArrayList assigned_metadata = getMetadataAssignedToFile(file);
    180 
    181     // ...then remove any folder-level metadata
    182     for (int i = assigned_metadata.size() - 1; i >= 0; i--) {
    183         if (((MetadataValue) assigned_metadata.get(i)).isInheritedMetadata()) {
    184         assigned_metadata.remove(i);
    185         }
    186     }
    187 
    188     return assigned_metadata;
    189     }
    190 
     182    static public ArrayList getMetadataAssignedDirectlyToFile(File file, boolean filenameEncodingMetaOnly)
     183    {
     184       
     185        // Get all the metadata assigned to the specified file...
     186        ArrayList assigned_metadata = getMetadataAssignedToFile(file, filenameEncodingMetaOnly);
     187
     188        // ...then remove any folder-level metadata
     189        for (int i = assigned_metadata.size() - 1; i >= 0; i--) {
     190            if (((MetadataValue) assigned_metadata.get(i)).isInheritedMetadata()) {
     191            assigned_metadata.remove(i);
     192            }
     193        }
     194
     195        return assigned_metadata;
     196        /*
     197        // Get all the metadata assigned to the specified file...
     198        // Build up a list of applicable metadata.xml files - which in this case
     199        // is exclusively the metadata file at this file/folder's own level.
     200        ArrayList applicable_metadata_xml_files = new ArrayList();
     201
     202        // Find the metadata.xml file (if any) that is at the same level as the file
     203        String file_directory_path = (file.isDirectory() ? file : file.getParentFile()).getAbsolutePath() + File.separator;
     204        for (int i = 0; i < metadata_xml_files.size(); i++) {
     205            MetadataXMLFile metadata_xml_file = (MetadataXMLFile) metadata_xml_files.get(i);
     206           
     207            if (file_directory_path.equals(metadata_xml_file.getParentFile().getAbsolutePath() + File.separator)) {
     208                //System.err.println("Found metadata_xml_file: " + metadata_xml_file);
     209                applicable_metadata_xml_files.add(metadata_xml_file);
     210            }
     211        }
     212       
     213        if(applicable_metadata_xml_files.size() == 0) {
     214            return new ArrayList(0);
     215        }
     216
     217        // Return the metadata assigned to the specified file from the applicable metadata.xml files
     218        return getMetadataAssignedToFile(file, applicable_metadata_xml_files, filenameEncodingMetaOnly);
     219        */
     220    }
     221   
    191222
    192223    /** Returns all the metadata assigned to a file inside the collection. */
    193224    static public ArrayList getMetadataAssignedToFile(File file)
     225    {
     226    return getMetadataAssignedToFile(file, false);
     227    }
     228   
     229    /** Returns all the metadata assigned to a file inside the collection (or
     230     *  just gs.filenameEncoding if parameter filenameEncodingMetaOnly is true),
     231     *  including folder-level/inherited metadata.
     232     */   
     233    static public ArrayList getMetadataAssignedToFile(File file, boolean filenameEncodingMetaOnly)
    194234    {
    195235    // Build up a list of applicable metadata.xml files
     
    207247    }
    208248
    209     // sort the metadataxml files in order starting from those in the
     249    // Sort the metadataxml files in order starting from those in the
    210250    // topmost folders down to the one in the lowest level folder.
    211251    Collections.sort(applicable_metadata_xml_files, metadataXMLFileComparator);
    212252    // Return the metadata assigned to the specified file from the applicable metadata.xml files
    213     return getMetadataAssignedToFile(file, applicable_metadata_xml_files);
    214     }
    215 
    216 
    217     static private ArrayList getMetadataAssignedToFile(File file, ArrayList applicable_metadata_xml_files)
     253    return getMetadataAssignedToFile(file, applicable_metadata_xml_files, filenameEncodingMetaOnly);
     254    }
     255
     256    // package access method
     257    static ArrayList getMetadataAssignedToFile(File file, ArrayList applicable_metadata_xml_files,
     258                               boolean filenameEncodingMetaOnly)   
    218259    {
    219260    // Build up a list of metadata values assigned to this file
     
    225266        DebugStream.println("Applicable metadata.xml file: " + metadata_xml_file);
    226267
    227         ArrayList metadata_values = metadata_xml_file.getMetadataAssignedToFile(file);
     268        ArrayList metadata_values = metadata_xml_file.getMetadataAssignedToFile(file, filenameEncodingMetaOnly);
    228269        for (int j = 0; j < metadata_values.size(); j++) {
    229270        MetadataValue metadata_value = (MetadataValue) metadata_values.get(j);
     
    316357        // This metadata.xml file is only potentially applicable if it is above or at the same level as the file
    317358        if (current_file_directory_path.startsWith(metadata_xml_file.getParentFile().getAbsolutePath())) {
    318             metadata_xml_file.removeMetadata(current_file, metadata_values);
     359            metadata_xml_file.removeMetadata(file_nodes[i], metadata_values);
    319360            if (!modified_metadata_xml_files.contains(metadata_xml_file)) {
    320361            modified_metadata_xml_files.add(metadata_xml_file);
     
    350391        // This metadata.xml file is only applicable if it is at the same level as the file
    351392        if (current_file_directory_path.equals(metadata_xml_file.getParentFile().getAbsolutePath())) {
    352             metadata_xml_file.replaceMetadata(current_file, old_metadata_value, new_metadata_value);
     393            metadata_xml_file.replaceMetadata(file_nodes[i], old_metadata_value, new_metadata_value);
    353394            if (!modified_metadata_xml_files.contains(metadata_xml_file)) {
    354395            modified_metadata_xml_files.add(metadata_xml_file);
     
    399440
    400441 
    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   }
     442    /**
     443      * Comparator to order MetadataXMLFiles in ascending order from
     444      * those in a higher level folder to those in a lower level folder
     445      * It is based on the assumption that all MetadataXMLFiles sent to
     446      * it to compare will be linear descendants of one toplevel folder
     447      * E.g. /A/metadata.xml, /A/B/metadata.xml, /A/B/C/D/metadata.xml.
     448      * In other words, that each is a substring of one of the others until we
     449      * the toplevel folder is reached.
     450    */
     451    private static class MetadataXMLFileComparator implements Comparator {
     452       
     453        public int compare(Object o1, Object o2) {
     454            if(!(o1 instanceof MetadataXMLFile)) {
     455                return -1;
     456            } else if (!(o2 instanceof MetadataXMLFile)) {
     457                return 1;
     458            }
     459
     460            // Both are MetadataXMLFiles objects. Remove the terminating
     461            // "metadata.xml" from their filenames to get their containing folder
     462            String filename1 = ((MetadataXMLFile)o1).getParentFile().getAbsolutePath();
     463            String filename2 = ((MetadataXMLFile)o2).getParentFile().getAbsolutePath();
     464
     465            // if 1 is a prefix of 2, then 1 < 2 in the ordering (1 comes before 2)
     466            if(filename2.startsWith(filename1)) {
     467                return -1;
     468            } else if(filename1.startsWith(filename2)) {
     469                return 1;
     470            } else {
     471                // unlikely that the metadata.xml files will be the same
     472                // or that neither is a prefix of the other
     473                return filename1.compareTo(filename2); // sorts in ascending order
     474            }     
     475
     476        }
     477
     478        public boolean equals(Object obj) {
     479            if(!(obj instanceof MetadataXMLFileComparator)) {
     480                return false;
     481            }
     482
     483            // else it is the same sort of comparator
     484            return true;
     485        }
     486
     487    }
    447488
    448489}
Note: See TracChangeset for help on using the changeset viewer.