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.MapUtil;
022    import com.liferay.portal.kernel.util.PropertiesUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    
026    import java.util.HashMap;
027    import java.util.Map;
028    import java.util.Properties;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class PropertiesTransformerListener extends BaseTransformerListener {
034    
035            @Override
036            public String onOutput(
037                    String output, String languageId, Map<String, String> tokens) {
038    
039                    if (_log.isDebugEnabled()) {
040                            _log.debug("onOutput");
041                    }
042    
043                    return replace(output, languageId, tokens);
044            }
045    
046            @Override
047            public String onScript(
048                    String script, String xml, String languageId,
049                    Map<String, String> tokens) {
050    
051                    if (_log.isDebugEnabled()) {
052                            _log.debug("onScript");
053                    }
054    
055                    return replace(script, languageId, tokens);
056            }
057    
058            /**
059             * Replace the properties in a given string with their values fetched from
060             * the template GLOBAL-PROPERTIES.
061             *
062             * @return the processed string
063             */
064            protected String replace(
065                    String s, String languageId, Map<String, String> tokens) {
066    
067                    String templateId = tokens.get("template_id");
068    
069                    if ((templateId == null) ||
070                            ((templateId != null) && templateId.equals(_GLOBAL_PROPERTIES))) {
071    
072                            // Return the original string if no template ID is specified or if
073                            // the template ID is GLOBAL-PROPERTIES to prevent an infinite loop.
074    
075                            return s;
076                    }
077    
078                    Properties properties = new Properties();
079    
080                    try {
081                            Map<String, String> newTokens = new HashMap<String, String>();
082    
083                            MapUtil.copy(tokens, newTokens);
084    
085                            newTokens.put("template_id", _GLOBAL_PROPERTIES);
086    
087                            long groupId = GetterUtil.getLong(tokens.get("group_id"));
088    
089                            String script = JournalUtil.getTemplateScript(
090                                    groupId, _GLOBAL_PROPERTIES, newTokens, languageId);
091    
092                            PropertiesUtil.load(properties, script);
093                    }
094                    catch (Exception e) {
095                            if (_log.isWarnEnabled()) {
096                                    _log.warn(e);
097                            }
098                    }
099    
100                    if (properties.isEmpty()) {
101                            return s;
102                    }
103    
104                    String[] escapedKeys = new String[properties.size()];
105                    String[] escapedValues = new String[properties.size()];
106    
107                    String[] keys = new String[properties.size()];
108                    String[] values = new String[properties.size()];
109    
110                    String[] tempEscapedKeys = new String[properties.size()];
111                    String[] tempEscapedValues = new String[properties.size()];
112    
113                    int counter = 0;
114    
115                    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
116                            String key = (String)entry.getKey();
117                            String value = (String)entry.getValue();
118    
119                            String escapedKey =
120                                    StringPool.AT + StringPool.AT + key + StringPool.AT +
121                                            StringPool.AT;
122    
123                            String actualKey = StringPool.AT + key + StringPool.AT;
124    
125                            String tempEscapedKey =
126                                    TokensTransformerListener.TEMP_ESCAPED_AT_OPEN +
127                                            key + TokensTransformerListener.TEMP_ESCAPED_AT_CLOSE;
128    
129                            escapedKeys[counter] = escapedKey;
130                            escapedValues[counter] = tempEscapedKey;
131    
132                            keys[counter] = actualKey;
133                            values[counter] = value;
134    
135                            tempEscapedKeys[counter] = tempEscapedKey;
136                            tempEscapedValues[counter] = actualKey;
137    
138                            counter++;
139                    }
140    
141                    s = StringUtil.replace(s, escapedKeys, escapedValues);
142    
143                    s = StringUtil.replace(s, keys, values);
144    
145                    s = StringUtil.replace(s, tempEscapedKeys, tempEscapedValues);
146    
147                    return s;
148            }
149    
150            private static final String _GLOBAL_PROPERTIES = "GLOBAL-PROPERTIES";
151    
152            private static Log _log = LogFactoryUtil.getLog(
153                    PropertiesTransformerListener.class);
154    
155    }