001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.admin.resources;
007:
008: import java.util.*;
009: import java.io.*;
010: import java.util.*;
011: import java.util.logging.Level;
012: import java.util.logging.Logger;
013:
014: import com.sun.portal.search.admin.CompassAdminConstants;
015: import com.sun.portal.log.common.PortalLogger;
016:
017: public class SearchResource {
018: private static Logger debugLogger = PortalLogger
019: .getLogger(SearchResource.class);
020: protected static Locale defaultLocale = Locale.US;
021: private static HashMap rbMap = new HashMap();
022:
023: public static ResourceBundle getResourceBundle(Locale locale) {
024: if (locale == null)
025: locale = defaultLocale;
026: else
027: defaultLocale = locale;
028: ResourceBundle rb = (ResourceBundle) rbMap.get(locale);
029: if (rb == null) {
030: try {
031: rb = ResourceBundle.getBundle(
032: CompassAdminConstants.RESOURCE_BUNDLE_FILE,
033: locale);
034: rbMap.put(locale, rb);
035: } catch (MissingResourceException e) {
036: debugLogger.log(Level.INFO, "PSSH_CSPSAR",
037: CompassAdminConstants.RESOURCE_BUNDLE_FILE);
038: }
039: }
040: return rb;
041: }
042:
043: public static String geti18nString(String key) {
044: return geti18nString(key, defaultLocale);
045: }
046:
047: public static String geti18nString(String key, Locale locale,
048: boolean returnKeyIfNotFound) {
049: ResourceBundle rb = getResourceBundle(locale);
050: if (rb != null) {
051: try {
052: String value = rb.getString(key);
053: return value;
054: } catch (MissingResourceException e) {
055: }
056: }
057: if (returnKeyIfNotFound) {
058: return key;
059: } else {
060: return null;
061: }
062: }
063:
064: public static String geti18nString(String key, Locale locale) {
065: return geti18nString(key, locale, true);
066: }
067:
068: public static String[] geti18nStrings(String[] keys, Locale locale) {
069: String[] translatedStrs = (String[]) keys.clone();
070: for (int i = 0; i < translatedStrs.length; i++) {
071: translatedStrs[i] = geti18nString(keys[i], locale, true);
072: }
073: return translatedStrs;
074: }
075:
076: public static String[] geti18nArray(String key, String sep,
077: Locale locale) {
078: ResourceBundle rb = getResourceBundle(locale);
079: String[] failed = { key };
080: if (rb != null) {
081: String original = null;
082: try {
083: original = rb.getString(key);
084: } catch (MissingResourceException e) {
085: return failed;
086: }
087: StringBuffer value = new StringBuffer(original);
088: int arraysize = 1;
089: for (int i = 0; i < value.length(); i++) {
090: if (sep.indexOf(value.charAt(i)) != -1) {
091: arraysize++;
092: }
093: }
094: String[] outgoing = new String[arraysize];
095: int y, z = 0;
096: if (value.toString().indexOf(sep) != -1) {
097: while (value.length() > 0) {
098: if (value.toString().indexOf(sep) != -1) {
099: y = value.toString().indexOf(sep);
100: if (y != value.toString().lastIndexOf(sep)) {
101: outgoing[z] = value.toString().substring(0,
102: y);
103: z++;
104: value.delete(0, y + 1);
105: } else if (value.toString().lastIndexOf(sep) == y) {
106: outgoing[z] = value.toString().substring(0,
107: value.toString().indexOf(sep));
108: z++;
109: value.delete(0, value.toString().indexOf(
110: sep) + 1);
111: outgoing[z] = value.toString();
112: z++;
113: value.delete(0, value.length());
114: }
115: }
116: }
117: } else {
118: outgoing[0] = value.toString();
119: }
120: value = null;
121: return outgoing;
122: }
123: return failed;
124: }
125: }
|