source: release-kits/shared/ant-tasks/orans/RegexSearchReplace.java@ 16532

Last change on this file since 16532 was 16532, checked in by oranfry, 16 years ago

made the rsr task safer

File size: 2.7 KB
Line 
1import org.apache.tools.ant.*;
2import org.apache.tools.ant.taskdefs.*;
3import java.util.*;
4import java.io.*;
5import java.util.regex.*;
6
7
8public class RegexSearchReplace extends Task {
9 private File file = null;
10 private String pattern = null;
11 private String replacement = null;
12
13
14 /**
15 * for testing
16 */
17 public static void main( String[] args ) {
18
19 RegexSearchReplace rsr = new RegexSearchReplace();
20 rsr.setFile(new File (args[0]));
21 rsr.setPattern(args[1]);
22 rsr.setReplacement(args[2]);
23
24 rsr.execute();
25
26 }
27
28 public void execute() {
29
30 if ( file == null ) {
31 throw new BuildException( "Error - No file specified !!" );
32 }
33
34 if ( pattern == null ) {
35 throw new BuildException( "Error - No pattern specified !!" );
36 }
37
38 if (replacement == null) {
39 throw new BuildException( "Error - No replacement specified !!" );
40 }
41
42
43 //create the output stream
44 BufferedWriter out = null;
45 File temp = null;
46 try {
47 //create temp file.
48 temp = File.createTempFile("rsr", ".tmp");
49
50 //delete temp file when program exits.
51 temp.deleteOnExit();
52
53 //writer to temp file
54 out = new BufferedWriter(new FileWriter(temp));
55
56 } catch (IOException e) {
57 throw new BuildException( "Error - Couldn't create the temp file" );
58 }
59
60
61 //create the input stream
62 BufferedReader in = null;
63 try {
64 in = new BufferedReader(new FileReader( file ));
65 } catch ( Exception e ) {
66 throw new BuildException( "Error - Couldn't open the specified file" );
67 }
68
69 //pass the file through, searching and replacing
70 String line;
71 try {
72 while ( (line = in.readLine()) != null ) {
73 line = line.replaceAll( pattern, replacement);
74 out.write(line);
75 out.newLine();
76 }
77 } catch ( Exception e ) {
78 throw new BuildException( "Error - Couldn't read from the specified file" );
79 }
80
81 try {
82
83 //close them both up
84 in.close();
85 out.close();
86
87 } catch ( Exception e ) {
88 throw new BuildException( "Error - Couldn't close a file" );
89 }
90
91
92 //copy the new file (temp) over the original
93 InputStream i;
94 OutputStream o;
95 try {
96 i = new FileInputStream(temp);
97 o = new FileOutputStream(file);
98
99 } catch ( Exception e ) {
100 throw new BuildException( "Error - Couldn't open the temp file" );
101 }
102
103 try {
104
105 // Transfer bytes from in to out
106 byte[] buf = new byte[1024];
107 int len;
108 while ((len = i.read(buf)) > 0) {
109 o.write(buf, 0, len);
110 }
111
112 //close them up
113 i.close();
114 o.close();
115 } catch ( Exception e ) {
116 throw new BuildException( "Error - Couldn't write to the specified file" );
117 }
118
119 }
120
121
122 public void setFile(File file) {
123 this.file = file;
124 }
125
126 public void setPattern(String pattern) {
127 this.pattern = pattern;
128 }
129
130 public void setReplacement(String replacement) {
131 this.replacement = replacement.replaceAll( "\\\\", "\\\\\\\\" );
132 }
133
134
135}
Note: See TracBrowser for help on using the repository browser.