1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.MapUtil;
21  import com.liferay.portal.kernel.util.PropertiesUtil;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.StringUtil;
24  
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.Map;
28  import java.util.Properties;
29  
30  /**
31   * <a href="PropertiesTransformerListener.java.html"><b><i>View Source</i></b>
32   * </a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class PropertiesTransformerListener extends TransformerListener {
37  
38      public String onXml(String s) {
39          if (_log.isDebugEnabled()) {
40              _log.debug("onXml");
41          }
42  
43          return s;
44      }
45  
46      public String onScript(String s) {
47          if (_log.isDebugEnabled()) {
48              _log.debug("onScript");
49          }
50  
51          s = replace(s);
52  
53          return s;
54      }
55  
56      public String onOutput(String s) {
57          if (_log.isDebugEnabled()) {
58              _log.debug("onOutput");
59          }
60  
61          s = replace(s);
62  
63          return s;
64      }
65  
66      /**
67       * Replace the properties in a given string with their values fetched from
68       * the template GLOBAL-PROPERTIES.
69       *
70       * @return the processed string
71       */
72      protected String replace(String s) {
73          Map<String, String> tokens = getTokens();
74  
75          String templateId = tokens.get("template_id");
76  
77          if ((templateId == null) ||
78              ((templateId != null) && (templateId.equals(_GLOBAL_PROPERTIES)))) {
79  
80              // Return the original string if no template ID is specified or if
81              // the template ID is GLOBAL-PROPERTIES to prevent an infinite loop.
82  
83              return s;
84          }
85  
86          Properties props = new Properties();
87  
88          try {
89              Map<String, String> newTokens = new HashMap<String, String>();
90  
91              MapUtil.copy(tokens, newTokens);
92  
93              newTokens.put("template_id", _GLOBAL_PROPERTIES);
94  
95              long groupId = GetterUtil.getLong(tokens.get("group_id"));
96  
97              String script = JournalUtil.getTemplateScript(
98                  groupId, _GLOBAL_PROPERTIES, newTokens, getLanguageId());
99  
100             PropertiesUtil.load(props, script);
101         }
102         catch (Exception e) {
103             if (_log.isWarnEnabled()) {
104                 _log.warn(e);
105             }
106         }
107 
108         if (props.size() == 0) {
109             return s;
110         }
111 
112         String[] escapedKeys = new String[props.size()];
113         String[] escapedValues = new String[props.size()];
114 
115         String[] keys = new String[props.size()];
116         String[] values = new String[props.size()];
117 
118         String[] tempEscapedKeys = new String[props.size()];
119         String[] tempEscapedValues = new String[props.size()];
120 
121         int counter = 0;
122 
123         Iterator<Map.Entry<Object, Object>> itr = props.entrySet().iterator();
124 
125         while (itr.hasNext()) {
126             Map.Entry<Object, Object> entry = itr.next();
127 
128             String key = (String)entry.getKey();
129             String value = (String)entry.getValue();
130 
131             String escapedKey =
132                 StringPool.AT + StringPool.AT + key + StringPool.AT +
133                     StringPool.AT;
134 
135             String actualKey = StringPool.AT + key + StringPool.AT;
136 
137             String tempEscapedKey =
138                 TokensTransformerListener.TEMP_ESCAPED_AT_OPEN +
139                     key + TokensTransformerListener.TEMP_ESCAPED_AT_CLOSE;
140 
141             escapedKeys[counter] = escapedKey;
142             escapedValues[counter] = tempEscapedKey;
143 
144             keys[counter] = actualKey;
145             values[counter] = value;
146 
147             tempEscapedKeys[counter] = tempEscapedKey;
148             tempEscapedValues[counter] = actualKey;
149 
150             counter++;
151         }
152 
153         s = StringUtil.replace(s, escapedKeys, escapedValues);
154 
155         s = StringUtil.replace(s, keys, values);
156 
157         s = StringUtil.replace(s, tempEscapedKeys, tempEscapedValues);
158 
159         return s;
160     }
161 
162     private static final String _GLOBAL_PROPERTIES = "GLOBAL-PROPERTIES";
163 
164     private static Log _log = LogFactoryUtil.getLog(
165         PropertiesTransformerListener.class);
166 
167 }