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