001    /**
002     * Copyright (c) 2000-2012 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     * @author Juan Fernández
039     */
040    public abstract class BaseTransformer implements Transformer {
041    
042            public String transform(
043                            ThemeDisplay themeDisplay, Map<String, Object> contextObjects,
044                            String script, String langType)
045                    throws Exception {
046    
047                    if (Validator.isNull(langType)) {
048                            return null;
049                    }
050    
051                    String templateParserClassName = getTemplateParserClassName(langType);
052    
053                    if (Validator.isNull(templateParserClassName)) {
054                            return null;
055                    }
056    
057                    if (_log.isDebugEnabled()) {
058                            _log.debug("Template parser class name " + templateParserClassName);
059                    }
060    
061                    TemplateParser templateParser = null;
062    
063                    try {
064                            templateParser = (TemplateParser)InstanceFactory.newInstance(
065                                    PortalClassLoaderUtil.getClassLoader(),
066                                    templateParserClassName);
067                    }
068                    catch (Exception e) {
069                            throw new TransformException(e);
070                    }
071    
072                    templateParser.setContextObjects(contextObjects);
073                    templateParser.setScript(script);
074                    templateParser.setThemeDisplay(themeDisplay);
075    
076                    return templateParser.transform();
077            }
078    
079            public String transform(
080                            ThemeDisplay themeDisplay, Map<String, String> tokens,
081                            String viewMode, String languageId, String xml, String script,
082                            String langType)
083                    throws Exception {
084    
085                    // Setup listeners
086    
087                    if (_log.isDebugEnabled()) {
088                            _log.debug("Language " + languageId);
089                    }
090    
091                    if (Validator.isNull(viewMode)) {
092                            viewMode = Constants.VIEW;
093                    }
094    
095                    if (_logTokens.isDebugEnabled()) {
096                            String tokensString = PropertiesUtil.list(tokens);
097    
098                            _logTokens.debug(tokensString);
099                    }
100    
101                    if (_logTransformBefore.isDebugEnabled()) {
102                            _logTransformBefore.debug(xml);
103                    }
104    
105                    List<TransformerListener> listenersList =
106                            new ArrayList<TransformerListener>();
107    
108                    String[] listeners = getTransformerListenersClassNames();
109    
110                    for (int i = 0; i < listeners.length; i++) {
111                            TransformerListener listener = null;
112    
113                            try {
114                                    if (_log.isDebugEnabled()) {
115                                            _log.debug("Instantiate listener " + listeners[i]);
116                                    }
117    
118                                    boolean templateDriven = Validator.isNotNull(langType);
119    
120                                    ClassLoader classLoader =
121                                            PortalClassLoaderUtil.getClassLoader();
122    
123                                    listener = (TransformerListener)InstanceFactory.newInstance(
124                                            classLoader, listeners[i]);
125    
126                                    listener.setTemplateDriven(templateDriven);
127                                    listener.setLanguageId(languageId);
128                                    listener.setTokens(tokens);
129    
130                                    listenersList.add(listener);
131                            }
132                            catch (Exception e) {
133                                    _log.error(e, e);
134                            }
135    
136                            // Modify XML
137    
138                            if (_logXmlBeforeListener.isDebugEnabled()) {
139                                    _logXmlBeforeListener.debug(xml);
140                            }
141    
142                            if (listener != null) {
143                                    xml = listener.onXml(xml);
144    
145                                    if (_logXmlAfterListener.isDebugEnabled()) {
146                                            _logXmlAfterListener.debug(xml);
147                                    }
148                            }
149    
150                            // Modify script
151    
152                            if (_logScriptBeforeListener.isDebugEnabled()) {
153                                    _logScriptBeforeListener.debug(script);
154                            }
155    
156                            if (listener != null) {
157                                    script = listener.onScript(script);
158    
159                                    if (_logScriptAfterListener.isDebugEnabled()) {
160                                            _logScriptAfterListener.debug(script);
161                                    }
162                            }
163                    }
164    
165                    // Transform
166    
167                    String output = null;
168    
169                    if (Validator.isNull(langType)) {
170                            output = LocalizationUtil.getLocalization(xml, languageId);
171                    }
172                    else {
173                            String templateParserClassName = getTemplateParserClassName(
174                                    langType);
175    
176                            if (_log.isDebugEnabled()) {
177                                    _log.debug(
178                                            "Template parser class name " + templateParserClassName);
179                            }
180    
181                            if (Validator.isNotNull(templateParserClassName)) {
182                                    TemplateParser templateParser = null;
183    
184                                    try {
185                                            templateParser =
186                                                    (TemplateParser)InstanceFactory.newInstance(
187                                                            PortalClassLoaderUtil.getClassLoader(),
188                                                            templateParserClassName);
189                                    }
190                                    catch (Exception e) {
191                                            throw new TransformException(e);
192                                    }
193    
194                                    templateParser.setLanguageId(languageId);
195                                    templateParser.setScript(script);
196                                    templateParser.setThemeDisplay(themeDisplay);
197                                    templateParser.setTokens(tokens);
198                                    templateParser.setViewMode(viewMode);
199                                    templateParser.setXML(xml);
200    
201                                    output = templateParser.transform();
202                            }
203                    }
204    
205                    // Postprocess output
206    
207                    for (int i = 0; i < listenersList.size(); i++) {
208                            TransformerListener listener = listenersList.get(i);
209    
210                            // Modify output
211    
212                            if (_logOutputBeforeListener.isDebugEnabled()) {
213                                    _logOutputBeforeListener.debug(output);
214                            }
215    
216                            output = listener.onOutput(output);
217    
218                            if (_logOutputAfterListener.isDebugEnabled()) {
219                                    _logOutputAfterListener.debug(output);
220                            }
221                    }
222    
223                    if (_logTransfromAfter.isDebugEnabled()) {
224                            _logTransfromAfter.debug(output);
225                    }
226    
227                    return output;
228            }
229    
230            protected abstract String getTemplateParserClassName(String langType);
231    
232            protected abstract String[] getTransformerListenersClassNames();
233    
234            private static Log _log = LogFactoryUtil.getLog(BaseTransformer.class);
235    
236            private static Log _logOutputAfterListener = LogFactoryUtil.getLog(
237                    BaseTransformer.class.getName() + ".OutputAfterListener");
238            private static Log _logOutputBeforeListener = LogFactoryUtil.getLog(
239                    BaseTransformer.class.getName() + ".OutputBeforeListener");
240            private static Log _logScriptAfterListener = LogFactoryUtil.getLog(
241                    BaseTransformer.class.getName() + ".ScriptAfterListener");
242            private static Log _logScriptBeforeListener = LogFactoryUtil.getLog(
243                    BaseTransformer.class.getName() + ".ScriptBeforeListener");
244            private static Log _logTokens = LogFactoryUtil.getLog(
245                    BaseTransformer.class.getName() + ".Tokens");
246            private static Log _logTransformBefore = LogFactoryUtil.getLog(
247                    BaseTransformer.class.getName() + ".TransformBefore");
248            private static Log _logTransfromAfter = LogFactoryUtil.getLog(
249                    BaseTransformer.class.getName() + ".TransformAfter");
250            private static Log _logXmlAfterListener = LogFactoryUtil.getLog(
251                    BaseTransformer.class.getName() + ".XmlAfterListener");
252            private static Log _logXmlBeforeListener = LogFactoryUtil.getLog(
253                    BaseTransformer.class.getName() + ".XmlBeforeListener");
254    
255    }