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 articleGroupId = GetterUtil.getLong(
088                                    tokens.get("article_group_id"));
089    
090                            String script = JournalUtil.getTemplateScript(
091                                    articleGroupId, _GLOBAL_PROPERTIES, newTokens, languageId);
092    
093                            PropertiesUtil.load(properties, script);
094                    }
095                    catch (Exception e) {
096                            if (_log.isWarnEnabled()) {
097                                    _log.warn(e);
098                            }
099                    }
100    
101                    if (properties.isEmpty()) {
102                            return s;
103                    }
104    
105                    String[] escapedKeys = new String[properties.size()];
106                    String[] escapedValues = new String[properties.size()];
107    
108                    String[] keys = new String[properties.size()];
109                    String[] values = new String[properties.size()];
110    
111                    String[] tempEscapedKeys = new String[properties.size()];
112                    String[] tempEscapedValues = new String[properties.size()];
113    
114                    int counter = 0;
115    
116                    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
117                            String key = (String)entry.getKey();
118                            String value = (String)entry.getValue();
119    
120                            String escapedKey =
121                                    StringPool.AT + StringPool.AT + key + StringPool.AT +
122                                            StringPool.AT;
123    
124                            String actualKey = StringPool.AT + key + StringPool.AT;
125    
126                            String tempEscapedKey =
127                                    TokensTransformerListener.TEMP_ESCAPED_AT_OPEN +
128                                            key + TokensTransformerListener.TEMP_ESCAPED_AT_CLOSE;
129    
130                            escapedKeys[counter] = escapedKey;
131                            escapedValues[counter] = tempEscapedKey;
132    
133                            keys[counter] = actualKey;
134                            values[counter] = value;
135    
136                            tempEscapedKeys[counter] = tempEscapedKey;
137                            tempEscapedValues[counter] = actualKey;
138    
139                            counter++;
140                    }
141    
142                    s = StringUtil.replace(s, escapedKeys, escapedValues);
143    
144                    s = StringUtil.replace(s, keys, values);
145    
146                    s = StringUtil.replace(s, tempEscapedKeys, tempEscapedValues);
147    
148                    return s;
149            }
150    
151            private static final String _GLOBAL_PROPERTIES = "GLOBAL-PROPERTIES";
152    
153            private static Log _log = LogFactoryUtil.getLog(
154                    PropertiesTransformerListener.class);
155    
156    }