001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.editor.configuration;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONException;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.language.LanguageUtil;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.LocaleUtil;
025    import com.liferay.portal.theme.ThemeDisplay;
026    
027    import java.util.Locale;
028    import java.util.Map;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public abstract class BaseEditorConfigContributor
034            implements EditorConfigContributor {
035    
036            protected String getContentsLanguageDir(
037                    Map<String, Object> inputEditorTaglibAttributes) {
038    
039                    Locale contentsLocale = getContentsLocale(inputEditorTaglibAttributes);
040    
041                    return LanguageUtil.get(contentsLocale, "lang.dir");
042            }
043    
044            protected String getContentsLanguageId(
045                    Map<String, Object> inputEditorTaglibAttributes) {
046    
047                    Locale contentsLocale = getContentsLocale(inputEditorTaglibAttributes);
048    
049                    return LocaleUtil.toLanguageId(contentsLocale);
050            }
051    
052            protected Locale getContentsLocale(
053                    Map<String, Object> inputEditorTaglibAttributes) {
054    
055                    String contentsLanguageId = (String)inputEditorTaglibAttributes.get(
056                            "liferay-ui:input-editor:contentsLanguageId");
057    
058                    return LocaleUtil.fromLanguageId(contentsLanguageId);
059            }
060    
061            protected String getLanguageId(ThemeDisplay themeDisplay) {
062                    String languageId = LocaleUtil.toLanguageId(themeDisplay.getLocale());
063    
064                    Locale locale = LocaleUtil.fromLanguageId(languageId);
065    
066                    return LocaleUtil.toLanguageId(locale);
067            }
068    
069            protected JSONArray toJSONArray(String json) {
070                    try {
071                            return JSONFactoryUtil.createJSONArray(json);
072                    }
073                    catch (JSONException jsone) {
074                            _log.error("Unable to create a JSON array from: " + json, jsone);
075                    }
076    
077                    return JSONFactoryUtil.createJSONArray();
078            }
079    
080            protected JSONArray toJSONArray(String... values) {
081                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
082    
083                    for (String value : values) {
084                            jsonArray.put(value);
085                    }
086    
087                    return jsonArray;
088            }
089    
090            protected JSONObject toJSONObject(String json) {
091                    try {
092                            return JSONFactoryUtil.createJSONObject(json);
093                    }
094                    catch (JSONException jsone) {
095                            _log.error("Unable to create a JSON object from: " + json, jsone);
096                    }
097    
098                    return JSONFactoryUtil.createJSONObject();
099            }
100    
101            private static final Log _log = LogFactoryUtil.getLog(
102                    BaseEditorConfigContributor.class);
103    
104    }