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 | 0 | { |
39 | 0 | Defense.notNull(source, "source"); |
40 | ||
41 | 0 | _source = source; |
42 | 0 | } |
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 | 0 | Defense.notNull(propertyName, "propertyName"); |
57 | ||
58 | 0 | LocalizedNameGenerator g = new LocalizedNameGenerator(propertyName, locale, ""); |
59 | ||
60 | 0 | while (g.more()) |
61 | { | |
62 | 0 | String localizedName = g.next(); |
63 | ||
64 | 0 | String result = _source.getPropertyValue(localizedName); |
65 | ||
66 | 0 | if (result != null) |
67 | 0 | return result; |
68 | 0 | } |
69 | ||
70 | 0 | return null; |
71 | } | |
72 | } |