1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.unitesk.atp.text.localize;
33
34 import java.util.HashMap;
35 import java.util.Locale;
36 import java.util.Map;
37 import java.util.MissingResourceException;
38 import java.util.ResourceBundle;
39
40 /***
41 * The default implementation of localized strings manager.
42 * Associates object classes and bundles of localized messages.
43 * That is for all objects of some class localized strings with the same names are equal.
44 *
45 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
46 * @version $Id: ConstantStringManager.java,v 1.4 2005/05/23 05:49:37 all-x Exp $
47 */
48 public class ConstantStringManager implements StringManager
49 {
50 /***
51 * Creates new localized strings manager using default locale
52 * and class loader of this class.
53 */
54 public ConstantStringManager()
55 {
56 this.locale = Locale.getDefault();
57 this.classLoader = getClass().getClassLoader();
58 }
59
60 /***
61 * Creates new localized strings manager using the specified locale
62 * and class loader of this class.
63 *
64 * @param locale The locale to be used by this manager.
65 */
66 public ConstantStringManager( Locale locale )
67 {
68 this.locale = locale;
69 this.classLoader = getClass().getClassLoader();
70 }
71
72 /***
73 * Creates new localized strings manager with the specified locale and class loader.
74 *
75 * @param locale The locale to be used by this manager.
76 * @param classLoader The class loader to be used by this maanger.
77 */
78 public ConstantStringManager( Locale locale, ClassLoader classLoader )
79 {
80 this.locale = locale;
81 this.classLoader = classLoader;
82 }
83
84 /***
85 * Loads bundle of localized strings for the specified class.
86 * <P>This implementation uses
87 * {@link ResourceBundle#getBundle(String,Locale,ClassLoader)
88 * ResourceBundle.getBundle( cls.getName(), locale, classLoader)}
89 * to obtain bundle. <code>locale</code> and <code>classLoader</code>
90 * are specified in constructor of this manager.
91 * <P>Inheritors can override this method to change a way of bundle loading.
92 *
93 * @param cls The specified class.
94 * @return Bundle for the specified class or <code>null</code> if it is not found.
95 */
96 protected ResourceBundle getBundle( Class cls )
97 {
98 try
99 {
100 return ResourceBundle.getBundle( cls.getName(), locale, classLoader );
101 }
102 catch( MissingResourceException e )
103 {
104 return null;
105 }
106 }
107
108 /***
109 * Get localized string for the specified class.
110 * The first time this method is called for the specified class
111 * it uses {@link #getBundle(Class) getBundle( cls )} to obtain resource bundle.
112 * Then {@link ResourceBundle#getString(String) gets string} <code>name</code> from this bundle
113 * and customize it with respect to <code>obj</code>.
114 *
115 * @param cls The specified class.
116 * @param name The name of localized string for this class.
117 * @return Localized string or <code>null</code> if it is not found.
118 */
119 public String getString( Class cls, String name )
120 {
121 String className = cls.getName();
122 ResourceBundle rb = (ResourceBundle)class_to_bundle.get( className );
123 if( rb == null )
124 {
125 rb = getBundle( cls );
126 if( rb == null ) return null;
127 class_to_bundle.put( className, rb );
128 }
129 try
130 {
131 return rb.getString( name );
132 }
133 catch( MissingResourceException e )
134 {
135 return null;
136 }
137 }
138
139 /***
140 * Get localized string for the specified object.
141 * It calls {@link #getString(Class,String) getString( obj.getClass(), name )}.
142 * This string manager is called 'constant' because it returns the same string
143 * for all objects of the same class.
144 *
145 * @param name The name of localized string for this object.
146 * @param obj The specified object.
147 * @return Localized string or <code>null</code> if it is not found.
148 */
149 public String getString( String name, Object obj )
150 {
151 return getString( obj.getClass(), name );
152 }
153
154 /***
155 * Get localized string for the specified class and customize it for
156 * the specified object.
157 * It calls {@link #getString(Class,String) getString( cls, name )}.
158 * This string manager is called 'constant' because it returns the same string
159 * for all objects of the same class.
160 *
161 * @param cls The specified class.
162 * @param name The name of localized string for this class.
163 * @param obj Not used.
164 * @return Localized string or <code>null</code> if it is not found.
165 */
166 public String getString( Class cls, String name, Object obj )
167 {
168 return getString( cls, name );
169 }
170
171 protected Locale locale;
172 protected ClassLoader classLoader;
173 protected Map
174 = new HashMap
175 }