001: package com.salmonllc.localizer;
002:
003: //** Copyright Statement ***************************************************
004: //The Salmon Open Framework for Internet Applications (SOFIA)
005: // Copyright (C) 1999 - 2002, Salmon LLC
006: //
007: // This program is free software; you can redistribute it and/or
008: // modify it under the terms of the GNU General Public License version 2
009: // as published by the Free Software Foundation;
010: //
011: // This program is distributed in the hope that it will be useful,
012: // but WITHOUT ANY WARRANTY; without even the implied warranty of
013: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: // GNU General Public License for more details.
015: //
016: // You should have received a copy of the GNU General Public License
017: // along with this program; if not, write to the Free Software
018: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: //
020: // For more information please visit http://www.salmonllc.com
021: //** End Copyright Statement ***************************************************
022: /////////////////////////
023: //$Archive: /JADE/SourceCode/com/salmonllc/localizer/LanguageResourceFinder.java $
024: //$Author: Dan $
025: //$Revision: 5 $
026: //$Modtime: 11/11/02 3:21p $
027: /////////////////////////
028: import java.util.Enumeration;
029: import java.util.Hashtable;
030:
031: import com.salmonllc.properties.Props;
032: import com.salmonllc.util.MessageLog;
033:
034: /**
035: * This class is used by the sytem to maintain a list of all language localizers used by the system
036: */
037: public class LanguageResourceFinder {
038: private static Hashtable _loaded = new Hashtable();
039: private static Hashtable _failures = new Hashtable();
040:
041: /**
042: * Clears out the resource hash table so that resources will be reloaded
043: */
044: public static synchronized void clearCache() {
045: if (_loaded != null) {
046: _loaded.clear();
047: _failures.clear();
048: }
049: }
050:
051: /**
052: * Returns an Enumeration of keys for one language
053: */
054: public static Enumeration getKeys(String appName, String language) {
055: if (appName == null)
056: return null;
057: String localizerClass = Props.getProps(appName, null)
058: .getProperty(Props.LOCALIZER_CLASS);
059: if (localizerClass == null)
060: return null;
061: Localizer l = getLocalizer(appName, language, localizerClass);
062: if (l == null)
063: return null;
064: else
065: return l.getKeys();
066: }
067:
068: private static Localizer getLocalizer(String app, String lang,
069: String loader) {
070: String key = app + ":" + lang;
071: if (_loaded.containsKey(key))
072: return (Localizer) _loaded.get(key);
073: else if (_failures.containsKey(key))
074: return null;
075: else
076: return loadLocalizer(key, app, lang, loader);
077: }
078:
079: /**
080: * Returns the resource specified by appName and key using the specified LanguagePreference object
081: */
082: public static String getResource(String appName, String key,
083: LanguagePreferences pref) {
084: if (key == null || appName == null)
085: return null;
086: String localizerClass = Props.getProps(appName, null)
087: .getProperty(Props.LOCALIZER_CLASS);
088: if (localizerClass == null || pref == null)
089: return null;
090: String lang = pref.getFirstLanguage();
091: while (lang != null) {
092: Localizer l = getLocalizer(appName, lang, localizerClass);
093: if (l != null) {
094: String resource = l.getResource(key);
095: if (resource != null)
096: return resource;
097: }
098: lang = pref.getNextLanguage();
099: }
100: return null;
101: }
102:
103: private static synchronized Localizer loadLocalizer(String key,
104: String app, String lang, String loader) {
105: try {
106: Class c = Class.forName(loader, true, Thread
107: .currentThread().getContextClassLoader());
108: Localizer l = (Localizer) c.newInstance();
109: boolean ret = l.loadData(app, lang);
110: if (ret) {
111: _loaded.put(key, l);
112: return l;
113: } else {
114: _failures.put(key, new Object());
115: return null;
116: }
117: } catch (Exception e) {
118: MessageLog.writeErrorMessage(
119: "Error loading Localizer class:" + loader, e, null);
120: _failures.put(key, new Object());
121: return null;
122: }
123:
124: }
125: }
|