001/* 
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.rendering.fm.extensions;
023
024import java.text.MessageFormat;
025import java.util.List;
026import java.util.ListResourceBundle;
027import java.util.MissingResourceException;
028import java.util.ResourceBundle;
029
030import freemarker.template.SimpleScalar;
031import freemarker.template.TemplateMethodModelEx;
032import freemarker.template.TemplateModelException;
033
034/**
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class MessagesMethod implements TemplateMethodModelEx {
038
039    protected static final ResourceBundle NULL_BUNDLE = new ListResourceBundle() {
040        @Override
041        protected Object[][] getContents() {
042            return new Object[0][0];
043        }
044    };
045
046    protected ResourceBundle bundle;
047
048    public MessagesMethod(ResourceBundle bundle) {
049        setBundle(bundle);
050    }
051
052    public void setBundle(ResourceBundle bundle) {
053        this.bundle = bundle;
054        if (this.bundle == null) {
055            try {
056                this.bundle = ResourceBundle.getBundle("messages");
057            } catch (MissingResourceException e) {
058                this.bundle = NULL_BUNDLE;
059            }
060        }
061    }
062
063    public ResourceBundle getBundle() {
064        return bundle;
065    }
066
067    public Object exec(List arguments) throws TemplateModelException {
068        int size = arguments.size();
069        if (size < 1) {
070            throw new TemplateModelException("Invalid number of arguments for messages(key, args ..) method");
071        }
072        String key;
073        SimpleScalar scalar = (SimpleScalar) arguments.get(0);
074        if (scalar != null) {
075            key = scalar.getAsString();
076        } else {
077            throw new TemplateModelException("the argument is not defined");
078        }
079        String value;
080        try {
081            value = bundle.getString(key);
082        } catch (MissingResourceException e) {
083            return '!' + key + '!';
084        }
085        if (size > 1) { // format the string using given args
086            String[] args = new String[size - 1];
087            for (int i = 0; i < args.length; i++) {
088                args[i] = ((SimpleScalar) arguments.get(i + 1)).getAsString();
089            }
090            value = MessageFormat.format(value, (Object[]) args);
091        }
092        return value;
093    }
094
095}