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