1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.MapUtil;
29  import com.liferay.portal.kernel.util.PropertiesUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.StringUtil;
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  public class PropertiesTransformerListener extends TransformerListener {
45  
46      public String onXml(String s) {
47          if (_log.isDebugEnabled()) {
48              _log.debug("onXml");
49          }
50  
51          return s;
52      }
53  
54      public String onScript(String s) {
55          if (_log.isDebugEnabled()) {
56              _log.debug("onScript");
57          }
58  
59          s = replace(s);
60  
61          return s;
62      }
63  
64      public String onOutput(String s) {
65          if (_log.isDebugEnabled()) {
66              _log.debug("onOutput");
67          }
68  
69          s = replace(s);
70  
71          return s;
72      }
73  
74      /**
75       * Replace the properties in a given string with their values fetched from
76       * the template GLOBAL-PROPERTIES.
77       *
78       * @return the processed string
79       */
80      protected String replace(String s) {
81          Map<String, String> tokens = getTokens();
82  
83          String templateId = tokens.get("template_id");
84  
85          if ((templateId == null) ||
86              ((templateId != null) && (templateId.equals(_GLOBAL_PROPERTIES)))) {
87  
88              // Return the original string if no template ID is specified or if
89              // the template ID is GLOBAL-PROPERTIES to prevent an infinite loop.
90  
91              return s;
92          }
93  
94          Properties props = new Properties();
95  
96          try {
97              Map<String, String> newTokens = new HashMap<String, String>();
98  
99              MapUtil.copy(tokens, newTokens);
100 
101             newTokens.put("template_id", _GLOBAL_PROPERTIES);
102 
103             long groupId = GetterUtil.getLong(tokens.get("group_id"));
104 
105             String script = JournalUtil.getTemplateScript(
106                 groupId, _GLOBAL_PROPERTIES, newTokens, getLanguageId());
107 
108             PropertiesUtil.load(props, script);
109         }
110         catch (Exception e) {
111             if (_log.isWarnEnabled()) {
112                 _log.warn(e);
113             }
114         }
115 
116         if (props.size() == 0) {
117             return s;
118         }
119 
120         String[] escapedKeys = new String[props.size()];
121         String[] escapedValues = new String[props.size()];
122 
123         String[] keys = new String[props.size()];
124         String[] values = new String[props.size()];
125 
126         String[] tempEscapedKeys = new String[props.size()];
127         String[] tempEscapedValues = new String[props.size()];
128 
129         int counter = 0;
130 
131         Iterator<Map.Entry<Object, Object>> itr = props.entrySet().iterator();
132 
133         while (itr.hasNext()) {
134             Map.Entry<Object, Object> entry = itr.next();
135 
136             String key = (String)entry.getKey();
137             String value = (String)entry.getValue();
138 
139             String escapedKey =
140                 StringPool.AT + StringPool.AT + key + StringPool.AT +
141                     StringPool.AT;
142 
143             String actualKey = StringPool.AT + key + StringPool.AT;
144 
145             String tempEscapedKey =
146                 TokensTransformerListener.TEMP_ESCAPED_AT_OPEN +
147                     key + TokensTransformerListener.TEMP_ESCAPED_AT_CLOSE;
148 
149             escapedKeys[counter] = escapedKey;
150             escapedValues[counter] = tempEscapedKey;
151 
152             keys[counter] = actualKey;
153             values[counter] = value;
154 
155             tempEscapedKeys[counter] = tempEscapedKey;
156             tempEscapedValues[counter] = actualKey;
157 
158             counter++;
159         }
160 
161         s = StringUtil.replace(s, escapedKeys, escapedValues);
162 
163         s = StringUtil.replace(s, keys, values);
164 
165         s = StringUtil.replace(s, tempEscapedKeys, tempEscapedValues);
166 
167         return s;
168     }
169 
170     private static final String _GLOBAL_PROPERTIES = "GLOBAL-PROPERTIES";
171 
172     private static Log _log =
173         LogFactoryUtil.getLog(PropertiesTransformerListener.class);
174 
175 }