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