|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
LocalizedResourceFinder.java | 100% | 100% | 100% | 100% |
|
1 | // Copyright 2004, 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.hivemind.util; | |
16 | ||
17 | import java.util.Locale; | |
18 | ||
19 | import org.apache.hivemind.ClassResolver; | |
20 | ||
21 | /** | |
22 | * Searches for a localization of a | |
23 | * particular resource in the classpath (using | |
24 | * a {@link org.apache.hivemind.ClassResolver}. | |
25 | * | |
26 | * | |
27 | * @author Howard Lewis Ship | |
28 | */ | |
29 | public class LocalizedResourceFinder | |
30 | { | |
31 | private ClassResolver _resolver; | |
32 | ||
33 | 8 | public LocalizedResourceFinder(ClassResolver resolver) |
34 | { | |
35 | 8 | _resolver = resolver; |
36 | } | |
37 | ||
38 | /** | |
39 | * Resolves the resource, returning a path representing | |
40 | * the closest match (with respect to the provided locale). | |
41 | * Returns null if no match. | |
42 | * | |
43 | * <p>The provided path is split into a base path | |
44 | * and a suffix (at the last period character). The locale | |
45 | * will provide different suffixes to the base path | |
46 | * and the first match is returned. | |
47 | */ | |
48 | ||
49 | 8 | public LocalizedResource resolve(String resourcePath, Locale locale) |
50 | { | |
51 | 8 | int dotx = resourcePath.lastIndexOf('.'); |
52 | 8 | String basePath; |
53 | 8 | String suffix; |
54 | 8 | if (dotx >= 0) { |
55 | 7 | basePath = resourcePath.substring(0, dotx); |
56 | 7 | suffix = resourcePath.substring(dotx); |
57 | } | |
58 | else | |
59 | { | |
60 | // Resource without extension | |
61 | 1 | basePath = resourcePath; |
62 | 1 | suffix = ""; |
63 | } | |
64 | ||
65 | 8 | LocalizedNameGenerator generator = new LocalizedNameGenerator(basePath, locale, suffix); |
66 | ||
67 | 8 | while (generator.more()) |
68 | { | |
69 | 13 | String candidatePath = generator.next(); |
70 | ||
71 | 13 | if (_resolver.getResource(candidatePath) != null) |
72 | 6 | return new LocalizedResource(candidatePath, generator.getCurrentLocale()); |
73 | } | |
74 | ||
75 | 2 | return null; |
76 | } | |
77 | } |
|