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 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 }