1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  /**
30   * <a href="TokensTransformerListener.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class TokensTransformerListener extends TransformerListener {
35  
36      public static final String TEMP_ESCAPED_AT_OPEN =
37          "[$TEMP_ESCAPED_AT_OPEN$]";
38  
39      public static final String TEMP_ESCAPED_AT_CLOSE =
40          "[$_TEMP_ESCAPED_AT_CLOSE$]";
41  
42      public String onXml(String s) {
43          if (_log.isDebugEnabled()) {
44              _log.debug("onXml");
45          }
46  
47          return s;
48      }
49  
50      public String onScript(String s) {
51          if (_log.isDebugEnabled()) {
52              _log.debug("onScript");
53          }
54  
55          return replace(s);
56      }
57  
58      public String onOutput(String s) {
59          if (_log.isDebugEnabled()) {
60              _log.debug("onOutput");
61          }
62  
63          return replace(s);
64      }
65  
66      /**
67       * Replace the standard tokens in a given string with their values.
68       *
69       * @return the processed string
70       */
71      protected String replace(String s) {
72          Map<String, String> tokens = getTokens();
73  
74          Set<Map.Entry<String, String>> tokensSet = tokens.entrySet();
75  
76          if (tokensSet.size() == 0) {
77              return s;
78          }
79  
80          List<String> escapedKeysList = new ArrayList<String>();
81          List<String> escapedValuesList = new ArrayList<String>();
82  
83          List<String> keysList = new ArrayList<String>();
84          List<String> valuesList = new ArrayList<String>();
85  
86          List<String> tempEscapedKeysList = new ArrayList<String>();
87          List<String> tempEscapedValuesList = new ArrayList<String>();
88  
89          for (Map.Entry<String, String> entry : tokensSet) {
90              String key = entry.getKey();
91              String value = GetterUtil.getString(entry.getValue());
92  
93              if (Validator.isNotNull(key)) {
94                  String escapedKey =
95                      StringPool.AT + StringPool.AT + key + StringPool.AT +
96                          StringPool.AT;
97  
98                  String actualKey = StringPool.AT + key + StringPool.AT;
99  
100                 String tempEscapedKey =
101                     TEMP_ESCAPED_AT_OPEN + key + TEMP_ESCAPED_AT_CLOSE;
102 
103                 escapedKeysList.add(escapedKey);
104                 escapedValuesList.add(tempEscapedKey);
105 
106                 keysList.add(actualKey);
107                 valuesList.add(value);
108 
109                 tempEscapedKeysList.add(tempEscapedKey);
110                 tempEscapedValuesList.add(actualKey);
111             }
112         }
113 
114         s = StringUtil.replace(
115             s,
116             escapedKeysList.toArray(new String[escapedKeysList.size()]),
117             escapedValuesList.toArray(new String[escapedValuesList.size()]));
118 
119         s = StringUtil.replace(
120             s,
121             keysList.toArray(new String[keysList.size()]),
122             valuesList.toArray(new String[valuesList.size()]));
123 
124         s = StringUtil.replace(
125             s,
126             tempEscapedKeysList.toArray(new String[tempEscapedKeysList.size()]),
127             tempEscapedValuesList.toArray(
128                 new String[tempEscapedValuesList.size()]));
129 
130         return s;
131     }
132 
133     private static Log _log = LogFactoryUtil.getLog(
134         TokensTransformerListener.class);
135 
136 }