001: /**
002: * Caption: Zaval Java Resource Editor
003: * $Revision: 0.37 $
004: * $Date: 2002/03/28 9:24:42 $
005: *
006: * @author: Victor Krapivin
007: * @version: 1.3
008: *
009: * Zaval JRC Editor is a visual editor which allows you to manipulate
010: * localization strings for all Java based software with appropriate
011: * support embedded.
012: *
013: * For more info on this product read Zaval Java Resource Editor User's Guide
014: * (It comes within this package).
015: * The latest product version is always available from the product's homepage:
016: * http://www.zaval.org/products/jrc-editor/
017: * and from the SourceForge:
018: * http://sourceforge.net/projects/zaval0002/
019: *
020: * Contacts:
021: * Support : support@zaval.org
022: * Change Requests : change-request@zaval.org
023: * Feedback : feedback@zaval.org
024: * Other : info@zaval.org
025: *
026: * Copyright (C) 2001-2002 Zaval Creative Engineering Group (http://www.zaval.org)
027: *
028: * This program is free software; you can redistribute it and/or
029: * modify it under the terms of the GNU General Public License
030: * (version 2) as published by the Free Software Foundation.
031: *
032: * This program is distributed in the hope that it will be useful,
033: * but WITHOUT ANY WARRANTY; without even the implied warranty of
034: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
035: * GNU General Public License for more details.
036: *
037: * You should have received a copy of the GNU General Public License
038: * along with this program; if not, write to the Free Software
039: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
040: *
041: */package org.zaval.util;
042:
043: import java.util.*;
044:
045: public class SafeResourceBundle {
046: protected ResourceBundle rb = null;
047:
048: public static final String FAILURE_STRING = "?????";
049: public static final String START_VAR = "[%";
050: public static final String FINISH_VAR = "%]";
051:
052: public SafeResourceBundle(String resName, Locale loc) {
053: try {
054: if (loc == null) {
055: Locale saved = Locale.getDefault();
056: Locale.setDefault(new Locale("en", "US"));
057: rb = ResourceBundle.getBundle(resName);
058: Locale.setDefault(saved);
059: } else
060: rb = ResourceBundle.getBundle(resName, loc);
061: } catch (Exception e) {
062: System.err.println(resName + ": resource not found");
063: }
064: }
065:
066: public String getString(String k) {
067: if (rb == null)
068: return FAILURE_STRING;
069: String res = null;
070: try {
071: res = rb.getString(k);
072: } catch (Exception e) {
073: System.err.println(k + ": resource not found");
074: }
075: if (res != null)
076: return res;
077: return FAILURE_STRING;
078: }
079:
080: public String getString(String k, Hashtable ht) {
081: String templ = getString(k);
082: String res = "";
083: int ind, ind2;
084: do {
085: ind = templ.indexOf(START_VAR);
086: if (ind < 0)
087: res += templ;
088: else {
089: res += templ.substring(0, ind);
090: ind2 = templ.indexOf(FINISH_VAR, ind);
091: if (ind2 >= 0) {
092: String repl = (String) ht.get(templ.substring(
093: ind + 2, ind2));
094: if (repl == null)
095: repl = "";
096: res += repl;
097: templ = templ.substring(ind2 + 2);
098: } else {
099: res += START_VAR;
100: templ = templ.substring(ind + 2);
101: }
102: }
103: } while (ind >= 0);
104: return res;
105: }
106:
107: public static Locale parseSuffix(String suffix) {
108: if (suffix == null || suffix.length() == 0)
109: return null;
110: int undInd = suffix.indexOf('_');
111: String sl = suffix;
112: String sc = "";
113: if (undInd > 0) {
114: sl = suffix.substring(0, undInd);
115: sc = suffix.substring(undInd + 1);
116: }
117: return new Locale(sl, sc);
118: }
119:
120: public SafeResourceBundle(String resName, String locale) {
121: this(resName, parseSuffix(locale));
122: }
123: }
|