/* * RegexSearchReplace.java * A task to replace occurences of a given pattern with a given replacement in a given file * * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ package org.greenstone.anttasks; import java.util.ArrayList; import org.apache.tools.ant.*; import org.apache.tools.ant.taskdefs.*; import java.util.*; import java.io.*; import java.util.regex.*; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.DirectoryScanner; public class RegexSearchReplace extends Task { private File file = null; private ArrayList jobs = null; private FileSet fileset = null; private String pattern = null; private String replacement = null; private boolean winPath = false; public static void main( String[] args ) { RegexSearchReplace rsr = new RegexSearchReplace(); rsr.setFile(new File (args[0])); rsr.setPattern(args[1]); rsr.setReplacement(args[2]); rsr.execute(); } public void execute() { if ( file == null && fileset == null ) { throw new BuildException( "Error - No file or fileset specified !!" ); } if ( file != null && !file.exists() ) { throw new BuildException( "Error - File not found !!" ); } DirectoryScanner dirScanner = null; if ( fileset != null ) { dirScanner = fileset.getDirectoryScanner(); dirScanner.scan(); } if ( jobs == null && pattern == null ) { throw new BuildException( "Error - No pattern attribute and no nested jobs !!" ); } if ( jobs != null && pattern != null ) { throw new BuildException( "Error - Both pattern attribute and nested jobs given !!" ); } if ( pattern != null && replacement == null ) { throw new BuildException( "Error - pattern attribute given but no replacement attribute given !!" ); } if ( jobs != null ) { for ( int i=0; i " + replacement ); } else { for ( int i=0; i " + ((RegexSearchReplaceJob)(jobs.get(i))).getReplacement() ); } } //output filename(s) /* if ( file != null ) { System.out.println( "Replace in file: " + file ); } else { System.out.println( "Replace in files:" ); for ( int i = 0; i < files.length; i++ ) { System.out.println(files[i]); } } */ System.out.println( "Files: " ); for ( int fileIndex=0; fileIndex 0) { o.write(buf, 0, len); } //close them up i.close(); o.close(); } catch ( Exception e ) { throw new BuildException( "Error - Couldn't write to the specified file" ); } if ( noReplaces == 0 ) { System.out.println( " " + inputFile + " : No Changes Made" ); } else if ( noReplaces == 1 ) { System.out.println( " " + inputFile + " : Successfully changed 1 line" ); } else { System.out.println( " " + inputFile + " : Successfully changed " + noReplaces + " lines" ); } } System.out.println(); } public void setFile(File file) { if ( this.fileset != null || this.file != null ) { throw new BuildException( "Error - Only one file or one fileset may be given!!" ); } this.file = file; } public void setPattern(String pattern) { this.pattern = pattern; } public void setReplacement(String replacement) { replacement = replacement.replaceAll( "\\\\", "\\\\\\\\" ); this.replacement = replacement; } public void setWinPath( boolean isWinPath ) { this.winPath = isWinPath; } public RegexSearchReplaceJob createJob() { RegexSearchReplaceJob job = new RegexSearchReplaceJob(); if ( jobs == null ) { jobs = new ArrayList(); } jobs.add( job ); return job; } public FileSet createFileset() { if ( this.fileset != null || this.file != null ) { throw new BuildException( "Error - Only one file or one fileset may be given!!" ); } fileset = new FileSet(); return fileset; } }