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