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