Ignore:
Timestamp:
2009-04-15T11:28:41+12:00 (15 years ago)
Author:
oranfry
Message:

modified rsr to take a range of line numbers to limit its operations to

File:
1 edited

Legend:

Unmodified
Added
Removed
  • other-projects/trunk/anttasks/src/org/greenstone/anttasks/RegexSearchReplace.java

    r17904 r18960  
    3737    private String pattern = null;
    3838    private String replacement = null;
     39    private int fromLine = -1;
     40    private int toLine = -1;
    3941    private boolean winPath = false;
    4042
     
    4547        rsr.setPattern(args[1]);
    4648        rsr.setReplacement(args[2]);
     49        rsr.setLines(args[3]);
    4750
    4851        rsr.execute();
     
    119122        System.out.println();
    120123
    121         //output filename(s)
    122         /*
    123         if ( file != null ) {
    124             System.out.println( "Replace in file: " + file );
    125         } else {
    126             System.out.println( "Replace in files:" );
    127             for ( int i = 0; i < files.length; i++ ) {
    128                 System.out.println(files[i]);
    129             }
    130         }
    131         */
    132 
     124        //output lines if necessary
     125        if ( this.fromLine != -1 || this.toLine != -1 ) {
     126            System.out.println( "Lines: " + (this.fromLine==-1?"Start":this.fromLine) + " ~ " + (this.toLine==-1?"End":this.toLine) );
     127            System.out.println();
     128        }
    133129
    134130        System.out.println( "Files: " );
     
    168164            //pass the file through, searching and replacing
    169165            String line = null;
     166            int lineNumber = 1;
    170167            boolean hasMoreLines = true;
    171168            while ( hasMoreLines ) {
     
    182179                } else {
    183180
    184                     String oldLine = line;
    185                     if ( pattern != null ) {
    186                         String rp = replacement;
    187                         if ( winPath ) {
    188                             rp = rp.replaceAll("\\\\","\\\\\\\\");
    189                         }
    190                         line = line.replaceAll((String)pattern, (String)rp);
    191                     } else {
    192                         for ( int i=0; i<jobs.size(); i++ ) {
    193                             String rp = ((RegexSearchReplaceJob)(jobs.get(i))).getReplacement();
    194                             if ( ((RegexSearchReplaceJob)(jobs.get(i))).getWinPath() ) {
     181                    //only if within the given line range
     182                    if ( ( fromLine == -1 || lineNumber >= fromLine ) && ( toLine == -1 || lineNumber < toLine ) ) {
     183                        String oldLine = line;
     184                        if ( pattern != null ) {
     185                            String rp = replacement;
     186                            if ( winPath ) {
    195187                                rp = rp.replaceAll("\\\\","\\\\\\\\");
    196188                            }
    197                             line = line.replaceAll( ((RegexSearchReplaceJob)(jobs.get(i))).getPattern(), rp );
     189                            line = line.replaceAll((String)pattern, (String)rp);
     190                        } else {
     191                            for ( int i=0; i<jobs.size(); i++ ) {
     192                                String rp = ((RegexSearchReplaceJob)(jobs.get(i))).getReplacement();
     193                                if ( ((RegexSearchReplaceJob)(jobs.get(i))).getWinPath() ) {
     194                                    rp = rp.replaceAll("\\\\","\\\\\\\\");
     195                                }
     196                                line = line.replaceAll( ((RegexSearchReplaceJob)(jobs.get(i))).getPattern(), rp );
     197                            }
    198198                        }
    199                     }
    200 
    201                     if ( !oldLine.equals( line ) ) {
    202                         noReplaces++;
     199
     200                        if ( !oldLine.equals( line ) ) {
     201                            noReplaces++;
     202                        }
    203203                    }
    204204
     
    210210                    }
    211211                }
     212                lineNumber++;
    212213            }
    213214           
     
    269270    }
    270271
     272    public void setLines(String lines) {
     273        //lines should be in the form "2" or "2-5"
     274
     275        //trim
     276        lines.replaceAll("\\s","");
     277
     278        if ( lines == "" ) {
     279            throw new BuildException( "Error - no line number(s) given in lines attribute!! " );
     280        }
     281
     282        //split into parts
     283        String[] parts = lines.split("-",2);
     284
     285        try {
     286            if ( parts.length == 1 ) {
     287                this.fromLine = Integer.parseInt( parts[0] );
     288                this.toLine = this.fromLine + 1;
     289            } else {
     290                if ( !parts[0].equals("") ) this.fromLine = Integer.parseInt( parts[0] );
     291                if ( !parts[1].equals("") ) this.toLine = Integer.parseInt( parts[1] );
     292            }
     293        } catch( NumberFormatException nfe ) {
     294            throw new BuildException( "Error - invalid line numbers given in lines attribute!! '" + parts[0] + "' - '" + parts[1] + "'" );
     295        }
     296
     297    }
     298
     299
    271300    public void setPattern(String pattern) {
    272301        this.pattern = pattern;
Note: See TracChangeset for help on using the changeset viewer.