001    /**
002     * Copyright (c) 2000-2010 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                    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 void setLocalizedPreferencesValues (
151                            PortletRequest portletRequest, PortletPreferences preferences,
152                            String parameter)
153                    throws Exception {
154    
155                    getLocalization().setLocalizedPreferencesValues(
156                            portletRequest, preferences, parameter);
157            }
158    
159            public static void setPreferencesValue(
160                            PortletPreferences preferences, String key, String languageId,
161                            String value)
162                    throws Exception {
163    
164                    getLocalization().setPreferencesValue(
165                            preferences, key, languageId, value);
166            }
167    
168            public static void setPreferencesValues(
169                            PortletPreferences preferences, String key, String languageId,
170                            String[] values)
171                    throws Exception {
172    
173                    getLocalization().setPreferencesValues(
174                            preferences, key, languageId, values);
175            }
176    
177            public static String updateLocalization(
178                    String xml, String key, String value) {
179    
180                    return getLocalization().updateLocalization(xml, key, value);
181            }
182    
183            public static String updateLocalization(
184                    String xml, String key, String value, String requestedLanguageId) {
185    
186                    return getLocalization().updateLocalization(
187                            xml, key, value, requestedLanguageId);
188            }
189    
190            public static String updateLocalization(
191                    String xml, String key, String value, String requestedLanguageId,
192                    String defaultLanguageId) {
193    
194                    return getLocalization().updateLocalization(
195                            xml, key, value, requestedLanguageId, defaultLanguageId);
196            }
197    
198            public static String updateLocalization(
199                    String xml, String key, String value, String requestedLanguageId,
200                    String defaultLanguageId, boolean cdata) {
201    
202                    return getLocalization().updateLocalization(
203                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata);
204            }
205    
206            public void setLocalization(Localization localization) {
207                    _localization = localization;
208            }
209    
210            private static Localization _localization;
211    
212    }