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