001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt;
029
030import org.opencms.file.CmsObject;
031import org.opencms.flex.CmsFlexController;
032import org.opencms.i18n.CmsLocaleManager;
033import org.opencms.i18n.CmsResourceBundleLoader;
034import org.opencms.i18n.I_CmsMessageBundle;
035import org.opencms.json.JSONException;
036import org.opencms.json.JSONObject;
037import org.opencms.main.CmsLog;
038import org.opencms.main.OpenCms;
039
040import java.lang.reflect.Method;
041import java.util.Enumeration;
042import java.util.Locale;
043import java.util.ResourceBundle;
044
045import javax.servlet.http.HttpServletRequest;
046
047import org.apache.commons.logging.Log;
048
049/**
050 * Convenience class to access the localized messages of this OpenCms package.<p>
051 *
052 * Intended only for test cases.<p>
053 *
054 * @since 8.0.0
055 */
056public abstract class A_CmsClientMessageBundle implements I_CmsClientMessageBundle {
057
058    /** Prefix for imported message values. */
059    public static final String IMPORT_PREFIX = "@@import:";
060
061    /** Static reference to the log. */
062    private static final Log LOG = CmsLog.getLog(A_CmsClientMessageBundle.class);
063
064    /**
065     * Hides the public constructor for this utility class.<p>
066     */
067    protected A_CmsClientMessageBundle() {
068
069        // empty
070    }
071
072    /**
073     * @see org.opencms.gwt.I_CmsClientMessageBundle#export(javax.servlet.http.HttpServletRequest)
074     */
075    public String export(HttpServletRequest request) {
076
077        CmsObject cms = CmsFlexController.getCmsObject(request);
078        return export(OpenCms.getWorkplaceManager().getWorkplaceLocale(cms));
079    }
080
081    /**
082     * @see org.opencms.gwt.I_CmsClientMessageBundle#export(java.util.Locale)
083     */
084    public String export(Locale locale) {
085
086        return export(locale, true);
087    }
088
089    /**
090     * @see org.opencms.gwt.I_CmsClientMessageBundle#export(java.util.Locale, boolean)
091     */
092    public String export(Locale locale, boolean wrapScript) {
093
094        JSONObject keys = new JSONObject();
095        try {
096            ResourceBundle resourceBundle = CmsResourceBundleLoader.getBundle(getBundleName(), locale);
097            Enumeration<String> bundleKeys = resourceBundle.getKeys();
098            while (bundleKeys.hasMoreElements()) {
099                String bundleKey = bundleKeys.nextElement();
100                String value = resourceBundle.getString(bundleKey);
101                if (value.startsWith(IMPORT_PREFIX)) {
102                    String importKey = value.replace(IMPORT_PREFIX, "");
103                    String importedValue = importMessage(importKey, locale);
104                    if (importedValue != null) {
105                        value = importedValue;
106                    }
107                }
108                keys.put(bundleKey, value);
109            }
110        } catch (Throwable e) {
111            LOG.error(e.getLocalizedMessage(), e);
112            try {
113                keys.put("error", e.getLocalizedMessage());
114            } catch (JSONException e1) {
115                // ignore, should never happen
116                LOG.error(e1.getLocalizedMessage(), e1);
117            }
118        }
119        StringBuffer sb = new StringBuffer();
120        String script = getBundleName().replace('.', '_') + "=" + keys.toString() + ";";
121        if (wrapScript) {
122            script = CmsGwtActionElement.wrapScript(script);
123        }
124        sb.append(script);
125        return sb.toString();
126    }
127
128    /**
129     * @see org.opencms.gwt.I_CmsClientMessageBundle#export(java.lang.String)
130     */
131    public String export(String localeName) {
132
133        return export(CmsLocaleManager.getLocale(localeName));
134    }
135
136    /**
137     * @see org.opencms.gwt.I_CmsClientMessageBundle#getBundleName()
138     */
139    public String getBundleName() {
140
141        return getClass().getPackage().getName() + ".clientmessages";
142    }
143
144    /**
145     * @see org.opencms.gwt.I_CmsClientMessageBundle#getClientImpl()
146     */
147    public Class<?> getClientImpl() throws Exception {
148
149        return Class.forName(getClass().getPackage().getName() + ".client.Messages");
150    }
151
152    /**
153     * Imports a message from another bundle.<p>
154     *
155     * @param key a key of the form classname#MESSAGE_FIELD_NAME
156     * @param locale the locale for which to import the message
157     *
158     * @return the imported message string
159     */
160    public String importMessage(String key, Locale locale) {
161
162        key = key.trim();
163        String[] tokens = key.split("#");
164        if (tokens.length != 2) {
165            return null;
166        }
167        String className = tokens[0];
168        String messageName = tokens[1];
169        try {
170            Method messagesGet = Class.forName(className).getMethod("get");
171            I_CmsMessageBundle bundle = (I_CmsMessageBundle)(messagesGet.invoke(null));
172            return bundle.getBundle(locale).key(messageName);
173        } catch (Exception e) {
174            LOG.error(e.getLocalizedMessage(), e);
175            return null;
176        }
177    }
178}