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.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    
025    import java.util.ArrayList;
026    import java.util.List;
027    import java.util.Map;
028    import java.util.Set;
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, String xml, 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                    Set<Map.Entry<String, String>> tokensSet = tokens.entrySet();
071    
072                    if (tokensSet.size() == 0) {
073                            return s;
074                    }
075    
076                    List<String> escapedKeysList = new ArrayList<String>();
077                    List<String> escapedValuesList = new ArrayList<String>();
078    
079                    List<String> keysList = new ArrayList<String>();
080                    List<String> valuesList = new ArrayList<String>();
081    
082                    List<String> tempEscapedKeysList = new ArrayList<String>();
083                    List<String> tempEscapedValuesList = new ArrayList<String>();
084    
085                    for (Map.Entry<String, String> entry : tokensSet) {
086                            String key = entry.getKey();
087                            String value = GetterUtil.getString(entry.getValue());
088    
089                            if (Validator.isNotNull(key)) {
090                                    String escapedKey =
091                                            StringPool.AT + StringPool.AT + key + StringPool.AT +
092                                                    StringPool.AT;
093    
094                                    String actualKey = StringPool.AT + key + StringPool.AT;
095    
096                                    String tempEscapedKey =
097                                            TEMP_ESCAPED_AT_OPEN + key + TEMP_ESCAPED_AT_CLOSE;
098    
099                                    escapedKeysList.add(escapedKey);
100                                    escapedValuesList.add(tempEscapedKey);
101    
102                                    keysList.add(actualKey);
103                                    valuesList.add(value);
104    
105                                    tempEscapedKeysList.add(tempEscapedKey);
106                                    tempEscapedValuesList.add(actualKey);
107                            }
108                    }
109    
110                    s = StringUtil.replace(
111                            s, escapedKeysList.toArray(new String[escapedKeysList.size()]),
112                            escapedValuesList.toArray(new String[escapedValuesList.size()]));
113    
114                    s = StringUtil.replace(
115                            s, keysList.toArray(new String[keysList.size()]),
116                            valuesList.toArray(new String[valuesList.size()]));
117    
118                    s = StringUtil.replace(
119                            s,
120                            tempEscapedKeysList.toArray(new String[tempEscapedKeysList.size()]),
121                            tempEscapedValuesList.toArray(
122                                    new String[tempEscapedValuesList.size()]));
123    
124                    return s;
125            }
126    
127            private static Log _log = LogFactoryUtil.getLog(
128                    TokensTransformerListener.class);
129    
130    }