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.Locale;
35
36 import com.unitesk.atp.text.filters.StringTextReceiver;
37 import com.unitesk.atp.text.generation.BasicGenerator;
38 import com.unitesk.atp.text.generation.Generator;
39
40 /***
41 * This {@link StringManager string manager} treats localized strings as
42 * {@link com.unitesk.atp.text.generation.Generator#txt(String) patterns}.
43 * Pattern parameters are printed using the same resource name.
44 * If resource not found {@link #toString()} is used.
45 *
46 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
47 * @version $Id: PatternStringManager.java,v 1.3 2006/02/07 15:44:41 all-x Exp $
48 */
49 public class PatternStringManager extends ConstantStringManager
50 {
51 /***
52 * Creates new localized strings manager using default locale
53 * and class loader of this class.
54 */
55 public PatternStringManager()
56 {
57 super();
58
59 initGenerator();
60 }
61
62 /***
63 * Creates new localized strings manager using the specified locale
64 * and class loader of this class.
65 *
66 * @param locale The locale to be used by this manager.
67 */
68 public PatternStringManager( Locale locale )
69 {
70 super( locale );
71
72 initGenerator();
73 }
74
75 /***
76 * Creates new localized strings manager with the specified locale and class loader.
77 *
78 * @param locale The locale to be used by this manager.
79 * @param classLoader The class loader to be used by this maanger.
80 */
81 public PatternStringManager( Locale locale, ClassLoader classLoader )
82 {
83 super( locale, classLoader );
84
85 initGenerator();
86 }
87
88 /***
89 * Get localized string for the specified class and object.
90 * <P>This method calls {@link ConstantStringManager#getString(Class,String,Object)
91 * super implementation}, treats returned value as
92 * {@link com.unitesk.atp.text.generation.Generator#txt(String) pattern} and substitute
93 * its parameters by attributes of the specified object.
94 *
95 * @param cls The specified class.
96 * @param name The name of localized string for this class.
97 * @param obj The specified object.
98 * @return Localized string.
99 */
100 public String getString( Class cls, String name, Object obj )
101 {
102 setResource( name );
103 return substitute( getPattern( cls, name ), obj );
104 }
105
106 /***
107 * Get localized string for the specified object.
108 * <P>This method calls {@link ConstantStringManager#getString(String,Object)
109 * super implementation}, treats returned value as
110 * {@link com.unitesk.atp.text.generation.Generator#txt(String) pattern} and substitute
111 * its parameters by attributes of the specified object.
112 *
113 * @param name The name of localized string for this object.
114 * @param obj The specified object.
115 * @return Localized string or <code>null</code> if it is not found.
116 */
117 public String getString( String name, Object obj )
118 {
119 setResource( name );
120 return substitute( getPattern( name, obj ), obj );
121 }
122
123 public String getPattern( String name, Object obj )
124 {
125 return super.getString( name, obj );
126 }
127
128 public String getPattern( Class cls, String name )
129 {
130 return super.getString( cls, name );
131 }
132
133 protected synchronized String substitute( String pattern, Object obj )
134 {
135 if( pattern == null ) return null;
136
137 generator.setVariable( Generator.DEFAULT_NAME, obj );
138 generator.txt( pattern );
139 return res.getText();
140 }
141
142 private void initGenerator()
143 {
144 generator.setFunction( Generator.DEFAULT_NAME, df );
145 }
146
147 protected void setResource( String resource )
148 {
149 df.setResource( resource );
150 }
151
152 protected void setResourceClass( Class resourceClass )
153 {
154 df.setResourceClass( resourceClass );
155 }
156
157 private StringTextReceiver res = new StringTextReceiver();
158 private BasicGenerator generator = new BasicGenerator( res );
159 private PatternDefaultFunction df = new PatternDefaultFunction( this );
160 }