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