01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.resource.loader;
18:
19: import java.lang.ref.WeakReference;
20: import java.util.Locale;
21:
22: import org.apache.wicket.Component;
23: import org.apache.wicket.Session;
24:
25: /**
26: * This string resource loader attempts to find a single resource bundle that
27: * has the same name and location as the clazz provided in the constructor. If
28: * the bundle is found than strings are obtained from here. This implementation
29: * is fully aware of both locale and style values when trying to obtain the
30: * appropriate bundle.
31: * <p>
32: * An instance of this loader is registered with the Application by default.
33: *
34: * @author Chris Turner
35: * @author Juergen Donnerstag
36: */
37: public class ClassStringResourceLoader extends
38: ComponentStringResourceLoader {
39: /** The application we are loading for. */
40: private final WeakReference/*<Class<? extends Application>>*/clazzRef;
41:
42: /**
43: * Create and initialise the resource loader.
44: *
45: * @param clazz
46: * The class that this resource loader is associated with
47: */
48: public ClassStringResourceLoader(
49: final Class/*<? extends Application>*/clazz) {
50: if (clazz == null) {
51: throw new IllegalArgumentException(
52: "Parameter 'clazz' must not be null");
53: }
54: this .clazzRef = new WeakReference(clazz);
55: }
56:
57: /**
58: * @see org.apache.wicket.resource.loader.ComponentStringResourceLoader#loadStringResource(java.lang.Class,
59: * java.lang.String, java.util.Locale, java.lang.String)
60: */
61: public String loadStringResource(final Class clazz,
62: final String key, final Locale locale, final String style) {
63: return super .loadStringResource((Class) this .clazzRef.get(),
64: key, locale, style);
65: }
66:
67: /**
68: * @see org.apache.wicket.resource.loader.ComponentStringResourceLoader#loadStringResource(org.apache.wicket.Component,
69: * java.lang.String)
70: */
71: public String loadStringResource(Component component, String key) {
72: if (component == null) {
73: return loadStringResource(null, key, Session.get()
74: .getLocale(), Session.get().getStyle());
75: }
76: return super.loadStringResource(component, key);
77: }
78: }
|