1 | /*
|
---|
2 | * RegexSearchReplace.java
|
---|
3 | * A task to replace occurences of a given pattern with a given replacement in a given file
|
---|
4 | *
|
---|
5 | * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
|
---|
6 | *
|
---|
7 | * This program is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This program is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
20 | */
|
---|
21 | package org.greenstone.anttasks;
|
---|
22 |
|
---|
23 | import org.apache.tools.ant.*;
|
---|
24 | import org.apache.tools.ant.taskdefs.*;
|
---|
25 | import java.util.*;
|
---|
26 | import java.io.*;
|
---|
27 | import java.util.regex.*;
|
---|
28 |
|
---|
29 |
|
---|
30 | public class RegexSearchReplace extends Task {
|
---|
31 | private File file = null;
|
---|
32 | private String pattern = null;
|
---|
33 | private String replacement = null;
|
---|
34 | private boolean winPath = false;
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * for testing
|
---|
39 | */
|
---|
40 | public static void main( String[] args ) {
|
---|
41 |
|
---|
42 | RegexSearchReplace rsr = new RegexSearchReplace();
|
---|
43 | rsr.setFile(new File (args[0]));
|
---|
44 | rsr.setPattern(args[1]);
|
---|
45 | rsr.setReplacement(args[2]);
|
---|
46 |
|
---|
47 | rsr.execute();
|
---|
48 |
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void execute() {
|
---|
52 |
|
---|
53 | if ( file == null ) {
|
---|
54 | throw new BuildException( "Error - No file specified !!" );
|
---|
55 | }
|
---|
56 |
|
---|
57 | if ( pattern == null ) {
|
---|
58 | throw new BuildException( "Error - No pattern specified !!" );
|
---|
59 | }
|
---|
60 |
|
---|
61 | if ( replacement == null ) {
|
---|
62 | throw new BuildException( "Error - No replacement specified !!" );
|
---|
63 | }
|
---|
64 |
|
---|
65 | if ( winPath ) {
|
---|
66 | replacement = replacement.replaceAll("\\\\","\\\\\\\\");
|
---|
67 | }
|
---|
68 |
|
---|
69 | System.out.println( "File: " + file );
|
---|
70 | System.out.println( "Pattern: " + pattern );
|
---|
71 | System.out.println( "Replacement: " + replacement );
|
---|
72 |
|
---|
73 | int noReplaces = 0;
|
---|
74 |
|
---|
75 | //create the output stream
|
---|
76 | BufferedWriter out = null;
|
---|
77 | File temp = null;
|
---|
78 | try {
|
---|
79 | //create temp file.
|
---|
80 | temp = File.createTempFile("rsr", ".tmp");
|
---|
81 |
|
---|
82 | //delete temp file when program exits.
|
---|
83 | temp.deleteOnExit();
|
---|
84 |
|
---|
85 | //writer to temp file
|
---|
86 | out = new BufferedWriter(new FileWriter(temp));
|
---|
87 |
|
---|
88 | } catch (IOException e) {
|
---|
89 | throw new BuildException( "Error - Couldn't create or open the temp file" );
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | //create the input stream
|
---|
94 | BufferedReader in = null;
|
---|
95 | try {
|
---|
96 | in = new BufferedReader( new FileReader( file ) );
|
---|
97 | } catch ( Exception e ) {
|
---|
98 | throw new BuildException( "Error - Couldn't open the specified file" );
|
---|
99 | }
|
---|
100 |
|
---|
101 | //pass the file through, searching and replacing
|
---|
102 | String line;
|
---|
103 | boolean hasMoreLines = true;
|
---|
104 | while ( hasMoreLines ) {
|
---|
105 |
|
---|
106 | try {
|
---|
107 | line = in.readLine();
|
---|
108 | } catch ( Exception e ) {
|
---|
109 | throw new BuildException( "Error - Couldn't read from the specified file" );
|
---|
110 | }
|
---|
111 |
|
---|
112 | if ( line == null ) {
|
---|
113 | hasMoreLines = false;
|
---|
114 | } else {
|
---|
115 |
|
---|
116 | String oldLine = line;
|
---|
117 | line = line.replaceAll(pattern, replacement);
|
---|
118 | if ( !oldLine.equals( line ) ) {
|
---|
119 | noReplaces++;
|
---|
120 | }
|
---|
121 |
|
---|
122 | try {
|
---|
123 | out.write(line);
|
---|
124 | out.newLine();
|
---|
125 | } catch ( Exception e ) {
|
---|
126 | throw new BuildException( "Error - Couldn't write to the temp file" );
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | try {
|
---|
133 | //close them both up
|
---|
134 | in.close();
|
---|
135 | out.close();
|
---|
136 | } catch ( Exception e ) {
|
---|
137 | throw new BuildException( "Error - Couldn't close a file" );
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | //copy the new file (temp) over the original
|
---|
142 | InputStream i;
|
---|
143 | OutputStream o;
|
---|
144 | try {
|
---|
145 | i = new FileInputStream(temp);
|
---|
146 | o = new FileOutputStream(file);
|
---|
147 |
|
---|
148 | } catch ( Exception e ) {
|
---|
149 | throw new BuildException( "Error - Couldn't open the temp file" );
|
---|
150 | }
|
---|
151 |
|
---|
152 | try {
|
---|
153 |
|
---|
154 | // Transfer bytes from in to out
|
---|
155 | byte[] buf = new byte[1024];
|
---|
156 | int len;
|
---|
157 | while ((len = i.read(buf)) > 0) {
|
---|
158 | o.write(buf, 0, len);
|
---|
159 | }
|
---|
160 |
|
---|
161 | //close them up
|
---|
162 | i.close();
|
---|
163 | o.close();
|
---|
164 | } catch ( Exception e ) {
|
---|
165 | throw new BuildException( "Error - Couldn't write to the specified file" );
|
---|
166 | }
|
---|
167 |
|
---|
168 | String linesWord = "lines";
|
---|
169 | if ( noReplaces == 1 ) {
|
---|
170 | linesWord = "line";
|
---|
171 | }
|
---|
172 | System.out.println( "Successfully changed " + noReplaces + " " + linesWord );
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | public void setFile(File file) {
|
---|
177 | this.file = file;
|
---|
178 | }
|
---|
179 |
|
---|
180 | public void setPattern(String pattern) {
|
---|
181 | this.pattern = pattern;
|
---|
182 | }
|
---|
183 |
|
---|
184 | public void setReplacement(String replacement) {
|
---|
185 | this.replacement = replacement.replaceAll( "\\\\", "\\\\\\\\" );
|
---|
186 | }
|
---|
187 |
|
---|
188 | public void setWinPath( boolean isWinPath ) {
|
---|
189 | this.winPath = isWinPath;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | }
|
---|