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.portal.kernel.templateparser;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.InstanceFactory;
021    import com.liferay.portal.kernel.util.LocalizationUtil;
022    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
023    import com.liferay.portal.kernel.util.PropertiesUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.theme.ThemeDisplay;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    import java.util.Map;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     * @author Raymond Aug??
034     * @author Wesley Gong
035     * @author Angelo Jefferson
036     * @author Hugo Huijser
037     * @author Marcellus Tavares
038     */
039    public abstract class BaseTransformer implements Transformer {
040    
041            @Override
042            public String transform(
043                            ThemeDisplay themeDisplay, Map<String, String> tokens,
044                            String viewMode, String languageId, String xml, String script,
045                            String langType)
046                    throws Exception {
047    
048                    // Setup Listeners
049    
050                    if (_log.isDebugEnabled()) {
051                            _log.debug("Language " + languageId);
052                    }
053    
054                    if (Validator.isNull(viewMode)) {
055                            viewMode = Constants.VIEW;
056                    }
057    
058                    if (_logTokens.isDebugEnabled()) {
059                            String tokensString = PropertiesUtil.list(tokens);
060    
061                            _logTokens.debug(tokensString);
062                    }
063    
064                    if (_logTransformBefore.isDebugEnabled()) {
065                            _logTransformBefore.debug(xml);
066                    }
067    
068                    List<TransformerListener> listenersList =
069                            new ArrayList<TransformerListener>();
070    
071                    String[] listeners = getTransformerListenersClassNames();
072    
073                    for (int i = 0; i < listeners.length; i++) {
074                            TransformerListener listener = null;
075    
076                            try {
077                                    if (_log.isDebugEnabled()) {
078                                            _log.debug("Instantiate listener " + listeners[i]);
079                                    }
080    
081                                    boolean templateDriven = Validator.isNotNull(langType);
082    
083                                    ClassLoader classLoader =
084                                            PortalClassLoaderUtil.getClassLoader();
085    
086                                    listener = (TransformerListener)InstanceFactory.newInstance(
087                                            classLoader, listeners[i]);
088    
089                                    listener.setTemplateDriven(templateDriven);
090                                    listener.setLanguageId(languageId);
091                                    listener.setTokens(tokens);
092    
093                                    listenersList.add(listener);
094                            }
095                            catch (Exception e) {
096                                    _log.error(e, e);
097                            }
098    
099                            // Modify XML
100    
101                            if (_logXmlBeforeListener.isDebugEnabled()) {
102                                    _logXmlBeforeListener.debug(xml);
103                            }
104    
105                            if (listener != null) {
106                                    xml = listener.onXml(xml);
107    
108                                    if (_logXmlAfterListener.isDebugEnabled()) {
109                                            _logXmlAfterListener.debug(xml);
110                                    }
111                            }
112    
113                            // Modify script
114    
115                            if (_logScriptBeforeListener.isDebugEnabled()) {
116                                    _logScriptBeforeListener.debug(script);
117                            }
118    
119                            if (listener != null) {
120                                    script = listener.onScript(script);
121    
122                                    if (_logScriptAfterListener.isDebugEnabled()) {
123                                            _logScriptAfterListener.debug(script);
124                                    }
125                            }
126                    }
127    
128                    // Transform
129    
130                    String output = null;
131    
132                    if (Validator.isNull(langType)) {
133                            output = LocalizationUtil.getLocalization(xml, languageId);
134                    }
135                    else {
136                            String templateParserClassName = getTemplateParserClassName(
137                                    langType);
138    
139                            if (_log.isDebugEnabled()) {
140                                    _log.debug(
141                                            "Template parser class name " + templateParserClassName);
142                            }
143    
144                            if (Validator.isNotNull(templateParserClassName)) {
145                                    TemplateParser templateParser = null;
146    
147                                    try {
148                                            templateParser =
149                                                    (TemplateParser)InstanceFactory.newInstance(
150                                                            PortalClassLoaderUtil.getClassLoader(),
151                                                            templateParserClassName);
152                                    }
153                                    catch (Exception e) {
154                                            throw new TransformException(e);
155                                    }
156    
157                                    templateParser.setLanguageId(languageId);
158                                    templateParser.setScript(script);
159                                    templateParser.setThemeDisplay(themeDisplay);
160                                    templateParser.setTokens(tokens);
161                                    templateParser.setViewMode(viewMode);
162                                    templateParser.setXML(xml);
163    
164                                    output = templateParser.transform();
165                            }
166                    }
167    
168                    // Postprocess output
169    
170                    for (int i = 0; i < listenersList.size(); i++) {
171                            TransformerListener listener = listenersList.get(i);
172    
173                            // Modify output
174    
175                            if (_logOutputBeforeListener.isDebugEnabled()) {
176                                    _logOutputBeforeListener.debug(output);
177                            }
178    
179                            output = listener.onOutput(output);
180    
181                            if (_logOutputAfterListener.isDebugEnabled()) {
182                                    _logOutputAfterListener.debug(output);
183                            }
184                    }
185    
186                    if (_logTransfromAfter.isDebugEnabled()) {
187                            _logTransfromAfter.debug(output);
188                    }
189    
190                    return output;
191            }
192    
193            protected abstract String getTemplateParserClassName(String langType);
194    
195            protected abstract String[] getTransformerListenersClassNames();
196    
197            private static Log _log = LogFactoryUtil.getLog(BaseTransformer.class);
198    
199            private static Log _logOutputAfterListener = LogFactoryUtil.getLog(
200                    BaseTransformer.class.getName() + ".OutputAfterListener");
201            private static Log _logOutputBeforeListener = LogFactoryUtil.getLog(
202                    BaseTransformer.class.getName() + ".OutputBeforeListener");
203            private static Log _logScriptAfterListener = LogFactoryUtil.getLog(
204                    BaseTransformer.class.getName() + ".ScriptAfterListener");
205            private static Log _logScriptBeforeListener = LogFactoryUtil.getLog(
206                    BaseTransformer.class.getName() + ".ScriptBeforeListener");
207            private static Log _logTokens = LogFactoryUtil.getLog(
208                    BaseTransformer.class.getName() + ".Tokens");
209            private static Log _logTransformBefore = LogFactoryUtil.getLog(
210                    BaseTransformer.class.getName() + ".TransformBefore");
211            private static Log _logTransfromAfter = LogFactoryUtil.getLog(
212                    BaseTransformer.class.getName() + ".TransformAfter");
213            private static Log _logXmlAfterListener = LogFactoryUtil.getLog(
214                    BaseTransformer.class.getName() + ".XmlAfterListener");
215            private static Log _logXmlBeforeListener = LogFactoryUtil.getLog(
216                    BaseTransformer.class.getName() + ".XmlBeforeListener");
217    
218    }