001    /**
002     * Copyright (c) 2000-2011 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.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                    PortletRequest portletRequest, String parameter) {
080    
081                    return getLocalization().getLocalizationMap(portletRequest, parameter);
082            }
083    
084            public static Map<Locale, String> getLocalizationMap(String xml) {
085                    return getLocalization().getLocalizationMap(xml);
086            }
087    
088            public static String getLocalizationXmlFromPreferences(
089                    PortletPreferences preferences, PortletRequest portletRequest,
090                    String parameter) {
091    
092                    return getLocalization().getLocalizationXmlFromPreferences(
093                            preferences, portletRequest, parameter);
094            }
095    
096            /**
097             * @deprecated Use <code>getLocalizationMap</code>.
098             */
099            public static Map<Locale, String> getLocalizedParameter(
100                    PortletRequest portletRequest, String parameter) {
101    
102                    return getLocalization().getLocalizedParameter(
103                            portletRequest, parameter);
104            }
105    
106            public static String getPreferencesValue(
107                    PortletPreferences preferences, String key, String languageId) {
108    
109                    return getLocalization().getPreferencesValue(
110                            preferences, key, languageId);
111            }
112    
113            public static String getPreferencesValue(
114                    PortletPreferences preferences, String key, String languageId,
115                    boolean useDefault) {
116    
117                    return getLocalization().getPreferencesValue(
118                            preferences, key, languageId, useDefault);
119            }
120    
121            public static String[] getPreferencesValues(
122                    PortletPreferences preferences, String key, String languageId) {
123    
124                    return getLocalization().getPreferencesValues(
125                            preferences, key, languageId);
126            }
127    
128            public static String[] getPreferencesValues(
129                    PortletPreferences preferences, String key, String languageId,
130                    boolean useDefault) {
131    
132                    return getLocalization().getPreferencesValues(
133                            preferences, key, languageId, useDefault);
134            }
135    
136            public static String removeLocalization(
137                    String xml, String key, String requestedLanguageId) {
138    
139                    return getLocalization().removeLocalization(
140                            xml, key, requestedLanguageId);
141            }
142    
143            public static String removeLocalization(
144                    String xml, String key, String requestedLanguageId, boolean cdata) {
145    
146                    return getLocalization().removeLocalization(
147                            xml, key, requestedLanguageId, cdata);
148            }
149    
150            public static String removeLocalization(
151                    String xml, String key, String requestedLanguageId, boolean cdata,
152                    boolean localized) {
153    
154                    return getLocalization().removeLocalization(
155                            xml, key, requestedLanguageId, cdata, localized);
156            }
157    
158            public static void setLocalizedPreferencesValues (
159                            PortletRequest portletRequest, PortletPreferences preferences,
160                            String parameter)
161                    throws Exception {
162    
163                    getLocalization().setLocalizedPreferencesValues(
164                            portletRequest, preferences, parameter);
165            }
166    
167            public static void setPreferencesValue(
168                            PortletPreferences preferences, String key, String languageId,
169                            String value)
170                    throws Exception {
171    
172                    getLocalization().setPreferencesValue(
173                            preferences, key, languageId, value);
174            }
175    
176            public static void setPreferencesValues(
177                            PortletPreferences preferences, String key, String languageId,
178                            String[] values)
179                    throws Exception {
180    
181                    getLocalization().setPreferencesValues(
182                            preferences, key, languageId, values);
183            }
184    
185            public static String updateLocalization(
186                    String xml, String key, String value) {
187    
188                    return getLocalization().updateLocalization(xml, key, value);
189            }
190    
191            public static String updateLocalization(
192                    String xml, String key, String value, String requestedLanguageId) {
193    
194                    return getLocalization().updateLocalization(
195                            xml, key, value, requestedLanguageId);
196            }
197    
198            public static String updateLocalization(
199                    String xml, String key, String value, String requestedLanguageId,
200                    String defaultLanguageId) {
201    
202                    return getLocalization().updateLocalization(
203                            xml, key, value, requestedLanguageId, defaultLanguageId);
204            }
205    
206            public static String updateLocalization(
207                    String xml, String key, String value, String requestedLanguageId,
208                    String defaultLanguageId, boolean cdata) {
209    
210                    return getLocalization().updateLocalization(
211                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata);
212            }
213    
214            public static String updateLocalization(
215                    String xml, String key, String value, String requestedLanguageId,
216                    String defaultLanguageId, boolean cdata, boolean localized) {
217    
218                    return getLocalization().updateLocalization(
219                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata,
220                            localized);
221            }
222    
223            public void setLocalization(Localization localization) {
224                    _localization = localization;
225            }
226    
227            private static Localization _localization;
228    
229    }