2 // This software is now distributed according to
\r
3 // the Lesser Gnu Public License. Please see
\r
4 // http://www.gnu.org/copyleft/lesser.txt for
\r
6 // -- Happy Computing!
\r
8 package com.stevesoft.pat;
\r
10 import com.stevesoft.pat.wrap.*;
\r
12 /** Replacement rule used by the Transformer.
\r
13 @see com.stevesoft.pat.Transformer
\r
19 TransRepRule(Transformer t)
\r
24 public String toString1()
\r
29 public Object clone1()
\r
31 return new TransRepRule(t);
\r
34 public void apply(StringBufferLike sb, RegRes rr)
\r
36 // get the ReplaceRule of the Regex that matched.
\r
37 next = t.tp.ra[t.tp.pn].getReplaceRule();
\r
41 /** Sometimes you want to replace a whole bunch of things
\r
42 that might occur within a single line of text. One efficient
\r
43 way to do this, both in terms of performance and programming
\r
44 ease, is with Transformer. The Transformer contains an array
\r
45 of Regex's and uses the Regex that matches earliest within the
\r
46 text to do the replacing, if two Regex's match at the same
\r
47 time it uses the one put in the Transformer first.
\r
49 This feature can be used to prevent transformations from
\r
50 occurring in certain regions. For example, if I add the rule
\r
51 s'//.*'$&' and then add the
\r
52 rule s/hello/goodbye/ the Transformer will replace "hello"
\r
53 with "goodbye" except when it occurs inside a double-slash
\r
54 style of comment. The transformation on the comment goes first,
\r
55 does nothing, and precludes transformation on the same region
\r
56 of text as the s/hello/goodbye/ rule.
\r
58 So far, at least, this class does not have the capability of
\r
59 turning into a giant robot :-)
\r
61 public class Transformer
\r
64 Regex rp = new Regex();
\r
65 boolean auto_optimize;
\r
67 /** Get a replacer to that works with the current Regex.
\r
68 @see com.stevesoft.pat.Replacer
\r
70 public Replacer getReplacer()
\r
72 return rp.getReplacer();
\r
75 /** Instantiate a new Transformer object. */
\r
76 public Transformer(boolean auto)
\r
78 auto_optimize = auto;
\r
79 tp = new TransPat();
\r
80 rp.setReplaceRule(new TransRepRule(this));
\r
84 /** Add a new Regex to the set of Regex's. */
\r
85 public void add(Regex r)
\r
91 tp.ra[tp.ra_len++] = r;
\r
92 if (tp.ra.length == tp.ra_len)
\r
94 Regex[] ra2 = new Regex[tp.ra_len + 10];
\r
95 for (int i = 0; i < tp.ra_len; i++)
\r
101 rp.numSubs_ = r.numSubs_ > rp.numSubs_ ? r.numSubs_ : rp.numSubs_;
\r
104 /** Returns the number of Regex's in this Transformer. */
\r
105 public int patterns()
\r
110 /** Get the Regex at position i in this Transformer. */
\r
111 public Regex getRegexAt(int i)
\r
113 if (i >= tp.ra_len)
\r
115 throw new ArrayIndexOutOfBoundsException("i=" + i + ">=" + patterns());
\r
119 throw new ArrayIndexOutOfBoundsException("i=" + i + "< 0");
\r
124 /** Set the Regex at position i in this Transformer. */
\r
125 public void setRegexAt(Regex rx, int i)
\r
127 if (i >= tp.ra_len)
\r
129 throw new ArrayIndexOutOfBoundsException("i=" + i + ">=" + patterns());
\r
133 throw new ArrayIndexOutOfBoundsException("i=" + i + "< 0");
\r
138 /** Add a new Regex by calling Regex.perlCode
\r
139 @see com.stevesoft.pat.Regex#perlCode(java.lang.String)
\r
141 public void add(String rs)
\r
143 Regex r = Regex.perlCode(rs);
\r
146 throw new NullPointerException("bad pattern to Regex.perlCode: " + rs);
\r
151 /** Add an array of Strings (which will be converted to
\r
152 Regex's via the Regex.perlCode method.
\r
153 @see com.stevesoft.pat.Regex#perlCode(java.lang.String)
\r
155 public void add(String[] array)
\r
157 for (int i = 0; i < array.length; i++)
\r
163 /** Replace all matches in the current String. */
\r
164 public String replaceAll(String s)
\r
166 return dorep(s, 0, s.length());
\r
169 public StringLike replaceAll(StringLike s)
\r
171 return dorep(s, 0, s.length());
\r
174 /** Replace all matching patterns beginning at position start. */
\r
175 public String replaceAllFrom(String s, int start)
\r
177 return dorep(s, start, s.length());
\r
180 /** Replace all matching patterns beginning between the positions
\r
181 start and end inclusive. */
\r
182 public String replaceAllRegion(String s, int start, int end)
\r
184 return dorep(s, start, end);
\r
187 Replacer repr = new Replacer();
\r
188 final StringLike dorep(StringLike s, int start, int end)
\r
190 StringLike tfmd = repr.replaceAllRegion(s, rp, start, end);
\r
191 tp.lastMatchedTo = repr.lastMatchedTo;
\r
195 final String dorep(String s, int start, int end)
\r
197 return dorep(new StringWrap(s), start, end).toString();
\r
200 /** Replace the first matching pattern in String s. */
\r
201 public String replaceFirst(String s)
\r
203 return dorep(s, 0, s.length());
\r
206 /** Replace the first matching pattern after position start in
\r
208 public String replaceFirstFrom(String s, int start)
\r
210 return dorep(s, start, s.length());
\r
213 /** Replace the first matching pattern that begins between
\r
214 start and end inclusive. */
\r
215 public String replaceFirstRegion(String s, int start, int end)
\r
217 return dorep(s, start, end);
\r