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 jalview.util.MessageManager;
12 import com.stevesoft.pat.wrap.StringWrap;
15 * Replacement rule used by the Transformer.
17 * @see com.stevesoft.pat.Transformer
19 class TransRepRule extends ReplaceRule
23 TransRepRule(Transformer t)
28 public String toString1()
33 public Object clone1()
35 return new TransRepRule(t);
38 public void apply(StringBufferLike sb, RegRes rr)
40 // get the ReplaceRule of the Regex that matched.
41 next = t.tp.ra[t.tp.pn].getReplaceRule();
46 * Sometimes you want to replace a whole bunch of things that might occur within
47 * a single line of text. One efficient way to do this, both in terms of
48 * performance and programming ease, is with Transformer. The Transformer
49 * contains an array of Regex's and uses the Regex that matches earliest within
50 * the text to do the replacing, if two Regex's match at the same time it uses
51 * the one put in the Transformer first.
53 * This feature can be used to prevent transformations from occurring in certain
54 * regions. For example, if I add the rule s'//.*'$&' and then add the rule
55 * s/hello/goodbye/ the Transformer will replace "hello" with "goodbye" except
56 * when it occurs inside a double-slash style of comment. The transformation on
57 * the comment goes first, does nothing, and precludes transformation on the
58 * same region of text as the s/hello/goodbye/ rule.
60 * So far, at least, this class does not have the capability of turning into a
63 public class Transformer
67 Regex rp = new Regex();
69 boolean auto_optimize;
72 * Get a replacer to that works with the current Regex.
74 * @see com.stevesoft.pat.Replacer
76 public Replacer getReplacer()
78 return rp.getReplacer();
81 /** Instantiate a new Transformer object. */
82 public Transformer(boolean auto)
86 rp.setReplaceRule(new TransRepRule(this));
90 /** Add a new Regex to the set of Regex's. */
91 public void add(Regex r)
97 tp.ra[tp.ra_len++] = r;
98 if (tp.ra.length == tp.ra_len)
100 Regex[] ra2 = new Regex[tp.ra_len + 10];
101 for (int i = 0; i < tp.ra_len; i++)
107 rp.numSubs_ = r.numSubs_ > rp.numSubs_ ? r.numSubs_ : rp.numSubs_;
110 /** Returns the number of Regex's in this Transformer. */
111 public int patterns()
116 /** Get the Regex at position i in this Transformer. */
117 public Regex getRegexAt(int i)
121 throw new ArrayIndexOutOfBoundsException("i=" + i + ">=" + patterns());
125 throw new ArrayIndexOutOfBoundsException("i=" + i + "< 0");
130 /** Set the Regex at position i in this Transformer. */
131 public void setRegexAt(Regex rx, int i)
135 throw new ArrayIndexOutOfBoundsException("i=" + i + ">=" + patterns());
139 throw new ArrayIndexOutOfBoundsException("i=" + i + "< 0");
145 * Add a new Regex by calling Regex.perlCode
147 * @see com.stevesoft.pat.Regex#perlCode(java.lang.String)
149 public void add(String rs)
151 Regex r = Regex.perlCode(rs);
154 throw new NullPointerException(MessageManager.formatMessage(
155 "exception.bad_pattern_to_regex_perl_code",
156 new String[] { rs }));
162 * Add an array of Strings (which will be converted to Regex's via the
163 * Regex.perlCode method.
165 * @see com.stevesoft.pat.Regex#perlCode(java.lang.String)
167 public void add(String[] array)
169 for (int i = 0; i < array.length; i++)
175 /** Replace all matches in the current String. */
176 public String replaceAll(String s)
178 return dorep(s, 0, s.length());
181 public StringLike replaceAll(StringLike s)
183 return dorep(s, 0, s.length());
186 /** Replace all matching patterns beginning at position start. */
187 public String replaceAllFrom(String s, int start)
189 return dorep(s, start, s.length());
193 * Replace all matching patterns beginning between the positions start and end
196 public String replaceAllRegion(String s, int start, int end)
198 return dorep(s, start, end);
201 Replacer repr = new Replacer();
203 final StringLike dorep(StringLike s, int start, int end)
205 StringLike tfmd = repr.replaceAllRegion(s, rp, start, end);
206 tp.lastMatchedTo = repr.lastMatchedTo;
210 final String dorep(String s, int start, int end)
212 return dorep(new StringWrap(s), start, end).toString();
215 /** Replace the first matching pattern in String s. */
216 public String replaceFirst(String s)
218 return dorep(s, 0, s.length());
222 * Replace the first matching pattern after position start in String s.
224 public String replaceFirstFrom(String s, int start)
226 return dorep(s, start, s.length());
230 * Replace the first matching pattern that begins between start and end
233 public String replaceFirstRegion(String s, int start, int end)
235 return dorep(s, start, end);