1 18 19 package org.objectweb.jonas.ant.jonasbase; 20 21 import java.io.BufferedReader ; 22 import java.io.BufferedWriter ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileOutputStream ; 27 import java.io.FileReader ; 28 import java.io.FileWriter ; 29 import java.io.IOException ; 30 import java.io.InputStreamReader ; 31 import java.io.OutputStreamWriter ; 32 import java.io.Reader ; 33 import java.io.Writer ; 34 import java.util.Enumeration ; 35 import java.util.Properties ; 36 import java.util.Vector ; 37 import org.apache.tools.ant.BuildException; 38 import org.apache.tools.ant.DirectoryScanner; 39 import org.apache.tools.ant.Project; 40 import org.apache.tools.ant.taskdefs.Delete; 41 import org.apache.tools.ant.taskdefs.MatchingTask; 42 import org.apache.tools.ant.taskdefs.Move; 43 import org.apache.tools.ant.util.FileUtils; 44 import org.apache.tools.ant.util.StringUtils; 45 46 57 public class Replace extends MatchingTask { 58 59 private File src = null; 60 private NestedString token = null; 61 private NestedString value = new NestedString(); 62 63 private File propertyFile = null; 64 private File replaceFilterFile = null; 65 private Properties properties = null; 66 private Vector replacefilters = new Vector (); 67 68 private File dir = null; 69 70 private int fileCount; 71 private int replaceCount; 72 private boolean summary = false; 73 74 75 private String encoding = null; 76 77 private FileUtils fileUtils = FileUtils.newFileUtils(); 78 79 82 public class NestedString { 83 84 private StringBuffer buf = new StringBuffer (); 85 86 91 public void addText(String val) { 92 buf.append(val); 93 } 94 95 98 public String getText() { 99 return buf.substring(0); 100 } 101 } 102 103 106 public class Replacefilter { 107 private String token; 108 private String value; 109 private String property; 110 111 115 public void validate() throws BuildException { 116 if (token == null) { 118 String message = "token is a mandatory attribute " 119 + "of replacefilter."; 120 throw new BuildException(message); 121 } 122 123 if ("".equals(token)) { 124 String message = "The token attribute must not be an empty " 125 + "string."; 126 throw new BuildException(message); 127 } 128 129 if ((value != null) && (property != null)) { 131 String message = "Either value or property " 132 + "can be specified, but a replacefilter " 133 + "element cannot have both."; 134 throw new BuildException(message); 135 } 136 137 if ((property != null)) { 138 if (propertyFile == null) { 140 String message = "The replacefilter's property attribute " 141 + "can only be used with the replacetask's " 142 + "propertyFile attribute."; 143 throw new BuildException(message); 144 } 145 146 if (properties == null 148 || properties.getProperty(property) == null) { 149 String message = "property \"" + property 150 + "\" was not found in " + propertyFile.getPath(); 151 throw new BuildException(message); 152 } 153 } 154 } 155 156 160 public String getReplaceValue() { 161 if (property != null) { 162 return properties.getProperty(property); 163 } else if (value != null) { 164 return value; 165 } else if (Replace.this.value != null) { 166 return Replace.this.value.getText(); 167 } else { 168 return new String (""); 170 } 171 } 172 173 177 public void setToken(String token) { 178 this.token = token; 179 } 180 181 185 public String getToken() { 186 return token; 187 } 188 189 194 public void setValue(String value) { 195 this.value = value; 196 } 197 198 202 public String getValue() { 203 return value; 204 } 205 206 211 public void setProperty(String property) { 212 this.property = property; 213 } 214 215 220 public String getProperty() { 221 return property; 222 } 223 } 224 225 229 public void execute() throws BuildException { 230 231 Vector savedFilters = (Vector ) replacefilters.clone(); 232 Properties savedProperties = 233 properties == null ? null : (Properties ) properties.clone(); 234 235 try { 236 if (replaceFilterFile != null) { 237 Properties props = getProperties(replaceFilterFile); 238 Enumeration e = props.keys(); 239 while (e.hasMoreElements()) { 240 String token = e.nextElement().toString(); 241 Replacefilter replaceFilter = createReplacefilter(); 242 replaceFilter.setToken(token); 243 replaceFilter.setValue(props.getProperty(token)); 244 } 245 } 246 247 validateAttributes(); 248 249 if (propertyFile != null) { 250 properties = getProperties(propertyFile); 251 } 252 253 validateReplacefilters(); 254 fileCount = 0; 255 replaceCount = 0; 256 257 if (src != null) { 258 processFile(src); 259 } 260 261 if (dir != null) { 262 DirectoryScanner ds = super.getDirectoryScanner(dir); 263 String [] srcs = ds.getIncludedFiles(); 264 265 for (int i = 0; i < srcs.length; i++) { 266 File file = new File (dir, srcs[i]); 267 processFile(file); 268 } 269 } 270 271 if (summary) { 272 log("Replaced " + replaceCount + " occurrences in " 273 + fileCount + " files.", Project.MSG_INFO); 274 } 275 } finally { 276 replacefilters = savedFilters; 277 properties = savedProperties; 278 } 280 } 281 282 288 public void validateAttributes() throws BuildException { 289 if (src == null && dir == null) { 290 String message = "Either the file or the dir attribute " 291 + "must be specified"; 292 throw new BuildException(message, getLocation()); 293 } 294 if (propertyFile != null && !propertyFile.exists()) { 295 String message = "Property file " + propertyFile.getPath() 296 + " does not exist."; 297 throw new BuildException(message, getLocation()); 298 } 299 if (token == null && replacefilters.size() == 0) { 300 String message = "Either token or a nested replacefilter " 301 + "must be specified"; 302 throw new BuildException(message, getLocation()); 303 } 304 if (token != null && "".equals(token.getText())) { 305 String message = "The token attribute must not be an empty string."; 306 throw new BuildException(message, getLocation()); 307 } 308 } 309 310 316 public void validateReplacefilters() 317 throws BuildException { 318 for (int i = 0; i < replacefilters.size(); i++) { 319 Replacefilter element = 320 (Replacefilter) replacefilters.elementAt(i); 321 element.validate(); 322 } 323 } 324 325 332 public Properties getProperties(File propertyFile) throws BuildException { 333 Properties properties = new Properties (); 334 335 FileInputStream in = null; 336 try { 337 in = new FileInputStream (propertyFile); 338 properties.load(in); 339 } catch (FileNotFoundException e) { 340 String message = "Property file (" + propertyFile.getPath() 341 + ") not found."; 342 throw new BuildException(message); 343 } catch (IOException e) { 344 String message = "Property file (" + propertyFile.getPath() 345 + ") cannot be loaded."; 346 throw new BuildException(message); 347 } finally { 348 if (in != null) { 349 try { 350 in.close(); 351 } catch (IOException e) { 352 } 354 } 355 } 356 357 return properties; 358 } 359 360 371 private void processFile(File src) throws BuildException { 372 if (!src.exists()) { 373 throw new BuildException("Replace: source file " + src.getPath() 374 + " doesn't exist", getLocation()); 375 } 376 377 File temp = fileUtils.createTempFile("rep", ".tmp", 378 fileUtils.getParentFile(src)); 379 temp.deleteOnExit(); 380 381 Reader reader = null; 382 Writer writer = null; 383 try { 384 reader = encoding == null ? new FileReader (src) 385 : new InputStreamReader (new FileInputStream (src), encoding); 386 writer = encoding == null ? new FileWriter (temp) 387 : new OutputStreamWriter (new FileOutputStream (temp), encoding); 388 389 BufferedReader br = new BufferedReader (reader); 390 BufferedWriter bw = new BufferedWriter (writer); 391 392 String buf = fileUtils.readFully(br); 393 if (buf == null) { 394 buf = ""; 395 } 396 397 String newString = new String (buf); 399 400 if (token != null) { 401 String val = stringReplace(value.getText(), "\r\n", 405 "\n", false); 406 val = stringReplace(val, "\n", 407 StringUtils.LINE_SEP, false); 408 String tok = stringReplace(token.getText(), "\r\n", 409 "\n", false); 410 tok = stringReplace(tok, "\n", 411 StringUtils.LINE_SEP, false); 412 413 log("Replacing in " + src.getPath() + ": " + token.getText() 415 + " --> " + value.getText(), Project.MSG_VERBOSE); 416 newString = stringReplace(newString, tok, val, true); 417 } 418 419 if (replacefilters.size() > 0) { 420 newString = processReplacefilters(newString, src.getPath()); 421 } 422 423 boolean changes = !newString.equals(buf); 424 if (changes) { 425 bw.write(newString, 0, newString.length()); 426 bw.flush(); 427 } 428 429 bw.close(); 431 writer = null; 432 br.close(); 433 reader = null; 434 435 if (changes) { 438 ++fileCount; 439 440 444 Project tempProject = new Project(); 446 Delete delete = new Delete(); 447 delete.setProject(tempProject); 448 delete.setFile(src); 449 delete.execute(); 450 Move mv =new Move(); 451 mv.setProject(tempProject); 452 mv.setFile(temp); 453 mv.setTofile(src); 454 mv.execute(); 455 457 temp = null; 458 } 459 } catch (IOException ioe) { 460 throw new BuildException("IOException in " + src + " - " 461 + ioe.getClass().getName() + ":" 462 + ioe.getMessage(), ioe, getLocation()); 463 } finally { 464 if (reader != null) { 465 try { 466 reader.close(); 467 } catch (IOException e) { 468 } 470 } 471 if (writer != null) { 472 try { 473 writer.close(); 474 } catch (IOException e) { 475 } 477 } 478 if (temp != null) { 479 temp.delete(); 480 } 481 } 482 483 } 484 485 491 private String processReplacefilters(String buffer, String filename) { 492 String newString = new String (buffer); 493 494 for (int i = 0; i < replacefilters.size(); i++) { 495 Replacefilter filter = (Replacefilter) replacefilters.elementAt(i); 496 497 log("Replacing in " + filename + ": " + filter.getToken() 499 + " --> " + filter.getReplaceValue(), Project.MSG_VERBOSE); 500 newString = stringReplace(newString, filter.getToken(), 501 filter.getReplaceValue(), true); 502 } 503 504 return newString; 505 } 506 507 508 512 public void setFile(File file) { 513 this.src = file; 514 } 515 516 524 public void setSummary(boolean summary) { 525 this.summary = summary; 526 } 527 528 529 536 public void setReplaceFilterFile(File filename) { 537 replaceFilterFile = filename; 538 } 539 540 545 public void setDir(File dir) { 546 this.dir = dir; 547 } 548 549 556 public void setToken(String token) { 557 createReplaceToken().addText(token); 558 } 559 560 565 public void setValue(String value) { 566 createReplaceValue().addText(value); 567 } 568 569 575 public void setEncoding(String encoding) { 576 this.encoding = encoding; 577 } 578 579 583 public NestedString createReplaceToken() { 584 if (token == null) { 585 token = new NestedString(); 586 } 587 return token; 588 } 589 590 594 public NestedString createReplaceValue() { 595 return value; 596 } 597 598 605 public void setPropertyFile(File filename) { 606 propertyFile = filename; 607 } 608 609 613 public Replacefilter createReplacefilter() { 614 Replacefilter filter = new Replacefilter(); 615 replacefilters.addElement(filter); 616 return filter; 617 } 618 619 622 private String stringReplace(String str, String str1, String str2, 623 boolean countReplaces) { 624 StringBuffer ret = new StringBuffer (); 625 int start = 0; 626 int found = str.indexOf(str1); 627 while (found >= 0) { 628 if (found > start) { 630 ret.append(str.substring(start, found)); 631 } 632 633 if (str2 != null) { 635 ret.append(str2); 636 } 637 638 start = found + str1.length(); 640 found = str.indexOf(str1, start); 641 if (countReplaces) { 642 ++replaceCount; 643 } 644 } 645 646 if (str.length() > start) { 648 ret.append(str.substring(start, str.length())); 649 } 650 651 return ret.toString(); 652 } 653 654 } 655 656 | Popular Tags |