1 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; private int lastfoundposition; private int lastreplaceposition; 17 private StringBuffer sb = null; 19 private String pattern; private String replaceWith; private String source; private boolean casesensitive; private boolean matchwholeword; private boolean backwards; 26 private boolean processing = false; 28 public FindReplace(){ pattern = new String (); 30 source = new String (); 31 replaceWith = new String (); 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 source, String pattern){ this.pattern = pattern; 45 this.source = source; 46 this.replaceWith = new String (); 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 source, String pattern, String replaceWith){ 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 source){ 74 this.source = source; 75 } 76 77 public String getSource(){ 78 return this.source; 79 } 80 81 public void setPattern(String pattern){ 82 this.pattern = pattern; 83 } 84 85 public String getPattern(){ 86 return this.pattern; 87 } 88 89 public void setReplaceWith(String replaceWith){ 90 this.replaceWith = replaceWith; 91 } 92 93 public String 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 } 120 121 } 122 123 public boolean getBackwards(){ 124 return this.backwards; 125 } 126 127 private void begin() { 129 if (!processing) 130 { 131 String source = this.source; 132 String pattern = this.pattern; 133 134 if(backwards) { 136 source = new StringBuffer (source).reverse().toString(); 137 pattern = new StringBuffer (pattern).reverse().toString(); 138 replaceWith = new StringBuffer (replaceWith).reverse().toString(); 139 } 140 141 if(matchwholeword) { 143 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 private void end(){ 159 if (processing){ 160 if(isFound) { 161 if(backwards) { 162 lastfoundposition = source.length() - m.end(); 163 165 replaceWith = new StringBuffer (replaceWith).reverse().toString(); 166 167 if (sb != null) { 168 source = sb.reverse().toString(); 169 sb = null; 170 } 171 else 172 source = new StringBuffer (m.toString()).reverse().toString(); 173 174 } 175 else { 176 lastfoundposition = m.start(); 177 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 public int find() { 196 begin(); 197 isFound = m.find(0); 198 end(); 199 return lastfoundposition; 200 } 201 202 public int findnext() { 204 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 public int replace(){ 217 begin(); 218 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 (); 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 public void replaceAll(){ 248 begin(); 249 String source = m.replaceAll(replaceWith); 250 if (source != null) 251 if (backwards) this.source = new StringBuffer (source).reverse().toString(); 252 else 253 this.source = source; 254 isFound = false; 255 end(); 256 } 257 } 258 | Popular Tags |