View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.rt.view;
22  
23  import org.apache.log4j.Logger;
24  
25  //import org.apache.regexp.RE;
26  //import org.apache.regexp.RESyntaxException;
27  import org.apache.oro.text.regex.*;
28  
29  /***
30   * Replace Regular expression with substitution string.
31   *
32   * Created on 16.12.2004
33   * Contributing Author(s):
34   *
35   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
36   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
37   *
38   * @author vlads
39   * @version $Revision: 1.1 $ ($Author: vlads $)  $Date: 2004/12/16 17:40:19 $
40   */
41  public class ReplacementExpression {
42      
43      protected static final Logger log = Logger.getLogger(ReplacementExpression.class);
44      
45      //private RE regex;
46      Pattern pattern;
47      Perl5Substitution subst;
48      private String name;
49      
50      private String substitution;
51      
52      static PatternMatcher matcher = new Perl5Matcher();
53      static PatternCompiler compiler = new Perl5Compiler();
54      
55      public ReplacementExpression(String regularExpression, String substitution) {
56          try {
57              pattern = compiler.compile(regularExpression);
58              int interps = Perl5Substitution.INTERPOLATE_ALL;
59              subst = new Perl5Substitution(substitution, interps);
60              //this.regex = new RE(expr);
61          } catch (MalformedPatternException e) {
62              //this.regex = null;
63              this.pattern= null;
64              log.error("Regular expression Syntax Error " + regularExpression, e);
65          }
66          this.substitution = substitution;
67          this.name = regularExpression + " -> " + substitution;
68      }
69      
70      public void debug() {
71          log.debug(this.name);
72      }
73      
74      /*
75       * If this regular expression object doesn't match at any position, the original String is returned unchanged
76       */
77      public String subst(String substituteIn) {
78          if (this.pattern == null) {
79              return substituteIn;
80          }
81          try {
82              //return this.regex.subst(substituteIn, this.substitution, RE.REPLACE_BACKREFERENCES | RE.REPLACE_FIRSTONLY);
83              return Util.substitute(matcher, pattern, this.subst, substituteIn, Util.SUBSTITUTE_ALL);
84          } catch (RuntimeException e) {
85              log.error("Error in regular expression " + this.name, e);
86              return substituteIn;
87          } 
88      }
89      
90      
91  }