[17089] | 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 |
|
---|
[17295] | 23 | import java.util.ArrayList;
|
---|
[17089] | 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 |
|
---|
[17295] | 30 | public class RegexSearchReplace extends Task {
|
---|
[17089] | 31 |
|
---|
| 32 | private File file = null;
|
---|
[17295] | 33 | private ArrayList jobs = null;
|
---|
[17089] | 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 |
|
---|
[17553] | 55 | if ( !file.exists() ) {
|
---|
| 56 | throw new BuildException( "Error - File not found !!" );
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[17295] | 59 | if ( jobs == null && pattern == null ) {
|
---|
| 60 | throw new BuildException( "Error - No pattern attribute and no nested jobs !!" );
|
---|
[17089] | 61 | }
|
---|
| 62 |
|
---|
[17295] | 63 | if ( jobs != null && pattern != null ) {
|
---|
| 64 | throw new BuildException( "Error - Both pattern attribute and nested jobs given !!" );
|
---|
[17089] | 65 | }
|
---|
| 66 |
|
---|
[17295] | 67 | if ( pattern != null && replacement == null ) {
|
---|
| 68 | throw new BuildException( "Error - pattern attribute given but no replacement attribute given !!" );
|
---|
[17089] | 69 | }
|
---|
| 70 |
|
---|
[17295] | 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 |
|
---|
[17089] | 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
|
---|
[17553] | 104 | out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(temp), "UTF8") );
|
---|
[17089] | 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 {
|
---|
[17553] | 114 | in = new BufferedReader( new InputStreamReader(new FileInputStream(file), "UTF8") );
|
---|
[17089] | 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
|
---|
[17553] | 120 | String line = null;
|
---|
[17089] | 121 | boolean hasMoreLines = true;
|
---|
| 122 | while ( hasMoreLines ) {
|
---|
| 123 |
|
---|
| 124 | try {
|
---|
| 125 | line = in.readLine();
|
---|
| 126 | } catch ( Exception e ) {
|
---|
[17553] | 127 | System.err.println( e.getMessage() );
|
---|
[17089] | 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;
|
---|
[17295] | 136 | if ( pattern != null ) {
|
---|
[17349] | 137 | String rp = replacement;
|
---|
| 138 | if ( winPath ) {
|
---|
| 139 | rp = rp.replaceAll("\\\\","\\\\\\\\");
|
---|
| 140 | }
|
---|
| 141 | line = line.replaceAll((String)pattern, (String)rp);
|
---|
[17295] | 142 | } else {
|
---|
| 143 | for ( int i=0; i<jobs.size(); i++ ) {
|
---|
| 144 | String rp = ((RegexSearchReplaceJob)(jobs.get(i))).getReplacement();
|
---|
[17349] | 145 | if ( ((RegexSearchReplaceJob)(jobs.get(i))).getWinPath() ) {
|
---|
[17295] | 146 | rp = rp.replaceAll("\\\\","\\\\\\\\");
|
---|
| 147 | }
|
---|
| 148 | line = line.replaceAll( ((RegexSearchReplaceJob)(jobs.get(i))).getPattern(), rp );
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[17089] | 152 | if ( !oldLine.equals( line ) ) {
|
---|
| 153 | noReplaces++;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | try {
|
---|
[17553] | 157 | out.write( line );
|
---|
[17089] | 158 | out.newLine();
|
---|
| 159 | } catch ( Exception e ) {
|
---|
| 160 | throw new BuildException( "Error - Couldn't write to the temp file" );
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
[17553] | 164 |
|
---|
[17089] | 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 |
|
---|
[17295] | 201 | if ( noReplaces == 0 ) {
|
---|
| 202 | System.out.println( "No Changes Made" );
|
---|
[17303] | 203 | } else if ( noReplaces == 1 ) {
|
---|
[17295] | 204 | System.out.println( "Successfully changed 1 line" );
|
---|
| 205 | } else {
|
---|
| 206 | System.out.println( "Successfully changed " + noReplaces + " lines" );
|
---|
[17089] | 207 | }
|
---|
[17295] | 208 |
|
---|
[17089] | 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) {
|
---|
[17295] | 220 | replacement = replacement.replaceAll( "\\\\", "\\\\\\\\" );
|
---|
| 221 | this.replacement = replacement;
|
---|
[17089] | 222 | }
|
---|
| 223 |
|
---|
| 224 | public void setWinPath( boolean isWinPath ) {
|
---|
| 225 | this.winPath = isWinPath;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[17295] | 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;
|
---|
[17089] | 235 |
|
---|
[17295] | 236 | }
|
---|
| 237 |
|
---|
[17089] | 238 | }
|
---|