001
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.Iterator;
028 import java.util.Map;
029 import java.util.Properties;
030
031
034 public class PropertiesTransformerListener extends BaseTransformerListener {
035
036 @Override
037 public String onOutput(String s) {
038 if (_log.isDebugEnabled()) {
039 _log.debug("onOutput");
040 }
041
042 s = replace(s);
043
044 return s;
045 }
046
047 @Override
048 public String onScript(String s) {
049 if (_log.isDebugEnabled()) {
050 _log.debug("onScript");
051 }
052
053 s = replace(s);
054
055 return s;
056 }
057
058 @Override
059 public String onXml(String s) {
060 if (_log.isDebugEnabled()) {
061 _log.debug("onXml");
062 }
063
064 return s;
065 }
066
067
073 protected String replace(String s) {
074 Map<String, String> tokens = getTokens();
075
076 String templateId = tokens.get("template_id");
077
078 if ((templateId == null) ||
079 ((templateId != null) && templateId.equals(_GLOBAL_PROPERTIES))) {
080
081
082
083
084 return s;
085 }
086
087 Properties properties = new Properties();
088
089 try {
090 Map<String, String> newTokens = new HashMap<String, String>();
091
092 MapUtil.copy(tokens, newTokens);
093
094 newTokens.put("template_id", _GLOBAL_PROPERTIES);
095
096 long groupId = GetterUtil.getLong(tokens.get("group_id"));
097
098 String script = JournalUtil.getTemplateScript(
099 groupId, _GLOBAL_PROPERTIES, newTokens, getLanguageId());
100
101 PropertiesUtil.load(properties, script);
102 }
103 catch (Exception e) {
104 if (_log.isWarnEnabled()) {
105 _log.warn(e);
106 }
107 }
108
109 if (properties.isEmpty()) {
110 return s;
111 }
112
113 String[] escapedKeys = new String[properties.size()];
114 String[] escapedValues = new String[properties.size()];
115
116 String[] keys = new String[properties.size()];
117 String[] values = new String[properties.size()];
118
119 String[] tempEscapedKeys = new String[properties.size()];
120 String[] tempEscapedValues = new String[properties.size()];
121
122 int counter = 0;
123
124 Iterator<Map.Entry<Object, Object>> itr =
125 properties.entrySet().iterator();
126
127 while (itr.hasNext()) {
128 Map.Entry<Object, Object> entry = itr.next();
129
130 String key = (String)entry.getKey();
131 String value = (String)entry.getValue();
132
133 String escapedKey =
134 StringPool.AT + StringPool.AT + key + StringPool.AT +
135 StringPool.AT;
136
137 String actualKey = StringPool.AT + key + StringPool.AT;
138
139 String tempEscapedKey =
140 TokensTransformerListener.TEMP_ESCAPED_AT_OPEN +
141 key + TokensTransformerListener.TEMP_ESCAPED_AT_CLOSE;
142
143 escapedKeys[counter] = escapedKey;
144 escapedValues[counter] = tempEscapedKey;
145
146 keys[counter] = actualKey;
147 values[counter] = value;
148
149 tempEscapedKeys[counter] = tempEscapedKey;
150 tempEscapedValues[counter] = actualKey;
151
152 counter++;
153 }
154
155 s = StringUtil.replace(s, escapedKeys, escapedValues);
156
157 s = StringUtil.replace(s, keys, values);
158
159 s = StringUtil.replace(s, tempEscapedKeys, tempEscapedValues);
160
161 return s;
162 }
163
164 private static final String _GLOBAL_PROPERTIES = "GLOBAL-PROPERTIES";
165
166 private static Log _log = LogFactoryUtil.getLog(
167 PropertiesTransformerListener.class);
168
169 }