/* * 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.*; public class RegexSearchReplace extends Task { private File file = null; private ArrayList jobs = 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 ) { throw new BuildException( "Error - No file specified !!" ); } if ( !file.exists() ) { throw new BuildException( "Error - File not found !!" ); } 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() ); } } int noReplaces = 0; //create the output stream BufferedWriter out = null; File temp = null; try { //create temp file. temp = File.createTempFile("rsr", ".tmp"); //delete temp file when program exits. temp.deleteOnExit(); //writer to temp file out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(temp), "UTF8") ); } catch (IOException e) { throw new BuildException( "Error - Couldn't create or open the temp file" ); } //create the input stream BufferedReader in = null; try { in = new BufferedReader( new InputStreamReader(new FileInputStream(file), "UTF8") ); } catch ( Exception e ) { throw new BuildException( "Error - Couldn't open the specified file" ); } //pass the file through, searching and replacing String line = null; boolean hasMoreLines = true; while ( hasMoreLines ) { try { line = in.readLine(); } catch ( Exception e ) { System.err.println( e.getMessage() ); throw new BuildException( "Error - Couldn't read from the specified file" ); } if ( line == null ) { hasMoreLines = false; } else { String oldLine = line; if ( pattern != null ) { String rp = replacement; if ( winPath ) { rp = rp.replaceAll("\\\\","\\\\\\\\"); } line = line.replaceAll((String)pattern, (String)rp); } else { for ( int i=0; i 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( "No Changes Made" ); } else if ( noReplaces == 1 ) { System.out.println( "Successfully changed 1 line" ); } else { System.out.println( "Successfully changed " + noReplaces + " lines" ); } } public void setFile(File file) { 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; } }