001    /**
002     * Copyright (c) 2000-2013 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    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
020    import com.liferay.portal.kernel.xml.Document;
021    
022    import java.util.ArrayList;
023    import java.util.Collections;
024    import java.util.List;
025    import java.util.Locale;
026    import java.util.Map;
027    
028    import javax.portlet.PortletPreferences;
029    import javax.portlet.PortletRequest;
030    
031    import javax.servlet.http.HttpServletRequest;
032    
033    /**
034     * @author Alexander Chow
035     * @author Jorge Ferrer
036     * @author Mauro Mariuzzo
037     * @author Julio Camarero
038     * @author Brian Wing Shun Chan
039     * @see    Localization
040     */
041    public class LocalizationUtil {
042    
043            public static Object deserialize(JSONObject jsonObject) {
044                    return getLocalization().deserialize(jsonObject);
045            }
046    
047            public static String[] getAvailableLanguageIds(Document document) {
048                    return getLocalization().getAvailableLanguageIds(document);
049            }
050    
051            public static String[] getAvailableLanguageIds(String xml) {
052                    return getLocalization().getAvailableLanguageIds(xml);
053            }
054    
055            /**
056             * @deprecated As of 6.2.0, replaced by {@link
057             *             #getAvailableLanguageIds(String)}
058             */
059            public static String[] getAvailableLocales(String xml) {
060                    return getAvailableLanguageIds(xml);
061            }
062    
063            public static Locale getDefaultImportLocale(
064                    String className, long classPK, Locale contentDefaultLocale,
065                    Locale[] contentAvailableLocales) {
066    
067                    return getLocalization().getDefaultImportLocale(
068                            className, classPK, contentDefaultLocale, contentAvailableLocales);
069            }
070    
071            public static String getDefaultLanguageId(Document document) {
072                    return getLocalization().getDefaultLanguageId(document);
073            }
074    
075            public static String getDefaultLanguageId(
076                    Document document, Locale defaultLocale) {
077    
078                    return getLocalization().getDefaultLanguageId(document, defaultLocale);
079            }
080    
081            public static String getDefaultLanguageId(String xml) {
082                    return getLocalization().getDefaultLanguageId(xml);
083            }
084    
085            public static String getDefaultLanguageId(
086                    String xml, Locale defaultLocale) {
087    
088                    return getLocalization().getDefaultLanguageId(xml, defaultLocale);
089            }
090    
091            /**
092             * @deprecated As of 6.2.0, replaced by {@link
093             *             #getDefaultLanguageId(String)}
094             */
095            public static String getDefaultLocale(String xml) {
096                    return getDefaultLanguageId(xml);
097            }
098    
099            public static Localization getLocalization() {
100                    PortalRuntimePermission.checkGetBeanProperty(LocalizationUtil.class);
101    
102                    return _localization;
103            }
104    
105            public static String getLocalization(
106                    String xml, String requestedLanguageId) {
107    
108                    return getLocalization().getLocalization(xml, requestedLanguageId);
109            }
110    
111            public static String getLocalization(
112                    String xml, String requestedLanguageId, boolean useDefault) {
113    
114                    return getLocalization().getLocalization(
115                            xml, requestedLanguageId, useDefault);
116            }
117    
118            public static Map<Locale, String> getLocalizationMap(
119                    HttpServletRequest request, String parameter) {
120    
121                    return getLocalization().getLocalizationMap(request, parameter);
122            }
123    
124            public static Map<Locale, String> getLocalizationMap(
125                    PortletPreferences preferences, String parameter) {
126    
127                    return getLocalization().getLocalizationMap(preferences, parameter);
128            }
129    
130            public static Map<Locale, String> getLocalizationMap(
131                    PortletRequest portletRequest, String parameter) {
132    
133                    return getLocalization().getLocalizationMap(portletRequest, parameter);
134            }
135    
136            public static Map<Locale, String> getLocalizationMap(String xml) {
137                    return getLocalization().getLocalizationMap(xml);
138            }
139    
140            public static Map<Locale, String> getLocalizationMap(
141                    String xml, boolean useDefault) {
142    
143                    return getLocalization().getLocalizationMap(xml, useDefault);
144            }
145    
146            public static Map<Locale, String> getLocalizationMap(
147                    String bundleName, ClassLoader classLoader, String key,
148                    boolean includeBetaLocales) {
149    
150                    return getLocalization().getLocalizationMap(
151                            bundleName, classLoader, key, includeBetaLocales);
152            }
153    
154            public static Map<Locale, String> getLocalizationMap(
155                    String[] languageIds, String[] values) {
156    
157                    return getLocalization().getLocalizationMap(languageIds, values);
158            }
159    
160            public static String getLocalizationXmlFromPreferences(
161                    PortletPreferences preferences, PortletRequest portletRequest,
162                    String parameter) {
163    
164                    return getLocalization().getLocalizationXmlFromPreferences(
165                            preferences, portletRequest, parameter);
166            }
167    
168            public static String getLocalizationXmlFromPreferences(
169                    PortletPreferences preferences, PortletRequest portletRequest,
170                    String parameter, String defaultValue) {
171    
172                    return getLocalization().getLocalizationXmlFromPreferences(
173                            preferences, portletRequest, parameter, defaultValue);
174            }
175    
176            /**
177             * @deprecated As of 6.2.0, replaced by {@link #getLocalizationMap}
178             */
179            public static Map<Locale, String> getLocalizedParameter(
180                    PortletRequest portletRequest, String parameter) {
181    
182                    return getLocalization().getLocalizedParameter(
183                            portletRequest, parameter);
184            }
185    
186            public static List<Locale> getModifiedLocales(
187                    Map<Locale, String> oldMap, Map<Locale, String> newMap) {
188    
189                    if ((newMap == null) || newMap.isEmpty()) {
190                            return Collections.emptyList();
191                    }
192    
193                    List<Locale> modifiedLocales = new ArrayList<Locale>();
194    
195                    Locale[] locales = LanguageUtil.getAvailableLocales();
196    
197                    for (Locale locale : locales) {
198                            String oldValue = oldMap.get(locale);
199                            String newValue = newMap.get(locale);
200    
201                            if (!oldValue.equals(newValue)) {
202                                    modifiedLocales.add(locale);
203                            }
204                    }
205    
206                    return modifiedLocales;
207            }
208    
209            public static String getPreferencesKey(String key, String languageId) {
210                    return getLocalization().getPreferencesKey(key, languageId);
211            }
212    
213            public static String getPreferencesValue(
214                    PortletPreferences preferences, String key, String languageId) {
215    
216                    return getLocalization().getPreferencesValue(
217                            preferences, key, languageId);
218            }
219    
220            public static String getPreferencesValue(
221                    PortletPreferences preferences, String key, String languageId,
222                    boolean useDefault) {
223    
224                    return getLocalization().getPreferencesValue(
225                            preferences, key, languageId, useDefault);
226            }
227    
228            public static String[] getPreferencesValues(
229                    PortletPreferences preferences, String key, String languageId) {
230    
231                    return getLocalization().getPreferencesValues(
232                            preferences, key, languageId);
233            }
234    
235            public static String[] getPreferencesValues(
236                    PortletPreferences preferences, String key, String languageId,
237                    boolean useDefault) {
238    
239                    return getLocalization().getPreferencesValues(
240                            preferences, key, languageId, useDefault);
241            }
242    
243            public static String removeLocalization(
244                    String xml, String key, String requestedLanguageId) {
245    
246                    return getLocalization().removeLocalization(
247                            xml, key, requestedLanguageId);
248            }
249    
250            public static String removeLocalization(
251                    String xml, String key, String requestedLanguageId, boolean cdata) {
252    
253                    return getLocalization().removeLocalization(
254                            xml, key, requestedLanguageId, cdata);
255            }
256    
257            public static String removeLocalization(
258                    String xml, String key, String requestedLanguageId, boolean cdata,
259                    boolean localized) {
260    
261                    return getLocalization().removeLocalization(
262                            xml, key, requestedLanguageId, cdata, localized);
263            }
264    
265            public static void setLocalizedPreferencesValues(
266                            PortletRequest portletRequest, PortletPreferences preferences,
267                            String parameter)
268                    throws Exception {
269    
270                    getLocalization().setLocalizedPreferencesValues(
271                            portletRequest, preferences, parameter);
272            }
273    
274            public static void setPreferencesValue(
275                            PortletPreferences preferences, String key, String languageId,
276                            String value)
277                    throws Exception {
278    
279                    getLocalization().setPreferencesValue(
280                            preferences, key, languageId, value);
281            }
282    
283            public static void setPreferencesValues(
284                            PortletPreferences preferences, String key, String languageId,
285                            String[] values)
286                    throws Exception {
287    
288                    getLocalization().setPreferencesValues(
289                            preferences, key, languageId, values);
290            }
291    
292            public static String updateLocalization(
293                    Map<Locale, String> localizationMap, String xml, String key,
294                    String defaultLanguageId) {
295    
296                    return getLocalization().updateLocalization(
297                            localizationMap, xml, key, defaultLanguageId);
298            }
299    
300            public static String updateLocalization(
301                    String xml, String key, String value) {
302    
303                    return getLocalization().updateLocalization(xml, key, value);
304            }
305    
306            public static String updateLocalization(
307                    String xml, String key, String value, String requestedLanguageId) {
308    
309                    return getLocalization().updateLocalization(
310                            xml, key, value, requestedLanguageId);
311            }
312    
313            public static String updateLocalization(
314                    String xml, String key, String value, String requestedLanguageId,
315                    String defaultLanguageId) {
316    
317                    return getLocalization().updateLocalization(
318                            xml, key, value, requestedLanguageId, defaultLanguageId);
319            }
320    
321            public static String updateLocalization(
322                    String xml, String key, String value, String requestedLanguageId,
323                    String defaultLanguageId, boolean cdata) {
324    
325                    return getLocalization().updateLocalization(
326                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata);
327            }
328    
329            public static String updateLocalization(
330                    String xml, String key, String value, String requestedLanguageId,
331                    String defaultLanguageId, boolean cdata, boolean localized) {
332    
333                    return getLocalization().updateLocalization(
334                            xml, key, value, requestedLanguageId, defaultLanguageId, cdata,
335                            localized);
336            }
337    
338            public void setLocalization(Localization localization) {
339                    PortalRuntimePermission.checkSetBeanProperty(getClass());
340    
341                    _localization = localization;
342            }
343    
344            private static Localization _localization;
345    
346    }