001    /**
002     * Copyright (c) 2000-2010 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.ActionRequest;
023    import javax.portlet.PortletPreferences;
024    import javax.portlet.PortletRequest;
025    
026    /**
027     * <p>
028     * This class is used to localize values stored in XML and is often used to add
029     * localization behavior to value objects.
030     * </p>
031     *
032     * <p>
033     * Caching of the localized values is done in this class rather than in the
034     * value object since value objects get flushed from cache fairly quickly.
035     * Though lookups performed on a key based on an XML file is slower than lookups
036     * done at the value object level in general, the value object will get flushed
037     * at a rate which works against the performance gain. The cache is a soft hash
038     * map which prevents memory leaks within the system while enabling the cache to
039     * live longer than in a weak hash map.
040     * </p>
041     *
042     * @author Alexander Chow
043     * @author Jorge Ferrer
044     * @author Mauro Mariuzzo
045     * @author Julio Camarero
046     * @author Brian Wing Shun Chan
047     */
048    public class LocalizationUtil {
049    
050            public static Object deserialize(JSONObject jsonObject) {
051                    return getLocalization().deserialize(jsonObject);
052            }
053    
054            public static String[] getAvailableLocales(String xml) {
055                    return getLocalization().getAvailableLocales(xml);
056            }
057    
058            public static String getDefaultLocale(String xml) {
059                    return getLocalization().getDefaultLocale(xml);
060            }
061    
062            public static Localization getLocalization() {
063                    return _localization;
064            }
065    
066            public static String getLocalization(
067                    String xml, String requestedLanguageId) {
068    
069                    return getLocalization().getLocalization(xml, requestedLanguageId);
070            }
071    
072            public static String getLocalization(
073                    String xml, String requestedLanguageId, boolean useDefault) {
074    
075                    return getLocalization().getLocalization(
076                            xml, requestedLanguageId, useDefault);
077            }
078    
079            public static Map<Locale, String> getLocalizationMap(
080                    PortletRequest portletRequest, String parameter) {
081    
082                    return getLocalization().getLocalizationMap(portletRequest, parameter);
083            }
084    
085            public static Map<Locale, String> getLocalizationMap(String xml) {
086                    return getLocalization().getLocalizationMap(xml);
087            }
088    
089            /**
090             * @deprecated Use <code>getLocalizationMap</code>.
091             */
092            public static Map<Locale, String> getLocalizedParameter(
093                    PortletRequest portletRequest, String parameter) {
094    
095                    return getLocalization().getLocalizedParameter(
096                            portletRequest, parameter);
097            }
098    
099            public static String getPreferencesValue(
100                    PortletPreferences preferences, String key, String languageId) {
101    
102                    return getLocalization().getPreferencesValue(
103                            preferences, key, languageId);
104            }
105    
106            public static String getPreferencesValue(
107                    PortletPreferences preferences, String key, String languageId,
108                    boolean useDefault) {
109    
110                    return getLocalization().getPreferencesValue(
111                            preferences, key, languageId, useDefault);
112            }
113    
114            public static String[] getPreferencesValues(
115                    PortletPreferences preferences, String key, String languageId) {
116    
117                    return getLocalization().getPreferencesValues(
118                            preferences, key, languageId);
119            }
120    
121            public static String[] getPreferencesValues(
122                    PortletPreferences preferences, String key, String languageId,
123                    boolean useDefault) {
124    
125                    return getLocalization().getPreferencesValues(
126                            preferences, key, languageId, useDefault);
127            }
128    
129            public static String removeLocalization(
130                    String xml, String key, String requestedLanguageId) {
131    
132                    return getLocalization().removeLocalization(
133                            xml, key, requestedLanguageId);
134            }
135    
136            public static String removeLocalization(
137                    String xml, String key, String requestedLanguageId, boolean cdata) {
138    
139                    return getLocalization().removeLocalization(
140                            xml, key, requestedLanguageId, cdata);
141            }
142    
143            public static void setLocalizedPreferencesValues (
144                            ActionRequest actionRequest, PortletPreferences preferences,
145                            String parameter)
146                    throws Exception {
147    
148                    getLocalization().setLocalizedPreferencesValues(
149                            actionRequest, preferences, parameter);
150            }
151    
152            public static void setPreferencesValue(
153                            PortletPreferences preferences, String key, String languageId,
154                            String value)
155                    throws Exception {
156    
157                    getLocalization().setPreferencesValue(
158                            preferences, key, languageId, value);
159            }
160    
161            public static void setPreferencesValues(
162                            PortletPreferences preferences, String key, String languageId,
163                            String[] values)
164                    throws Exception {
165    
166                    getLocalization().setPreferencesValues(
167                            preferences, key, languageId, values);
168            }
169    
170            public static String updateLocalization(
171                    String xml, String key, String value) {
172    
173                    return getLocalization().updateLocalization(xml, key, value);
174            }
175    
176            public static String updateLocalization(
177                    String xml, String key, String value, String requestedLanguageId) {
178    
179                    return getLocalization().updateLocalization(
180                            xml, key, value, requestedLanguageId);
181            }
182    
183            public static String updateLocalization(
184                    String xml, String key, String value, String requestedLanguageId,
185                    String defaultLanguageId) {
186    
187                    return getLocalization().updateLocalization(
188                            xml, key, value, requestedLanguageId, defaultLanguageId);
189            }
190    
191            public static String updateLocalization(
192                    String xml, String key, String value, String requestedLanguageId,
193                    String defaultLanguageId, boolean cdata) {
194    
195                    return getLocalization().updateLocalization(
196                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata);
197            }
198    
199            public void setLocalization(Localization localization) {
200                    _localization = localization;
201            }
202    
203            private static Localization _localization;
204    
205    }