001// Copyright 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 015package org.apache.tapestry.services.impl; 016 017import java.util.Locale; 018 019import org.apache.hivemind.util.Defense; 020import org.apache.hivemind.util.LocalizedNameGenerator; 021import org.apache.tapestry.engine.IPropertySource; 022 023/** 024 * Wraps around a {@link org.apache.tapestry.engine.IPropertySource}to query a series of localized 025 * keys. 026 * <p> 027 * This is much simpler than the old {@link org.apache.tapestry.util.LocalizedPropertySource}, and 028 * allows the locale to be specified on a thread-safe, per-invocation basis. 029 * 030 * @author Howard M. Lewis Ship 031 * @since 4.0 032 */ 033public class LocalizedPropertySource 034{ 035 private IPropertySource _source; 036 037 public LocalizedPropertySource(IPropertySource source) 038 { 039 Defense.notNull(source, "source"); 040 041 _source = source; 042 } 043 044 /** 045 * Get the property from the source, trying different variations of propertyName (adding 046 * suffixes). 047 * 048 * @param propertyName 049 * the base property name to search with. 050 * @param local 051 * the Locale used to generate suffixes (may be null). 052 */ 053 054 public String getPropertyValue(String propertyName, Locale locale) 055 { 056 Defense.notNull(propertyName, "propertyName"); 057 058 LocalizedNameGenerator g = new LocalizedNameGenerator(propertyName, locale, ""); 059 060 while (g.more()) 061 { 062 String localizedName = g.next(); 063 064 String result = _source.getPropertyValue(localizedName); 065 066 if (result != null) 067 return result; 068 } 069 070 return null; 071 } 072}