001
014
015 package com.liferay.portlet.journal.util;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.templateparser.BaseTransformerListener;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.MapUtil;
022 import com.liferay.portal.kernel.util.PropertiesUtil;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.StringUtil;
025 import com.liferay.portal.kernel.xml.Document;
026
027 import java.util.HashMap;
028 import java.util.Map;
029 import java.util.Properties;
030
031
034 public class PropertiesTransformerListener extends BaseTransformerListener {
035
036 @Override
037 public String onOutput(
038 String output, String languageId, Map<String, String> tokens) {
039
040 if (_log.isDebugEnabled()) {
041 _log.debug("onOutput");
042 }
043
044 return replace(output, languageId, tokens);
045 }
046
047 @Override
048 public String onScript(
049 String script, Document document, String languageId,
050 Map<String, String> tokens) {
051
052 if (_log.isDebugEnabled()) {
053 _log.debug("onScript");
054 }
055
056 return replace(script, languageId, tokens);
057 }
058
059
065 protected String replace(
066 String s, String languageId, Map<String, String> tokens) {
067
068 String templateId = tokens.get("template_id");
069
070 if ((templateId == null) ||
071 ((templateId != null) && templateId.equals(_GLOBAL_PROPERTIES))) {
072
073
074
075
076 return s;
077 }
078
079 Properties properties = new Properties();
080
081 try {
082 Map<String, String> newTokens = new HashMap<>();
083
084 MapUtil.copy(tokens, newTokens);
085
086 newTokens.put("template_id", _GLOBAL_PROPERTIES);
087
088 long articleGroupId = GetterUtil.getLong(
089 tokens.get("article_group_id"));
090
091 String script = JournalUtil.getTemplateScript(
092 articleGroupId, _GLOBAL_PROPERTIES, newTokens, languageId);
093
094 PropertiesUtil.load(properties, script);
095 }
096 catch (Exception e) {
097 if (_log.isWarnEnabled()) {
098 _log.warn(e);
099 }
100 }
101
102 if (properties.isEmpty()) {
103 return s;
104 }
105
106 String[] escapedKeys = new String[properties.size()];
107 String[] escapedValues = new String[properties.size()];
108
109 String[] keys = new String[properties.size()];
110 String[] values = new String[properties.size()];
111
112 String[] tempEscapedKeys = new String[properties.size()];
113 String[] tempEscapedValues = new String[properties.size()];
114
115 int counter = 0;
116
117 for (Map.Entry<Object, Object> entry : properties.entrySet()) {
118 String key = (String)entry.getKey();
119 String value = (String)entry.getValue();
120
121 String escapedKey =
122 StringPool.AT + StringPool.AT + key + StringPool.AT +
123 StringPool.AT;
124
125 String actualKey = StringPool.AT + key + StringPool.AT;
126
127 String tempEscapedKey =
128 TokensTransformerListener.TEMP_ESCAPED_AT_OPEN +
129 key + TokensTransformerListener.TEMP_ESCAPED_AT_CLOSE;
130
131 escapedKeys[counter] = escapedKey;
132 escapedValues[counter] = tempEscapedKey;
133
134 keys[counter] = actualKey;
135 values[counter] = value;
136
137 tempEscapedKeys[counter] = tempEscapedKey;
138 tempEscapedValues[counter] = actualKey;
139
140 counter++;
141 }
142
143 s = StringUtil.replace(s, escapedKeys, escapedValues);
144
145 s = StringUtil.replace(s, keys, values);
146
147 s = StringUtil.replace(s, tempEscapedKeys, tempEscapedValues);
148
149 return s;
150 }
151
152 private static final String _GLOBAL_PROPERTIES = "GLOBAL-PROPERTIES";
153
154 private static final Log _log = LogFactoryUtil.getLog(
155 PropertiesTransformerListener.class);
156
157 }