001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.hivemind.schema.rules;
016
017 import java.util.Locale;
018
019 import org.apache.hivemind.ApplicationRuntimeException;
020 import org.apache.hivemind.HiveMind;
021 import org.apache.hivemind.Location;
022 import org.apache.hivemind.Resource;
023 import org.apache.hivemind.internal.Module;
024 import org.apache.hivemind.schema.Translator;
025
026 /**
027 * Translator that converts the value to be a resource relative to
028 * the contributing module's deployment descriptor.
029 *
030 * @author Howard Lewis Ship
031 */
032 public class ResourceTranslator implements Translator
033 {
034 /**
035 * Finds the resource. If the inputValue is blank, then returns null.
036 * Interprets the inputValue as a relative path from the contributing module's descriptor.
037 * In addition, a localized resource will be returned if avaiable (localized to
038 * the {@link org.apache.hivemind.Registry#getLocale() registry's locale}.
039 *
040 */
041 public Object translate(
042 Module contributingModule,
043 Class propertyType,
044 String inputValue,
045 Location location)
046 {
047 if (HiveMind.isBlank(inputValue))
048 return null;
049
050 Locale locale = contributingModule.getLocale();
051
052 Resource descriptor = contributingModule.getLocation().getResource();
053
054 Resource baseResource = descriptor.getRelativeResource(inputValue);
055
056 Resource result = baseResource.getLocalization(locale);
057
058 if (result == null)
059 throw new ApplicationRuntimeException(
060 RulesMessages.resourceLocalizationError(inputValue, contributingModule));
061
062 return result;
063 }
064
065 }