KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > widget > LocaleString


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29
30 package com.caucho.widget;
31
32 import com.caucho.util.L10N;
33
34 import java.util.HashMap JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 /**
40  * A locale String maps locales to String
41  */

42 public class LocaleString
43 {
44   private static L10N L = new L10N( LocaleString.class );
45
46   static protected final Logger JavaDoc log =
47     Logger.getLogger( LocaleString.class.getName() );
48
49   private static ThreadLocal JavaDoc _threadLocale = new ThreadLocal JavaDoc<Locale JavaDoc>();
50
51   private Locale JavaDoc _locale;
52
53   private String JavaDoc _text = null; // the text for _locale
54

55   private String JavaDoc _anyText = null; // the first item in _textMap
56

57   // any locale-->text mappings other than _locale --> _text
58
private HashMap JavaDoc<Locale JavaDoc,String JavaDoc> _textMap;
59
60   /**
61    * Set the default locale to use for this thread when using
62    * the getValue() method.
63    */

64   public static void setThreadLocale( Locale JavaDoc locale )
65   {
66     _threadLocale.set( locale );
67   }
68
69   public static LocaleString add( LocaleString localeString,
70                                   LocaleString value )
71   {
72     if ( localeString == null )
73       localeString = new LocaleString();
74
75     localeString.add( value );
76
77     return localeString;
78   }
79
80   public static LocaleString add( LocaleString localeString,
81                                   String JavaDoc value )
82   {
83     if ( localeString == null )
84       localeString = new LocaleString();
85
86     localeString.addText( value );
87
88     return localeString;
89   }
90
91   public static LocaleString add( LocaleString localeString,
92                                   Locale JavaDoc locale, String JavaDoc value )
93   {
94     if ( localeString == null )
95       localeString = new LocaleString();
96
97     localeString.put( locale, value );
98
99     return localeString;
100   }
101
102   public static String JavaDoc get( LocaleString localeString, Locale JavaDoc locale )
103   {
104     return localeString == null ? null : localeString.get( locale );
105   }
106
107   public static String JavaDoc get( LocaleString localeString )
108   {
109     return localeString == null ? null : localeString.getValue();
110   }
111
112   public LocaleString()
113   {
114     setLocale( null );
115   }
116
117   /**
118    * Construct and set the default locale.
119    */

120   public LocaleString( Locale JavaDoc locale )
121   {
122     setLocale( locale );
123   }
124
125   /**
126    * Construct and set the text for the default locale.
127    */

128   public LocaleString( String JavaDoc text )
129   {
130     setLocale( null );
131     addText( text );
132   }
133
134   /**
135    * Construct, set the default locale, and set the text for the default locale.
136    */

137   public LocaleString( Locale JavaDoc locale, String JavaDoc text )
138   {
139     setLocale( locale );
140     addText( text );
141   }
142
143   /**
144    * Set the default locale for text set with addText, the default is
145    * the platform default.
146    */

147   public void setLocale( Locale JavaDoc locale )
148   {
149     if ( locale == null ) {
150       String JavaDoc lang = System.getProperty( "user.language" );
151       String JavaDoc country = System.getProperty( "user.region" );
152
153       if ( lang != null && country != null )
154         locale = new Locale JavaDoc( lang, country );
155       else if ( lang != null )
156         locale = new Locale JavaDoc( lang );
157       else
158         locale = Locale.ENGLISH;
159     }
160
161     _locale = locale;
162   }
163
164   public Locale JavaDoc getLocale()
165   {
166     return _locale;
167   }
168
169   /**
170    * Convert the xml style <i>lang</i> to a {@link java.util.Locale}
171    * and then call {@link setLocale(Locale)}.
172    */

173   public void setLang( String JavaDoc lang )
174   {
175     int index = lang.indexOf('-');
176
177     if (index > -1 ) {
178       String JavaDoc language = lang.substring( 0, index );
179       String JavaDoc country = lang.substring( index + 1 );
180       setLocale( new Locale JavaDoc( language, country ) );
181     }
182     else
183       setLocale( new Locale JavaDoc( lang ) );
184   }
185
186   public void addText( String JavaDoc text )
187   {
188     _text = text;
189   }
190
191   public void put( Locale JavaDoc locale, String JavaDoc text )
192   {
193     if ( locale == null )
194       _text = text;
195     else {
196       if ( locale.equals( _locale ) )
197         _text = text;
198       else {
199         if ( _textMap == null ) {
200           _textMap = new HashMap JavaDoc<Locale JavaDoc,String JavaDoc>();
201           _anyText = text;
202         }
203
204         _textMap.put( locale, text );
205       }
206     }
207   }
208
209   /**
210    * Return the most specific text available.
211    * <ul>
212    * <li>the text for the thread locale set with setThreadLocale()
213    * <li>the text for the thread locale without variant
214    * <li>the text for the thread locale wthout variant or country
215    * <li>the text for the default locale
216    * <li>any text available
217    * </ul>
218    */

219   public String JavaDoc getValue()
220   {
221     Locale JavaDoc locale = (Locale JavaDoc) _threadLocale.get();
222
223     return get( locale );
224   }
225
226   /**
227    * Return the most specific text available.
228    * <ul>
229    * <li>the text that matches the full locale
230    * <li>the text that matches the locale without variant
231    * <li>the text that matches the locale without variant or country
232    * <li>the text for the default locale
233    * <li>any text available
234    * </ul>
235    */

236   public String JavaDoc get( Locale JavaDoc locale )
237   {
238     if ( locale == null ) {
239       if ( _text != null )
240         return _text;
241       else if ( _anyText != null )
242         return _anyText;
243       else
244         return null;
245     }
246
247     String JavaDoc text = null;
248
249     if ( _text != null && locale.equals( _locale ) )
250       text = _text;
251     else if ( _textMap != null )
252       text = _textMap.get( locale );
253
254     if ( text != null )
255       return text;
256
257     Locale JavaDoc reducedLocale = reduceLocale( locale );
258
259     return get( reducedLocale );
260   }
261
262   /**
263    * Add the strings from another locale to this locale.
264    * In the case of duplicate locales, the current values are overridden
265    * by the new values
266    */

267   public void add( LocaleString localeString )
268   {
269     if ( localeString._text != null )
270       put( localeString._locale, localeString._text );
271
272     if ( localeString._textMap != null ) {
273       for ( Map.Entry JavaDoc<Locale JavaDoc,String JavaDoc> entry : localeString._textMap.entrySet() )
274       {
275         put( entry.getKey(), entry.getValue() );
276       }
277     }
278   }
279
280   private static Locale JavaDoc reduceLocale( Locale JavaDoc locale )
281   {
282     String JavaDoc language = locale.getLanguage();
283     String JavaDoc country = locale.getCountry();
284     String JavaDoc variant = locale.getVariant();
285
286     if ( variant.length() > 0 )
287       return new Locale JavaDoc( language, country );
288     else if ( country.length() > 0 )
289       return new Locale JavaDoc( language );
290     else
291       return null;
292   }
293
294   public String JavaDoc toString()
295   {
296     return getValue();
297   }
298 }
299
Popular Tags