001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.util.ArrayList;
025    import java.util.List;
026    import java.util.Map;
027    import java.util.Set;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class TokensTransformerListener extends TransformerListener {
033    
034            public static final String TEMP_ESCAPED_AT_OPEN =
035                    "[$TEMP_ESCAPED_AT_OPEN$]";
036    
037            public static final String TEMP_ESCAPED_AT_CLOSE =
038                    "[$_TEMP_ESCAPED_AT_CLOSE$]";
039    
040            public String onXml(String s) {
041                    if (_log.isDebugEnabled()) {
042                            _log.debug("onXml");
043                    }
044    
045                    return s;
046            }
047    
048            public String onScript(String s) {
049                    if (_log.isDebugEnabled()) {
050                            _log.debug("onScript");
051                    }
052    
053                    return replace(s);
054            }
055    
056            public String onOutput(String s) {
057                    if (_log.isDebugEnabled()) {
058                            _log.debug("onOutput");
059                    }
060    
061                    return replace(s);
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) {
070                    Map<String, String> tokens = getTokens();
071    
072                    Set<Map.Entry<String, String>> tokensSet = tokens.entrySet();
073    
074                    if (tokensSet.size() == 0) {
075                            return s;
076                    }
077    
078                    List<String> escapedKeysList = new ArrayList<String>();
079                    List<String> escapedValuesList = new ArrayList<String>();
080    
081                    List<String> keysList = new ArrayList<String>();
082                    List<String> valuesList = new ArrayList<String>();
083    
084                    List<String> tempEscapedKeysList = new ArrayList<String>();
085                    List<String> tempEscapedValuesList = new ArrayList<String>();
086    
087                    for (Map.Entry<String, String> entry : tokensSet) {
088                            String key = entry.getKey();
089                            String value = GetterUtil.getString(entry.getValue());
090    
091                            if (Validator.isNotNull(key)) {
092                                    String escapedKey =
093                                            StringPool.AT + StringPool.AT + key + StringPool.AT +
094                                                    StringPool.AT;
095    
096                                    String actualKey = StringPool.AT + key + StringPool.AT;
097    
098                                    String tempEscapedKey =
099                                            TEMP_ESCAPED_AT_OPEN + key + TEMP_ESCAPED_AT_CLOSE;
100    
101                                    escapedKeysList.add(escapedKey);
102                                    escapedValuesList.add(tempEscapedKey);
103    
104                                    keysList.add(actualKey);
105                                    valuesList.add(value);
106    
107                                    tempEscapedKeysList.add(tempEscapedKey);
108                                    tempEscapedValuesList.add(actualKey);
109                            }
110                    }
111    
112                    s = StringUtil.replace(
113                            s,
114                            escapedKeysList.toArray(new String[escapedKeysList.size()]),
115                            escapedValuesList.toArray(new String[escapedValuesList.size()]));
116    
117                    s = StringUtil.replace(
118                            s,
119                            keysList.toArray(new String[keysList.size()]),
120                            valuesList.toArray(new String[valuesList.size()]));
121    
122                    s = StringUtil.replace(
123                            s,
124                            tempEscapedKeysList.toArray(new String[tempEscapedKeysList.size()]),
125                            tempEscapedValuesList.toArray(
126                                    new String[tempEscapedValuesList.size()]));
127    
128                    return s;
129            }
130    
131            private static Log _log = LogFactoryUtil.getLog(
132                    TokensTransformerListener.class);
133    
134    }