1 /***
2 * StringManager.java
3 *
4 * Project: Dependency Tool
5 *
6 * WHEN WHO WHAT
7 * 06.06.2003 pko initial public release
8 * 20.01.2003 pko modification
9 * 10.12.2002 pko creation
10 *
11 * Copyright 2003 ELCA Informatique SA
12 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland
13 * www.elca.ch
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public License
17 * as published by the Free Software Foundation; either version 2.1 of
18 * the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 * USA
29 */
30
31 package ch.elca.dependency.util;
32
33 import java.text.MessageFormat;
34 import java.util.Hashtable;
35 import java.util.MissingResourceException;
36 import java.util.ResourceBundle;
37
38 /***
39 * A <code>StringManager</code> manages access to Strings and enables
40 * internationalization.
41 *
42 * @author Pawel Kowalski
43 * @version 1.0-beta
44 */
45 public class StringManager {
46
47 /***
48 * A ResourceBundle.
49 */
50 private ResourceBundle bundle;
51
52 /***
53 * A Hashtable with all created StringManagers.
54 */
55 private static Hashtable managers = new Hashtable();
56
57 /***
58 * Creates a new <code>StringManager</code> instance.
59 *
60 * @param packageName a <code>String</code> value
61 */
62 privateStringManager(String packageName) {/package-summary.html"> StringManager(String packageName) {
63 String bundleName = packageName + ".Strings";
64 bundle = ResourceBundle.getBundle(bundleName);
65 }
66
67 /***
68 * Get a String for a key.
69 *
70 * @param key a <code>String</code> value
71 * @return a <code>String</code> value
72 */
73 public String getString(String key) {
74 if (key == null) {
75 String message = "null-key not allowed";
76 throw new NullPointerException(message);
77 }
78
79 String str = null;
80 try {
81 str = bundle.getString(key);
82 } catch (MissingResourceException mre) {
83 str = "Cannot find message associated with key '" + key + "'";
84 }
85 return str;
86 }
87
88 /***
89 * Get a String for a key and format it with the supplied value.
90 *
91 * @param key a <code>String</code> value
92 * @param value a <code>String</code> value
93 * @return a <code>String</code> value
94 */
95 public String getString(String key, String value) {
96 try {
97 String string = getString(key);
98 return MessageFormat.format(string, new String[] {value});
99 } catch (IllegalArgumentException e) {
100 return "Could not find or format String associated with key '" + key + "'";
101 }
102 }
103
104 /***
105 * Get a StringManager for the package where the requesting class
106 * is placed. If no StringManager has been created for this
107 * package create one.
108 *
109 * @param clazz a <code>Class</code> value
110 * @return a <code>StringManager</code> value
111 */
112 public synchronized static StringManager getManager(Class clazz) {
113 String packageName = clazz.getPackage().getName();
114 StringManager mgr = (StringManager)managers/get(packageName)/package-summary.html">StringManager mgr = (StringManager)managers.get(packageName);
115 if (mgr == null) {
116 StringManager(packageName)/package-summary.html">mgr = new StringManager(packageName);
117 managers.put(packageName, mgr);
118 }
119 return mgr;
120 }
121 }
This page was automatically generated by Maven