1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
39   * <a href="PropertiesTransformerListener.java.html"><b><i>View Source</i></b>
40   * </a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
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      /**
76       * Replace the properties in a given string with their values fetched from
77       * the template GLOBAL-PROPERTIES.
78       *
79       * @param       s the given string
80       * @return      the processed string
81       */
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              // Return the original string if no template ID is specified or if
91              // the template ID is GLOBAL-PROPERTIES to prevent an infinite loop.
92  
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 }