001: /**
002: * $Id: NetFileResource.java,v 1.19 2005/11/30 11:26:34 ss150821 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.netfile.servlet.java1;
014:
015: import java.util.*;
016: import com.sun.portal.log.common.PortalLogger;
017: import java.util.logging.*;
018: import java.text.MessageFormat;
019:
020: public class NetFileResource {
021:
022: private static Logger logger = PortalLogger
023: .getLogger(NetFileResource.class);
024:
025: private java.util.HashMap ht_resource_bundle_entry_set;
026:
027: private String s_bundle;
028:
029: private String s_locale;
030:
031: private String s_empty_string = "";
032:
033: private static final java.util.HashMap ht_locales = new java.util.HashMap(
034: 6);
035:
036: private static final java.util.HashMap ht_s_locales = new java.util.HashMap(
037: 6);
038:
039: private static NetFileResource nfRes;
040:
041: private NetFileResource() {
042: }
043:
044: private NetFileResource(String bundle, String locale)
045: throws NetFileException {
046: this ();
047: if (bundle == null) {
048: throw new NetFileException("null bundle name passed");
049: }
050: if (locale == null) {
051: throw new NetFileException("null locale name passed");
052: }
053: s_bundle = bundle;
054: s_locale = locale;
055: ht_resource_bundle_entry_set = getLocalisedBundle(bundle,
056: s_locale);
057: }
058:
059: /*
060: * This will pick up default locale from the AMConfig.properties
061: */
062: private NetFileResource(String bundle) throws NetFileException {
063: this ();
064: if (bundle == null) {
065: throw new NetFileException("null bundle name passed");
066: }
067: ResourceBundle rb_amconfig = ResourceBundle
068: .getBundle("AMConfig");
069: String s_locale = rb_amconfig
070: .getString("com.iplanet.am.locale");
071: if (s_locale == null) {
072: throw new NetFileException(
073: "There is no key as com.iplanet.am.locale in AMConfig.properties");
074: }
075: if (s_locale.equals(s_empty_string)) {
076: throw new NetFileException(
077: "There is no value for com.iplanet.am.locale in AMConfig.properties");
078: }
079: s_bundle = bundle;
080: this .s_locale = s_locale;
081: ht_resource_bundle_entry_set = getLocalisedBundle(bundle,
082: s_locale);
083: }
084:
085: public static synchronized NetFileResource getInstance(
086: String bundle, String locale) throws NetFileException {
087: if (nfRes == null) {
088: nfRes = new NetFileResource(bundle, locale);
089: return nfRes;
090: } else {
091: return nfRes;
092: }
093: }
094:
095: public static synchronized NetFileResource getInstance(String bundle)
096: throws NetFileException {
097: if (nfRes == null) {
098: nfRes = new NetFileResource(bundle);
099: return nfRes;
100: } else {
101: return nfRes;
102: }
103: }
104:
105: private HashMap getLocalisedBundle(String bundle, String locale)
106: throws NetFileException {
107: HashMap ht_locale_specific_bundles = getLocaleHashtable(locale);
108: HashMap ht_locale_specific_bundle = (HashMap) (ht_locale_specific_bundles
109: .get(bundle));
110: if (ht_locale_specific_bundle == null) {
111: ht_locale_specific_bundle = populatei18nBucket(bundle,
112: locale);
113: ht_locale_specific_bundles.put(bundle,
114: ht_locale_specific_bundle);
115: } else {
116: }
117: return ht_locale_specific_bundle;
118: }
119:
120: private HashMap getLocaleHashtable(String locale)
121: throws NetFileException {
122: try {
123: HashMap ht_locale_specific_bundles = (HashMap) (ht_locales
124: .get(locale));
125: if (ht_locale_specific_bundles == null) {
126: ht_locale_specific_bundles = new HashMap(6);
127: } else {
128: }
129: ht_locales.put(locale, ht_locale_specific_bundles);
130: return ht_locale_specific_bundles;
131: } catch (Exception e) {
132: // logger.log(Level.SEVERE, "Exception in searching for hashtable
133: // containg bundles in a locale", e);
134: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1137");
135: throw new NetFileException(e);
136: }
137: }
138:
139: public String getString(String s_key) {
140: Object o_value = ht_resource_bundle_entry_set.get(s_key);
141: if (o_value == null) {
142: // logger.severe("Key "+s_key+" does not exist in bundle
143: // "+s_bundle+" for locale "+s_locale);
144: Object[] params1 = { s_key, " does not exist in bundle ",
145: s_bundle, " for locale ", s_locale };
146: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1138", params1);
147: return s_key;
148: }
149: return ((String) (o_value));
150: }
151:
152: public String getString(String key, Object[] args) {
153: MessageFormat mf = new MessageFormat("");
154: Locale loc = getLocale(s_locale);
155: mf.setLocale(loc);
156: mf.applyPattern(getString(key));
157:
158: return mf.format(args, new StringBuffer(), null).toString();
159: }
160:
161: // get the resource bundle and enurmerate through all
162: // key values and append to string as "key=value"
163: // end each string with a new line.
164:
165: public String getAppletResources(String bundle, String locale)
166: throws NetFileException {
167: HashMap locale_specific_bundles = (HashMap) (ht_s_locales
168: .get(locale));
169: if (locale_specific_bundles == null) {
170: locale_specific_bundles = new HashMap(6);
171: ht_s_locales.put(locale, locale_specific_bundles);
172: }
173: String bundle_as_string = (String) (locale_specific_bundles
174: .get(bundle));
175: if (bundle_as_string == null) {
176: HashMap localised_bundle = getLocalisedBundle(bundle,
177: locale);
178: StringBuffer st = new StringBuffer();
179: String value = s_empty_string;
180: String key = s_empty_string;
181: Set keySet = localised_bundle.keySet();
182: for (Iterator iter = keySet.iterator(); iter.hasNext();) {
183: key = (String) iter.next();
184: value = (String) localised_bundle.get(key);
185: st.append(key + "=" + value).append('\n');
186: }
187: bundle_as_string = st.toString();
188: locale_specific_bundles.put(bundle, bundle_as_string);
189: }
190: return bundle_as_string;
191: }
192:
193: private HashMap populatei18nBucket(String bundle, String locale)
194: throws NetFileException {
195: HashMap bucket = new HashMap();
196: try {
197: Object key = null;
198: Object value = null;
199: Locale loc = getLocale(locale);
200: java.util.ResourceBundle resBundle = java.util.ResourceBundle
201: .getBundle(bundle, loc);
202:
203: for (Enumeration e = resBundle.getKeys(); e
204: .hasMoreElements();) {
205: key = e.nextElement();
206: value = resBundle.getObject((String) key);
207: bucket.put(key, value);
208: }
209: } catch (Exception e) {
210: throw new NetFileException(e);
211: }
212: return bucket;
213: }
214:
215: private Locale getLocale(String locale) {
216: if ((locale == null) || (locale.equals(""))) {
217: locale = Locale.getDefault().toString();
218: }
219:
220: StringTokenizer locst = new StringTokenizer(locale, "_");
221: String lang = (locst.hasMoreTokens() ? locst.nextToken() : "");
222: String country = (locst.hasMoreTokens() ? locst.nextToken()
223: : "");
224: String variant = (locst.hasMoreTokens() ? locst.nextToken()
225: : "");
226:
227: while (locst.hasMoreTokens()) {
228: variant += "_" + locst.nextToken();
229: }
230:
231: Locale loc = new java.util.Locale(lang, country, variant);
232:
233: return loc;
234: }
235:
236: }
|