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    @Override
068    public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException {
069        int size = arguments.size();
070        if (size < 1) {
071            throw new TemplateModelException("Invalid number of arguments for messages(key, args ..) method");
072        }
073        String key;
074        SimpleScalar scalar = (SimpleScalar) arguments.get(0);
075        if (scalar != null) {
076            key = scalar.getAsString();
077        } else {
078            throw new TemplateModelException("the argument is not defined");
079        }
080        String value;
081        try {
082            value = bundle.getString(key);
083        } catch (MissingResourceException e) {
084            return '!' + key + '!';
085        }
086        if (size > 1) { // format the string using given args
087            String[] args = new String[size - 1];
088            for (int i = 0; i < args.length; i++) {
089                args[i] = ((SimpleScalar) arguments.get(i + 1)).getAsString();
090            }
091            value = MessageFormat.format(value, (Object[]) args);
092        }
093        return value;
094    }
095
096}