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