|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ResourceTranslator.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.schema.rules; | |
16 | ||
17 | import java.util.Locale; | |
18 | ||
19 | import org.apache.hivemind.ApplicationRuntimeException; | |
20 | import org.apache.hivemind.HiveMind; | |
21 | import org.apache.hivemind.Location; | |
22 | import org.apache.hivemind.Resource; | |
23 | import org.apache.hivemind.internal.Module; | |
24 | import org.apache.hivemind.schema.Translator; | |
25 | ||
26 | /** | |
27 | * Translator that converts the value to be a resource relative to | |
28 | * the contributing module's deployment descriptor. | |
29 | * | |
30 | * @author Howard Lewis Ship | |
31 | */ | |
32 | public class ResourceTranslator implements Translator | |
33 | { | |
34 | /** | |
35 | * Finds the resource. If the inputValue is blank, then returns null. | |
36 | * Interprets the inputValue as a relative path from the contributing module's descriptor. | |
37 | * In addition, a localized resource will be returned if avaiable (localized to | |
38 | * the {@link org.apache.hivemind.Registry#getLocale() registry's locale}. | |
39 | * | |
40 | */ | |
41 | 4 | public Object translate( |
42 | Module contributingModule, | |
43 | Class propertyType, | |
44 | String inputValue, | |
45 | Location location) | |
46 | { | |
47 | 4 | if (HiveMind.isBlank(inputValue)) |
48 | 1 | return null; |
49 | ||
50 | 3 | Locale locale = contributingModule.getLocale(); |
51 | ||
52 | 3 | Resource descriptor = contributingModule.getLocation().getResource(); |
53 | ||
54 | 3 | Resource baseResource = descriptor.getRelativeResource(inputValue); |
55 | ||
56 | 3 | Resource result = baseResource.getLocalization(locale); |
57 | ||
58 | 3 | if (result == null) |
59 | 1 | throw new ApplicationRuntimeException( |
60 | RulesMessages.resourceLocalizationError(inputValue, contributingModule)); | |
61 | ||
62 | 2 | return result; |
63 | } | |
64 | ||
65 | } |
|