KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > WinstoneResourceBundle


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone;
8
9 import java.util.Locale JavaDoc;
10 import java.util.ResourceBundle JavaDoc;
11
12 /**
13  * A ResourceBundle that includes the ability to do string replacement on the
14  * resources it retrieves.
15  *
16  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
17  */

18 public class WinstoneResourceBundle {
19     private ResourceBundle JavaDoc resources;
20
21     /**
22      * Constructor
23      */

24     public WinstoneResourceBundle(String JavaDoc baseName) {
25         this.resources = ResourceBundle.getBundle(baseName);
26     }
27
28     public WinstoneResourceBundle(String JavaDoc baseName, Locale JavaDoc loc) {
29         this.resources = ResourceBundle.getBundle(baseName, loc);
30     }
31
32     public WinstoneResourceBundle(String JavaDoc baseName, Locale JavaDoc loc, ClassLoader JavaDoc cl) {
33         this.resources = ResourceBundle.getBundle(baseName, loc, cl);
34     }
35
36     /**
37      * Default getString method
38      */

39     public String JavaDoc getString(String JavaDoc key) {
40         return this.resources.getString(key);
41     }
42
43     /**
44      * Perform a string replace for a single from/to pair.
45      */

46     public String JavaDoc getString(String JavaDoc key, String JavaDoc parameter) {
47         return getString(key, new String JavaDoc[] { parameter });
48     }
49
50     /**
51      * Perform a string replace for a double from/to pair.
52      */

53     public String JavaDoc getString(String JavaDoc key, String JavaDoc[] parameters) {
54         String JavaDoc myCopy = this.resources.getString(key);
55         if (parameters != null)
56             for (int n = 0; n < parameters.length; n++)
57                 myCopy = globalReplace(myCopy, "[#" + n + "]", parameters[n]);
58         return myCopy;
59     }
60
61     /**
62      * Just does a string swap, replacing occurrences of from with to.
63      */

64     public static String JavaDoc globalReplace(String JavaDoc input, String JavaDoc fromMarker,
65             String JavaDoc toValue) {
66         if (input == null) {
67             return null;
68         } else if (fromMarker == null) {
69             return input;
70         }
71
72         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
73         int index = 0;
74         int foundAt = input.indexOf(fromMarker, index);
75         while (foundAt != -1) {
76             out.append(input.substring(index, foundAt));
77             out.append(toValue);
78             index = foundAt + fromMarker.length();
79             foundAt = input.indexOf(fromMarker, index);
80         }
81         out.append(input.substring(index));
82         return out.toString();
83     }
84     
85     public static String JavaDoc globalReplace(String JavaDoc input, String JavaDoc parameters[][]) {
86         if (parameters != null)
87             for (int n = 0; n < parameters.length; n++)
88                 input = globalReplace(input, parameters[n][0], parameters[n][1]);
89         return input;
90     }
91 }
92
Popular Tags