KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > util > Property


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

48
49 package org.jpublish.util;
50
51 import java.util.HashMap JavaDoc;
52 import java.util.Locale JavaDoc;
53 import java.util.Map JavaDoc;
54
55 /**
56  * A name/value property which can be localized.
57  *
58  * @author Anthony Eden
59  */

60
61 public class Property {
62
63     private String JavaDoc name = null;
64     private Map JavaDoc values = new HashMap JavaDoc();
65
66     /**
67      * Construct a property with the given name. The name must be a non-null value.
68      *
69      * @param name The name of the property
70      */

71
72     public Property(String JavaDoc name) {
73         if (name == null) {
74             throw new IllegalArgumentException JavaDoc("Property name cannot be null");
75         }
76         this.name = name;
77     }
78
79     /**
80      * Get the property name.
81      *
82      * @return The property name
83      */

84
85     public String JavaDoc getName() {
86         return name;
87     }
88
89     /**
90      * Get the property value using the default Locale.
91      *
92      * @return The property value using the default Locale
93      */

94
95     public String JavaDoc getValue() {
96         return getValue(Locale.getDefault());
97     }
98
99     /**
100      * Get the value for the given locale. This method will try to find the most suitable locale by searching the
101      * property values in the following manner:
102      *
103      * <p> result of locale.toString() language + "_" + country + "_" + variant<br> language + "_" + country<br>
104      * langauge<br> "" </p>
105      *
106      * This method may return null if no suitable value is found.
107      *
108      * @param locale The locale
109      * @return The value or null
110      */

111
112     public String JavaDoc getValue(Locale JavaDoc locale) {
113         String JavaDoc language = locale.getLanguage();
114         String JavaDoc country = locale.getCountry();
115         String JavaDoc variant = locale.getVariant();
116
117         String JavaDoc value = null;
118
119         value = (String JavaDoc) values.get(locale.toString());
120         if (value != null) {
121             return value;
122         }
123
124         if (variant != null) {
125             value = (String JavaDoc) values.get(language + "_" + country + "_" + variant);
126             if (value != null) {
127                 return value;
128             }
129         }
130
131         if (country != null) {
132             value = (String JavaDoc) values.get(language + "_" + country);
133             if (value != null) {
134                 return value;
135             }
136         }
137
138         if (language != null) {
139             value = (String JavaDoc) values.get(language);
140             if (value != null) {
141                 return value;
142             }
143         }
144
145         return (String JavaDoc) values.get("");
146     }
147
148     /**
149      * Set the property value for the given Locale.
150      *
151      * @param value The value
152      * @param locale The Locale
153      */

154
155     public void setValue(String JavaDoc value, Locale JavaDoc locale) {
156         String JavaDoc localeString = null;
157         if (locale != null) {
158             localeString = locale.toString();
159         }
160         setValue(value, localeString);
161     }
162
163     /**
164      * Set the property value for the given locale String. The locale String should be in the form
165      * language_country_variant as described in the Locale.toString() method.
166      *
167      * @param value The value
168      * @param locale The locale String
169      */

170
171     public void setValue(String JavaDoc value, String JavaDoc locale) {
172         if (locale == null) {
173             locale = "";
174         }
175         values.put(locale, value);
176     }
177
178     /**
179      * Get the Map of all locale values.
180      *
181      * @return A map of locale String/value pairs
182      */

183
184     public Map JavaDoc getValues() {
185         return values;
186     }
187
188 }
189
Popular Tags