Changeset 33769 for main


Ignore:
Timestamp:
2019-12-09T11:29:20+13:00 (4 years ago)
Author:
kjdon
Message:

updated getTextSTring to contain all the functionality from ServiceRack's getTextString, so need to pass in class, and stop_at_class arguments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/Dictionary.java

    r33671 r33769  
    3030    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.Dictionary.class.getName());
    3131
     32    /** The default servlet dictionary */
     33    public static final String core_servlet_dictionary_name = "core_servlet_dictionary";
     34   
    3235    /** The locale of this dictionary. */
    3336    protected Locale locale = null;
     
    97100        }
    98101        // try the specified class loader
    99         try
    100         {
     102        if (loader != null) {
     103            try
     104            {
    101105            this.raw = ResourceBundle.getBundle(this.resource, this.locale, loader);
    102106            return;
    103         }
    104         catch (Exception e)
    105         {
    106         }
    107         ;
    108 
     107            }
     108            catch (Exception e)
     109            {
     110            }
     111        }
     112
     113        // try with default class loader
    109114        try
    110115        {
    111             this.raw = ResourceBundle.getBundle(this.resource, this.locale);
     116            this.raw = ResourceBundle.getBundle(this.resource, this.locale);
    112117        }
    113118        catch (Exception ex)
     
    234239    }
    235240
    236   public static String getTextString(String key, String lang, String dictionary_name, ClassLoader class_loader, String[] args)
     241   
     242    public static String getTextString(String key, String lang, String[] args, String dictionary_name, Class class_obj, String stop_at_class, ClassLoader class_loader)
    237243  {
    238244
    239       // we want to use the collection class loader in case there are coll specific files
    240       if (dictionary_name != null)
    241         {
    242           // just try the one specified dictionary
    243           Dictionary dict = new Dictionary(dictionary_name, lang, class_loader);
    244           String result = dict.get(key, args);
    245           if (result == null)
    246         { // not found
    247          
    248           logger.error("couldn't find key in dict, "+key);
    249           return "_" + key + "_";
    250           //return null;
    251         }
     245      if (dictionary_name == null && class_obj == null) {
     246      // no dictionary or class was specified, just use the default
     247      // dictionary
     248      dictionary_name = core_servlet_dictionary_name;
     249      }
     250     
     251      if (dictionary_name != null)
     252      {
     253      // just try the one specified dictionary
     254      Dictionary dict = new Dictionary(dictionary_name, lang, class_loader);
     255      String result = dict.get(key, args);
     256      if (result == null)
     257      { // not found
     258         
     259          logger.error("couldn't find key in dict, "+key);
     260          //return "_" + key + "_"; // should we use this??
     261          return null;
     262      }
     263      return result;
     264      }
     265
     266      // try the class names first before the default dictionary
     267      String original_class_name = class_obj.getName();
     268      original_class_name = original_class_name.substring(original_class_name.lastIndexOf('.') + 1);
     269      Dictionary dict = new Dictionary(original_class_name, lang, class_loader);
     270      String result = dict.get(key, args);
     271      if (result != null)
     272      {
     273      return result;
     274      }
     275
     276      // we have to try super classes
     277      String class_name;
     278      Class c = class_obj.getSuperclass();
     279      while (c != null)
     280      {
     281      class_name = c.getName();
     282      class_name = class_name.substring(class_name.lastIndexOf('.') + 1);
     283      if (class_name.equals(stop_at_class))
     284      {
     285          // this is as far as we go
     286          break;
     287      }
     288      dict = new Dictionary(class_name, lang, class_loader);
     289      result = dict.get(key, args);
     290      if (result != null) {
    252291          return result;
    253         }
    254       return "_" + key + "_";
     292      }
     293      c = c.getSuperclass();
     294      }
     295
     296      // if we got here, class names were no help, try the default dictionary
     297      dict = new Dictionary(core_servlet_dictionary_name, lang, class_loader);
     298
     299      // First we try the key qualified with the original class name
     300      // (eg have TextQuery.name and LuceneSearch.TextQuery.name in the
     301      // default properties file)
     302      String full_key = original_class_name+"."+key;
     303      result = dict.get(full_key, args);
     304      if (result == null) {
     305      result = dict.get(key, args);
     306      }
     307      return result;
     308       
    255309  }
    256310
Note: See TracChangeset for help on using the changeset viewer.