KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > resourcebundle > ResourceBundleMarker


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13

14 package org.jahia.resourcebundle;
15
16 import java.util.Collections JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.Locale JavaDoc;
19 import java.util.MissingResourceException JavaDoc;
20 import java.util.ResourceBundle JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import org.jahia.exceptions.JahiaException;
24 import org.jahia.registries.ServicesRegistry;
25 import org.jahia.utils.JahiaTools;
26
27
28 /**
29  *
30  * <p>Title: Holds information from a resource bundle marker tag</p>
31  * <p>Description: </p>
32  * <p>Copyright: Copyright (c) 2002</p>
33  * <p>Company: </p>
34  * @author Khue Nguyen
35  * @version 1.0
36  */

37 public class ResourceBundleMarker implements Comparator JavaDoc {
38
39     /**
40      * The unique id that identifies a resource bundle file @see ResourceBundleDefinition
41      */

42     private String JavaDoc resourceBundleID;
43
44     /**
45      * The resource key
46      */

47     private String JavaDoc resourceKey;
48
49     /**
50      * The value matching the resource
51      */

52     private String JavaDoc value;
53
54     /**
55      * A default value to use when the resource is not found
56      */

57     private String JavaDoc defaultValue;
58
59     public ResourceBundleMarker( String JavaDoc resourceBundleID,
60                                  String JavaDoc resourceKey,
61                                  String JavaDoc defaultValue ){
62         this.resourceBundleID = resourceBundleID;
63         this.resourceKey = resourceKey;
64         this.defaultValue = defaultValue;
65     }
66
67     /**
68      * Returns the resource bundle identifier.
69      *
70      * @return the resource bundle identifie
71      */

72     public String JavaDoc getResourceBundleID(){
73         return this.resourceBundleID;
74     }
75
76     /**
77      * Returns the resource key
78      *
79      * @return resource resource key
80      */

81     public String JavaDoc getResourceKey(){
82         return this.resourceKey;
83     }
84
85     /**
86      * Returns the internal value.
87      * You must call the setValue(String value, ParamBean jParams, Locale locale)
88      * once to set the internal value with a value that matches the resource.
89      *
90      * @return the internal value
91      */

92     public String JavaDoc getValue(){
93         if ( this.value == null ){
94             return "";
95         }
96         return this.value;
97     }
98
99     /**
100      * Returns the real value ( from resource bundle ) given internal states info.
101      *
102      * @param locale
103      * @return
104      * @throws JahiaException
105      */

106     public String JavaDoc getValue(Locale JavaDoc locale)
107     throws JahiaException {
108
109         String JavaDoc result = this.getDefaultValue();
110
111         ResourceBundleDefinition rbDef =
112                 (ResourceBundleDefinition)ServicesRegistry.getInstance()
113                 .getResourceBundleService()
114                 .getResourceBundle(this.getResourceBundleID());
115         if ( rbDef == null ){
116             return result;
117         }
118
119         try {
120             ResourceBundle JavaDoc res =
121                     ResourceBundle.getBundle(rbDef.getResourceBundleFile(),
122                     locale);
123             //result = res.getString(this.getResourceKey());
124
result = JahiaResourceBundle.getString(res, this.getResourceKey(), locale);
125         } catch ( MissingResourceException JavaDoc mre ) {
126             //mre.printStackTrace();
127
}
128         return result;
129     }
130
131     /**
132      * Returns the real value ( from resource bundle ) if the value is a valid resource bundle marker tag
133      *
134      * <jahia-resource id="MySiteResource" key="product.001" default-value="Crew"/>
135      *
136      * or the original value on any other case ( not a valid resource bundle marker tag ).
137      *
138      * @param resourceKey
139      * @param locale
140      * @return
141      * @throws JahiaException
142      */

143     public String JavaDoc getValueFromResourceKey(String JavaDoc resourceKey, Locale JavaDoc locale)
144     throws JahiaException {
145
146         String JavaDoc result = this.getDefaultValue();
147
148         ResourceBundleDefinition rbDef =
149                 (ResourceBundleDefinition)ServicesRegistry.getInstance()
150                 .getResourceBundleService()
151                 .getResourceBundle(this.getResourceBundleID());
152         if ( rbDef == null ){
153             return this.getDefaultValue();
154         }
155
156         try {
157             ResourceBundle JavaDoc res =
158                     ResourceBundle.getBundle(rbDef.getResourceBundleFile(),
159                     locale);
160             //result = res.getString(this.getResourceKey());
161
result = JahiaResourceBundle.getString(res, this.getResourceKey(), locale);
162         } catch ( MissingResourceException JavaDoc mre ) {
163             //mre.printStackTrace();
164
}
165         return result;
166     }
167
168     /**
169      * Returns the default value
170      *
171      * @return resource resource key
172      */

173     public String JavaDoc getDefaultValue(){
174         return this.defaultValue;
175     }
176
177     /**
178      * Generates a valid resource bundle marker from internal value
179      *
180      * <jahia-resource id="MySiteResource" key="product.001" default-value="Crew"/>
181      *
182      * @param resourceBundleID
183      * @param resourceKey
184      * @param defaultValue
185      * @return
186      */

187     public String JavaDoc drawMarker(){
188         return drawMarker(this.getResourceBundleID(),
189                           this.getResourceKey(),
190                           this.getDefaultValue());
191     }
192
193     /**
194      * Set the internal value.
195      *
196      * @param value
197      */

198     public void setValue(String JavaDoc value){
199         this.value = value;
200     }
201
202     /**
203      * Set the internal value with the result returned by the resource lookup for a given locale.
204      *
205      * @param locale
206      * @return
207      * @throws JahiaException
208      */

209     public void setValue(Locale JavaDoc locale)
210     throws JahiaException {
211         this.value = this.getValueFromResourceKey(this.getResourceKey(),locale);
212     }
213
214     /**
215      * Generates a ResourceBundleMarker Bean from a resource bundle marker value String
216      *
217      * @param value a valid tag :
218      * <jahia-resource id="MySiteResource" key="product.001" default-value="Crew"/>
219      * @return a resource bundle marker bean or null on any parsing error.
220      */

221     public static ResourceBundleMarker parseMarkerValue(String JavaDoc markerStr){
222
223         if ( markerStr == null ){
224             return null;
225         }
226
227         ResourceBundleMarker marker = null;
228         String JavaDoc val = markerStr.trim();
229         String JavaDoc resourceBundleID = "";
230         String JavaDoc resourceKey = "";
231         String JavaDoc defaultValue = "";
232
233         if ( val.startsWith("<jahia-resource") && val.endsWith("/>") ){
234
235             try {
236
237                 int pos = val.indexOf(" id=\"");
238                 if ( pos != -1 ){
239                     resourceBundleID =
240                             val.substring(pos + 5,
241                             pos + 5 + val.substring(pos + 5).indexOf("\""));
242                 }
243
244                 pos = val.indexOf(" key=\"");
245                 if ( pos != -1 ){
246                     resourceKey =
247                             val.substring(pos + 6,
248                             pos + 6 + val.substring(pos + 6).indexOf("\""));
249                 }
250
251                 pos = val.indexOf(" default-value=\"");
252                 if ( pos != -1 ){
253                     defaultValue =
254                             val.substring(pos + 16,
255                             val.lastIndexOf("\""));
256                 }
257
258                 marker = new ResourceBundleMarker(resourceBundleID,resourceKey,
259                         defaultValue);
260             } catch ( Throwable JavaDoc t ){
261                 //t.printStackTrace();
262
}
263         }
264
265         return marker;
266     }
267
268     /**
269      * Returns the real value ( from resource bundle ) if the value is a valid resource bundle marker tag
270      *
271      * <jahia-resource id="MySiteResource" key="product.001" default-value="Crew"/>
272      *
273      * or the original value on any other case ( not a valid resource bundle marker tag ).
274      *
275      * @param markerStr
276      * @param locale
277      * @return
278      * @throws JahiaException
279      */

280     public static String JavaDoc getValue(String JavaDoc value, Locale JavaDoc locale)
281     throws JahiaException {
282
283         ResourceBundleMarker marker =
284                 ResourceBundleMarker.parseMarkerValue(value);
285         if ( marker == null ){
286             return value;
287         }
288
289         String JavaDoc result = marker.getDefaultValue();
290
291         ResourceBundleDefinition rbDef =
292                 (ResourceBundleDefinition)ServicesRegistry.getInstance()
293                 .getResourceBundleService()
294                 .getResourceBundle(marker.getResourceBundleID());
295         if ( rbDef == null ){
296             return result;
297         }
298
299         try {
300             ResourceBundle JavaDoc res =
301                     ResourceBundle.getBundle(rbDef.getResourceBundleFile(),
302                     locale);
303             //result = res.getString(marker.getResourceKey());
304
result = JahiaResourceBundle.getString(res, marker.getResourceKey(), locale);
305         } catch ( MissingResourceException JavaDoc mre ) {
306             //mre.printStackTrace();
307
}
308         return result;
309     }
310
311     /**
312      * Build a vector of resource bundle markers from an enumeration of values
313      *
314      * val1:val2:val3
315      *
316      * @param enumValues
317      * @param processingLocale
318      * @return
319      * @throws JahiaException
320      */

321     public static Vector JavaDoc buildResourceBundleMarkers(String JavaDoc enumValues,
322                                               Locale JavaDoc processingLocale)
323     throws JahiaException {
324
325         String JavaDoc[] tokens = JahiaTools.getTokens(enumValues,":");
326         Vector JavaDoc markers = new Vector JavaDoc();
327         for ( int i=0 ; i<tokens.length ; i++ )
328         {
329             //System.out.println("smalltext_field.buildResourceBundlemarker : token=" + tokens[i]);
330
ResourceBundleMarker marker =
331                 ResourceBundleMarker.parseMarkerValue(tokens[i]);
332             if ( marker == null ){
333                 //System.out.println("smalltext_field.buildResourceBundlemarker : marker is null !");
334
// invalid or not a resource bundle marker signature
335
// build a marker that return the value as it.
336
marker = new ResourceBundleMarker("","","");
337                 marker.setValue(tokens[i]);
338             } else {
339                 //System.out.println("smalltext_field.buildResourceBundlemarker : marker=[" + marker.getResourceKey() + "," + marker.getValue() + "]");
340
marker.setValue(processingLocale);
341             }
342             markers.add(marker);
343         }
344         // sorts the markers
345
ResourceBundleMarker compMarker = new ResourceBundleMarker("","","");
346         compMarker.setValue("");
347         Collections.sort(markers,compMarker);
348         return markers;
349     }
350
351     /**
352      * Generates a valid resource bundle marker
353      *
354      * <jahia-resource id="MySiteResource" key="product.001" default-value="Crew"/>
355      *
356      * @param resourceBundleID
357      * @param resourceKey
358      * @param defaultValue
359      * @return
360      */

361     public static String JavaDoc drawMarker(String JavaDoc resourceBundleID,
362                                     String JavaDoc resourceKey, String JavaDoc defaultValue ){
363
364         StringBuffer JavaDoc buff = new StringBuffer JavaDoc("<jahia-resource ");
365         buff.append("id=\"");
366         buff.append(resourceBundleID);
367         buff.append("\" key=\"");
368         buff.append(resourceKey);
369         buff.append("\" default-value=\"");
370         buff.append(defaultValue);
371         buff.append("\"/>");
372         return buff.toString();
373
374     }
375
376     //-------------------------------------------------------------------------
377
/**
378      * Compare between two objects, sort by their value
379      *
380      * @param Object
381      * @param Object
382      */

383     public int compare(Object JavaDoc c1, Object JavaDoc c2) throws ClassCastException JavaDoc {
384
385         return ((ResourceBundleMarker)c1)
386                 .getValue()
387                 .compareToIgnoreCase(((ResourceBundleMarker)c2).getValue().toLowerCase());
388
389     }
390
391 }
Popular Tags