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                                                            templateParserClassName);
150                                    }
151                                    catch (Exception e) {
152                                            throw new TransformException(e);
153                                    }
154    
155                                    templateParser.setLanguageId(languageId);
156                                    templateParser.setScript(script);
157                                    templateParser.setThemeDisplay(themeDisplay);
158                                    templateParser.setTokens(tokens);
159                                    templateParser.setViewMode(viewMode);
160                                    templateParser.setXML(xml);
161    
162                                    output = templateParser.transform();
163                            }
164                    }
165    
166                    // Postprocess output
167    
168                    for (int i = 0; i < listenersList.size(); i++) {
169                            TransformerListener listener = listenersList.get(i);
170    
171                            // Modify output
172    
173                            if (_logOutputBeforeListener.isDebugEnabled()) {
174                                    _logOutputBeforeListener.debug(output);
175                            }
176    
177                            output = listener.onOutput(output);
178    
179                            if (_logOutputAfterListener.isDebugEnabled()) {
180                                    _logOutputAfterListener.debug(output);
181                            }
182                    }
183    
184                    if (_logTransfromAfter.isDebugEnabled()) {
185                            _logTransfromAfter.debug(output);
186                    }
187    
188                    return output;
189            }
190    
191            protected abstract String getTemplateParserClassName(String langType);
192    
193            protected abstract String[] getTransformerListenersClassNames();
194    
195            private static Log _log = LogFactoryUtil.getLog(BaseTransformer.class);
196    
197            private static Log _logOutputAfterListener = LogFactoryUtil.getLog(
198                    BaseTransformer.class.getName() + ".OutputAfterListener");
199            private static Log _logOutputBeforeListener = LogFactoryUtil.getLog(
200                    BaseTransformer.class.getName() + ".OutputBeforeListener");
201            private static Log _logScriptAfterListener = LogFactoryUtil.getLog(
202                    BaseTransformer.class.getName() + ".ScriptAfterListener");
203            private static Log _logScriptBeforeListener = LogFactoryUtil.getLog(
204                    BaseTransformer.class.getName() + ".ScriptBeforeListener");
205            private static Log _logTokens = LogFactoryUtil.getLog(
206                    BaseTransformer.class.getName() + ".Tokens");
207            private static Log _logTransformBefore = LogFactoryUtil.getLog(
208                    BaseTransformer.class.getName() + ".TransformBefore");
209            private static Log _logTransfromAfter = LogFactoryUtil.getLog(
210                    BaseTransformer.class.getName() + ".TransformAfter");
211            private static Log _logXmlAfterListener = LogFactoryUtil.getLog(
212                    BaseTransformer.class.getName() + ".XmlAfterListener");
213            private static Log _logXmlBeforeListener = LogFactoryUtil.getLog(
214                    BaseTransformer.class.getName() + ".XmlBeforeListener");
215    
216    }