Changeset 18960 for other-projects/trunk/anttasks/src/org/greenstone/anttasks/RegexSearchReplace.java
- Timestamp:
- 2009-04-15T11:28:41+12:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/trunk/anttasks/src/org/greenstone/anttasks/RegexSearchReplace.java
r17904 r18960 37 37 private String pattern = null; 38 38 private String replacement = null; 39 private int fromLine = -1; 40 private int toLine = -1; 39 41 private boolean winPath = false; 40 42 … … 45 47 rsr.setPattern(args[1]); 46 48 rsr.setReplacement(args[2]); 49 rsr.setLines(args[3]); 47 50 48 51 rsr.execute(); … … 119 122 System.out.println(); 120 123 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 } 133 129 134 130 System.out.println( "Files: " ); … … 168 164 //pass the file through, searching and replacing 169 165 String line = null; 166 int lineNumber = 1; 170 167 boolean hasMoreLines = true; 171 168 while ( hasMoreLines ) { … … 182 179 } else { 183 180 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 ) { 195 187 rp = rp.replaceAll("\\\\","\\\\\\\\"); 196 188 } 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 } 198 198 } 199 } 200 201 if ( !oldLine.equals( line ) ) {202 noReplaces++;199 200 if ( !oldLine.equals( line ) ) { 201 noReplaces++; 202 } 203 203 } 204 204 … … 210 210 } 211 211 } 212 lineNumber++; 212 213 } 213 214 … … 269 270 } 270 271 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 271 300 public void setPattern(String pattern) { 272 301 this.pattern = pattern;
Note:
See TracChangeset
for help on using the changeset viewer.