Changeset 6147


Ignore:
Timestamp:
2003-12-08T14:20:49+13:00 (20 years ago)
Author:
jmt12
Message:

Changed the toString so it follows the format: title (filename)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/collection/BasicCollectionConfiguration.java

    r6051 r6147  
    4848/** This class provides access to a collection configuration file. This version accepts either a valid xml document or a historical collect.cfg file. */
    4949public class BasicCollectionConfiguration
    50     implements Comparable {
    51 
    52     private Element creator_element;
    53     private Element description_element;
    54     private Element maintainer_element;
    55     private Element name_element;
    56     private File file;
    57     private String creator;
    58     private String description;
    59     private String maintainer;
    60     private String name;
    61 
    62     public BasicCollectionConfiguration(File file) {
    63         this.file = file;
    64         try {
    65             String filename = file.getName().toLowerCase();
    66             if(filename.endsWith(".xml")) {
    67                 // This is where G3 compliant code will one day live
    68             }
    69             else if(filename.endsWith(".cfg")) {
    70                 FileReader fr = new FileReader(file);
    71                 BufferedReader br = new BufferedReader(fr);
    72                 String command = null;
    73                 while((command = br.readLine()) != null) {
    74                     if(command.length() > 0) {
    75                         // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
    76                         while(command.trim().endsWith("\\")) {
    77                             command = command.substring(0, command.lastIndexOf("\\"));
    78                             String next_line = br.readLine();
    79                             if(next_line != null) {
    80                                 command = command + next_line;
    81                             }
    82                             next_line = null;
    83                         }
    84                         CommandTokenizer tokenizer = new CommandTokenizer(command, br);
    85                         String command_type_str = tokenizer.nextToken().toLowerCase();
    86                         if(command_type_str == null) {
    87                             // Bad command. Do nothing
    88                         }
    89                         else if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_CREATOR_STR)) {
    90                             creator = tokenizer.nextToken();
    91                         }
    92                         else if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR)) {
    93                             maintainer = tokenizer.nextToken();
    94                         }
    95                         else if(command_type_str.equalsIgnoreCase(StaticStrings.COLLECTIONMETADATA_STR)) {
    96                             String meta_type_str = tokenizer.nextToken();
    97                             String value_str = tokenizer.nextToken();
    98                             // check for language
    99                             String language_str = StaticStrings.ENGLISH_LANGUAGE_STR; // assume the no-lang ones are english, but we shouldn't really do this.
    100                             if(meta_type_str != null && value_str != null) {
    101                                 meta_type_str = meta_type_str.toLowerCase();
    102                                 if(value_str.startsWith(StaticStrings.LBRACKET_CHARACTER) && value_str.endsWith(StaticStrings.RBRACKET_CHARACTER)) {
    103                                     language_str = value_str.substring(value_str.indexOf(StaticStrings.EQUALS_CHARACTER) + 1, value_str.length() - 1);
    104                                     language_str = language_str.toLowerCase();
    105                                     value_str = tokenizer.nextToken();
    106                                 }
    107                                 // now we can work out which coll meta we are dealing with
    108                                 if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR)) {
    109                                     // If this is the first collection title found, then use it, otherwise search for one that more closely matches our choosen interface language
    110                                     if (name == null || language_str.equals(Gatherer.dictionary.getLanguage())) {
    111                                         name = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
    112                                     }
    113                                 }
    114                                 else if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR)) {
    115                                     // Again we are either looking for the first description, then after that a language specific one
    116                                     if (description == null || language_str.equals(Gatherer.dictionary.getLanguage())) {
    117                                         description = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
    118                                     }
    119                                 }
    120                             }
    121                             language_str = null;
    122                             value_str = null;
    123                             meta_type_str = null;
    124                         } // end of coll meta bit
    125                         command_type_str = null;
    126                         tokenizer = null;
    127                     } // if command.length > 0
    128                 } // while
    129                 command = null;
    130                 br.close();
    131                 fr.close();
    132                 br = null;
    133                 fr = null;
    134             } // cfg file
    135             ///ystem.err.println("Parsed collect.cfg");
    136             ///ystem.err.println("name      = " + name);
    137             ///ystem.err.println("creator    = " + creator);
    138             ///ystem.err.println("maintainer  = " + maintainer);
    139             ///ystem.err.println("description = " + description);
    140         }
    141         catch(Exception error) {
    142             Gatherer.println("Error in CollectionConfiguration.<init>(): " + error);
    143             Gatherer.printStackTrace(error);
    144         }
    145     }
    146 
    147     /** Compare this configuration to another for ordering, which essentially compares two strings for ordering.
    148     * @param other the other Object which is presumably another basic collection configuration
    149     * @return an integer which is either <0, 0 or >0 if this configuration is naturally less than, equal to or greater than the target object
    150     */
    151     public int compareTo(Object other) {
    152         if(other == null) {
    153             return -1;
    154         }
    155         return toString().compareTo(other.toString());
    156     }
    157 
    158     public boolean equals(Object other) {
    159         return (compareTo(other) == 0);
    160     }
    161 
    162     /** Retrieve the creators email for this collection.
    163     * @return a String
    164     */
    165     public String getCreator() {
    166         String result = StaticStrings.EMPTY_STR;
    167         if(creator_element != null) {
    168             result = MSMUtils.getValue(creator_element);
    169         }
    170         else if(creator != null) {
    171             result = creator;
    172         }
    173         return result;
    174     }
    175 
    176     public String getDescription() {
     50    implements Comparable {
     51   
     52    private Element creator_element;
     53    private Element description_element;
     54    private Element maintainer_element;
     55    private Element name_element;
     56    private File file;
     57    private String creator;
     58    private String description;
     59    private String maintainer;
     60    private String name;
     61   
     62    public BasicCollectionConfiguration(File file) {
     63    this.file = file;
     64    try {
     65        String filename = file.getName().toLowerCase();
     66        if(filename.endsWith(".xml")) {
     67        // This is where G3 compliant code will one day live
     68        }
     69        else if(filename.endsWith(".cfg")) {
     70        FileReader fr = new FileReader(file);
     71        BufferedReader br = new BufferedReader(fr);
     72        String command = null;
     73        while((command = br.readLine()) != null) {
     74            if(command.length() > 0) {
     75            // We have to test the end of command for the special character '\'. If found, remove it and append the next line, then repeat.
     76            while(command.trim().endsWith("\\")) {
     77                command = command.substring(0, command.lastIndexOf("\\"));
     78                String next_line = br.readLine();
     79                if(next_line != null) {
     80                command = command + next_line;
     81                }
     82                next_line = null;
     83            }
     84            CommandTokenizer tokenizer = new CommandTokenizer(command, br);
     85            String command_type_str = tokenizer.nextToken().toLowerCase();
     86            if(command_type_str == null) {
     87                // Bad command. Do nothing
     88            }
     89            else if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_CREATOR_STR)) {
     90                creator = tokenizer.nextToken();
     91            }
     92            else if(command_type_str.equals(StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR)) {
     93                maintainer = tokenizer.nextToken();
     94            }
     95            else if(command_type_str.equalsIgnoreCase(StaticStrings.COLLECTIONMETADATA_STR)) {
     96                String meta_type_str = tokenizer.nextToken();
     97                String value_str = tokenizer.nextToken();
     98                // check for language
     99                String language_str = StaticStrings.ENGLISH_LANGUAGE_STR; // assume the no-lang ones are english, but we shouldn't really do this.
     100                if(meta_type_str != null && value_str != null) {
     101                meta_type_str = meta_type_str.toLowerCase();
     102                if(value_str.startsWith(StaticStrings.LBRACKET_CHARACTER) && value_str.endsWith(StaticStrings.RBRACKET_CHARACTER)) {
     103                    language_str = value_str.substring(value_str.indexOf(StaticStrings.EQUALS_CHARACTER) + 1, value_str.length() - 1);
     104                    language_str = language_str.toLowerCase();
     105                    value_str = tokenizer.nextToken();
     106                }
     107                // now we can work out which coll meta we are dealing with
     108                if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR)) {
     109                    // If this is the first collection title found, then use it, otherwise search for one that more closely matches our choosen interface language
     110                    if (name == null || language_str.equals(Gatherer.dictionary.getLanguage())) {
     111                    name = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
     112                    }
     113                }
     114                else if (meta_type_str.equals(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR)) {
     115                    // Again we are either looking for the first description, then after that a language specific one
     116                    if (description == null || language_str.equals(Gatherer.dictionary.getLanguage())) {
     117                    description = Codec.transform(value_str, Codec.GREENSTONE_TO_TEXT);
     118                    }
     119                }
     120                }
     121                language_str = null;
     122                value_str = null;
     123                meta_type_str = null;
     124            } // end of coll meta bit
     125            command_type_str = null;
     126            tokenizer = null;
     127            } // if command.length > 0
     128        } // while
     129        command = null;
     130        br.close();
     131        fr.close();
     132        br = null;
     133        fr = null;
     134        } // cfg file
     135        ///ystem.err.println("Parsed collect.cfg");
     136        ///ystem.err.println("name      = " + name);
     137        ///ystem.err.println("creator    = " + creator);
     138        ///ystem.err.println("maintainer  = " + maintainer);
     139        ///ystem.err.println("description = " + description);
     140    }
     141    catch(Exception error) {
     142        Gatherer.println("Error in CollectionConfiguration.<init>(): " + error);
     143        Gatherer.printStackTrace(error);
     144    }
     145    }
     146
     147    /** Compare this configuration to another for ordering, which essentially compares two strings for ordering.
     148    * @param other the other Object which is presumably another basic collection configuration
     149    * @return an integer which is either <0, 0 or >0 if this configuration is naturally less than, equal to or greater than the target object
     150    */
     151    public int compareTo(Object other) {
     152    if(other == null) {
     153        return -1;
     154    }
     155    return toString().compareTo(other.toString());
     156    }
     157
     158    public boolean equals(Object other) {
     159    return (compareTo(other) == 0);
     160    }
     161
     162    /** Retrieve the creators email for this collection.
     163    * @return a String
     164    */
     165    public String getCreator() {
     166    String result = StaticStrings.EMPTY_STR;
     167    if(creator_element != null) {
     168        result = MSMUtils.getValue(creator_element);
     169    }
     170    else if(creator != null) {
     171        result = creator;
     172    }
     173    return result;
     174    }
     175
     176    public String getDescription() {
    177177    String result = StaticStrings.EMPTY_STR;
    178178    if(description_element != null) {
    179         result = MSMUtils.getValue(description_element);
     179        result = MSMUtils.getValue(description_element);
    180180    }
    181181    else if(description != null) {
    182         result = description;
    183     }
    184     return result;
    185     }
    186 
    187     public File getFile() {
    188         return file;
    189     }
    190 
    191     public String getMaintainer() {
     182        result = description;
     183    }
     184    return result;
     185    }
     186
     187    public File getFile() {
     188    return file;
     189    }
     190   
     191    public String getMaintainer() {
    192192    String result = StaticStrings.EMPTY_STR;
    193193    if(maintainer_element != null) {
    194         result = MSMUtils.getValue(maintainer_element);
     194        result = MSMUtils.getValue(maintainer_element);
    195195    }
    196196    else if(maintainer != null) {
    197         result = maintainer;
    198     }
    199     return result;
    200     }
    201 
    202     public String getName() {
     197        result = maintainer;
     198    }
     199    return result;
     200    }
     201
     202    public String getName() {
    203203    String result = StaticStrings.EMPTY_STR;
    204204    if(name_element != null) {
    205         result = MSMUtils.getValue(name_element);
     205        result = MSMUtils.getValue(name_element);
    206206    }
    207207    else if(name != null) {
    208         result = name;
    209     }
    210     return result;
    211     }
    212 
    213     /** Retrieve the short name for this collection which, given this current file is in <col_name>/etc/collect.cfg, is the name of this file's parent file's parent.
    214      * @return the short name of this collection as a String
    215      */
    216     /*
    217     public String getShortName() {
     208        result = name;
     209    }
     210    return result;
     211    }
     212   
     213    /** Retrieve the short name for this collection which, given this current file is in <col_name>/etc/collect.cfg, is the name of this file's parent file's parent.
     214     * @return the short name of this collection as a String
     215     */
     216    public String getShortName() {
    218217    return file.getParentFile().getParentFile().getName();
    219     }
    220     */
    221 
    222     /** Display the title for this collection. */
    223     public String toString() {
    224         return getName();
    225     }
     218    }
     219   
     220    /** Display the title for this collection. */
     221    public String toString() {
     222    return getName() + StaticStrings.SPACE_CHARACTER + StaticStrings.OPEN_PARENTHESIS_CHARACTER + getShortName() + StaticStrings.CLOSE_PARENTHESIS_CHARACTER;
     223    }
    226224}
Note: See TracChangeset for help on using the changeset viewer.