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

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

changes to a few custom tasks

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