1   /**
2    * Copyright (c) 2000-2008 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.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  /**
40   * <a href="PropertiesTransformerListener.java.html"><b><i>View Source</i></b>
41   * </a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
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 = replace(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 = replace(s);
72  
73          return s;
74      }
75  
76      /**
77       * Replace the properties in a given string with their values fetched from
78       * the template GLOBAL-PROPERTIES.
79       *
80       * @param       s the given string
81       * @return      the processed string
82       */
83      protected String replace(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              // Return the original string if no template ID is specified or if
92              // the template ID is GLOBAL-PROPERTIES to prevent an infinite loop.
93  
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 }