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