001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.resource.loader; 018 019import java.util.Locale; 020import java.util.MissingResourceException; 021import java.util.ResourceBundle; 022 023import org.apache.wicket.Component; 024import org.apache.wicket.Session; 025 026 027/** 028 * Implementation of a string resource loader that sits on top of the ordinary Java resource bundle 029 * mechanism. When created this loader must be given the name of the resource bundle that it is to 030 * sit on top of. Note that this implementation does not make use of any style or component specific 031 * knowledge - it utilizes just the bundle name, the resource key and the locale. 032 * 033 * @author Chris Turner 034 */ 035public class BundleStringResourceLoader implements IStringResourceLoader 036{ 037 /** The name of the underlying resource bundle. */ 038 private final String bundleName; 039 040 /** 041 * Create the loader with the name of the given Java resource bundle. 042 * 043 * @param bundleName 044 * The name of the resource bundle 045 */ 046 public BundleStringResourceLoader(final String bundleName) 047 { 048 this.bundleName = bundleName; 049 } 050 051 /** 052 * Get the value via a Java ResourceBundle 053 */ 054 @Override 055 public final String loadStringResource(final Class<?> clazz, final String key, Locale locale, 056 final String style, final String variation) 057 { 058 if (locale == null) 059 { 060 locale = Session.exists() ? Session.get().getLocale() : Locale.getDefault(); 061 } 062 try 063 { 064 return ResourceBundle.getBundle(bundleName, locale).getString(key); 065 } 066 catch (MissingResourceException mrx) 067 { 068 try 069 { 070 return ResourceBundle.getBundle(bundleName, locale, Thread.currentThread().getContextClassLoader()).getString(key); 071 } 072 catch (MissingResourceException mrx2) 073 { 074 return null; 075 } 076 } 077 } 078 079 /** 080 * Get the requested string resource from the underlying resource bundle. The bundle is selected 081 * by locale and the string obtained from the best matching bundle. 082 * 083 * @param component 084 * Used to get the locale 085 * @param key 086 * The key to obtain the string for 087 * @param locale 088 * If != null, it supersedes the component's locale 089 * @param style 090 * ignored 091 * @param variation 092 * ignored 093 * @return The string resource value or null if resource not found 094 */ 095 @Override 096 public final String loadStringResource(final Component component, final String key, 097 Locale locale, final String style, final String variation) 098 { 099 return loadStringResource((Class<?>)null, key, locale, style, variation); 100 } 101}