View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.integration.commons.lang;
17  
18  import net.sf.morph.Defaults;
19  import net.sf.morph.lang.DecoratedLanguage;
20  
21  import org.apache.commons.lang.text.StrLookup;
22  
23  /**
24   * Implement StrLookup using a Morph Language and a source object.
25   *
26   * @author Matt Benson
27   * @since Morph 1.1
28   */
29  public class LanguageStrLookup extends StrLookup {
30  	private Object source;
31  	private DecoratedLanguage language;
32  
33  	/**
34  	 * Create a new LanguageStrLookup.
35  	 * @param source Object
36  	 */
37  	public LanguageStrLookup(Object source) {
38  		this.source = source;
39  	}
40  
41  	/**
42  	 * Create a new LanguageStrLookup.
43  	 * @param source Object
44  	 * @param language DecoratedLanguage
45  	 */
46  	public LanguageStrLookup(Object source, DecoratedLanguage language) {
47  		this(source);
48  		this.language = language;
49  	}
50  
51  	/**
52  	 * Resolve the specified "property".
53  	 * @param key String
54  	 */
55  	public String lookup(String key) {
56  		return (String) getLanguage().get(source, key, String.class);
57  	}
58  
59  	/**
60  	 * Get the language.
61  	 * @return the language
62  	 */
63  	public synchronized DecoratedLanguage getLanguage() {
64  		if (language == null) {
65  			setLanguage(Defaults.createLanguage());
66  		}
67  		return language;
68  	}
69  
70  	/**
71  	 * Set the language.
72  	 * @param language the language to set
73  	 */
74  	public synchronized void setLanguage(DecoratedLanguage language) {
75  		this.language = language;
76  	}
77  
78  }