Remove redundancy in Eclipse
[jalview.git] / src / com / stevesoft / pat / Transformer.java
1 //\r
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
5 // the details.\r
6 //    -- Happy Computing!\r
7 //\r
8 package com.stevesoft.pat;\r
9 \r
10 import com.stevesoft.pat.wrap.StringWrap;\r
11 \r
12 /** Replacement rule used by the Transformer.\r
13     @see com.stevesoft.pat.Transformer\r
14     */\r
15 class TransRepRule extends ReplaceRule {\r
16     Transformer t;\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
23     }\r
24 }\r
25 \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
33   <p>\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
42   <p>\r
43   So far, at least, this class does not have the capability of\r
44   turning into a giant robot :-)\r
45   */\r
46 public class Transformer {\r
47     TransPat tp;\r
48     Regex rp = new Regex();\r
49     boolean auto_optimize;\r
50 \r
51     /** Get a replacer to that works with the current Regex.\r
52      @see com.stevesoft.pat.Replacer\r
53      */\r
54     public Replacer getReplacer() { return rp.getReplacer(); }\r
55 \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
61         rp.thePattern = tp;\r
62     }\r
63 \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
71                 ra2[i] = tp.ra[i];\r
72             tp.ra = ra2;\r
73         }\r
74         rp.numSubs_ = r.numSubs_ > rp.numSubs_ ? r.numSubs_ : rp.numSubs_;\r
75     }\r
76 \r
77     /** Returns the number of Regex's in this Transformer. */\r
78     public int patterns() { return tp.ra_len; }\r
79 \r
80     /** Get the Regex at position i in this Transformer. */\r
81     public Regex getRegexAt(int i) {\r
82         if(i >= tp.ra_len)\r
83             throw new ArrayIndexOutOfBoundsException("i="+i+">="+patterns());\r
84         if(i < 0)\r
85             throw new ArrayIndexOutOfBoundsException("i="+i+"< 0");\r
86         return tp.ra[i];\r
87     }\r
88     /** Set the Regex at position i in this Transformer. */\r
89     public void setRegexAt(Regex rx,int i) {\r
90         if(i >= tp.ra_len)\r
91             throw new ArrayIndexOutOfBoundsException("i="+i+">="+patterns());\r
92         if(i < 0)\r
93             throw new ArrayIndexOutOfBoundsException("i="+i+"< 0");\r
94         tp.ra[i] = rx;\r
95     }\r
96 \r
97     /** Add a new Regex by calling Regex.perlCode\r
98         @see com.stevesoft.pat.Regex#perlCode(java.lang.String)\r
99         */\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
103         add(r);\r
104     }\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
108         */\r
109     public void add(String[] array) {\r
110         for(int i=0;i<array.length;i++)\r
111             add(array[i]);\r
112     }\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
116     }\r
117     public StringLike replaceAll(StringLike s) {\r
118         return dorep(s,0,s.length());\r
119     }\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
123     }\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
128     }\r
129 \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
134         return tfmd;\r
135     }\r
136     final String dorep(String s,int start,int end) {\r
137         return dorep(new StringWrap(s),start,end).toString();\r
138     }\r
139 \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
143     }\r
144     /** Replace the first matching pattern after position start in\r
145         String s. */\r
146     public String replaceFirstFrom(String s,int start) {\r
147         return dorep(s,start,s.length());\r
148     }\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
153     }\r
154 }\r