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.Map;
028 import java.util.Properties;
029
030
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
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
073
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 }