001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.log.Log;
024: import com.liferay.portal.kernel.log.LogFactoryUtil;
025: import com.liferay.portal.kernel.util.LocaleUtil;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.util.xml.XMLFormatter;
030:
031: import java.io.StringReader;
032:
033: import java.util.Iterator;
034:
035: import javax.portlet.PortletPreferences;
036:
037: import org.dom4j.Document;
038: import org.dom4j.Element;
039: import org.dom4j.io.SAXReader;
040:
041: /**
042: * <a href="LocalizationUtil.java.html"><b><i>View Source</i></b></a>
043: *
044: * @author Alexander Chow
045: *
046: */
047: public class LocalizationUtil {
048:
049: public static String[] getAvailableLocales(String xml) {
050: String[] availableLocales = new String[0];
051:
052: try {
053: SAXReader reader = new SAXReader();
054:
055: Document doc = reader.read(new StringReader(xml));
056:
057: Element root = doc.getRootElement();
058:
059: availableLocales = StringUtil.split(root
060: .attributeValue("available-locales"));
061: } catch (Exception e) {
062: if (_log.isWarnEnabled()) {
063: _log.warn(e);
064: }
065: }
066:
067: return availableLocales;
068: }
069:
070: public static String getDefaultLocale(String xml) {
071: String defaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil
072: .getDefault());
073:
074: try {
075: SAXReader reader = new SAXReader();
076:
077: Document doc = reader.read(new StringReader(xml));
078:
079: Element root = doc.getRootElement();
080:
081: return root.attributeValue("default-locale",
082: defaultLanguageId);
083: } catch (Exception e) {
084: if (_log.isWarnEnabled()) {
085: _log.warn(e);
086: }
087: }
088:
089: return defaultLanguageId;
090: }
091:
092: public static String getLocalization(String xml,
093: String requestedLanguageId) {
094:
095: return getLocalization(xml, requestedLanguageId, true);
096: }
097:
098: public static String getLocalization(String xml,
099: String requestedLanguageId, boolean useDefault) {
100:
101: String defaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil
102: .getDefault());
103:
104: String value = StringPool.BLANK;
105:
106: String defaultValue = StringPool.BLANK;
107:
108: try {
109: SAXReader reader = new SAXReader();
110:
111: Document doc = reader.read(new StringReader(xml));
112:
113: Element root = doc.getRootElement();
114:
115: Iterator itr = root.elements().iterator();
116:
117: while (itr.hasNext()) {
118: Element el = (Element) itr.next();
119:
120: String languageId = el.attributeValue("language-id",
121: defaultLanguageId);
122:
123: if (languageId.equals(defaultLanguageId)) {
124: defaultValue = el.getText();
125: }
126:
127: if (languageId.equals(requestedLanguageId)) {
128: value = el.getText();
129: }
130: }
131:
132: if (useDefault && Validator.isNull(value)) {
133: value = defaultValue;
134: }
135: } catch (Exception e) {
136: if (_log.isWarnEnabled()) {
137: _log.warn(e);
138: }
139: }
140:
141: return value;
142: }
143:
144: public static String getPrefsValue(PortletPreferences prefs,
145: String key, String languageId) {
146:
147: return getPrefsValue(prefs, key, languageId, true);
148: }
149:
150: public static String getPrefsValue(PortletPreferences prefs,
151: String key, String languageId, boolean useDefault) {
152:
153: String localizedKey = _getPrefsKey(key, languageId);
154:
155: String value = prefs.getValue(localizedKey, StringPool.BLANK);
156:
157: if (useDefault && Validator.isNull(value)) {
158: value = prefs.getValue(key, StringPool.BLANK);
159: }
160:
161: return value;
162: }
163:
164: public static String[] getPrefsValues(PortletPreferences prefs,
165: String key, String languageId) {
166:
167: return getPrefsValues(prefs, key, languageId, true);
168: }
169:
170: public static String[] getPrefsValues(PortletPreferences prefs,
171: String key, String languageId, boolean useDefault) {
172:
173: String localizedKey = _getPrefsKey(key, languageId);
174:
175: String[] values = prefs.getValues(localizedKey, new String[0]);
176:
177: if (useDefault && Validator.isNull(values)) {
178: values = prefs.getValues(key, new String[0]);
179: }
180:
181: return values;
182: }
183:
184: public static String removeLocalization(String xml, String key,
185: String requestedLanguageId) {
186:
187: if (Validator.isNull(xml) || (xml.indexOf("<root") == -1)) {
188: xml = "<root />";
189: }
190:
191: String defaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil
192: .getDefault());
193:
194: try {
195: SAXReader reader = new SAXReader();
196:
197: Document doc = reader.read(new StringReader(xml));
198:
199: Element root = doc.getRootElement();
200:
201: String availableLocales = root
202: .attributeValue("available-locales");
203:
204: defaultLanguageId = root.attributeValue("default-locale",
205: defaultLanguageId);
206:
207: if (availableLocales.indexOf(requestedLanguageId) != -1) {
208: Iterator itr = root.elements().iterator();
209:
210: while (itr.hasNext()) {
211: Element el = (Element) itr.next();
212:
213: String languageId = el.attributeValue(
214: "language-id", defaultLanguageId);
215:
216: if (languageId.equals(requestedLanguageId)) {
217: root.remove(el);
218:
219: break;
220: }
221: }
222:
223: availableLocales = StringUtil.remove(availableLocales,
224: requestedLanguageId, StringPool.COMMA);
225:
226: root
227: .addAttribute("available-locales",
228: availableLocales);
229: }
230:
231: xml = XMLFormatter.toString(doc, " ");
232: } catch (Exception e) {
233: if (_log.isWarnEnabled()) {
234: _log.warn(e);
235: }
236: }
237:
238: return xml;
239: }
240:
241: public static void setPrefsValue(PortletPreferences prefs,
242: String key, String languageId, String value)
243: throws Exception {
244:
245: prefs.setValue(_getPrefsKey(key, languageId), value);
246: }
247:
248: public static void setPrefsValues(PortletPreferences prefs,
249: String key, String languageId, String[] values)
250: throws Exception {
251:
252: prefs.setValues(_getPrefsKey(key, languageId), values);
253: }
254:
255: public static String updateLocalization(String xml, String key,
256: String value) {
257:
258: String defaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil
259: .getDefault());
260:
261: return updateLocalization(xml, key, value, defaultLanguageId,
262: defaultLanguageId);
263: }
264:
265: public static String updateLocalization(String xml, String key,
266: String value, String requestedLanguageId) {
267:
268: String defaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil
269: .getDefault());
270:
271: return updateLocalization(xml, key, value, requestedLanguageId,
272: defaultLanguageId);
273: }
274:
275: public static String updateLocalization(String xml, String key,
276: String value, String requestedLanguageId,
277: String defaultLanguageId) {
278:
279: return updateLocalization(xml, key, value, requestedLanguageId,
280: defaultLanguageId, false);
281: }
282:
283: public static String updateLocalization(String xml, String key,
284: String value, String requestedLanguageId,
285: String defaultLanguageId, boolean cdata) {
286:
287: if (Validator.isNull(xml) || (xml.indexOf("<root") == -1)) {
288: xml = "<root />";
289: }
290:
291: String updatedXml = xml;
292:
293: try {
294: SAXReader reader = new SAXReader();
295:
296: Document doc = reader.read(new StringReader(xml));
297:
298: Element root = doc.getRootElement();
299:
300: String availableLocales = root
301: .attributeValue("available-locales");
302:
303: Element localeEl = null;
304:
305: Iterator itr = root.elements().iterator();
306:
307: while (itr.hasNext()) {
308: Element el = (Element) itr.next();
309:
310: String languageId = el.attributeValue("language-id",
311: defaultLanguageId);
312:
313: if (languageId.equals(requestedLanguageId)) {
314: localeEl = el;
315:
316: break;
317: }
318: }
319:
320: if (localeEl != null) {
321: localeEl.addAttribute("language-id",
322: requestedLanguageId);
323:
324: if (cdata) {
325: localeEl.clearContent();
326: localeEl.addCDATA(value);
327: } else {
328: localeEl.setText(value);
329: }
330: } else {
331: localeEl = root.addElement(key);
332:
333: localeEl.addAttribute("language-id",
334: requestedLanguageId);
335:
336: if (cdata) {
337: localeEl.addCDATA(value);
338: } else {
339: localeEl.setText(value);
340: }
341:
342: if (availableLocales == null) {
343: availableLocales = defaultLanguageId;
344: }
345:
346: if (!requestedLanguageId.equals(defaultLanguageId)) {
347: availableLocales += StringPool.COMMA
348: + requestedLanguageId;
349: }
350:
351: root
352: .addAttribute("available-locales",
353: availableLocales);
354: }
355:
356: root.addAttribute("default-locale", defaultLanguageId);
357:
358: updatedXml = XMLFormatter.toString(doc, " ");
359: } catch (Exception e) {
360: if (_log.isWarnEnabled()) {
361: _log.warn(e);
362: }
363: }
364:
365: return updatedXml;
366: }
367:
368: private static String _getPrefsKey(String key, String languageId) {
369: String defaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil
370: .getDefault());
371:
372: if (!languageId.equals(defaultLanguageId)) {
373: key += "_" + languageId;
374: }
375:
376: return key;
377: }
378:
379: private static Log _log = LogFactoryUtil
380: .getLog(LocalizationUtil.class);
381:
382: }
|