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.widgets;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.i18n.CmsEncoder;
033import org.opencms.i18n.CmsLocaleManager;
034import org.opencms.i18n.CmsMessages;
035import org.opencms.util.CmsMacroResolver;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.util.I_CmsMacroResolver;
038import org.opencms.xml.content.I_CmsXmlContentHandler.DisplayType;
039import org.opencms.xml.types.A_CmsXmlContentValue;
040import org.opencms.xml.types.I_CmsXmlContentValue;
041
042import java.util.Iterator;
043import java.util.List;
044import java.util.Locale;
045import java.util.Map;
046import java.util.regex.Matcher;
047import java.util.regex.Pattern;
048
049/**
050 * Provides a standard HTML form input widget for overwriting localized values of a resource bundle, for use on a widget dialog.<p>
051 *
052 * The resource bundle is configured with the widget configuration attribute. An optional key name to look up in the bundle
053 * can be given, too, in case it is different from the element name: <code>key=mykey</code>.<p>
054 *
055 * The locale to get the value for can be configured, too, by adding a configuration directive: <code>locale=en</code>.<p>
056 *
057 * Example: <code><layout element="elemname" widget="LocalizationWidget" configuration="org.opencms.workplace.messages|key=mykey|locale=en" /></code>.<p>
058 *
059 * To use the stored localization values and have the values of the resource bundles as fallback,
060 * use the {@link org.opencms.xml.CmsXmlMessages} object.<p>
061 *
062 * @since 6.5.4
063 */
064public class CmsLocalizationWidget extends A_CmsWidget implements I_CmsADEWidget {
065
066    /** The option for the localized key name. */
067    public static final String OPTION_KEY = "key=";
068
069    /** The option for the locale to use.  */
070    public static final String OPTION_LOCALE = "locale=";
071
072    /** Pattern to get OpenCms like macros, e.g. "%(0)". */
073    private static Pattern PATTERN_MACRO = Pattern.compile(
074        ".*("
075            + I_CmsMacroResolver.MACRO_DELIMITER
076            + "\\"
077            + I_CmsMacroResolver.MACRO_START
078            + ")(\\d*)(\\"
079            + I_CmsMacroResolver.MACRO_END
080            + ").*");
081
082    /** Pattern to get message bundle arguments, e.g. "{0}". */
083    private static Pattern PATTERN_MESSAGEARGUMENT = Pattern.compile(".*(\\{)(\\d*)(\\}).*");
084
085    /** The bundle key (optional, if not equal to the element name). */
086    private String m_bundleKey;
087
088    /** The locale to get the value for. */
089    private Locale m_locale;
090
091    /** The localized bundle to get the value from. */
092    private CmsMessages m_messages;
093
094    /**
095     * Creates a new input localization widget.<p>
096     */
097    public CmsLocalizationWidget() {
098
099        // empty constructor is required for class registration
100        this("");
101    }
102
103    /**
104     * Creates a new input localization widget with the given configuration.<p>
105     *
106     * @param configuration the configuration to use
107     */
108    public CmsLocalizationWidget(String configuration) {
109
110        super(configuration);
111    }
112
113    /**
114     * @see org.opencms.widgets.I_CmsADEWidget#getConfiguration(org.opencms.file.CmsObject, org.opencms.xml.types.A_CmsXmlContentValue, org.opencms.i18n.CmsMessages, org.opencms.file.CmsResource, java.util.Locale)
115     */
116    public String getConfiguration(
117        CmsObject cms,
118        A_CmsXmlContentValue schemaType,
119        CmsMessages messages,
120        CmsResource resource,
121        Locale contentLocale) {
122
123        initConfiguration(cms, schemaType);
124        return m_messages.key(m_bundleKey);
125    }
126
127    /**
128     * @see org.opencms.widgets.I_CmsADEWidget#getCssResourceLinks(org.opencms.file.CmsObject)
129     */
130    public List<String> getCssResourceLinks(CmsObject cms) {
131
132        return null;
133    }
134
135    /**
136     * @see org.opencms.widgets.I_CmsADEWidget#getDefaultDisplayType()
137     */
138    public DisplayType getDefaultDisplayType() {
139
140        return DisplayType.wide;
141    }
142
143    /**
144     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
145     */
146    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
147
148        String id = param.getId();
149        // initialize bundle
150        initConfiguration(cms, param);
151
152        StringBuffer result = new StringBuffer(256);
153
154        result.append("<td class=\"xmlTd\">");
155        result.append("<input class=\"xmlInput textInput");
156        if (param.hasError()) {
157            result.append(" xmlInputError");
158        }
159        result.append("\"");
160        result.append(" name=\"");
161        result.append(id);
162        result.append("\" id=\"");
163        result.append(id);
164        result.append("\" value=\"");
165
166        // determine value to show in editor
167        String value = getValue(cms, param);
168        result.append(CmsEncoder.escapeXml(value));
169        result.append("\">");
170        result.append("</td>");
171
172        return result.toString();
173    }
174
175    /**
176     * @see org.opencms.widgets.I_CmsADEWidget#getInitCall()
177     */
178    public String getInitCall() {
179
180        return null;
181    }
182
183    /**
184     * @see org.opencms.widgets.I_CmsADEWidget#getJavaScriptResourceLinks(org.opencms.file.CmsObject)
185     */
186    public List<String> getJavaScriptResourceLinks(CmsObject cms) {
187
188        return null;
189    }
190
191    /**
192     * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName()
193     */
194    public String getWidgetName() {
195
196        return CmsLocalizationWidget.class.getName();
197    }
198
199    /**
200     * @see org.opencms.widgets.I_CmsADEWidget#isInternal()
201     */
202    public boolean isInternal() {
203
204        return true;
205    }
206
207    /**
208     * @see org.opencms.widgets.I_CmsWidget#newInstance()
209     */
210    public I_CmsWidget newInstance() {
211
212        return new CmsLocalizationWidget(getConfiguration());
213    }
214
215    /**
216     * @see org.opencms.widgets.I_CmsWidget#setEditorValue(org.opencms.file.CmsObject, java.util.Map, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
217     */
218    @Override
219    public void setEditorValue(
220        CmsObject cms,
221        Map<String, String[]> formParameters,
222        I_CmsWidgetDialog widgetDialog,
223        I_CmsWidgetParameter param) {
224
225        String[] values = formParameters.get(param.getId());
226        if ((values != null) && (values.length > 0)) {
227            // initialize bundle
228            initConfiguration(cms, param);
229            String value = m_messages.key(m_bundleKey);
230            if (value.equals(values[0].trim())) {
231                // value is equal to localized value, do not save
232                value = "";
233            } else {
234                // value is different, save it
235                value = values[0];
236                // now replace message bundle like argument placeholders like "{0}" with OpenCms macros
237                Matcher matcher = PATTERN_MESSAGEARGUMENT.matcher(value);
238                while (matcher.matches()) {
239                    int startIndex = matcher.start(1);
240                    int endIndex = matcher.end(3);
241                    String number = CmsMacroResolver.formatMacro(matcher.group(2));
242                    // replace arguments with macros
243                    value = value.substring(0, startIndex) + number + value.substring(endIndex);
244                    matcher = PATTERN_MESSAGEARGUMENT.matcher(value);
245                }
246            }
247            param.setStringValue(cms, value);
248        }
249    }
250
251    /**
252     * Initializes the localized bundle to get the value from, the optional key name and the optional locale.<p>
253     *
254     * @param cms an initialized instance of a CmsObject
255     * @param schemaType the widget parameter to generate the widget for
256     */
257    protected void initConfiguration(CmsObject cms, A_CmsXmlContentValue schemaType) {
258
259        // set the default bundle key
260        m_bundleKey = schemaType.getName();
261        // set the default locale for XML contents
262        m_locale = cms.getRequestContext().getLocale();
263        try {
264            I_CmsXmlContentValue value = schemaType;
265            m_locale = value.getLocale();
266        } catch (Exception e) {
267            // ignore, this is no XML content
268        }
269
270        // check the message bundle
271        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getConfiguration())) {
272            //initialize messages, the optional bundle key name and the optional locale from configuration String
273            String bundleName = "";
274            List<String> configs = CmsStringUtil.splitAsList(getConfiguration(), '|');
275            Iterator<String> i = configs.iterator();
276            while (i.hasNext()) {
277                String config = i.next();
278                if (config.startsWith(OPTION_KEY)) {
279                    m_bundleKey = config.substring(OPTION_KEY.length());
280                } else if (config.startsWith(OPTION_LOCALE)) {
281                    m_locale = CmsLocaleManager.getLocale(config.substring(OPTION_LOCALE.length()));
282                } else {
283                    bundleName = config;
284                }
285            }
286            // create messages object
287            m_messages = new CmsMessages(bundleName, m_locale);
288        } else {
289            // initialize empty messages object to avoid NPE
290            m_messages = new CmsMessages("", m_locale);
291        }
292    }
293
294    /**
295     * Initializes the localized bundle to get the value from, the optional key name and the optional locale.<p>
296     *
297     * @param cms an initialized instance of a CmsObject
298     * @param param the widget parameter to generate the widget for
299     */
300    protected void initConfiguration(CmsObject cms, I_CmsWidgetParameter param) {
301
302        // set the default bundle key
303        m_bundleKey = param.getName();
304        // set the default locale for XML contents
305        m_locale = cms.getRequestContext().getLocale();
306        try {
307            I_CmsXmlContentValue value = (I_CmsXmlContentValue)param;
308            m_locale = value.getLocale();
309        } catch (Exception e) {
310            // ignore, this is no XML content
311        }
312
313        // check the message bundle
314        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getConfiguration())) {
315            //initialize messages, the optional bundle key name and the optional locale from configuration String
316            String bundleName = "";
317            List<String> configs = CmsStringUtil.splitAsList(getConfiguration(), '|');
318            Iterator<String> i = configs.iterator();
319            while (i.hasNext()) {
320                String config = i.next();
321                if (config.startsWith(OPTION_KEY)) {
322                    m_bundleKey = config.substring(OPTION_KEY.length());
323                } else if (config.startsWith(OPTION_LOCALE)) {
324                    m_locale = CmsLocaleManager.getLocale(config.substring(OPTION_LOCALE.length()));
325                } else {
326                    bundleName = config;
327                }
328            }
329            // create messages object
330            m_messages = new CmsMessages(bundleName, m_locale);
331        } else {
332            // initialize empty messages object to avoid NPE
333            m_messages = new CmsMessages("", m_locale);
334        }
335    }
336
337    /**
338     * Determine value to show in editor.<p>
339     * @param cms an initialized instance of a CmsObject
340     * @param param the widget parameter to generate the widget for
341     *
342     * @return value to show in editor
343     */
344    private String getValue(CmsObject cms, I_CmsWidgetParameter param) {
345
346        String value = m_messages.key(m_bundleKey);
347        if ((CmsStringUtil.isNotEmptyOrWhitespaceOnly(param.getStringValue(cms))
348            && !value.equals(param.getStringValue(cms))) || value.startsWith(CmsMessages.UNKNOWN_KEY_EXTENSION)) {
349            // saved value is provided and different from localized value in bundle or no value found in bundle, use it
350            value = param.getStringValue(cms);
351            // replace OpenCms macro syntax with message bundle arguments
352            Matcher matcher = PATTERN_MACRO.matcher(value);
353            while (matcher.matches()) {
354                int startIndex = matcher.start(1);
355                int endIndex = matcher.end(3);
356                String number = matcher.group(2);
357                value = value.substring(0, startIndex) + "{" + number + "}" + value.substring(endIndex);
358                matcher = PATTERN_MACRO.matcher(value);
359            }
360
361        }
362        return value;
363    }
364
365}