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

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

more specific error messages

File size: 3.4 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 System.out.println( "File: " + file );
43 System.out.println( "Pattern: " + pattern );
44 System.out.println( "Replacement: " + replacement );
45
46 int noReplaces = 0;
47
48 //create the output stream
49 BufferedWriter out = null;
50 File temp = null;
51 try {
52 //create temp file.
53 temp = File.createTempFile("rsr", ".tmp");
54
55 //delete temp file when program exits.
56 temp.deleteOnExit();
57
58 //writer to temp file
59 out = new BufferedWriter(new FileWriter(temp));
60
61 } catch (IOException e) {
62 throw new BuildException( "Error - Couldn't create or open the temp file" );
63 }
64
65
66 //create the input stream
67 BufferedReader in = null;
68 try {
69 in = new BufferedReader( new FileReader( file ) );
70 } catch ( Exception e ) {
71 throw new BuildException( "Error - Couldn't open the specified file" );
72 }
73
74 //pass the file through, searching and replacing
75 String line;
76 boolean hasMoreLines = true;
77 while ( hasMoreLines ) {
78
79 try {
80 line = in.readLine();
81 } catch ( Exception e ) {
82 throw new BuildException( "Error - Couldn't read from the specified file" );
83 }
84
85 if ( line == null ) {
86 hasMoreLines = false;
87 } else {
88
89 String oldLine = line;
90 line = line.replaceAll(pattern, replacement);
91 if ( !oldLine.equals( line ) ) {
92 noReplaces++;
93 }
94
95 try {
96 out.write(line);
97 out.newLine();
98 } catch ( Exception e ) {
99 throw new BuildException( "Error - Couldn't write to the temp file" );
100 }
101 }
102 }
103
104
105 try {
106 //close them both up
107 in.close();
108 out.close();
109 } catch ( Exception e ) {
110 throw new BuildException( "Error - Couldn't close a file" );
111 }
112
113
114 //copy the new file (temp) over the original
115 InputStream i;
116 OutputStream o;
117 try {
118 i = new FileInputStream(temp);
119 o = new FileOutputStream(file);
120
121 } catch ( Exception e ) {
122 throw new BuildException( "Error - Couldn't open the temp file" );
123 }
124
125 try {
126
127 // Transfer bytes from in to out
128 byte[] buf = new byte[1024];
129 int len;
130 while ((len = i.read(buf)) > 0) {
131 o.write(buf, 0, len);
132 }
133
134 //close them up
135 i.close();
136 o.close();
137 } catch ( Exception e ) {
138 throw new BuildException( "Error - Couldn't write to the specified file" );
139 }
140
141 String linesWord = "lines";
142 if ( noReplaces == 1 ) {
143 linesWord = "line";
144 }
145 System.out.println( "Successfully changed " + noReplaces + " " + linesWord );
146 }
147
148
149 public void setFile(File file) {
150 this.file = file;
151 }
152
153 public void setPattern(String pattern) {
154 this.pattern = pattern;
155 }
156
157 public void setReplacement(String replacement) {
158 this.replacement = replacement.replaceAll( "\\\\", "\\\\\\\\" );
159 }
160
161
162}
Note: See TracBrowser for help on using the repository browser.