2 // This software is now distributed according to
3 // the Lesser Gnu Public License. Please see
4 // http://www.gnu.org/copyleft/lesser.txt for
8 package com.stevesoft.pat;
10 import com.stevesoft.pat.wrap.*;
13 * Replacement rule used by the Transformer.
15 * @see com.stevesoft.pat.Transformer
17 class TransRepRule extends ReplaceRule
21 TransRepRule(Transformer t)
26 public String toString1()
31 public Object clone1()
33 return new TransRepRule(t);
36 public void apply(StringBufferLike sb, RegRes rr)
38 // get the ReplaceRule of the Regex that matched.
39 next = t.tp.ra[t.tp.pn].getReplaceRule();
44 * Sometimes you want to replace a whole bunch of things that might occur within
45 * a single line of text. One efficient way to do this, both in terms of
46 * performance and programming ease, is with Transformer. The Transformer
47 * contains an array of Regex's and uses the Regex that matches earliest within
48 * the text to do the replacing, if two Regex's match at the same time it uses
49 * the one put in the Transformer first.
51 * This feature can be used to prevent transformations from occurring in certain
52 * regions. For example, if I add the rule s'//.*'$&' and then add the rule
53 * s/hello/goodbye/ the Transformer will replace "hello" with "goodbye" except
54 * when it occurs inside a double-slash style of comment. The transformation on
55 * the comment goes first, does nothing, and precludes transformation on the
56 * same region of text as the s/hello/goodbye/ rule.
58 * So far, at least, this class does not have the capability of turning into a
61 public class Transformer
65 Regex rp = new Regex();
67 boolean auto_optimize;
70 * Get a replacer to that works with the current Regex.
72 * @see com.stevesoft.pat.Replacer
74 public Replacer getReplacer()
76 return rp.getReplacer();
79 /** Instantiate a new Transformer object. */
80 public Transformer(boolean auto)
84 rp.setReplaceRule(new TransRepRule(this));
88 /** Add a new Regex to the set of Regex's. */
89 public void add(Regex r)
95 tp.ra[tp.ra_len++] = r;
96 if (tp.ra.length == tp.ra_len)
98 Regex[] ra2 = new Regex[tp.ra_len + 10];
99 for (int i = 0; i < tp.ra_len; i++)
105 rp.numSubs_ = r.numSubs_ > rp.numSubs_ ? r.numSubs_ : rp.numSubs_;
108 /** Returns the number of Regex's in this Transformer. */
109 public int patterns()
114 /** Get the Regex at position i in this Transformer. */
115 public Regex getRegexAt(int i)
119 throw new ArrayIndexOutOfBoundsException("i=" + i + ">=" + patterns());
123 throw new ArrayIndexOutOfBoundsException("i=" + i + "< 0");
128 /** Set the Regex at position i in this Transformer. */
129 public void setRegexAt(Regex rx, int i)
133 throw new ArrayIndexOutOfBoundsException("i=" + i + ">=" + patterns());
137 throw new ArrayIndexOutOfBoundsException("i=" + i + "< 0");
143 * Add a new Regex by calling Regex.perlCode
145 * @see com.stevesoft.pat.Regex#perlCode(java.lang.String)
147 public void add(String rs)
149 Regex r = Regex.perlCode(rs);
152 throw new NullPointerException("bad pattern to Regex.perlCode: " + rs);
158 * Add an array of Strings (which will be converted to Regex's via the
159 * Regex.perlCode method.
161 * @see com.stevesoft.pat.Regex#perlCode(java.lang.String)
163 public void add(String[] array)
165 for (int i = 0; i < array.length; i++)
171 /** Replace all matches in the current String. */
172 public String replaceAll(String s)
174 return dorep(s, 0, s.length());
177 public StringLike replaceAll(StringLike s)
179 return dorep(s, 0, s.length());
182 /** Replace all matching patterns beginning at position start. */
183 public String replaceAllFrom(String s, int start)
185 return dorep(s, start, s.length());
189 * Replace all matching patterns beginning between the positions start and end
192 public String replaceAllRegion(String s, int start, int end)
194 return dorep(s, start, end);
197 Replacer repr = new Replacer();
199 final StringLike dorep(StringLike s, int start, int end)
201 StringLike tfmd = repr.replaceAllRegion(s, rp, start, end);
202 tp.lastMatchedTo = repr.lastMatchedTo;
206 final String dorep(String s, int start, int end)
208 return dorep(new StringWrap(s), start, end).toString();
211 /** Replace the first matching pattern in String s. */
212 public String replaceFirst(String s)
214 return dorep(s, 0, s.length());
218 * Replace the first matching pattern after position start in String s.
220 public String replaceFirstFrom(String s, int start)
222 return dorep(s, start, s.length());
226 * Replace the first matching pattern that begins between start and end
229 public String replaceFirstRegion(String s, int start, int end)
231 return dorep(s, start, end);