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