001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.json.JSONObject;
018    
019    import java.util.Locale;
020    import java.util.Map;
021    
022    import javax.portlet.PortletPreferences;
023    import javax.portlet.PortletRequest;
024    
025    /**
026     * <p>
027     * This class is used to localize values stored in XML and is often used to add
028     * localization behavior to value objects.
029     * </p>
030     *
031     * <p>
032     * Caching of the localized values is done in this class rather than in the
033     * value object since value objects get flushed from cache fairly quickly.
034     * Though lookups performed on a key based on an XML file is slower than lookups
035     * done at the value object level in general, the value object will get flushed
036     * at a rate which works against the performance gain. The cache is a soft hash
037     * map which prevents memory leaks within the system while enabling the cache to
038     * live longer than in a weak hash map.
039     * </p>
040     *
041     * @author Alexander Chow
042     * @author Jorge Ferrer
043     * @author Mauro Mariuzzo
044     * @author Julio Camarero
045     * @author Brian Wing Shun Chan
046     */
047    public class LocalizationUtil {
048    
049            public static Object deserialize(JSONObject jsonObject) {
050                    return getLocalization().deserialize(jsonObject);
051            }
052    
053            public static String[] getAvailableLocales(String xml) {
054                    return getLocalization().getAvailableLocales(xml);
055            }
056    
057            public static String getDefaultLocale(String xml) {
058                    return getLocalization().getDefaultLocale(xml);
059            }
060    
061            public static Localization getLocalization() {
062                    return _localization;
063            }
064    
065            public static String getLocalization(
066                    String xml, String requestedLanguageId) {
067    
068                    return getLocalization().getLocalization(xml, requestedLanguageId);
069            }
070    
071            public static String getLocalization(
072                    String xml, String requestedLanguageId, boolean useDefault) {
073    
074                    return getLocalization().getLocalization(
075                            xml, requestedLanguageId, useDefault);
076            }
077    
078            public static Map<Locale, String> getLocalizationMap(
079                    PortletPreferences preferences, String parameter) {
080    
081                    return getLocalization().getLocalizationMap(preferences, parameter);
082            }
083    
084            public static Map<Locale, String> getLocalizationMap(
085                    PortletRequest portletRequest, String parameter) {
086    
087                    return getLocalization().getLocalizationMap(portletRequest, parameter);
088            }
089    
090            public static Map<Locale, String> getLocalizationMap(String xml) {
091                    return getLocalization().getLocalizationMap(xml);
092            }
093    
094            public static String getLocalizationXmlFromPreferences(
095                    PortletPreferences preferences, PortletRequest portletRequest,
096                    String parameter) {
097    
098                    return getLocalization().getLocalizationXmlFromPreferences(
099                            preferences, portletRequest, parameter);
100            }
101    
102            /**
103             * @deprecated Use <code>getLocalizationMap</code>.
104             */
105            public static Map<Locale, String> getLocalizedParameter(
106                    PortletRequest portletRequest, String parameter) {
107    
108                    return getLocalization().getLocalizedParameter(
109                            portletRequest, parameter);
110            }
111    
112            public static String getPreferencesKey(String key, String languageId) {
113                    return getLocalization().getPreferencesKey(key, languageId);
114            }
115    
116            public static String getPreferencesValue(
117                    PortletPreferences preferences, String key, String languageId) {
118    
119                    return getLocalization().getPreferencesValue(
120                            preferences, key, languageId);
121            }
122    
123            public static String getPreferencesValue(
124                    PortletPreferences preferences, String key, String languageId,
125                    boolean useDefault) {
126    
127                    return getLocalization().getPreferencesValue(
128                            preferences, key, languageId, useDefault);
129            }
130    
131            public static String[] getPreferencesValues(
132                    PortletPreferences preferences, String key, String languageId) {
133    
134                    return getLocalization().getPreferencesValues(
135                            preferences, key, languageId);
136            }
137    
138            public static String[] getPreferencesValues(
139                    PortletPreferences preferences, String key, String languageId,
140                    boolean useDefault) {
141    
142                    return getLocalization().getPreferencesValues(
143                            preferences, key, languageId, useDefault);
144            }
145    
146            public static String removeLocalization(
147                    String xml, String key, String requestedLanguageId) {
148    
149                    return getLocalization().removeLocalization(
150                            xml, key, requestedLanguageId);
151            }
152    
153            public static String removeLocalization(
154                    String xml, String key, String requestedLanguageId, boolean cdata) {
155    
156                    return getLocalization().removeLocalization(
157                            xml, key, requestedLanguageId, cdata);
158            }
159    
160            public static String removeLocalization(
161                    String xml, String key, String requestedLanguageId, boolean cdata,
162                    boolean localized) {
163    
164                    return getLocalization().removeLocalization(
165                            xml, key, requestedLanguageId, cdata, localized);
166            }
167    
168            public static void setLocalizedPreferencesValues (
169                            PortletRequest portletRequest, PortletPreferences preferences,
170                            String parameter)
171                    throws Exception {
172    
173                    getLocalization().setLocalizedPreferencesValues(
174                            portletRequest, preferences, parameter);
175            }
176    
177            public static void setPreferencesValue(
178                            PortletPreferences preferences, String key, String languageId,
179                            String value)
180                    throws Exception {
181    
182                    getLocalization().setPreferencesValue(
183                            preferences, key, languageId, value);
184            }
185    
186            public static void setPreferencesValues(
187                            PortletPreferences preferences, String key, String languageId,
188                            String[] values)
189                    throws Exception {
190    
191                    getLocalization().setPreferencesValues(
192                            preferences, key, languageId, values);
193            }
194    
195            public static String updateLocalization(
196                    String xml, String key, String value) {
197    
198                    return getLocalization().updateLocalization(xml, key, value);
199            }
200    
201            public static String updateLocalization(
202                    String xml, String key, String value, String requestedLanguageId) {
203    
204                    return getLocalization().updateLocalization(
205                            xml, key, value, requestedLanguageId);
206            }
207    
208            public static String updateLocalization(
209                    String xml, String key, String value, String requestedLanguageId,
210                    String defaultLanguageId) {
211    
212                    return getLocalization().updateLocalization(
213                            xml, key, value, requestedLanguageId, defaultLanguageId);
214            }
215    
216            public static String updateLocalization(
217                    String xml, String key, String value, String requestedLanguageId,
218                    String defaultLanguageId, boolean cdata) {
219    
220                    return getLocalization().updateLocalization(
221                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata);
222            }
223    
224            public static String updateLocalization(
225                    String xml, String key, String value, String requestedLanguageId,
226                    String defaultLanguageId, boolean cdata, boolean localized) {
227    
228                    return getLocalization().updateLocalization(
229                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata,
230                            localized);
231            }
232    
233            public void setLocalization(Localization localization) {
234                    _localization = localization;
235            }
236    
237            private static Localization _localization;
238    
239    }