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