1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.rt.view;
22
23 import org.apache.log4j.Logger;
24
25
26
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
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
61 } catch (MalformedPatternException e) {
62
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
76
77 public String subst(String substituteIn) {
78 if (this.pattern == null) {
79 return substituteIn;
80 }
81 try {
82
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 }