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 ( 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 | String rp = replacement;
|
---|
133 | if ( winPath ) {
|
---|
134 | rp = rp.replaceAll("\\\\","\\\\\\\\");
|
---|
135 | }
|
---|
136 | line = line.replaceAll((String)pattern, (String)rp);
|
---|
137 | } else {
|
---|
138 | for ( int i=0; i<jobs.size(); i++ ) {
|
---|
139 | String rp = ((RegexSearchReplaceJob)(jobs.get(i))).getReplacement();
|
---|
140 | if ( ((RegexSearchReplaceJob)(jobs.get(i))).getWinPath() ) {
|
---|
141 | rp = rp.replaceAll("\\\\","\\\\\\\\");
|
---|
142 | }
|
---|
143 | line = line.replaceAll( ((RegexSearchReplaceJob)(jobs.get(i))).getPattern(), rp );
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | if ( !oldLine.equals( line ) ) {
|
---|
148 | noReplaces++;
|
---|
149 | }
|
---|
150 |
|
---|
151 | try {
|
---|
152 | out.write(line);
|
---|
153 | out.newLine();
|
---|
154 | } catch ( Exception e ) {
|
---|
155 | throw new BuildException( "Error - Couldn't write to the temp file" );
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | try {
|
---|
162 | //close them both up
|
---|
163 | in.close();
|
---|
164 | out.close();
|
---|
165 | } catch ( Exception e ) {
|
---|
166 | throw new BuildException( "Error - Couldn't close a file" );
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | //copy the new file (temp) over the original
|
---|
171 | InputStream i;
|
---|
172 | OutputStream o;
|
---|
173 | try {
|
---|
174 | i = new FileInputStream(temp);
|
---|
175 | o = new FileOutputStream(file);
|
---|
176 |
|
---|
177 | } catch ( Exception e ) {
|
---|
178 | throw new BuildException( "Error - Couldn't open the temp file" );
|
---|
179 | }
|
---|
180 |
|
---|
181 | try {
|
---|
182 |
|
---|
183 | // Transfer bytes from in to out
|
---|
184 | byte[] buf = new byte[1024];
|
---|
185 | int len;
|
---|
186 | while ((len = i.read(buf)) > 0) {
|
---|
187 | o.write(buf, 0, len);
|
---|
188 | }
|
---|
189 |
|
---|
190 | //close them up
|
---|
191 | i.close();
|
---|
192 | o.close();
|
---|
193 | } catch ( Exception e ) {
|
---|
194 | throw new BuildException( "Error - Couldn't write to the specified file" );
|
---|
195 | }
|
---|
196 |
|
---|
197 | if ( noReplaces == 0 ) {
|
---|
198 | System.out.println( "No Changes Made" );
|
---|
199 | } else if ( noReplaces == 1 ) {
|
---|
200 | System.out.println( "Successfully changed 1 line" );
|
---|
201 | } else {
|
---|
202 | System.out.println( "Successfully changed " + noReplaces + " lines" );
|
---|
203 | }
|
---|
204 |
|
---|
205 | }
|
---|
206 |
|
---|
207 | public void setFile(File file) {
|
---|
208 | this.file = file;
|
---|
209 | }
|
---|
210 |
|
---|
211 | public void setPattern(String pattern) {
|
---|
212 | this.pattern = pattern;
|
---|
213 | }
|
---|
214 |
|
---|
215 | public void setReplacement(String replacement) {
|
---|
216 | replacement = replacement.replaceAll( "\\\\", "\\\\\\\\" );
|
---|
217 | this.replacement = replacement;
|
---|
218 | }
|
---|
219 |
|
---|
220 | public void setWinPath( boolean isWinPath ) {
|
---|
221 | this.winPath = isWinPath;
|
---|
222 | }
|
---|
223 |
|
---|
224 | public RegexSearchReplaceJob createJob() {
|
---|
225 | RegexSearchReplaceJob job = new RegexSearchReplaceJob();
|
---|
226 | if ( jobs == null ) {
|
---|
227 | jobs = new ArrayList();
|
---|
228 | }
|
---|
229 | jobs.add( job );
|
---|
230 | return job;
|
---|
231 |
|
---|
232 | }
|
---|
233 |
|
---|
234 | }
|
---|