KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > schema > LocaleMap


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17
18  ============================================================================
19                    The Apache Software License, Version 1.1
20  ============================================================================
21
22  Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
23
24  Redistribution and use in source and binary forms, with or without modifica-
25  tion, are permitted provided that the following conditions are met:
26
27  1. Redistributions of source code must retain the above copyright notice,
28     this list of conditions and the following disclaimer.
29
30  2. Redistributions in binary form must reproduce the above copyright notice,
31     this list of conditions and the following disclaimer in the documentation
32     and/or other materials provided with the distribution.
33
34  3. The end-user documentation included with the redistribution, if any, must
35     include the following acknowledgment: "This product includes software
36     developed by the Apache Software Foundation (http://www.apache.org/)."
37     Alternately, this acknowledgment may appear in the software itself, if
38     and wherever such third-party acknowledgments normally appear.
39
40  4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
41     used to endorse or promote products derived from this software without
42     prior written permission. For written permission, please contact
43     apache@apache.org.
44
45  5. Products derived from this software may not be called "Apache", nor may
46     "Apache" appear in their name, without prior written permission of the
47     Apache Software Foundation.
48
49  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
50  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
51  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
53  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
54  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
55  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
56  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59
60  This software consists of voluntary contributions made by many individuals
61  on behalf of the Apache Software Foundation and was originally created by
62  Stefano Mazzocchi <stefano@apache.org>. For more information on the Apache
63  Software Foundation, please see <http://www.apache.org/>.
64
65 */

66 package org.outerj.daisy.repository.commonimpl.schema;
67
68 import org.outerx.daisy.x10.LabelsDocument;
69 import org.outerx.daisy.x10.DescriptionsDocument;
70 import org.outerj.daisy.repository.LocaleHelper;
71
72 import java.util.Locale JavaDoc;
73 import java.util.Set JavaDoc;
74 import java.util.Iterator JavaDoc;
75 import java.util.Map JavaDoc;
76
77 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
78
79 /**
80  * Map using Locale objects as keys.
81  *
82  * <b>This class is based on code from Apache Cocoon.</b>
83  *
84  * <p>This map should be filled once using calls to {@link #put}, before any calls
85  * are made to {@link #get}.
86  *
87  */

88 public class LocaleMap {
89     /** Contains the original values the have been put in the map. */
90     private ConcurrentReaderHashMap map = new ConcurrentReaderHashMap();
91     /** Contains 'resolved' locales for quick lookup. */
92     private ConcurrentReaderHashMap searchMap = new ConcurrentReaderHashMap();
93     private static final String JavaDoc NO_RESULT = "(no result: you should never see this)";
94
95     /**
96      * Gets an object based on the given locale. An automatic fallback mechanism is used:
97      * if nothing is found for language-COUNTRY-variant, then language-COUNTRY is searched,
98      * the language, and finally "" (empty string). If nothing is found null is returned.
99      */

100     public Object JavaDoc get(Locale JavaDoc locale) {
101         if (map.size() == 0)
102             return null;
103
104         String JavaDoc full = getFullKey(locale);
105
106         if (!searchMap.containsKey(full)) {
107             if (map.containsKey(full)) {
108                 Object JavaDoc object = map.get(full);
109                 searchMap.put(full, object);
110                 return object;
111             }
112
113             String JavaDoc altKey = locale.getLanguage() + '-' + locale.getCountry();
114             Object JavaDoc object = map.get(altKey);
115             if (object != null) {
116                 searchMap.put(full, object);
117                 return object;
118             }
119
120             altKey = locale.getLanguage();
121             object = map.get(altKey);
122             if (object != null) {
123                 searchMap.put(full, object);
124                 return object;
125             }
126
127             object = map.get("");
128             if (object != null) {
129                 searchMap.put(full, object);
130                 return object;
131             }
132
133             searchMap.put(full, NO_RESULT);
134         }
135
136         Object JavaDoc result = searchMap.get(full);
137         return result == NO_RESULT ? null : result;
138     }
139
140     public Object JavaDoc getExact(Locale JavaDoc locale) {
141         return map.get(LocaleHelper.getString(locale));
142     }
143
144     public void clear() {
145         map.clear();
146         searchMap.clear();
147     }
148
149     public void remove(Locale JavaDoc locale) {
150         put(locale, null);
151     }
152
153     public Locale JavaDoc[] getLocales() {
154         String JavaDoc[] localeNames = (String JavaDoc[])map.keySet().toArray(new String JavaDoc[0]);
155         Locale JavaDoc[] locales = new Locale JavaDoc[localeNames.length];
156         for (int i = 0; i < locales.length; i++)
157             locales[i] = LocaleHelper.parseLocale(localeNames[i]);
158         return locales;
159     }
160
161     public Set JavaDoc entrySet() {
162         return map.entrySet();
163     }
164
165     private String JavaDoc getFullKey(Locale JavaDoc locale) {
166         return locale.getLanguage() + '-' + locale.getCountry() + '-' + locale.getVariant();
167     }
168
169     public void put(Locale JavaDoc locale, Object JavaDoc object) {
170         if (object == null)
171             map.remove(LocaleHelper.getString(locale));
172         else
173             map.put(LocaleHelper.getString(locale), object);
174         searchMap.clear();
175     }
176
177     public LabelsDocument.Labels getAsLabelsXml() {
178         LabelsDocument.Labels labels = LabelsDocument.Factory.newInstance().addNewLabels();
179         Iterator JavaDoc labelIt = map.entrySet().iterator();
180         while (labelIt.hasNext()) {
181             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)labelIt.next();
182             LabelsDocument.Labels.Label label = labels.addNewLabel();
183             label.setLocale((String JavaDoc)entry.getKey());
184             label.setStringValue((String JavaDoc)entry.getValue());
185         }
186         return labels;
187     }
188
189     public DescriptionsDocument.Descriptions getAsDescriptionsXml() {
190         DescriptionsDocument.Descriptions descriptions = DescriptionsDocument.Factory.newInstance().addNewDescriptions();
191         Iterator JavaDoc descriptionIt = map.entrySet().iterator();
192         while (descriptionIt.hasNext()) {
193             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)descriptionIt.next();
194             DescriptionsDocument.Descriptions.Description description = descriptions.addNewDescription();
195             description.setLocale((String JavaDoc)entry.getKey());
196             description.setStringValue((String JavaDoc)entry.getValue());
197         }
198         return descriptions;
199     }
200
201     public void readFromLabelsXml(LabelsDocument.Labels labelsXml) {
202         LabelsDocument.Labels.Label[] labels = labelsXml.getLabelArray();
203         for (int i = 0; i < labels.length; i++) {
204             String JavaDoc locale = labels[i].getLocale();
205             String JavaDoc value = labels[i].getStringValue();
206             map.put(locale, value);
207         }
208         searchMap.clear();
209     }
210
211     public void readFromDescriptionsXml(DescriptionsDocument.Descriptions descriptionsAsXml) {
212         DescriptionsDocument.Descriptions.Description[] descriptions = descriptionsAsXml.getDescriptionArray();
213         for (int i = 0; i < descriptions.length; i++) {
214             String JavaDoc locale = descriptions[i].getLocale();
215             String JavaDoc value = descriptions[i].getStringValue();
216             map.put(locale, value);
217         }
218         searchMap.clear();
219     }
220
221     public boolean isEmpty() {
222         return map.isEmpty();
223     }
224 }
225
Popular Tags