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.StringWrap;
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
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)
82 rp = new Regex(null, "");
85 rp.setReplaceRule(new TransRepRule(this));
89 /** Add a new Regex to the set of Regex's. */
90 public void add(Regex r)
96 tp.ra[tp.ra_len++] = r;
97 if (tp.ra.length == tp.ra_len)
99 Regex[] ra2 = new Regex[tp.ra_len + 10];
100 for (int i = 0; i < tp.ra_len; i++)
106 rp.numSubs_ = r.numSubs_ > rp.numSubs_ ? r.numSubs_ : rp.numSubs_;
109 /** Returns the number of Regex's in this Transformer. */
110 public int patterns()
115 /** Get the Regex at position i in this Transformer. */
116 public Regex getRegexAt(int i)
120 throw new ArrayIndexOutOfBoundsException("i=" + i + ">=" + patterns());
124 throw new ArrayIndexOutOfBoundsException("i=" + i + "< 0");
129 /** Set the Regex at position i in this Transformer. */
130 public void setRegexAt(Regex rx, int i)
134 throw new ArrayIndexOutOfBoundsException("i=" + i + ">=" + patterns());
138 throw new ArrayIndexOutOfBoundsException("i=" + i + "< 0");
144 * Add a new Regex by calling Regex.perlCode
146 * @see com.stevesoft.pat.Regex#perlCode(java.lang.String)
148 public void add(String rs)
150 Regex r = Regex.perlCode(rs);
153 throw new NullPointerException(MessageManager.formatMessage("exception.bad_pattern_to_regex_perl_code", new String[]{rs}));
159 * Add an array of Strings (which will be converted to Regex's via the
160 * Regex.perlCode method.
162 * @see com.stevesoft.pat.Regex#perlCode(java.lang.String)
164 public void add(String[] array)
166 for (int i = 0; i < array.length; i++)
172 /** Replace all matches in the current String. */
173 public String replaceAll(String s)
175 return dorep(s, 0, s.length());
178 public StringLike replaceAll(StringLike s)
180 return dorep(s, 0, s.length());
183 /** Replace all matching patterns beginning at position start. */
184 public String replaceAllFrom(String s, int start)
186 return dorep(s, start, s.length());
190 * Replace all matching patterns beginning between the positions start and end
193 public String replaceAllRegion(String s, int start, int end)
195 return dorep(s, start, end);
198 Replacer repr = new Replacer();
200 final StringLike dorep(StringLike s, int start, int end)
202 StringLike tfmd = repr.replaceAllRegion(s, rp, start, end);
203 tp.lastMatchedTo = repr.lastMatchedTo;
207 final String dorep(String s, int start, int end)
209 return dorep(new StringWrap(s), start, end).toString();
212 /** Replace the first matching pattern in String s. */
213 public String replaceFirst(String s)
215 return dorep(s, 0, s.length());
219 * Replace the first matching pattern after position start in String s.
221 public String replaceFirstFrom(String s, int start)
223 return dorep(s, start, s.length());
227 * Replace the first matching pattern that begins between start and end
230 public String replaceFirstRegion(String s, int start, int end)
232 return dorep(s, start, end);