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