1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import com.liferay.portal.kernel.json.JSONObject;
18  
19  import java.util.Locale;
20  import java.util.Map;
21  
22  import javax.portlet.ActionRequest;
23  import javax.portlet.PortletPreferences;
24  import javax.portlet.PortletRequest;
25  
26  /**
27   * <a href="LocalizationUtil.java.html"><b><i>View Source</i></b></a>
28   *
29   * <p>
30   * This class is used to localize values stored in XML and is often used to add
31   * localization behavior to value objects.
32   * </p>
33   *
34   * <p>
35   * Caching of the localized values is done in this class rather than in the
36   * value object since value objects get flushed from cache fairly quickly.
37   * Though lookups performed on a key based on an XML file is slower than lookups
38   * done at the value object level in general, the value object will get flushed
39   * at a rate which works against the performance gain. The cache is a soft hash
40   * map which prevents memory leaks within the system while enabling the cache to
41   * live longer than in a weak hash map.
42   * </p>
43   *
44   * @author Alexander Chow
45   * @author Jorge Ferrer
46   * @author Mauro Mariuzzo
47   * @author Julio Camarero
48   * @author Brian Wing Shun Chan
49   */
50  public class LocalizationUtil {
51  
52      public static Object deserialize(JSONObject jsonObject) {
53          return getLocalization().deserialize(jsonObject);
54      }
55  
56      public static String[] getAvailableLocales(String xml) {
57          return getLocalization().getAvailableLocales(xml);
58      }
59  
60      public static String getDefaultLocale(String xml) {
61          return getLocalization().getDefaultLocale(xml);
62      }
63  
64      public static Localization getLocalization() {
65          return _localization;
66      }
67  
68      public static String getLocalization(
69          String xml, String requestedLanguageId) {
70  
71          return getLocalization().getLocalization(xml, requestedLanguageId);
72      }
73  
74      public static String getLocalization(
75          String xml, String requestedLanguageId, boolean useDefault) {
76  
77          return getLocalization().getLocalization(
78              xml, requestedLanguageId, useDefault);
79      }
80  
81      public static Map<Locale, String> getLocalizationMap(
82          PortletRequest portletRequest, String parameter) {
83  
84          return getLocalization().getLocalizationMap(portletRequest, parameter);
85      }
86  
87      public static Map<Locale, String> getLocalizationMap(String xml) {
88          return getLocalization().getLocalizationMap(xml);
89      }
90  
91      /**
92       * @deprecated Use <code>getLocalizationMap</code>.
93       */
94      public static Map<Locale, String> getLocalizedParameter(
95          PortletRequest portletRequest, String parameter) {
96  
97          return getLocalization().getLocalizedParameter(
98              portletRequest, parameter);
99      }
100 
101     public static String getPreferencesValue(
102         PortletPreferences preferences, String key, String languageId) {
103 
104         return getLocalization().getPreferencesValue(
105             preferences, key, languageId);
106     }
107 
108     public static String getPreferencesValue(
109         PortletPreferences preferences, String key, String languageId,
110         boolean useDefault) {
111 
112         return getLocalization().getPreferencesValue(
113             preferences, key, languageId, useDefault);
114     }
115 
116     public static String[] getPreferencesValues(
117         PortletPreferences preferences, String key, String languageId) {
118 
119         return getLocalization().getPreferencesValues(
120             preferences, key, languageId);
121     }
122 
123     public static String[] getPreferencesValues(
124         PortletPreferences preferences, String key, String languageId,
125         boolean useDefault) {
126 
127         return getLocalization().getPreferencesValues(
128             preferences, key, languageId, useDefault);
129     }
130 
131     public static String removeLocalization(
132         String xml, String key, String requestedLanguageId) {
133 
134         return getLocalization().removeLocalization(
135             xml, key, requestedLanguageId);
136     }
137 
138     public static String removeLocalization(
139         String xml, String key, String requestedLanguageId, boolean cdata) {
140 
141         return getLocalization().removeLocalization(
142             xml, key, requestedLanguageId, cdata);
143     }
144 
145     public static void setLocalizedPreferencesValues (
146             ActionRequest actionRequest, PortletPreferences preferences,
147             String parameter)
148         throws Exception {
149 
150         getLocalization().setLocalizedPreferencesValues(
151             actionRequest, preferences, parameter);
152     }
153 
154     public static void setPreferencesValue(
155             PortletPreferences preferences, String key, String languageId,
156             String value)
157         throws Exception {
158 
159         getLocalization().setPreferencesValue(
160             preferences, key, languageId, value);
161     }
162 
163     public static void setPreferencesValues(
164             PortletPreferences preferences, String key, String languageId,
165             String[] values)
166         throws Exception {
167 
168         getLocalization().setPreferencesValues(
169             preferences, key, languageId, values);
170     }
171 
172     public static String updateLocalization(
173         String xml, String key, String value) {
174 
175         return getLocalization().updateLocalization(xml, key, value);
176     }
177 
178     public static String updateLocalization(
179         String xml, String key, String value, String requestedLanguageId) {
180 
181         return getLocalization().updateLocalization(
182             xml, key, value, requestedLanguageId);
183     }
184 
185     public static String updateLocalization(
186         String xml, String key, String value, String requestedLanguageId,
187         String defaultLanguageId) {
188 
189         return getLocalization().updateLocalization(
190             xml, key, value, requestedLanguageId, defaultLanguageId);
191     }
192 
193     public static String updateLocalization(
194         String xml, String key, String value, String requestedLanguageId,
195         String defaultLanguageId, boolean cdata) {
196 
197         return getLocalization().updateLocalization(
198             xml, key, value, requestedLanguageId, defaultLanguageId, cdata);
199     }
200 
201     public void setLocalization(Localization localization) {
202         _localization = localization;
203     }
204 
205     private static Localization _localization;
206 
207 }