1 19 20 package org.netbeans.lexer.gen; 21 22 import java.io.File ; 23 import java.io.FileWriter ; 24 import java.io.IOException ; 25 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.Task; 28 29 34 public class StringReplace extends Task { 35 36 private String replaceWhat; 37 38 private String replaceWith; 39 40 private String replaceIn; 41 42 private String property; 43 44 public StringReplace() { 45 } 46 47 public String getReplaceWhat() { 48 return replaceWhat; 49 } 50 51 public void setReplaceWhat(String replaceWhat) { 52 this.replaceWhat = replaceWhat; 53 } 54 55 public String getReplaceWith() { 56 return replaceWith; 57 } 58 59 public void setReplaceWith(String replaceWith) { 60 this.replaceWith = replaceWith; 61 } 62 63 public String getReplaceIn() { 64 return replaceIn; 65 } 66 67 public void setReplaceIn(String replaceIn) { 68 this.replaceIn = replaceIn; 69 } 70 71 public String getProperty() { 72 return property; 73 } 74 75 public void setProperty(String property) { 76 this.property = property; 77 } 78 79 public void execute() throws BuildException { 80 if (getReplaceWhat() == null || "".equals(getReplaceWhat())) { 81 throw new BuildException("replaceWhat attribute must be set"); 82 } 83 if (getProperty() == null || "".equals(getProperty())) { 84 throw new BuildException("property attribute must be set"); 85 } 86 87 String output = getReplaceIn(); 88 int startIndex = 0; 89 while (true) { 90 int foundIndex = output.indexOf(getReplaceWhat(), startIndex); 91 if (foundIndex == -1) { 92 break; 93 } 94 95 output = output.substring(0, foundIndex) 96 + getReplaceWith() 97 + output.substring(foundIndex + getReplaceWhat().length()); 98 99 foundIndex += getReplaceWith().length(); 100 } 101 102 getOwningTarget().getProject().setProperty(getProperty(), output); 104 } 105 106 } 107 | Popular Tags |