1
19
20 package com.liferay.portlet.journal.util;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.util.GetterUtil;
25 import com.liferay.portal.kernel.util.MapUtil;
26 import com.liferay.portal.kernel.util.PropertiesUtil;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.kernel.util.StringUtil;
29
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.Properties;
34
35
42 public class PropertiesTransformerListener extends TransformerListener {
43
44 public String onXml(String s) {
45 if (_log.isDebugEnabled()) {
46 _log.debug("onXml");
47 }
48
49 return s;
50 }
51
52 public String onScript(String s) {
53 if (_log.isDebugEnabled()) {
54 _log.debug("onScript");
55 }
56
57 s = replace(s);
58
59 return s;
60 }
61
62 public String onOutput(String s) {
63 if (_log.isDebugEnabled()) {
64 _log.debug("onOutput");
65 }
66
67 s = replace(s);
68
69 return s;
70 }
71
72
79 protected String replace(String s) {
80 Map<String, String> tokens = getTokens();
81
82 String templateId = tokens.get("template_id");
83
84 if ((templateId == null) ||
85 ((templateId != null) && (templateId.equals(_GLOBAL_PROPERTIES)))) {
86
87
90 return s;
91 }
92
93 Properties props = new Properties();
94
95 try {
96 Map<String, String> newTokens = new HashMap<String, String>();
97
98 MapUtil.copy(tokens, newTokens);
99
100 newTokens.put("template_id", _GLOBAL_PROPERTIES);
101
102 long groupId = GetterUtil.getLong(tokens.get("group_id"));
103
104 String script = JournalUtil.getTemplateScript(
105 groupId, _GLOBAL_PROPERTIES, newTokens, getLanguageId());
106
107 PropertiesUtil.load(props, script);
108 }
109 catch (Exception e) {
110 if (_log.isWarnEnabled()) {
111 _log.warn(e);
112 }
113 }
114
115 if (props.size() == 0) {
116 return s;
117 }
118
119 String[] escapedKeys = new String[props.size()];
120 String[] escapedValues = new String[props.size()];
121
122 String[] keys = new String[props.size()];
123 String[] values = new String[props.size()];
124
125 String[] tempEscapedKeys = new String[props.size()];
126 String[] tempEscapedValues = new String[props.size()];
127
128 int counter = 0;
129
130 Iterator<Map.Entry<Object, Object>> itr = props.entrySet().iterator();
131
132 while (itr.hasNext()) {
133 Map.Entry<Object, Object> entry = itr.next();
134
135 String key = (String)entry.getKey();
136 String value = (String)entry.getValue();
137
138 String escapedKey =
139 StringPool.AT + StringPool.AT + key + StringPool.AT +
140 StringPool.AT;
141
142 String actualKey = StringPool.AT + key + StringPool.AT;
143
144 String tempEscapedKey =
145 TokensTransformerListener.TEMP_ESCAPED_AT_OPEN +
146 key + TokensTransformerListener.TEMP_ESCAPED_AT_CLOSE;
147
148 escapedKeys[counter] = escapedKey;
149 escapedValues[counter] = tempEscapedKey;
150
151 keys[counter] = actualKey;
152 values[counter] = value;
153
154 tempEscapedKeys[counter] = tempEscapedKey;
155 tempEscapedValues[counter] = actualKey;
156
157 counter++;
158 }
159
160 s = StringUtil.replace(s, escapedKeys, escapedValues);
161
162 s = StringUtil.replace(s, keys, values);
163
164 s = StringUtil.replace(s, tempEscapedKeys, tempEscapedValues);
165
166 return s;
167 }
168
169 private static final String _GLOBAL_PROPERTIES = "GLOBAL-PROPERTIES";
170
171 private static Log _log =
172 LogFactoryUtil.getLog(PropertiesTransformerListener.class);
173
174 }