001    /**
002     * Copyright (c) 2000-present 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.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.templateparser.BaseTransformerListener;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.xml.Document;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    import java.util.Map;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class TokensTransformerListener extends BaseTransformerListener {
034    
035            public static final String TEMP_ESCAPED_AT_CLOSE =
036                    "[$_TEMP_ESCAPED_AT_CLOSE$]";
037    
038            public static final String TEMP_ESCAPED_AT_OPEN =
039                    "[$TEMP_ESCAPED_AT_OPEN$]";
040    
041            @Override
042            public String onOutput(
043                    String output, String languageId, Map<String, String> tokens) {
044    
045                    if (_log.isDebugEnabled()) {
046                            _log.debug("onOutput");
047                    }
048    
049                    return replace(output, tokens);
050            }
051    
052            @Override
053            public String onScript(
054                    String script, Document document, String languageId,
055                    Map<String, String> tokens) {
056    
057                    if (_log.isDebugEnabled()) {
058                            _log.debug("onScript");
059                    }
060    
061                    return replace(script, tokens);
062            }
063    
064            /**
065             * Replace the standard tokens in a given string with their values.
066             *
067             * @return the processed string
068             */
069            protected String replace(String s, Map<String, String> tokens) {
070                    if (tokens.isEmpty()) {
071                            return s;
072                    }
073    
074                    List<String> escapedKeysList = null;
075                    List<String> escapedValuesList = null;
076    
077                    List<String> keysList = null;
078                    List<String> valuesList = null;
079    
080                    List<String> tempEscapedKeysList = null;
081                    List<String> tempEscapedValuesList = null;
082    
083                    boolean hasKey = false;
084    
085                    for (Map.Entry<String, String> entry : tokens.entrySet()) {
086                            String key = entry.getKey();
087    
088                            if (Validator.isNotNull(key) && s.contains(key)) {
089                                    if (!hasKey) {
090                                            escapedKeysList = new ArrayList<String>();
091                                            escapedValuesList = new ArrayList<String>();
092                                            keysList = new ArrayList<String>();
093                                            valuesList = new ArrayList<String>();
094                                            tempEscapedKeysList = new ArrayList<String>();
095                                            tempEscapedValuesList = new ArrayList<String>();
096    
097                                            hasKey = true;
098                                    }
099    
100                                    String actualKey = StringPool.AT.concat(
101                                            key).concat(StringPool.AT);
102    
103                                    String escapedKey = StringPool.AT.concat(
104                                            actualKey).concat(StringPool.AT);
105    
106                                    String tempEscapedKey = TEMP_ESCAPED_AT_OPEN.concat(key).concat(
107                                            TEMP_ESCAPED_AT_CLOSE);
108    
109                                    escapedKeysList.add(escapedKey);
110                                    escapedValuesList.add(tempEscapedKey);
111    
112                                    keysList.add(actualKey);
113                                    valuesList.add(GetterUtil.getString(entry.getValue()));
114    
115                                    tempEscapedKeysList.add(tempEscapedKey);
116                                    tempEscapedValuesList.add(actualKey);
117                            }
118                    }
119    
120                    if (!hasKey) {
121                            return s;
122                    }
123    
124                    s = StringUtil.replace(
125                            s, escapedKeysList.toArray(new String[escapedKeysList.size()]),
126                            escapedValuesList.toArray(new String[escapedValuesList.size()]));
127    
128                    s = StringUtil.replace(
129                            s, keysList.toArray(new String[keysList.size()]),
130                            valuesList.toArray(new String[valuesList.size()]));
131    
132                    s = StringUtil.replace(
133                            s,
134                            tempEscapedKeysList.toArray(new String[tempEscapedKeysList.size()]),
135                            tempEscapedValuesList.toArray(
136                                    new String[tempEscapedValuesList.size()]));
137    
138                    return s;
139            }
140    
141            private static final Log _log = LogFactoryUtil.getLog(
142                    TokensTransformerListener.class);
143    
144    }