Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/hivemind/schema/rules/ResourceTranslator.java


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      public Object translate(
42          Module contributingModule,
43          Class propertyType,
44          String inputValue,
45          Location location)
46      {
47          if (HiveMind.isBlank(inputValue))
48              return null;
49  
50          Locale locale = contributingModule.getLocale();
51  
52          Resource descriptor = contributingModule.getLocation().getResource();
53  
54          Resource baseResource = descriptor.getRelativeResource(inputValue);
55  
56          Resource result = baseResource.getLocalization(locale);
57  
58          if (result == null)
59              throw new ApplicationRuntimeException(
60                  RulesMessages.resourceLocalizationError(inputValue, contributingModule));
61  
62          return result;
63      }
64  
65  }