source: other-projects/trunk/anttasks/src/org/greenstone/anttasks/RegexSearchReplace.java@ 17295

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

added a delete chunk from file task and made rsr nicer with nested elements

File size: 6.0 KB
Line 
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 */
21package org.greenstone.anttasks;
22
23import java.util.ArrayList;
24import org.apache.tools.ant.*;
25import org.apache.tools.ant.taskdefs.*;
26import java.util.*;
27import java.io.*;
28import java.util.regex.*;
29
30public class RegexSearchReplace extends Task {
31
32 private File file = null;
33 private ArrayList jobs = null;
34 private String pattern = null;
35 private String replacement = null;
36 private boolean winPath = false;
37
38 public static void main( String[] args ) {
39
40 RegexSearchReplace rsr = new RegexSearchReplace();
41 rsr.setFile(new File (args[0]));
42 rsr.setPattern(args[1]);
43 rsr.setReplacement(args[2]);
44
45 rsr.execute();
46
47 }
48
49 public void execute() {
50
51 if ( file == null ) {
52 throw new BuildException( "Error - No file specified !!" );
53 }
54
55 if ( jobs == null && pattern == null ) {
56 throw new BuildException( "Error - No pattern attribute and no nested jobs !!" );
57 }
58
59 if ( jobs != null && pattern != null ) {
60 throw new BuildException( "Error - Both pattern attribute and nested jobs given !!" );
61 }
62
63 if ( pattern != null && replacement == null ) {
64 throw new BuildException( "Error - pattern attribute given but no replacement attribute given !!" );
65 }
66
67 if ( jobs != null ) {
68 for ( int i=0; i<jobs.size(); i++ ) {
69 if ( ((RegexSearchReplaceJob)(jobs.get(i))).getPattern() == null ) {
70 throw new BuildException( "Error - One of the jobs lacks a pattern!!" );
71 }
72 if ( ((RegexSearchReplaceJob)(jobs.get(i))).getReplacement() == null ) {
73 throw new BuildException( "Error - One of the jobs lacks a replacement!!" );
74 }
75 }
76 }
77
78 System.out.println( "Replace in file: " + file );
79 if ( pattern != null ) {
80 System.out.println( " " + pattern + " -> " + replacement );
81 } else {
82 for ( int i=0; i<jobs.size(); i++ ) {
83 System.out.println( " " + ((RegexSearchReplaceJob)(jobs.get(i))).getPattern() + " -> " + ((RegexSearchReplaceJob)(jobs.get(i))).getReplacement() );
84 }
85 }
86
87 int noReplaces = 0;
88
89 //create the output stream
90 BufferedWriter out = null;
91 File temp = null;
92 try {
93 //create temp file.
94 temp = File.createTempFile("rsr", ".tmp");
95
96 //delete temp file when program exits.
97 temp.deleteOnExit();
98
99 //writer to temp file
100 out = new BufferedWriter(new FileWriter(temp));
101
102 } catch (IOException e) {
103 throw new BuildException( "Error - Couldn't create or open the temp file" );
104 }
105
106
107 //create the input stream
108 BufferedReader in = null;
109 try {
110 in = new BufferedReader( new FileReader( file ) );
111 } catch ( Exception e ) {
112 throw new BuildException( "Error - Couldn't open the specified file" );
113 }
114
115 //pass the file through, searching and replacing
116 String line;
117 boolean hasMoreLines = true;
118 while ( hasMoreLines ) {
119
120 try {
121 line = in.readLine();
122 } catch ( Exception e ) {
123 throw new BuildException( "Error - Couldn't read from the specified file" );
124 }
125
126 if ( line == null ) {
127 hasMoreLines = false;
128 } else {
129
130 String oldLine = line;
131 if ( pattern != null ) {
132 line = line.replaceAll((String)pattern, (String)replacement);
133 } else {
134 for ( int i=0; i<jobs.size(); i++ ) {
135 String rp = ((RegexSearchReplaceJob)(jobs.get(i))).getReplacement();
136 if ( winPath ) {
137 rp = rp.replaceAll("\\\\","\\\\\\\\");
138 }
139 line = line.replaceAll( ((RegexSearchReplaceJob)(jobs.get(i))).getPattern(), rp );
140 }
141 }
142
143 if ( !oldLine.equals( line ) ) {
144 noReplaces++;
145 }
146
147 try {
148 out.write(line);
149 out.newLine();
150 } catch ( Exception e ) {
151 throw new BuildException( "Error - Couldn't write to the temp file" );
152 }
153 }
154 }
155
156
157 try {
158 //close them both up
159 in.close();
160 out.close();
161 } catch ( Exception e ) {
162 throw new BuildException( "Error - Couldn't close a file" );
163 }
164
165
166 //copy the new file (temp) over the original
167 InputStream i;
168 OutputStream o;
169 try {
170 i = new FileInputStream(temp);
171 o = new FileOutputStream(file);
172
173 } catch ( Exception e ) {
174 throw new BuildException( "Error - Couldn't open the temp file" );
175 }
176
177 try {
178
179 // Transfer bytes from in to out
180 byte[] buf = new byte[1024];
181 int len;
182 while ((len = i.read(buf)) > 0) {
183 o.write(buf, 0, len);
184 }
185
186 //close them up
187 i.close();
188 o.close();
189 } catch ( Exception e ) {
190 throw new BuildException( "Error - Couldn't write to the specified file" );
191 }
192
193 if ( noReplaces == 0 ) {
194 System.out.println( "No Changes Made" );
195 } if ( noReplaces == 1 ) {
196 System.out.println( "Successfully changed 1 line" );
197 } else {
198 System.out.println( "Successfully changed " + noReplaces + " lines" );
199 }
200
201 }
202
203 public void setFile(File file) {
204 this.file = file;
205 }
206
207 public void setPattern(String pattern) {
208 this.pattern = pattern;
209 }
210
211 public void setReplacement(String replacement) {
212 replacement = replacement.replaceAll( "\\\\", "\\\\\\\\" );
213 if ( winPath ) {
214 replacement = replacement.replaceAll("\\\\","\\\\\\\\");
215 }
216 this.replacement = replacement;
217 }
218
219 public void setWinPath( boolean isWinPath ) {
220 this.winPath = isWinPath;
221 }
222
223 public RegexSearchReplaceJob createJob() {
224 RegexSearchReplaceJob job = new RegexSearchReplaceJob();
225 if ( jobs == null ) {
226 jobs = new ArrayList();
227 }
228 jobs.add( job );
229 return job;
230
231 }
232
233}
Note: See TracBrowser for help on using the repository browser.