Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
LocalizedPropertySource |
|
| 2.5;2.5 |
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; |
|
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 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 | 2 | { |
39 | 2 | Defense.notNull(source, "source"); |
40 | ||
41 | 2 | _source = source; |
42 | 2 | } |
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 locale |
|
51 | * the Locale used to generate suffixes (may be null). |
|
52 | */ |
|
53 | ||
54 | public String getPropertyValue(String propertyName, Locale locale) |
|
55 | { |
|
56 | 2 | Defense.notNull(propertyName, "propertyName"); |
57 | ||
58 | 2 | LocalizedNameGenerator g = new LocalizedNameGenerator(propertyName, locale, ""); |
59 | ||
60 | 5 | while (g.more()) |
61 | { |
|
62 | 4 | String localizedName = g.next(); |
63 | ||
64 | 4 | String result = _source.getPropertyValue(localizedName); |
65 | ||
66 | 4 | if (result != null) |
67 | 1 | return result; |
68 | 3 | } |
69 | ||
70 | 1 | return null; |
71 | } |
|
72 | } |