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 org.apache.wicket.core.util.resource.locator.IResourceNameIterator; 020import org.apache.wicket.resource.IPropertiesFactory; 021import org.apache.wicket.resource.Properties; 022import org.apache.wicket.util.string.Strings; 023 024import java.util.Locale; 025 026 027/** 028 * This is one of Wicket's default string resource loaders. 029 * <p> 030 * The package based string resource loader attempts to find the resource from a bundle that 031 * corresponds to the supplied component objects package or one of its parent packages. 032 * <p> 033 * The search order for resources is component object package towards root package. 034 * <p> 035 * This implementation is fully aware of both locale and style values when trying to obtain the 036 * appropriate resources. 037 * <p> 038 * Looks for file called <code>package.properties</code> 039 * 040 * @author Juergen Donnerstag 041 * @author igor.vaynberg 042 */ 043public class PackageStringResourceLoader extends ComponentStringResourceLoader 044{ 045 /** The name (without extension) of the properties file */ 046 private String filename = "wicket-package"; 047 048 /** 049 * Create and initialize the resource loader. 050 */ 051 public PackageStringResourceLoader() 052 { 053 } 054 055 @Override 056 public String loadStringResource(Class<?> clazz, final String key, final Locale locale, 057 final String style, final String variation) 058 { 059 if (clazz == null) 060 { 061 return null; 062 } 063 064 // Load the properties associated with the path 065 IPropertiesFactory propertiesFactory = getPropertiesFactory(); 066 067 while (true) 068 { 069 Package pkg = clazz.getPackage(); 070 String packageName = (pkg == null) ? "" : pkg.getName(); 071 packageName = packageName.replace('.', '/'); 072 073 do 074 { 075 // Create the base path 076 String path = filename; 077 if (packageName.length() > 0) 078 { 079 path = packageName + "/" + path; 080 } 081 082 // Iterator over all the combinations 083 IResourceNameIterator iter = newResourceNameIterator(path, locale, style, variation); 084 while (iter.hasNext()) 085 { 086 String newPath = iter.next(); 087 088 Properties props = propertiesFactory.load(clazz, newPath); 089 if (props != null) 090 { 091 // Lookup the value 092 String value = props.getString(key); 093 if (value != null) 094 { 095 return value; 096 } 097 } 098 } 099 100 // Didn't find the key yet, continue searching if possible 101 packageName = Strings.beforeLast(packageName, '/'); 102 } 103 while (packageName.length() > 0); 104 105 clazz = clazz.getSuperclass(); 106 if (clazz == null) 107 { 108 break; 109 } 110 } 111 // not found 112 return null; 113 } 114 115 116 /** 117 * Gets the properties file filename (without extension) 118 * 119 * @return filename 120 */ 121 public String getFilename() 122 { 123 return filename; 124 } 125 126 /** 127 * Sets the properties filename (without extension) 128 * 129 * @param filename 130 * filename 131 */ 132 public void setFilename(String filename) 133 { 134 this.filename = filename; 135 } 136}