1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.LocaleUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.xml.Document;
23  import com.liferay.portal.kernel.xml.Element;
24  import com.liferay.portal.kernel.xml.SAXReaderUtil;
25  
26  import java.util.List;
27  
28  /**
29   * <a href="LocaleTransformerListener.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Raymond Augé
32   */
33  public class LocaleTransformerListener extends TransformerListener {
34  
35      public String onXml(String s) {
36          if (_log.isDebugEnabled()) {
37              _log.debug("onXml");
38          }
39  
40          s = replace(s);
41  
42          return s;
43      }
44  
45      public String onScript(String s) {
46          if (_log.isDebugEnabled()) {
47              _log.debug("onScript");
48          }
49  
50          s = StringUtil.replace(s, "@language_id@", _requestedLocale);
51  
52          return s;
53      }
54  
55      public String onOutput(String s) {
56          if (_log.isDebugEnabled()) {
57              _log.debug("onOutput");
58          }
59  
60          return s;
61      }
62  
63      protected String replace(String xml) {
64          if (xml == null) {
65              return xml;
66          }
67  
68          _requestedLocale = getLanguageId();
69  
70          try {
71              Document doc = SAXReaderUtil.read(xml);
72  
73              Element root = doc.getRootElement();
74  
75              String defaultLanguageId = LocaleUtil.toLanguageId(
76                  LocaleUtil.getDefault());
77  
78              String[] availableLocales = StringUtil.split(
79                  root.attributeValue("available-locales", defaultLanguageId));
80  
81              String defaultLocale = root.attributeValue(
82                  "default-locale", defaultLanguageId);
83  
84              boolean isSupportedLocale = false;
85  
86              for (int i = 0; i < availableLocales.length; i++) {
87                  if (availableLocales[i].equalsIgnoreCase(getLanguageId())) {
88                      isSupportedLocale = true;
89  
90                      break;
91                  }
92              }
93  
94              if (!isSupportedLocale) {
95                  setLanguageId(defaultLocale);
96              }
97  
98              replace(root);
99  
100             xml = JournalUtil.formatXML(doc);
101         }
102         catch (Exception e) {
103             _log.error(e);
104         }
105 
106         return xml;
107     }
108 
109     protected void replace(Element root) {
110         List<Element> children = root.elements();
111 
112         int listIndex = children.size() - 1;
113 
114         while (listIndex >= 0) {
115             Element child = children.get(listIndex);
116 
117             String languageId = child.attributeValue(
118                 "language-id", getLanguageId());
119 
120             if (!languageId.equalsIgnoreCase(getLanguageId())) {
121                 root.remove(child);
122             }
123             else{
124                 replace(child);
125             }
126 
127             listIndex--;
128         }
129     }
130 
131     private static Log _log = LogFactoryUtil.getLog(
132         LocaleTransformerListener.class);
133 
134     private String _requestedLocale = StringPool.BLANK;
135 
136 }