KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > util > FindReplace


1 /**
2  * This file defines a class which implements the search
3  * and replace functionallity for a text.
4  * @author Dmytro Podalyuk, Matthias Buechler
5  */

6 package org.columba.core.util;
7 import java.util.regex.*;
8
9 public class FindReplace {
10     
11     private Matcher m;
12     private Pattern p;
13     private boolean isFound; // is true if the last search was successful
14
private int lastfoundposition; // a position of the word after the last search ivokation; -1 if nothing found
15
private int lastreplaceposition; // a position of the word after the last replace ivokation; -1 if nothing was replaced
16

17     private StringBuffer JavaDoc sb = null; //intern variable which is used for text replace purposes
18

19     private String JavaDoc pattern; // text which we are looking for, or want to replace
20
private String JavaDoc replaceWith; // replaced text which we want to have
21
private String JavaDoc source; // text where we make a search
22
private boolean casesensitive; // search/replace is case sensitive
23
private boolean matchwholeword; // search/replace matches whole words only
24
private boolean backwards; // search/replace works backwards on the text
25

26     private boolean processing = false; // false if search/replace is currently working
27

28     public FindReplace(){ // constructor for the class
29
pattern = new String JavaDoc();
30         source = new String JavaDoc();
31         replaceWith = new String JavaDoc();
32         casesensitive = false;
33         matchwholeword = false;
34         backwards = false;
35         
36         isFound = false;
37         lastreplaceposition = lastfoundposition = -1;
38         
39         m = null;
40         p = null;
41     }
42
43     public FindReplace(String JavaDoc source, String JavaDoc pattern){ // some other version of the constructor
44
this.pattern = pattern;
45         this.source = source;
46         this.replaceWith = new String JavaDoc();
47         
48         
49         casesensitive = false;
50         matchwholeword = false;
51         backwards = false;
52         
53         isFound = false;
54         lastreplaceposition = lastfoundposition = -1;
55         m = null;
56         p = null;
57     }
58
59     public FindReplace(String JavaDoc source, String JavaDoc pattern, String JavaDoc replaceWith){ // some other version of the constructor
60
this.pattern = pattern;
61         this.source = source;
62         this.replaceWith = replaceWith;
63         casesensitive = false;
64         matchwholeword = false;
65         backwards = false;
66         
67         isFound = false;
68         lastreplaceposition = lastfoundposition = -1;
69         m = null;
70         p = null;
71     }
72     
73     public void setSource(String JavaDoc source){
74         this.source = source;
75     }
76     
77     public String JavaDoc getSource(){
78         return this.source;
79     }
80
81     public void setPattern(String JavaDoc pattern){
82         this.pattern = pattern;
83     }
84     
85     public String JavaDoc getPattern(){
86         return this.pattern;
87     }
88
89     public void setReplaceWith(String JavaDoc replaceWith){
90         this.replaceWith = replaceWith;
91     }
92     
93     public String JavaDoc getReplaceWith(){
94         return this.replaceWith;
95     }
96     
97     
98     public void setCaseSensitive(boolean state){
99         this.casesensitive = state;
100     }
101     
102     public boolean getCaseSensitive(){
103         return this.casesensitive;
104     }
105     
106     public void setMatchWholeWord(boolean state){
107         this.matchwholeword = state;
108     }
109     
110     public boolean getMatchWholeWord(){
111         return this.matchwholeword;
112     }
113
114     public void setBackwards(boolean state){
115         this.backwards = state;
116         if (state && isFound) {
117             lastfoundposition = source.length() - lastfoundposition;
118             //if (lastreplaceposition != -1) lastreplaceposition = source.length() - lastreplaceposition;
119
}
120             
121     }
122     
123     public boolean getBackwards(){
124         return this.backwards;
125     }
126
127     // need to be called before search/replace to set up all the states we need
128
private void begin() {
129         if (!processing)
130         {
131             String JavaDoc source = this.source;
132             String JavaDoc pattern = this.pattern;
133             
134             // find backwards
135
if(backwards) {
136                 source = new StringBuffer JavaDoc(source).reverse().toString();
137                 pattern = new StringBuffer JavaDoc(pattern).reverse().toString();
138                 replaceWith = new StringBuffer JavaDoc(replaceWith).reverse().toString();
139             }
140
141             // match whole word
142
if(matchwholeword) {
143                 // create pattern
144
pattern = "\\b" + pattern + "\\b";
145             }
146     
147             if(!casesensitive)
148                 p = Pattern.compile(pattern,Pattern.CASE_INSENSITIVE);
149             else
150                 p = Pattern.compile(pattern);
151
152             m = p.matcher(source);
153             processing = true;
154         }
155     }
156     
157     // need to be called after search/replace to make some changes in the states
158
private void end(){
159         if (processing){
160             if(isFound) {
161                 if(backwards) {
162                     lastfoundposition = source.length() - m.end();
163                     //lastreplaceposition = lastfoundposition;
164

165                     replaceWith = new StringBuffer JavaDoc(replaceWith).reverse().toString();
166
167                     if (sb != null) {
168                         source = sb.reverse().toString();
169                         sb = null;
170                     }
171                     else
172                         source = new StringBuffer JavaDoc(m.toString()).reverse().toString();
173     
174                 }
175                 else {
176                     lastfoundposition = m.start();
177                     //lastreplaceposition = lastfoundposition; //+ replaceWith.length();
178

179                     if (sb != null) {
180                         source = sb.toString();
181                         sb = null;
182                     }
183                     else
184                         source = m.toString();
185                 }
186                     
187             }
188             else
189                 lastfoundposition = -1;
190             processing = false;
191         }
192     }
193     
194     // the implementation of the search function. It returns always the position of the FIRST found word.
195
public int find() {
196         begin();
197             isFound = m.find(0);
198         end();
199         return lastfoundposition;
200     }
201     
202     // the implementation of the search next function. It returns always the position of the NEXT found word.
203
public int findnext() {
204         // end of text was reached -> begin at the start
205
begin();
206             if(!isFound)
207                 find();
208             else
209                 isFound = m.find(backwards?lastfoundposition:lastfoundposition+1);
210         end();
211         lastreplaceposition = -1;
212         return lastfoundposition;
213     }
214     
215     // the implementation of the replace function. It returns always the position of the NEXT replaced word.
216
public int replace(){
217         begin();
218         //if (lastfoundposition == -1 || lastreplaceposition == -1) {lastreplaceposition = lastfoundposition = 0;}
219

220         if (lastfoundposition == -1) lastfoundposition = 0;
221         
222         if (lastreplaceposition == -1 && backwards) {
223             lastreplaceposition = lastfoundposition - pattern.length();
224             if (lastreplaceposition <0) lastreplaceposition = 0;
225         }
226         
227         if (lastreplaceposition == -1 && !backwards) lastreplaceposition = lastfoundposition;
228         if (lastreplaceposition == -2 && backwards) lastreplaceposition = lastfoundposition;
229         if (lastreplaceposition == -2 && !backwards) lastreplaceposition = lastfoundposition+replaceWith.length();
230         
231         isFound = m.find(lastreplaceposition);
232         if (isFound) {
233             sb = new StringBuffer JavaDoc();
234              
235             m.appendReplacement(sb, replaceWith);
236             m.appendTail(sb);
237             lastreplaceposition = -2;
238         }
239         else
240             lastreplaceposition = -1;
241         
242         end();
243         return lastfoundposition;
244     }
245     
246     // the implementation of the replace all function.
247
public void replaceAll(){
248         begin();
249             String JavaDoc source = m.replaceAll(replaceWith);
250             if (source != null)
251                 if (backwards) this.source = new StringBuffer JavaDoc(source).reverse().toString();
252                 else
253                     this.source = source;
254         isFound = false;
255         end();
256     }
257 }
258
Popular Tags