KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > page > PageProperty


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 /*--
14
15  Copyright (C) 2001-2002 Anthony Eden.
16  All rights reserved.
17
18  Redistribution and use in source and binary forms, with or without
19  modification, are permitted provided that the following conditions
20  are met:
21
22  1. Redistributions of source code must retain the above copyright
23     notice, this list of conditions, and the following disclaimer.
24
25  2. Redistributions in binary form must reproduce the above copyright
26     notice, this list of conditions, and the disclaimer that follows
27     these conditions in the documentation and/or other materials
28     provided with the distribution.
29
30  3. The name "JPublish" must not be used to endorse or promote products
31     derived from this software without prior written permission. For
32     written permission, please contact me@anthonyeden.com.
33
34  4. Products derived from this software may not be called "JPublish", nor
35     may "JPublish" appear in their name, without prior written permission
36     from Anthony Eden (me@anthonyeden.com).
37
38  In addition, I request (but do not require) that you include in the
39  end-user documentation provided with the redistribution and/or in the
40  software itself an acknowledgement equivalent to the following:
41      "This product includes software developed by
42       Anthony Eden (http://www.anthonyeden.com/)."
43
44  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
48  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
53  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54  POSSIBILITY OF SUCH DAMAGE.
55
56  For more information on JPublish, please see <http://www.jpublish.org/>.
57
58  */

59 package com.openedit.page;
60
61 import java.util.HashMap JavaDoc;
62 import java.util.Locale JavaDoc;
63 import java.util.Map JavaDoc;
64
65
66 /**
67  * A name/value property which is accessible to a specific page.
68  *
69  * @author Anthony Eden
70  */

71 public class PageProperty
72 {
73     private Map JavaDoc values;
74     private String JavaDoc name;
75     protected String JavaDoc fieldPath;
76
77     /**
78      * Construct a page property with the given name. The name must be a non-null value.
79      *
80      * @param name The name of the page property
81      *
82      * @throws IllegalArgumentException DOCME
83      */

84     public PageProperty(String JavaDoc name)
85     {
86         if (name == null)
87         {
88             throw new IllegalArgumentException JavaDoc("Property name cannot be null");
89         }
90
91         this.name = name;
92         values = new HashMap JavaDoc();
93     }
94
95     /**
96      * Get the property name.
97      *
98      * @return The property name
99      */

100     public String JavaDoc getName()
101     {
102         return name;
103     }
104
105     /**
106      * Set the property value for the given Locale.
107      *
108      * @param value The value
109      * @param locale The Locale
110      */

111     public void setValue(String JavaDoc value, Locale JavaDoc locale)
112     {
113         StringBuffer JavaDoc localeString = new StringBuffer JavaDoc();
114         if (locale != null)
115         {
116             String JavaDoc language = locale.getLanguage();
117             String JavaDoc country = locale.getCountry();
118             String JavaDoc variant = locale.getVariant();
119
120             localeString.append(language );
121             if ( country != null)
122             {
123                 localeString.append("_");
124                 localeString.append(country);
125             }
126             if ( variant != null)
127             {
128                 localeString.append("_");
129                 localeString.append(variant);
130             }
131         }
132         setValue(value, localeString.toString() );
133     }
134
135     /**
136      * Set the property value for the given locale String. The locale String should be in the form
137      * language_country_variant as described in the Locale.toString() method.
138      *
139      * @param value The value
140      * @param locale The locale String
141      */

142     public void setValue(String JavaDoc value, String JavaDoc locale)
143     {
144         if (locale == null)
145         {
146             locale = "";
147         }
148
149         values.put(locale, value);
150     }
151
152     /**
153      * Get the property value using the default Locale.
154      *
155      * @return The property value using the default Locale
156      */

157     public String JavaDoc getValue()
158     {
159         return getValue(Locale.getDefault());
160     }
161
162     /**
163      * Get the value for the given locale. This method will try to find the most suitable locale
164      * by searching the property values in the following manner:
165      *
166      * <p>
167      * result of locale.toString() language + "_" + country + "_" + variant<br>
168      * language + "_" + country<br>
169      * langauge<br>
170      * ""
171      * </p>
172      * This method may return null if no suitable value is found.
173      *
174      * @param locale The locale
175      *
176      * @return The value or null
177      */

178     public String JavaDoc getValue(Locale JavaDoc locale)
179     {
180         if( locale == null)
181         {
182             locale = Locale.getDefault();
183         }
184         String JavaDoc language = locale.getLanguage();
185         String JavaDoc country = locale.getCountry();
186         String JavaDoc variant = locale.getVariant();
187         return getValue( language, country, variant);
188     }
189     public String JavaDoc getValue(String JavaDoc inLocale)
190     {
191         String JavaDoc lang = "";
192         String JavaDoc country = "";
193         String JavaDoc variant = "";
194
195         int first = inLocale.indexOf('_');
196         if( first == -1)
197         {
198             lang = inLocale;
199         }
200         else
201         {
202             lang = inLocale.substring(0,first);
203             int second = inLocale.indexOf(first, '_');
204             if( second == -1 )
205             {
206                 country = inLocale.substring(first + 1);
207             }
208             else
209             {
210                 country = inLocale.substring(first, second);
211                 variant = inLocale.substring(second + 1);
212             }
213         }
214         return getValue(lang,country, variant);
215     }
216
217     public String JavaDoc getValue(String JavaDoc language, String JavaDoc country, String JavaDoc variant)
218     {
219         if( "default".equals(language) )
220         {
221             language = "";
222         }
223         String JavaDoc value = null;
224
225         /* //this check might be a duplicate of the next one below. TODO: Remove it?
226                 value = (String) values.get(locale.toString());
227
228                 if (value != null)
229                 {
230                     return value;
231                 }
232         */

233                 //Go from Specific to generatic
234
if (variant != null)
235                 {
236                     value = (String JavaDoc) values.get(language + "_" + country + "_" + variant);
237
238                     if (value != null)
239                     {
240                         return value;
241                     }
242                 }
243
244                 if (country != null)
245                 {
246                     value = (String JavaDoc) values.get(language + "_" + country);
247
248                     if (value != null)
249                     {
250                         return value;
251                     }
252                 }
253
254                 if (language != null)
255                 {
256                     value = (String JavaDoc) values.get(language);
257
258                     if (value != null)
259                     {
260                         return value;
261                     }
262                 }
263
264                 return (String JavaDoc) values.get("");
265     }
266
267     /**
268      * Get the Map of all locale values.
269      *
270      * @return A map of locale String/value pairs
271      */

272     public Map JavaDoc getValues()
273     {
274         return values;
275     }
276     public String JavaDoc toString()
277     {
278         return getValue();
279     }
280     /**
281      * @return Returns the path.
282      */

283     public String JavaDoc getPath() {
284         return fieldPath;
285     }
286     /**
287      * @param inPath The path to set.
288      */

289     public void setPath(String JavaDoc inPath) {
290         fieldPath = inPath;
291     }
292
293     public void setValue(String JavaDoc inString)
294     {
295         setValue(inString,(String JavaDoc)null);
296     
297     }
298 }
299
Popular Tags