001: /*
002: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: */
007: package winstone;
008:
009: import java.util.Locale;
010: import java.util.ResourceBundle;
011:
012: /**
013: * A ResourceBundle that includes the ability to do string replacement on the
014: * resources it retrieves.
015: *
016: * @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
017: */
018: public class WinstoneResourceBundle {
019: private ResourceBundle resources;
020:
021: /**
022: * Constructor
023: */
024: public WinstoneResourceBundle(String baseName) {
025: this .resources = ResourceBundle.getBundle(baseName);
026: }
027:
028: public WinstoneResourceBundle(String baseName, Locale loc) {
029: this .resources = ResourceBundle.getBundle(baseName, loc);
030: }
031:
032: public WinstoneResourceBundle(String baseName, Locale loc,
033: ClassLoader cl) {
034: this .resources = ResourceBundle.getBundle(baseName, loc, cl);
035: }
036:
037: /**
038: * Default getString method
039: */
040: public String getString(String key) {
041: return this .resources.getString(key);
042: }
043:
044: /**
045: * Perform a string replace for a single from/to pair.
046: */
047: public String getString(String key, String parameter) {
048: return globalReplace(this .resources.getString(key), "[#0]",
049: parameter);
050: }
051:
052: /**
053: * Perform a string replace for a set of from/to pairs.
054: */
055: public String getString(String key, String[] parameters) {
056: String myCopy = this .resources.getString(key);
057: if (parameters != null) {
058: String tokens[][] = new String[parameters.length][2];
059: for (int n = 0; n < parameters.length; n++) {
060: tokens[n] = new String[] { "[#" + n + "]",
061: parameters[n] };
062: }
063: myCopy = globalReplace(myCopy, tokens);
064: }
065: return myCopy;
066: }
067:
068: /**
069: * Just does a string swap, replacing occurrences of from with to.
070: */
071: public static String globalReplace(String input, String fromMarker,
072: String toValue) {
073: StringBuffer out = new StringBuffer(input);
074: globalReplace(out, fromMarker, toValue);
075: return out.toString();
076: }
077:
078: private static void globalReplace(StringBuffer input,
079: String fromMarker, String toValue) {
080: if (input == null) {
081: return;
082: } else if (fromMarker == null) {
083: return;
084: }
085:
086: int index = 0;
087: int foundAt = input.indexOf(fromMarker, index);
088: while (foundAt != -1) {
089: if (toValue == null) {
090: toValue = "(null)";
091: }
092: input.replace(foundAt, foundAt + fromMarker.length(),
093: toValue);
094: index = foundAt + toValue.length();
095: foundAt = input.indexOf(fromMarker, index);
096: }
097: }
098:
099: public static String globalReplace(String input,
100: String parameters[][]) {
101: if (parameters != null) {
102: StringBuffer out = new StringBuffer(input);
103: for (int n = 0; n < parameters.length; n++) {
104: globalReplace(out, parameters[n][0], parameters[n][1]);
105: }
106: return out.toString();
107: } else {
108: return input;
109: }
110: }
111: }
|