1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.journal.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  import java.util.List;
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  
24  /**
25   * <a href="RegexTransformerListener.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   */
29  public class RegexTransformerListener extends TransformerListener {
30  
31      public String onXml(String s) {
32          if (_log.isDebugEnabled()) {
33              _log.debug("onXml");
34          }
35  
36          return s;
37      }
38  
39      public String onScript(String s) {
40          if (_log.isDebugEnabled()) {
41              _log.debug("onScript");
42          }
43  
44          s = replace(s);
45  
46          return s;
47      }
48  
49      public String onOutput(String s) {
50          if (_log.isDebugEnabled()) {
51              _log.debug("onOutput");
52          }
53  
54          s = replace(s);
55  
56          return s;
57      }
58  
59      protected String replace(String s) {
60          if (s == null) {
61              return s;
62          }
63  
64          List<Pattern> patterns = RegexTransformerUtil.getPatterns();
65          List<String> replacements = RegexTransformerUtil.getReplacements();
66  
67          for (int i = 0; i < patterns.size(); i++) {
68              Pattern pattern = patterns.get(i);
69              String replacement = replacements.get(i);
70  
71              Matcher matcher = pattern.matcher(s);
72  
73              s = matcher.replaceAll(replacement);
74          }
75  
76          return s;
77      }
78  
79      private static Log _log = LogFactoryUtil.getLog(
80          RegexTransformerListener.class);
81  
82  }