1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class TokensTransformerListener extends TransformerListener {
43  
44      public static final String TEMP_ESCAPED_AT_OPEN =
45          "[$TEMP_ESCAPED_AT_OPEN$]";
46  
47      public static final String TEMP_ESCAPED_AT_CLOSE =
48          "[$_TEMP_ESCAPED_AT_CLOSE$]";
49  
50      public String onXml(String s) {
51          if (_log.isDebugEnabled()) {
52              _log.debug("onXml");
53          }
54  
55          return s;
56      }
57  
58      public String onScript(String s) {
59          if (_log.isDebugEnabled()) {
60              _log.debug("onScript");
61          }
62  
63          return replace(s);
64      }
65  
66      public String onOutput(String s) {
67          if (_log.isDebugEnabled()) {
68              _log.debug("onOutput");
69          }
70  
71          return replace(s);
72      }
73  
74      /**
75       * Replace the standard tokens in a given string with their values.
76       *
77       * @return the processed string
78       */
79      protected String replace(String s) {
80          Map<String, String> tokens = getTokens();
81  
82          Set<Map.Entry<String, String>> tokensSet = tokens.entrySet();
83  
84          if (tokensSet.size() == 0) {
85              return s;
86          }
87  
88          List<String> escapedKeysList = new ArrayList<String>();
89          List<String> escapedValuesList = new ArrayList<String>();
90  
91          List<String> keysList = new ArrayList<String>();
92          List<String> valuesList = new ArrayList<String>();
93  
94          List<String> tempEscapedKeysList = new ArrayList<String>();
95          List<String> tempEscapedValuesList = new ArrayList<String>();
96  
97          for (Map.Entry<String, String> entry : tokensSet) {
98              String key = entry.getKey();
99              String value = GetterUtil.getString(entry.getValue());
100 
101             if (Validator.isNotNull(key)) {
102                 String escapedKey =
103                     StringPool.AT + StringPool.AT + key + StringPool.AT +
104                         StringPool.AT;
105 
106                 String actualKey = StringPool.AT + key + StringPool.AT;
107 
108                 String tempEscapedKey =
109                     TEMP_ESCAPED_AT_OPEN + key + TEMP_ESCAPED_AT_CLOSE;
110 
111                 escapedKeysList.add(escapedKey);
112                 escapedValuesList.add(tempEscapedKey);
113 
114                 keysList.add(actualKey);
115                 valuesList.add(value);
116 
117                 tempEscapedKeysList.add(tempEscapedKey);
118                 tempEscapedValuesList.add(actualKey);
119             }
120         }
121 
122         s = StringUtil.replace(
123             s,
124             escapedKeysList.toArray(new String[escapedKeysList.size()]),
125             escapedValuesList.toArray(new String[escapedValuesList.size()]));
126 
127         s = StringUtil.replace(
128             s,
129             keysList.toArray(new String[keysList.size()]),
130             valuesList.toArray(new String[valuesList.size()]));
131 
132         s = StringUtil.replace(
133             s,
134             tempEscapedKeysList.toArray(new String[tempEscapedKeysList.size()]),
135             tempEscapedValuesList.toArray(
136                 new String[tempEscapedValuesList.size()]));
137 
138         return s;
139     }
140 
141     private static Log _log =
142         LogFactoryUtil.getLog(TokensTransformerListener.class);
143 
144 }