001: /*
002: * Copyright (c) 2003 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.ext.beans;
054:
055: import java.text.MessageFormat;
056: import java.util.Hashtable;
057: import java.util.Iterator;
058: import java.util.Enumeration;
059: import java.util.List;
060: import java.util.Map;
061: import java.util.Set;
062: import java.util.MissingResourceException;
063: import java.util.ResourceBundle;
064:
065: import freemarker.ext.util.ModelFactory;
066: import freemarker.template.ObjectWrapper;
067: import freemarker.template.TemplateMethodModelEx;
068: import freemarker.template.TemplateModel;
069: import freemarker.template.TemplateModelException;
070:
071: /**
072: * <p>A hash model that wraps a resource bundle. Makes it convenient to store
073: * localized content in the data model. It also acts as a method model that will
074: * take a resource key and arbitrary number of arguments and will apply
075: * {@link MessageFormat} with arguments on the string represented by the key.</p>
076: *
077: * <p>Typical usages:</p>
078: * <ul>
079: * <li><tt>bundle.resourceKey</tt> will retrieve the object from resource bundle
080: * with key <tt>resourceKey</tt></li>
081: * <li><tt>bundle("patternKey", arg1, arg2, arg3)</tt> will retrieve the string
082: * from resource bundle with key <tt>patternKey</tt>, and will use it as a pattern
083: * for MessageFormat with arguments arg1, arg2 and arg3</li>
084: * </ul>
085: * @author Attila Szegedi
086: * @version $Id: ResourceBundleModel.java,v 1.22.2.2 2007/04/02 13:19:37 szegedia Exp $
087: */
088: public class ResourceBundleModel extends BeanModel implements
089: TemplateMethodModelEx {
090: static final ModelFactory FACTORY = new ModelFactory() {
091: public TemplateModel create(Object object, ObjectWrapper wrapper) {
092: return new ResourceBundleModel((ResourceBundle) object,
093: (BeansWrapper) wrapper);
094: }
095: };
096:
097: private Hashtable formats = null;
098:
099: public ResourceBundleModel(ResourceBundle bundle,
100: BeansWrapper wrapper) {
101: super (bundle, wrapper);
102: }
103:
104: /**
105: * Overridden to invoke the getObject method of the resource bundle.
106: */
107: protected TemplateModel invokeGenericGet(Map keyMap, Class clazz,
108: String key) throws TemplateModelException {
109: try {
110: return wrap(((ResourceBundle) object).getObject(key));
111: } catch (MissingResourceException e) {
112: throw new TemplateModelException("No such key: " + key);
113: }
114: }
115:
116: /**
117: * Returns true if this bundle contains no objects.
118: */
119: public boolean isEmpty() {
120: return !((ResourceBundle) object).getKeys().hasMoreElements()
121: && super .isEmpty();
122: }
123:
124: public int size() {
125: return keySet().size();
126: }
127:
128: protected Set keySet() {
129: Set set = super .keySet();
130: Enumeration e = ((ResourceBundle) object).getKeys();
131: while (e.hasMoreElements()) {
132: set.add(e.nextElement());
133: }
134: return set;
135: }
136:
137: /**
138: * Takes first argument as a resource key, looks up a string in resource bundle
139: * with this key, then applies a MessageFormat.format on the string with the
140: * rest of the arguments. The created MessageFormats are cached for later reuse.
141: */
142: public Object exec(List arguments) throws TemplateModelException {
143: // Must have at least one argument - the key
144: if (arguments.size() < 1)
145: throw new TemplateModelException(
146: "No message key was specified");
147: // Read it
148: Iterator it = arguments.iterator();
149: String key = unwrap((TemplateModel) it.next()).toString();
150: try {
151: if (!it.hasNext()) {
152: return wrap(((ResourceBundle) object).getObject(key));
153: }
154:
155: // Copy remaining arguments into an Object[]
156: int args = arguments.size() - 1;
157: Object[] params = new Object[args];
158: for (int i = 0; i < args; ++i)
159: params[i] = unwrap((TemplateModel) it.next());
160:
161: // Invoke format
162: return new StringModel(format(key, params), wrapper);
163: } catch (MissingResourceException e) {
164: throw new TemplateModelException("No such key: " + key);
165: } catch (Exception e) {
166: throw new TemplateModelException(e.getMessage());
167: }
168: }
169:
170: /**
171: * Provides direct access to caching format engine from code (instead of from script).
172: */
173: public String format(String key, Object[] params)
174: throws MissingResourceException {
175: // Check to see if we already have a cache for message formats
176: // and construct it if we don't
177: // NOTE: this block statement should be synchronized. However
178: // concurrent creation of two caches will have no harmful
179: // consequences, and we avoid a performance hit.
180: /* synchronized(this) */
181: {
182: if (formats == null)
183: formats = new Hashtable();
184: }
185:
186: MessageFormat format = null;
187: // Check to see if we already have a requested MessageFormat cached
188: // and construct it if we don't
189: // NOTE: this block statement should be synchronized. However
190: // concurrent creation of two formats will have no harmful
191: // consequences, and we avoid a performance hit.
192: /* synchronized(formats) */
193: {
194: format = (MessageFormat) formats.get(key);
195: if (format == null) {
196: format = new MessageFormat(((ResourceBundle) object)
197: .getString(key));
198: format.setLocale(getBundle().getLocale());
199: formats.put(key, format);
200: }
201: }
202:
203: // Perform the formatting. We synchronize on it in case it
204: // contains date formatting, which is not thread-safe.
205: synchronized (format) {
206: return format.format(params);
207: }
208: }
209:
210: public ResourceBundle getBundle() {
211: return (ResourceBundle) object;
212: }
213: }
|