001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.StringPool;
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    
026    import java.util.List;
027    
028    /**
029     * @author Raymond Augé
030     */
031    public class LocaleTransformerListener extends TransformerListener {
032    
033            public String onXml(String s) {
034                    if (_log.isDebugEnabled()) {
035                            _log.debug("onXml");
036                    }
037    
038                    s = replace(s);
039    
040                    return s;
041            }
042    
043            public String onScript(String s) {
044                    if (_log.isDebugEnabled()) {
045                            _log.debug("onScript");
046                    }
047    
048                    s = StringUtil.replace(s, "@language_id@", _requestedLocale);
049    
050                    return s;
051            }
052    
053            public String onOutput(String s) {
054                    if (_log.isDebugEnabled()) {
055                            _log.debug("onOutput");
056                    }
057    
058                    return s;
059            }
060    
061            protected String replace(String xml) {
062                    if (xml == null) {
063                            return xml;
064                    }
065    
066                    _requestedLocale = getLanguageId();
067    
068                    try {
069                            Document doc = SAXReaderUtil.read(xml);
070    
071                            Element root = doc.getRootElement();
072    
073                            String defaultLanguageId = LocaleUtil.toLanguageId(
074                                    LocaleUtil.getDefault());
075    
076                            String[] availableLocales = StringUtil.split(
077                                    root.attributeValue("available-locales", defaultLanguageId));
078    
079                            String defaultLocale = root.attributeValue(
080                                    "default-locale", defaultLanguageId);
081    
082                            boolean isSupportedLocale = false;
083    
084                            for (int i = 0; i < availableLocales.length; i++) {
085                                    if (availableLocales[i].equalsIgnoreCase(getLanguageId())) {
086                                            isSupportedLocale = true;
087    
088                                            break;
089                                    }
090                            }
091    
092                            if (!isSupportedLocale) {
093                                    setLanguageId(defaultLocale);
094                            }
095    
096                            replace(root);
097    
098                            xml = JournalUtil.formatXML(doc);
099                    }
100                    catch (Exception e) {
101                            _log.error(e);
102                    }
103    
104                    return xml;
105            }
106    
107            protected void replace(Element root) {
108                    List<Element> children = root.elements();
109    
110                    int listIndex = children.size() - 1;
111    
112                    while (listIndex >= 0) {
113                            Element child = children.get(listIndex);
114    
115                            String languageId = child.attributeValue(
116                                    "language-id", getLanguageId());
117    
118                            if (!languageId.equalsIgnoreCase(getLanguageId())) {
119                                    root.remove(child);
120                            }
121                            else{
122                                    replace(child);
123                            }
124    
125                            listIndex--;
126                    }
127            }
128    
129            private static Log _log = LogFactoryUtil.getLog(
130                    LocaleTransformerListener.class);
131    
132            private String _requestedLocale = StringPool.BLANK;
133    
134    }