View Javadoc

1   /*
2    * This file is part of hyphenType. hyphenType is free software: you can
3    * redistribute it and/or modify it under the terms of the GNU General Public
4    * License as published by the Free Software Foundation, either version 3 of the
5    * License, or (at your option) any later version. hyphenType is distributed in
6    * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
8    * the GNU General Public License for more details. You should have received a
9    * copy of the GNU General Public License along with hyphenType. If not, see
10   * <http://www.gnu.org/licenses/>.
11   */
12  package org.hyphenType.util;
13  
14  import java.util.Enumeration;
15  import java.util.Locale;
16  import java.util.MissingResourceException;
17  import java.util.PropertyResourceBundle;
18  import java.util.ResourceBundle;
19  
20  import org.hyphenType.datastructure.Options;
21  import org.hyphenType.datastructure.annotations.ArgumentsObject;
22  import org.hyphenType.debug.HTLogger;
23  import org.hyphenType.documentation.OptionListResourceBundle;
24  import org.hyphenType.util.resourcebundles.AliasResourceBundle;
25  import org.hyphenType.util.resourcebundles.GenericEncodingControl;
26  
27  /**
28   * @author Aurelio Akira M. Matsui
29   */
30  public class I18NResourceBundle extends ResourceBundle {
31  
32      private ResourceBundle rb;
33  
34      public I18NResourceBundle(Class<? extends Options<?>> clazz) {
35          String bundlePath = "";
36  
37          ArgumentsObject ao = (ArgumentsObject) clazz.getAnnotation(ArgumentsObject.class);
38  
39          if (ao == null || ao.resourceBundlesLocation().equals(""))
40              bundlePath = clazz.getName();
41          else
42              bundlePath = ao.resourceBundlesLocation();
43  
44          try {
45              HTLogger.log("Trying to read " + bundlePath + "...");
46              ResourceBundle result;
47              if (ao == null)
48                  result = PropertyResourceBundle.getBundle(bundlePath, Locale.getDefault(), I18NResourceBundle.class.getClassLoader(), new GenericEncodingControl(ArgumentsObject.DEFAULT_RB_ENCODING));
49              else
50                  result = PropertyResourceBundle.getBundle(bundlePath, Locale.getDefault(), I18NResourceBundle.class.getClassLoader(), new GenericEncodingControl(ao.resourceBundlesEncoding()));
51              rb = AliasResourceBundle.convert(result);
52          } catch (MissingResourceException e) {
53              HTLogger.log(e);
54              rb = new OptionListResourceBundle(clazz);
55          }
56          if(HTLogger.debugMode()) {
57              for(String key :rb.keySet()) {
58                  HTLogger.log(String.format("%s = %s", key, rb.getString(key)));
59              }
60          }
61      }
62  
63      @Override
64      public Enumeration<String> getKeys() {
65          return rb.getKeys();
66      }
67  
68      @Override
69      protected Object handleGetObject(String key) {
70          return rb.getObject(key);
71      }
72  }