KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > LocalizedPropertySource


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

15 package org.apache.tapestry.services.impl;
16
17 import java.util.Locale JavaDoc;
18
19 import org.apache.hivemind.util.Defense;
20 import org.apache.hivemind.util.LocalizedNameGenerator;
21 import org.apache.tapestry.engine.IPropertySource;
22
23 /**
24  * Wraps around a {@link org.apache.tapestry.engine.IPropertySource}to query a series of localized
25  * keys.
26  * <p>
27  * This is much simpler than the old {@link org.apache.tapestry.util.LocalizedPropertySource}, and
28  * allows the locale to be specified on a thread-safe, per-invocation basis.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class LocalizedPropertySource
34 {
35     private IPropertySource _source;
36
37     public LocalizedPropertySource(IPropertySource source)
38     {
39         Defense.notNull(source, "source");
40
41         _source = source;
42     }
43
44     /**
45      * Get the property from the source, trying different variations of propertyName (adding
46      * suffixes).
47      *
48      * @param propertyName
49      * the base property name to search with.
50      * @param local
51      * the Locale used to generate suffixes (may be null).
52      */

53
54     public String JavaDoc getPropertyValue(String JavaDoc propertyName, Locale JavaDoc locale)
55     {
56         Defense.notNull(propertyName, "propertyName");
57
58         LocalizedNameGenerator g = new LocalizedNameGenerator(propertyName, locale, "");
59
60         while (g.more())
61         {
62             String JavaDoc localizedName = g.next();
63
64             String JavaDoc result = _source.getPropertyValue(localizedName);
65
66             if (result != null)
67                 return result;
68         }
69
70         return null;
71     }
72 }
Popular Tags