1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journal.util;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.MapUtil;
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  
30  import java.util.HashMap;
31  import java.util.Iterator;
32  import java.util.Map;
33  import java.util.Properties;
34  
35  /**
36   * <a href="PropertiesTransformerListener.java.html"><b><i>View Source</i></b>
37   * </a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class PropertiesTransformerListener extends TransformerListener {
43  
44      public String onXml(String s) {
45          if (_log.isDebugEnabled()) {
46              _log.debug("onXml");
47          }
48  
49          return s;
50      }
51  
52      public String onScript(String s) {
53          if (_log.isDebugEnabled()) {
54              _log.debug("onScript");
55          }
56  
57          s = replace(s);
58  
59          return s;
60      }
61  
62      public String onOutput(String s) {
63          if (_log.isDebugEnabled()) {
64              _log.debug("onOutput");
65          }
66  
67          s = replace(s);
68  
69          return s;
70      }
71  
72      /**
73       * Replace the properties in a given string with their values fetched from
74       * the template GLOBAL-PROPERTIES.
75       *
76       * @param       s the given string
77       * @return      the processed string
78       */
79      protected String replace(String s) {
80          Map<String, String> tokens = getTokens();
81  
82          String templateId = tokens.get("template_id");
83  
84          if ((templateId == null) ||
85              ((templateId != null) && (templateId.equals(_GLOBAL_PROPERTIES)))) {
86  
87              // Return the original string if no template ID is specified or if
88              // the template ID is GLOBAL-PROPERTIES to prevent an infinite loop.
89  
90              return s;
91          }
92  
93          Properties props = new Properties();
94  
95          try {
96              Map<String, String> newTokens = new HashMap<String, String>();
97  
98              MapUtil.copy(tokens, newTokens);
99  
100             newTokens.put("template_id", _GLOBAL_PROPERTIES);
101 
102             long groupId = GetterUtil.getLong(tokens.get("group_id"));
103 
104             String script = JournalUtil.getTemplateScript(
105                 groupId, _GLOBAL_PROPERTIES, newTokens, getLanguageId());
106 
107             PropertiesUtil.load(props, script);
108         }
109         catch (Exception e) {
110             if (_log.isWarnEnabled()) {
111                 _log.warn(e);
112             }
113         }
114 
115         if (props.size() == 0) {
116             return s;
117         }
118 
119         String[] escapedKeys = new String[props.size()];
120         String[] escapedValues = new String[props.size()];
121 
122         String[] keys = new String[props.size()];
123         String[] values = new String[props.size()];
124 
125         String[] tempEscapedKeys = new String[props.size()];
126         String[] tempEscapedValues = new String[props.size()];
127 
128         int counter = 0;
129 
130         Iterator<Map.Entry<Object, Object>> itr = props.entrySet().iterator();
131 
132         while (itr.hasNext()) {
133             Map.Entry<Object, Object> entry = itr.next();
134 
135             String key = (String)entry.getKey();
136             String value = (String)entry.getValue();
137 
138             String escapedKey =
139                 StringPool.AT + StringPool.AT + key + StringPool.AT +
140                     StringPool.AT;
141 
142             String actualKey = StringPool.AT + key + StringPool.AT;
143 
144             String tempEscapedKey =
145                 TokensTransformerListener.TEMP_ESCAPED_AT_OPEN +
146                     key + TokensTransformerListener.TEMP_ESCAPED_AT_CLOSE;
147 
148             escapedKeys[counter] = escapedKey;
149             escapedValues[counter] = tempEscapedKey;
150 
151             keys[counter] = actualKey;
152             values[counter] = value;
153 
154             tempEscapedKeys[counter] = tempEscapedKey;
155             tempEscapedValues[counter] = actualKey;
156 
157             counter++;
158         }
159 
160         s = StringUtil.replace(s, escapedKeys, escapedValues);
161 
162         s = StringUtil.replace(s, keys, values);
163 
164         s = StringUtil.replace(s, tempEscapedKeys, tempEscapedValues);
165 
166         return s;
167     }
168 
169     private static final String _GLOBAL_PROPERTIES = "GLOBAL-PROPERTIES";
170 
171     private static Log _log =
172         LogFactoryUtil.getLog(PropertiesTransformerListener.class);
173 
174 }