Changeset 17295 for other-projects/trunk
- Timestamp:
- 2008-09-17T09:23:43+12:00 (16 years ago)
- Location:
- other-projects/trunk/anttasks
- Files:
-
- 3 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/trunk/anttasks/build.xml
r17089 r17295 22 22 </target> 23 23 24 <target name="clean"> 25 <delete dir="classes"/> 26 <delete dir="lib"/> 27 </target> 28 24 29 <target name="check-libs" unless="libs.set"> 25 30 <echo>Please set crypt.jar property to the path of the crypt.jar file</echo> … … 28 33 </target> 29 34 35 36 30 37 </project> -
other-projects/trunk/anttasks/src/org/greenstone/anttasks/RegexSearchReplace.java
r17089 r17295 21 21 package org.greenstone.anttasks; 22 22 23 import java.util.ArrayList; 23 24 import org.apache.tools.ant.*; 24 25 import org.apache.tools.ant.taskdefs.*; … … 27 28 import java.util.regex.*; 28 29 29 30 30 public class RegexSearchReplace extends Task { 31 31 32 private File file = null; 33 private ArrayList jobs = null; 32 34 private String pattern = null; 33 35 private String replacement = null; 34 36 private boolean winPath = false; 35 36 37 /** 38 * for testing 39 */ 37 40 38 public static void main( String[] args ) { 41 39 … … 55 53 } 56 54 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 } 72 86 73 87 int noReplaces = 0; … … 115 129 116 130 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 118 143 if ( !oldLine.equals( line ) ) { 119 144 noReplaces++; … … 166 191 } 167 192 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 176 203 public void setFile(File file) { 177 204 this.file = file; … … 183 210 184 211 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; 186 217 } 187 218 … … 190 221 } 191 222 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 } 192 232 193 233 }
Note:
See TracChangeset
for help on using the changeset viewer.