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