1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.LocaleUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.xml.Document;
31  import com.liferay.portal.kernel.xml.Element;
32  import com.liferay.portal.kernel.xml.SAXReaderUtil;
33  
34  import java.util.List;
35  
36  /**
37   * <a href="LocaleTransformerListener.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Raymond Augé
40   *
41   */
42  public class LocaleTransformerListener extends TransformerListener {
43  
44      public String onXml(String s) {
45          if (_log.isDebugEnabled()) {
46              _log.debug("onXml");
47          }
48  
49          s = replace(s);
50  
51          return s;
52      }
53  
54      public String onScript(String s) {
55          if (_log.isDebugEnabled()) {
56              _log.debug("onScript");
57          }
58  
59          s = StringUtil.replace(s, "@language_id@", _requestedLocale);
60  
61          return s;
62      }
63  
64      public String onOutput(String s) {
65          if (_log.isDebugEnabled()) {
66              _log.debug("onOutput");
67          }
68  
69          return s;
70      }
71  
72      protected String replace(String xml) {
73          if (xml == null) {
74              return xml;
75          }
76  
77          _requestedLocale = getLanguageId();
78  
79          try {
80              Document doc = SAXReaderUtil.read(xml);
81  
82              Element root = doc.getRootElement();
83  
84              String defaultLanguageId = LocaleUtil.toLanguageId(
85                  LocaleUtil.getDefault());
86  
87              String[] availableLocales = StringUtil.split(
88                  root.attributeValue("available-locales", defaultLanguageId));
89  
90              String defaultLocale = root.attributeValue(
91                  "default-locale", defaultLanguageId);
92  
93              boolean isSupportedLocale = false;
94  
95              for (int i = 0; i < availableLocales.length; i++) {
96                  if (availableLocales[i].equalsIgnoreCase(getLanguageId())) {
97                      isSupportedLocale = true;
98  
99                      break;
100                 }
101             }
102 
103             if (!isSupportedLocale) {
104                 setLanguageId(defaultLocale);
105             }
106 
107             replace(root);
108 
109             xml = JournalUtil.formatXML(doc);
110         }
111         catch (Exception e) {
112             _log.error(e);
113         }
114 
115         return xml;
116     }
117 
118     protected void replace(Element root) {
119         List<Element> children = root.elements();
120 
121         int listIndex = children.size() - 1;
122 
123         while (listIndex >= 0) {
124             Element child = children.get(listIndex);
125 
126             String languageId = child.attributeValue(
127                 "language-id", getLanguageId());
128 
129             if (!languageId.equalsIgnoreCase(getLanguageId())) {
130                 root.remove(child);
131             }
132             else{
133                 replace(child);
134             }
135 
136             listIndex--;
137         }
138     }
139 
140     private static Log _log =
141         LogFactoryUtil.getLog(LocaleTransformerListener.class);
142 
143     private String _requestedLocale = StringPool.BLANK;
144 
145 }