// // This software is now distributed according to // the Lesser Gnu Public License. Please see // http://www.gnu.org/copyleft/lesser.txt for // the details. // -- Happy Computing! // package com.stevesoft.pat; import com.stevesoft.pat.wrap.StringWrap; /** Replacement rule used by the Transformer. @see com.stevesoft.pat.Transformer */ class TransRepRule extends ReplaceRule { Transformer t; TransRepRule(Transformer t) { this.t = t; } public String toString1() { return ""; } public Object clone1() { return new TransRepRule(t); } public void apply(StringBufferLike sb,RegRes rr) { // get the ReplaceRule of the Regex that matched. next = t.tp.ra[t.tp.pn].getReplaceRule(); } } /** Sometimes you want to replace a whole bunch of things that might occur within a single line of text. One efficient way to do this, both in terms of performance and programming ease, is with Transformer. The Transformer contains an array of Regex's and uses the Regex that matches earliest within the text to do the replacing, if two Regex's match at the same time it uses the one put in the Transformer first.
This feature can be used to prevent transformations from occurring in certain regions. For example, if I add the rule s'//.*'$&' and then add the rule s/hello/goodbye/ the Transformer will replace "hello" with "goodbye" except when it occurs inside a double-slash style of comment. The transformation on the comment goes first, does nothing, and precludes transformation on the same region of text as the s/hello/goodbye/ rule.
So far, at least, this class does not have the capability of
turning into a giant robot :-)
*/
public class Transformer {
TransPat tp;
Regex rp = new Regex();
boolean auto_optimize;
/** Get a replacer to that works with the current Regex.
@see com.stevesoft.pat.Replacer
*/
public Replacer getReplacer() { return rp.getReplacer(); }
/** Instantiate a new Transformer object. */
public Transformer(boolean auto) {
auto_optimize = auto;
tp = new TransPat();
rp.setReplaceRule(new TransRepRule(this));
rp.thePattern = tp;
}
/** Add a new Regex to the set of Regex's. */
public void add(Regex r) {
if(auto_optimize) r.optimize();
tp.ra[tp.ra_len++] = r;
if(tp.ra.length==tp.ra_len) {
Regex[] ra2 = new Regex[tp.ra_len+10];
for(int i=0;i