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.StringWrap;
\r
12 /** Replacement rule used by the Transformer.
\r
13 @see com.stevesoft.pat.Transformer
\r
15 class TransRepRule extends ReplaceRule {
\r
17 TransRepRule(Transformer t) { this.t = t; }
\r
18 public String toString1() { return ""; }
\r
19 public Object clone1() { return new TransRepRule(t); }
\r
20 public void apply(StringBufferLike sb,RegRes rr) {
\r
21 // get the ReplaceRule of the Regex that matched.
\r
22 next = t.tp.ra[t.tp.pn].getReplaceRule();
\r
26 /** Sometimes you want to replace a whole bunch of things
\r
27 that might occur within a single line of text. One efficient
\r
28 way to do this, both in terms of performance and programming
\r
29 ease, is with Transformer. The Transformer contains an array
\r
30 of Regex's and uses the Regex that matches earliest within the
\r
31 text to do the replacing, if two Regex's match at the same
\r
32 time it uses the one put in the Transformer first.
\r
34 This feature can be used to prevent transformations from
\r
35 occurring in certain regions. For example, if I add the rule
\r
36 s'//.*'$&' and then add the
\r
37 rule s/hello/goodbye/ the Transformer will replace "hello"
\r
38 with "goodbye" except when it occurs inside a double-slash
\r
39 style of comment. The transformation on the comment goes first,
\r
40 does nothing, and precludes transformation on the same region
\r
41 of text as the s/hello/goodbye/ rule.
\r
43 So far, at least, this class does not have the capability of
\r
44 turning into a giant robot :-)
\r
46 public class Transformer {
\r
48 Regex rp = new Regex();
\r
49 boolean auto_optimize;
\r
51 /** Get a replacer to that works with the current Regex.
\r
52 @see com.stevesoft.pat.Replacer
\r
54 public Replacer getReplacer() { return rp.getReplacer(); }
\r
56 /** Instantiate a new Transformer object. */
\r
57 public Transformer(boolean auto) {
\r
58 auto_optimize = auto;
\r
59 tp = new TransPat();
\r
60 rp.setReplaceRule(new TransRepRule(this));
\r
64 /** Add a new Regex to the set of Regex's. */
\r
65 public void add(Regex r) {
\r
66 if(auto_optimize) r.optimize();
\r
67 tp.ra[tp.ra_len++] = r;
\r
68 if(tp.ra.length==tp.ra_len) {
\r
69 Regex[] ra2 = new Regex[tp.ra_len+10];
\r
70 for(int i=0;i<tp.ra_len;i++)
\r
74 rp.numSubs_ = r.numSubs_ > rp.numSubs_ ? r.numSubs_ : rp.numSubs_;
\r
77 /** Returns the number of Regex's in this Transformer. */
\r
78 public int patterns() { return tp.ra_len; }
\r
80 /** Get the Regex at position i in this Transformer. */
\r
81 public Regex getRegexAt(int i) {
\r
83 throw new ArrayIndexOutOfBoundsException("i="+i+">="+patterns());
\r
85 throw new ArrayIndexOutOfBoundsException("i="+i+"< 0");
\r
88 /** Set the Regex at position i in this Transformer. */
\r
89 public void setRegexAt(Regex rx,int i) {
\r
91 throw new ArrayIndexOutOfBoundsException("i="+i+">="+patterns());
\r
93 throw new ArrayIndexOutOfBoundsException("i="+i+"< 0");
\r
97 /** Add a new Regex by calling Regex.perlCode
\r
98 @see com.stevesoft.pat.Regex#perlCode(java.lang.String)
\r
100 public void add(String rs) {
\r
101 Regex r = Regex.perlCode(rs);
\r
102 if(r == null) throw new NullPointerException("bad pattern to Regex.perlCode: "+rs);
\r
105 /** Add an array of Strings (which will be converted to
\r
106 Regex's via the Regex.perlCode method.
\r
107 @see com.stevesoft.pat.Regex#perlCode(java.lang.String)
\r
109 public void add(String[] array) {
\r
110 for(int i=0;i<array.length;i++)
\r
113 /** Replace all matches in the current String. */
\r
114 public String replaceAll(String s) {
\r
115 return dorep(s,0,s.length());
\r
117 public StringLike replaceAll(StringLike s) {
\r
118 return dorep(s,0,s.length());
\r
120 /** Replace all matching patterns beginning at position start. */
\r
121 public String replaceAllFrom(String s,int start) {
\r
122 return dorep(s,start,s.length());
\r
124 /** Replace all matching patterns beginning between the positions
\r
125 start and end inclusive. */
\r
126 public String replaceAllRegion(String s,int start,int end) {
\r
127 return dorep(s,start,end);
\r
130 Replacer repr = new Replacer();
\r
131 final StringLike dorep(StringLike s,int start,int end) {
\r
132 StringLike tfmd = repr.replaceAllRegion(s,rp,start,end);
\r
133 tp.lastMatchedTo = repr.lastMatchedTo;
\r
136 final String dorep(String s,int start,int end) {
\r
137 return dorep(new StringWrap(s),start,end).toString();
\r
140 /** Replace the first matching pattern in String s. */
\r
141 public String replaceFirst(String s) {
\r
142 return dorep(s,0,s.length());
\r
144 /** Replace the first matching pattern after position start in
\r
146 public String replaceFirstFrom(String s,int start) {
\r
147 return dorep(s,start,s.length());
\r
149 /** Replace the first matching pattern that begins between
\r
150 start and end inclusive. */
\r
151 public String replaceFirstRegion(String s,int start,int end) {
\r
152 return dorep(s,start,end);
\r