Changeset 6854


Ignore:
Timestamp:
2004-02-20T12:38:04+13:00 (20 years ago)
Author:
mdewsnip
Message:

Fixed another problem with dictionary strings not being read as UTF-8 properly. This time it was the argument processing at fault.

File:
1 edited

Legend:

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

    r6849 r6854  
    7070    extends HashMap {
    7171
    72     static final private boolean KEY_LIST_DEBUG = false;
    73     static final private String KEY_LIST_FILENAME = "keylist.txt";
    74 
    75     /** A String which more explicitly states the Locale of this dictionary. */
    76     public String language = null;
    7772    /** The font used when displaying various html text. */
    78     static private FontUIResource font = null;
     73    private FontUIResource font = null;
    7974    /** A reference to remind us of the current locale. */
    8075    private Locale locale = null;
    8176    /** The ResourceBundle which contains the raw key-value mappings. Loaded from a file named "dictionary<I>locale</I>.properties*/
    82     static private ResourceBundle dictionary = null;
    83 
    84     private TreeSet key_list = null;
    85 
    86     /**  Constructs the Dictionary class by first checking if a Locale has been set. If not the default locale is used, and a ResourceBundle is created. Finally a single important String, Language, is made available outside the class so a more read-able version of the Locale of this Dictionary is present.
    87      * @param locale The <strong>Locale</strong> used to load the desired dictionary resource bundle.
    88      */
    89     public Dictionary(Locale locale, FontUIResource font) {
     77    private ResourceBundle dictionary = null;
     78
     79
     80    public Dictionary(Locale locale, FontUIResource font)
     81    {
    9082    super();
    9183
    92     if (KEY_LIST_DEBUG) {
    93         // Reload the keylist
    94         File file = new File(KEY_LIST_FILENAME);
    95         key_list = new TreeSet();
    96         try {
    97         BufferedReader br = new BufferedReader(new FileReader(file));
    98         String line;
    99         while((line = br.readLine()) != null) {
    100             key_list.add(line);
    101         }
    102         br.close();
    103         br = null;
    104         }
    105         catch(Exception error) {
    106         error.printStackTrace();
    107         }
    108     }
    109 
    110     // Initialize.
     84    // Initialize
    11185    this.font = font;
    112     if (locale == null) {
    113         this.locale = Locale.getDefault();
    114     }
    115     else {
    116         this.locale = locale;
    117         //Locale.setDefault(locale);
    118     }
    119     dictionary = ResourceBundle.getBundle(Utility.DICTIONARY, this.locale);
    120     // Now quickly read in language name.
    121     language = dictionary.getString("Language");
    122     }
    123 
    124 
    125     public void destroy() {
    126 //      if(key_list != null) {
    127 //          try {
    128 //          FileOutputStream fos = new FileOutputStream(KEY_LIST_FILENAME);
    129 //          for(Iterator iter = key_list.iterator(); iter.hasNext(); ) {
    130 //              String value = (String) iter.next();
    131 //              fos.write(value.getBytes());
    132 //              fos.write('\n');
    133 //              value = null;
    134 //          }
    135 //          fos.close();
    136 //          fos = null;
    137 //          }
    138 //          catch(Exception error) {
    139 //          error.printStackTrace();
    140 //          }
    141 //      }
     86    this.locale = ((locale == null) ? Locale.getDefault() : locale);
     87    this.dictionary = ResourceBundle.getBundle(Utility.DICTIONARY, this.locale);
    14288    }
    14389
     
    14692     * @return A <strong>String</strong> containing the two letter ISO639 language code.
    14793     */
    148     public String getLanguage() {
     94    public String getLanguage()
     95    {
    14996    return locale.getLanguage();
    15097    }
     
    173120    static public String get(String key, String[] args)
    174121    {
    175 //      if(key_list != null) {
    176 //          synchronized(this) {
    177 //          key_list.add(key);
    178 //          }
    179 //      }
    180122    try {
    181         String initial = dictionary.getString(key);
     123        String initial_raw = dictionary.getString(key);
     124
     125        // Convert into Unicode
     126        String initial = "";
     127        try {
     128        // This "ISO-8859-1" looks out of place, but it is very important.
     129        // It is essential to call getBytes with an 8-bit encoding, otherwise
     130        // Java kindly deems some characters "undisplayable", and replaces
     131        // them with question marks. This is NOT good.
     132        initial = new String(initial_raw.getBytes("ISO-8859-1"), "UTF-8");
     133        }
     134        catch (Exception ex) {
     135        System.err.println("Exception: " + ex);
     136        ex.printStackTrace();
     137        return initial_raw;
     138        }
    182139
    183140        // If the string contains arguments we have to insert them.
     
    190147        int closing = initial.indexOf('}');
    191148        int comment_mark = initial.indexOf('-', opening); // May not exist
    192         if(comment_mark > closing) { // May also be detecting a later comment
     149        if (comment_mark > closing) { // May also be detecting a later comment
    193150            comment_mark = -1;
    194151        }
     
    196153        // Parse arg_num
    197154        String arg_str = null;
    198         if(comment_mark != -1) {
     155        if (comment_mark != -1) {
    199156            arg_str = initial.substring(opening + 1, comment_mark);
    200157        }
     
    203160        }
    204161        int arg_num = Integer.parseInt(arg_str);
    205         if(closing + 1 < initial.length()) {
     162        if (closing + 1 < initial.length()) {
    206163            initial = initial.substring(closing + 1);
    207164        }
     
    210167        }
    211168        // Insert argument
    212         if(args != null && 0 <= arg_num && arg_num < args.length) {
     169        if (args != null && 0 <= arg_num && arg_num < args.length) {
    213170            complete = complete + args[arg_num];
    214171        }
    215         else if(arg_num >= 32) {
     172        else if (arg_num >= 32) {
    216173            String f_subargs[] = new String[1];
    217             if(font != null) {
     174            if (font != null) {
    218175            f_subargs[0] = font.getFontName();
    219176            }
     
    226183        complete = complete + initial;
    227184
    228         String unicode = "";
    229         try {
    230         // This "ISO-8859-1" looks out of place, but it is very important.
    231         // It is essential to call getBytes with an 8-bit encoding, otherwise
    232         // Java kindly deems some characters "undisplayable", and replaces
    233         // them with question marks. This is NOT good.
    234         unicode = new String(complete.getBytes("ISO-8859-1"), "UTF-8");
    235         }
    236         catch (Exception ex) {
    237         System.err.println("Exception: " + ex);
    238         Gatherer.printStackTrace(ex);
    239         return initial;
    240         }
    241         return unicode;
     185        return complete;
    242186    }
    243187    catch (Exception e) {
Note: See TracChangeset for help on using the changeset viewer.