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.portlet.journal.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.templateparser.BaseTransformerListener;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.xml.Document;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
027    
028    import java.util.List;
029    
030    /**
031     * @author Raymond Aug??
032     */
033    public class LocaleTransformerListener extends BaseTransformerListener {
034    
035            @Override
036            public String onOutput(String s) {
037                    if (_log.isDebugEnabled()) {
038                            _log.debug("onOutput");
039                    }
040    
041                    return s;
042            }
043    
044            @Override
045            public String onScript(String s) {
046                    if (_log.isDebugEnabled()) {
047                            _log.debug("onScript");
048                    }
049    
050                    s = StringUtil.replace(s, "@language_id@", _requestedLocale);
051    
052                    return s;
053            }
054    
055            @Override
056            public String onXml(String s) {
057                    if (_log.isDebugEnabled()) {
058                            _log.debug("onXml");
059                    }
060    
061                    s = replace(s);
062    
063                    return s;
064            }
065    
066            protected void replace(Element root) {
067                    List<Element> elements = root.elements();
068    
069                    int listIndex = elements.size() - 1;
070    
071                    while (listIndex >= 0) {
072                            Element element = elements.get(listIndex);
073    
074                            String languageId = element.attributeValue(
075                                    "language-id", getLanguageId());
076    
077                            if (!languageId.equalsIgnoreCase(getLanguageId())) {
078                                    root.remove(element);
079                            }
080                            else {
081                                    replace(element);
082                            }
083    
084                            listIndex--;
085                    }
086            }
087    
088            protected String replace(String xml) {
089                    if (xml == null) {
090                            return xml;
091                    }
092    
093                    _requestedLocale = getLanguageId();
094    
095                    try {
096                            Document document = SAXReaderUtil.read(xml);
097    
098                            Element rootElement = document.getRootElement();
099    
100                            String defaultLanguageId = LocaleUtil.toLanguageId(
101                                    LocaleUtil.getDefault());
102    
103                            String[] availableLocales = StringUtil.split(
104                                    rootElement.attributeValue(
105                                            "available-locales", defaultLanguageId));
106    
107                            String defaultLocale = rootElement.attributeValue(
108                                    "default-locale", defaultLanguageId);
109    
110                            boolean supportedLocale = false;
111    
112                            for (String availableLocale : availableLocales) {
113                                    if (availableLocale.equalsIgnoreCase(getLanguageId())) {
114                                            supportedLocale = true;
115    
116                                            break;
117                                    }
118                            }
119    
120                            if (!supportedLocale) {
121                                    setLanguageId(defaultLocale);
122                            }
123    
124                            replace(rootElement);
125    
126                            xml = DDMXMLUtil.formatXML(document);
127                    }
128                    catch (Exception e) {
129                            _log.error(e);
130                    }
131    
132                    return xml;
133            }
134    
135            private static Log _log = LogFactoryUtil.getLog(
136                    LocaleTransformerListener.class);
137    
138            private String _requestedLocale = StringPool.BLANK;
139    
140    }