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