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.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(String s) {
043                    if (_log.isDebugEnabled()) {
044                            _log.debug("onOutput");
045                    }
046    
047                    return replace(s);
048            }
049    
050            @Override
051            public String onScript(String s) {
052                    if (_log.isDebugEnabled()) {
053                            _log.debug("onScript");
054                    }
055    
056                    return s;
057            }
058    
059            @Override
060            public String onXml(String s) {
061                    if (_log.isDebugEnabled()) {
062                            _log.debug("onXml");
063                    }
064    
065                    return s;
066            }
067    
068            /**
069             * Replace the standard tokens in a given string with their values.
070             *
071             * @return the processed string
072             */
073            protected String replace(String s) {
074                    Map<String, String> tokens = getTokens();
075    
076                    Set<Map.Entry<String, String>> tokensSet = tokens.entrySet();
077    
078                    if (tokensSet.size() == 0) {
079                            return s;
080                    }
081    
082                    List<String> escapedKeysList = new ArrayList<String>();
083                    List<String> escapedValuesList = new ArrayList<String>();
084    
085                    List<String> keysList = new ArrayList<String>();
086                    List<String> valuesList = new ArrayList<String>();
087    
088                    List<String> tempEscapedKeysList = new ArrayList<String>();
089                    List<String> tempEscapedValuesList = new ArrayList<String>();
090    
091                    for (Map.Entry<String, String> entry : tokensSet) {
092                            String key = entry.getKey();
093                            String value = GetterUtil.getString(entry.getValue());
094    
095                            if (Validator.isNotNull(key)) {
096                                    String escapedKey =
097                                            StringPool.AT + StringPool.AT + key + StringPool.AT +
098                                                    StringPool.AT;
099    
100                                    String actualKey = StringPool.AT + key + StringPool.AT;
101    
102                                    String tempEscapedKey =
103                                            TEMP_ESCAPED_AT_OPEN + key + TEMP_ESCAPED_AT_CLOSE;
104    
105                                    escapedKeysList.add(escapedKey);
106                                    escapedValuesList.add(tempEscapedKey);
107    
108                                    keysList.add(actualKey);
109                                    valuesList.add(value);
110    
111                                    tempEscapedKeysList.add(tempEscapedKey);
112                                    tempEscapedValuesList.add(actualKey);
113                            }
114                    }
115    
116                    s = StringUtil.replace(
117                            s, escapedKeysList.toArray(new String[escapedKeysList.size()]),
118                            escapedValuesList.toArray(new String[escapedValuesList.size()]));
119    
120                    s = StringUtil.replace(
121                            s, keysList.toArray(new String[keysList.size()]),
122                            valuesList.toArray(new String[valuesList.size()]));
123    
124                    s = StringUtil.replace(
125                            s,
126                            tempEscapedKeysList.toArray(new String[tempEscapedKeysList.size()]),
127                            tempEscapedValuesList.toArray(
128                                    new String[tempEscapedValuesList.size()]));
129    
130                    return s;
131            }
132    
133            private static Log _log = LogFactoryUtil.getLog(
134                    TokensTransformerListener.class);
135    
136    }