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