Changeset 17295 for other-projects/trunk


Ignore:
Timestamp:
2008-09-17T09:23:43+12:00 (16 years ago)
Author:
oranfry
Message:

added a delete chunk from file task and made rsr nicer with nested elements

Location:
other-projects/trunk/anttasks
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • other-projects/trunk/anttasks/build.xml

    r17089 r17295  
    2222    </target>
    2323
     24    <target name="clean">
     25        <delete dir="classes"/>
     26        <delete dir="lib"/>
     27    </target>
     28
    2429    <target name="check-libs" unless="libs.set">
    2530        <echo>Please set crypt.jar property to the path of the crypt.jar file</echo>
     
    2833    </target>
    2934
     35
     36
    3037</project>
  • other-projects/trunk/anttasks/src/org/greenstone/anttasks/RegexSearchReplace.java

    r17089 r17295  
    2121package org.greenstone.anttasks;
    2222
     23import java.util.ArrayList;
    2324import org.apache.tools.ant.*;
    2425import org.apache.tools.ant.taskdefs.*;
     
    2728import java.util.regex.*;
    2829
    29 
    3030public class RegexSearchReplace extends Task {
     31
    3132    private File file = null;
     33    private ArrayList jobs = null;
    3234    private String pattern = null;
    3335    private String replacement = null;
    3436    private boolean winPath = false;
    35    
    36 
    37     /**
    38      * for testing
    39      */
     37
    4038    public static void main( String[] args ) {
    4139
     
    5553        }
    5654
    57         if ( pattern == null ) {
    58             throw new BuildException( "Error - No pattern specified !!" );
    59         }
    60 
    61         if ( replacement == null ) {
    62             throw new BuildException( "Error - No replacement specified !!" );
    63         }
    64 
    65         if ( winPath ) {
    66             replacement = replacement.replaceAll("\\\\","\\\\\\\\");
    67         }
    68        
    69         System.out.println( "File: " + file );
    70         System.out.println( "Pattern: " + pattern );
    71         System.out.println( "Replacement: " + replacement );
     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        }
    7286
    7387        int noReplaces = 0;
     
    115129
    116130                String oldLine = line;
    117                 line = line.replaceAll(pattern, replacement);
     131                if ( pattern != null ) {
     132                    line = line.replaceAll((String)pattern, (String)replacement);
     133                } else {
     134                    for ( int i=0; i<jobs.size(); i++ ) {
     135                        String rp = ((RegexSearchReplaceJob)(jobs.get(i))).getReplacement();
     136                        if ( winPath ) {
     137                            rp = rp.replaceAll("\\\\","\\\\\\\\");
     138                        }
     139                        line = line.replaceAll( ((RegexSearchReplaceJob)(jobs.get(i))).getPattern(), rp );
     140                    }
     141                }
     142
    118143                if ( !oldLine.equals( line ) ) {
    119144                    noReplaces++;
     
    166191        }
    167192
    168         String linesWord = "lines";     
    169         if ( noReplaces == 1 ) {
    170             linesWord = "line";
    171         }
    172         System.out.println( "Successfully changed " + noReplaces + " " + linesWord );
    173     }
    174 
    175    
     193        if ( noReplaces == 0 ) {
     194            System.out.println( "No Changes Made" );
     195        } if ( noReplaces == 1 ) {
     196            System.out.println( "Successfully changed 1 line" );
     197        } else {
     198            System.out.println( "Successfully changed " + noReplaces + " lines" );
     199        }
     200
     201    }
     202
    176203    public void setFile(File file) {
    177204        this.file = file;
     
    183210
    184211    public void setReplacement(String replacement) {
    185         this.replacement = replacement.replaceAll( "\\\\", "\\\\\\\\" );
     212        replacement = replacement.replaceAll( "\\\\", "\\\\\\\\" );
     213        if ( winPath ) {
     214            replacement = replacement.replaceAll("\\\\","\\\\\\\\");
     215        }
     216        this.replacement = replacement;
    186217    }
    187218
     
    190221    }
    191222
     223    public RegexSearchReplaceJob createJob() {
     224        RegexSearchReplaceJob job = new RegexSearchReplaceJob();
     225        if ( jobs == null ) {
     226            jobs = new ArrayList();
     227        }
     228        jobs.add( job );
     229        return job;
     230
     231    }
    192232
    193233}
Note: See TracChangeset for help on using the changeset viewer.