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