1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }