001: package org.enhydra.jawe;
002:
003: import java.net.URL;
004: import java.util.ArrayList;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Locale;
008: import java.util.Map;
009: import java.util.MissingResourceException;
010: import java.util.Properties;
011: import java.util.ResourceBundle;
012:
013: /**
014: * Utility for implementing multi language support, and other manipulation with property
015: * files.
016: *
017: * @author Sasa Bojanic
018: */
019: public class ResourceManager {
020:
021: public static final String RESOURCE_PATH = "org.enhydra.jawe.language.JaWE";
022:
023: protected static Locale defaultLocale;
024:
025: protected static ResourceBundle defaultResourceBoundle;
026:
027: protected static Locale choosenLocale;
028:
029: protected static ResourceBundle choosenResourceBundle;
030:
031: protected static AdditionalResourceManager addResMgr;
032:
033: static {
034: reconfigure();
035: }
036:
037: /**
038: * Gets a language dependent string from the resource bundle.
039: * <p>
040: * Resource bundle represents the <i>property file</i>. For example, if property file
041: * contains something like this:<BR>
042: * <CENTER>menubar=file edit help</CENTER> method call
043: * getLanguageDependentString("menubar") will give the string <i>file edit help</i> as
044: * a result. <BR>
045: * This method reads information from property file. If can't find desired resource,
046: * returns <b>null</b>.
047: *
048: * @param nm name of the resource to fetch.
049: * @return String value of named resource.
050: */
051: public static String getLanguageDependentString(String nm) {
052: String str;
053: try {
054: str = choosenResourceBundle.getString(nm);
055: } catch (MissingResourceException mre) {
056: try {
057: str = defaultResourceBoundle.getString(nm);
058: } catch (MissingResourceException mre1) {
059: try {
060: str = addResMgr.getLanguageDependentString(nm);
061: } catch (Exception ex) {
062: str = null;
063: }
064: }
065: }
066: return str;
067: }
068:
069: public static String getLanguageDependentString(
070: AdditionalResourceManager arm, String nm) {
071: String str = ResourceManager.getLanguageDependentString(nm);
072: if (str == null && arm != null) {
073: str = arm.getLanguageDependentString(nm);
074: }
075: return str;
076: }
077:
078: public static String getResourceString(Properties properties,
079: String nm) {
080: String str = null;
081: try {
082: str = properties.getProperty(nm);
083: } catch (Exception ex) {
084: }
085: return str;
086: }
087:
088: public static List getResourceStrings(Properties properties,
089: String startsWith) {
090: return getResourceStrings(properties, startsWith, true);
091: }
092:
093: public static List getResourceStrings(Properties properties,
094: String startsWith, boolean values) {
095: List rStrs = new ArrayList();
096: int startIndex = startsWith.length();
097: Iterator it = properties.entrySet().iterator();
098: while (it.hasNext()) {
099: Map.Entry me = (Map.Entry) it.next();
100: if (((String) me.getKey()).startsWith(startsWith)) {
101: if (values)
102: rStrs.add(me.getValue());
103: else
104: rStrs.add(((String) me.getKey())
105: .substring(startIndex));
106: }
107: }
108:
109: return rStrs;
110: }
111:
112: /**
113: * Gets the url from a resource string.
114: *
115: * @param key the string key to the url in the properties.
116: * @return the resource location.
117: * @see java.lang.Class#getResource
118: */
119: public static URL getResource(Properties properties, String key) {
120: String name = properties.getProperty(key);
121: if (name != null) {
122: URL url = ResourceManager.class.getClassLoader()
123: .getResource(name);
124: return url;
125: }
126: return null;
127: }
128:
129: public static void setDefault() {
130: choosenResourceBundle = defaultResourceBoundle;
131: choosenLocale = defaultLocale;
132: // XMLUtil.setChoosenResources(choosenResourceBundle);
133: }
134:
135: public static void setSystem() {
136: choosenLocale = Locale.getDefault();
137: choosenResourceBundle = ResourceBundle.getBundle(RESOURCE_PATH,
138: choosenLocale);
139: // XMLUtil.setChoosenResources(choosenResourceBundle);
140: }
141:
142: /**
143: * Returns the default resource bundle.
144: */
145: public static ResourceBundle getDefaultResourceBundle() {
146: return defaultResourceBoundle;
147: }
148:
149: /**
150: * Returns the current locale.
151: */
152: public static ResourceBundle getChoosenResourceBundle() {
153: return choosenResourceBundle;
154: }
155:
156: /**
157: * Returns the default locale.
158: */
159: public static Locale getDefaultLocale() {
160: return defaultLocale;
161: }
162:
163: /**
164: * Returns the current locale.
165: */
166: public static Locale getChoosenLocale() {
167: return choosenLocale;
168: }
169:
170: /**
171: * Sets the new resource and locale to be used.
172: */
173: public static void setChoosen(Locale loc)
174: throws MissingResourceException {
175: Locale previousLocale = choosenLocale;
176: try {
177: choosenLocale = loc;
178: choosenResourceBundle = ResourceBundle.getBundle(
179: RESOURCE_PATH, loc);
180: // XMLUtil.setChoosenResources(choosenResourceBundle);
181: } catch (Exception ex) {
182: choosenLocale = previousLocale;
183: }
184: }
185:
186: public static void reconfigure() {
187: try {
188: // default is English
189: defaultLocale = new Locale("");
190: defaultResourceBoundle = ResourceBundle.getBundle(
191: RESOURCE_PATH, defaultLocale);
192: // chose the default system settings at the start
193: String startingLocale = JaWEManager.getInstance()
194: .getStartingLocale();
195: if (startingLocale != null && startingLocale.length() > 0) {
196: if (!startingLocale.equals("default")) {
197: choosenLocale = new Locale(startingLocale);
198: } else {
199: choosenLocale = defaultLocale;
200: }
201: } else {
202: choosenLocale = Locale.getDefault();
203: }
204: if (startingLocale != null
205: && !startingLocale.equals("default")) {
206: choosenResourceBundle = ResourceBundle.getBundle(
207: RESOURCE_PATH, choosenLocale);
208: } else {
209: choosenResourceBundle = defaultResourceBoundle;
210: }
211: Properties properties = new Properties();
212: properties.put("AdditionalLanguagePropertyFile.1",
213: JaWEConstants.JAWE_LANGUAGE_MISC_PROPERTYFILE_NAME);
214: addResMgr = new AdditionalResourceManager(properties);
215:
216: } catch (MissingResourceException mre) {
217: System.err.println(RESOURCE_PATH + ".properties not found");
218: System.exit(1);
219: }
220:
221: // XMLUtil.setDefaultResources(defaultResourceBoundle);
222: // XMLUtil.setChoosenResources(choosenResourceBundle);
223: }
224:
225: }
|