1 | package org.greenstone.anttasks;
|
---|
2 |
|
---|
3 | import org.apache.tools.ant.*;
|
---|
4 | import org.apache.tools.ant.taskdefs.*;
|
---|
5 | import java.util.*;
|
---|
6 | import java.io.*;
|
---|
7 | import java.util.regex.*;
|
---|
8 |
|
---|
9 |
|
---|
10 | public class RegexSearchReplace extends Task {
|
---|
11 | private File file = null;
|
---|
12 | private String pattern = null;
|
---|
13 | private String replacement = null;
|
---|
14 |
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * for testing
|
---|
18 | */
|
---|
19 | public static void main( String[] args ) {
|
---|
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 | replacement = "";
|
---|
41 | }
|
---|
42 |
|
---|
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 | // Write to temp file
|
---|
55 | out = new BufferedWriter(new FileWriter(temp));
|
---|
56 |
|
---|
57 | } catch (IOException e) {}
|
---|
58 |
|
---|
59 |
|
---|
60 | //create the input stream
|
---|
61 | BufferedReader in = null;
|
---|
62 | try {
|
---|
63 | in = new BufferedReader(new FileReader( file ));
|
---|
64 | } catch ( Exception e ) {}
|
---|
65 |
|
---|
66 | //pass the file through, searching and replacing
|
---|
67 | String line;
|
---|
68 | try {
|
---|
69 | while ( (line = in.readLine()) != null ) {
|
---|
70 | line = line.replaceAll( pattern, replacement);
|
---|
71 | out.write(line);
|
---|
72 | }
|
---|
73 | //close them both up
|
---|
74 | in.close();
|
---|
75 | out.close();
|
---|
76 |
|
---|
77 | } catch ( Exception e ) {
|
---|
78 | e.printStackTrace();
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 |
|
---|
83 | //copy the new file (temp) over the original
|
---|
84 | try {
|
---|
85 | InputStream i = new FileInputStream(temp);
|
---|
86 | OutputStream o = new FileOutputStream(file);
|
---|
87 |
|
---|
88 | // Transfer bytes from in to out
|
---|
89 | byte[] buf = new byte[1024];
|
---|
90 | int len;
|
---|
91 | while ((len = i.read(buf)) > 0) {
|
---|
92 | o.write(buf, 0, len);
|
---|
93 | }
|
---|
94 |
|
---|
95 | //close them up
|
---|
96 | i.close();
|
---|
97 | o.close();
|
---|
98 | } catch ( Exception e ) {}
|
---|
99 |
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | public void setFile(File file) {
|
---|
104 | this.file = file;
|
---|
105 | }
|
---|
106 |
|
---|
107 | public void setPattern(String pattern) {
|
---|
108 | this.pattern = pattern;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public void setReplacement(String replacement) {
|
---|
112 | this.replacement = replacement;
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | }
|
---|