Changeset 17902 for other-projects/trunk
- Timestamp:
- 2008-11-21T15:04:11+13:00 (15 years ago)
- Location:
- other-projects/trunk/anttasks
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/trunk/anttasks/build.xml
r17836 r17902 75 75 </rsr> 76 76 77 <!-- test 5 --> 78 <copy file="src/test/rsr.txt" tofile="test/rsr5-1.txt"/> 79 <copy file="src/test/rsr.txt" tofile="test/rsr5-2.txt"/> 80 <rsr> 81 <fileset dir="test" includes="rsr5-*"/> 82 <job pattern="@path@" replacement="C:\Program Files\Greenstone 2.80"/> 83 <job pattern="@path-with-winpath@" replacement="C:\Program Files\Greenstone 2.80" winPath="true"/> 84 </rsr> 85 86 77 87 </target> 78 88 -
other-projects/trunk/anttasks/src/org/greenstone/anttasks/RegexSearchReplace.java
r17553 r17902 27 27 import java.io.*; 28 28 import java.util.regex.*; 29 import org.apache.tools.ant.types.FileSet; 30 import org.apache.tools.ant.DirectoryScanner; 29 31 30 32 public class RegexSearchReplace extends Task { … … 32 34 private File file = null; 33 35 private ArrayList jobs = null; 36 private FileSet fileset = null; 34 37 private String pattern = null; 35 38 private String replacement = null; … … 49 52 public void execute() { 50 53 51 if ( file == null ) {52 throw new BuildException( "Error - No file specified !!" );53 } 54 55 if ( !file.exists() ) {54 if ( file == null && fileset == null ) { 55 throw new BuildException( "Error - No file or fileset specified !!" ); 56 } 57 58 if ( file != null && !file.exists() ) { 56 59 throw new BuildException( "Error - File not found !!" ); 60 } 61 62 DirectoryScanner dirScanner = null; 63 if ( fileset != null ) { 64 dirScanner = fileset.getDirectoryScanner(); 65 dirScanner.scan(); 57 66 } 58 67 … … 80 89 } 81 90 82 System.out.println( "Replace in file: " + file ); 91 92 //sus out filename(s) 93 File[] files = null; 94 if ( file != null ) { 95 files = new File[1]; 96 files[0] = file; 97 } else { 98 String[] fileStrings = dirScanner.getIncludedFiles(); 99 files = new File[fileStrings.length]; 100 for ( int i=0; i<fileStrings.length; i++ ) { 101 files[i] = new File( dirScanner.getBasedir() + File.separator + fileStrings[i] ); 102 } 103 } 104 105 //output greeting 106 System.out.println(); 107 System.out.println( "--------------------" ); 108 System.out.println( " RegexSearchReplace " ); 109 System.out.println( "--------------------" ); 110 111 //output replacement(s) 112 System.out.println( "Replacements:" ); 83 113 if ( pattern != null ) { 84 114 System.out.println( " " + pattern + " -> " + replacement ); … … 89 119 } 90 120 91 int noReplaces = 0; 92 93 //create the output stream 94 BufferedWriter out = null; 95 File temp = null; 96 try { 97 //create temp file. 98 temp = File.createTempFile("rsr", ".tmp"); 99 100 //delete temp file when program exits. 101 temp.deleteOnExit(); 102 103 //writer to temp file 104 out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(temp), "UTF8") ); 105 106 } catch (IOException e) { 107 throw new BuildException( "Error - Couldn't create or open the temp file" ); 108 } 109 110 111 //create the input stream 112 BufferedReader in = null; 113 try { 114 in = new BufferedReader( new InputStreamReader(new FileInputStream(file), "UTF8") ); 115 } catch ( Exception e ) { 116 throw new BuildException( "Error - Couldn't open the specified file" ); 117 } 118 119 //pass the file through, searching and replacing 120 String line = null; 121 boolean hasMoreLines = true; 122 while ( hasMoreLines ) { 123 124 try { 125 line = in.readLine(); 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 133 134 System.out.println( "Files: " ); 135 for ( int fileIndex=0; fileIndex<files.length; fileIndex++ ) { 136 137 int noReplaces = 0; 138 139 File inputFile = files[fileIndex]; 140 141 //create the output stream 142 BufferedWriter out = null; 143 File temp = null; 144 try { 145 //create temp file. 146 temp = File.createTempFile("rsr", ".tmp"); 147 148 //delete temp file when program exits. 149 temp.deleteOnExit(); 150 151 //writer to temp file 152 out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(temp), "UTF8") ); 153 154 } catch (IOException e) { 155 throw new BuildException( "Error - Couldn't create or open the temp file" ); 156 } 157 158 159 //create the input stream 160 BufferedReader in = null; 161 try { 162 in = new BufferedReader( new InputStreamReader(new FileInputStream(inputFile), "UTF8") ); 126 163 } catch ( Exception e ) { 127 System.err.println( e.getMessage() ); 128 throw new BuildException( "Error - Couldn't read from the specified file" ); 129 } 130 131 if ( line == null ) { 132 hasMoreLines = false; 133 } else { 134 135 String oldLine = line; 136 if ( pattern != null ) { 137 String rp = replacement; 138 if ( winPath ) { 139 rp = rp.replaceAll("\\\\","\\\\\\\\"); 140 } 141 line = line.replaceAll((String)pattern, (String)rp); 164 throw new BuildException( "Error - Couldn't open the specified file" ); 165 } 166 167 //pass the file through, searching and replacing 168 String line = null; 169 boolean hasMoreLines = true; 170 while ( hasMoreLines ) { 171 172 try { 173 line = in.readLine(); 174 } catch ( Exception e ) { 175 System.err.println( e.getMessage() ); 176 throw new BuildException( "Error - Couldn't read from the specified file" ); 177 } 178 179 if ( line == null ) { 180 hasMoreLines = false; 142 181 } else { 143 for ( int i=0; i<jobs.size(); i++ ) { 144 String rp = ((RegexSearchReplaceJob)(jobs.get(i))).getReplacement(); 145 if ( ((RegexSearchReplaceJob)(jobs.get(i))).getWinPath() ) { 182 183 String oldLine = line; 184 if ( pattern != null ) { 185 String rp = replacement; 186 if ( winPath ) { 146 187 rp = rp.replaceAll("\\\\","\\\\\\\\"); 147 188 } 148 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 } 149 198 } 150 } 151 152 if ( !oldLine.equals( line ) ) { 153 noReplaces++; 154 } 155 156 try { 157 out.write( line ); 158 out.newLine(); 159 } catch ( Exception e ) { 160 throw new BuildException( "Error - Couldn't write to the temp file" ); 161 } 162 } 163 } 199 200 if ( !oldLine.equals( line ) ) { 201 noReplaces++; 202 } 203 204 try { 205 out.write( line ); 206 out.newLine(); 207 } catch ( Exception e ) { 208 throw new BuildException( "Error - Couldn't write to the temp file" ); 209 } 210 } 211 } 212 213 try { 214 //close them both up 215 in.close(); 216 out.close(); 217 } catch ( Exception e ) { 218 throw new BuildException( "Error - Couldn't close a file" ); 219 } 220 221 222 //copy the new file (temp) over the original 223 InputStream i; 224 OutputStream o; 225 try { 226 i = new FileInputStream(temp); 227 o = new FileOutputStream(inputFile); 228 229 } catch ( Exception e ) { 230 throw new BuildException( "Error - Couldn't open the temp file" ); 231 } 232 233 try { 164 234 165 try { 166 //close them both up 167 in.close(); 168 out.close(); 169 } catch ( Exception e ) { 170 throw new BuildException( "Error - Couldn't close a file" ); 171 } 172 173 174 //copy the new file (temp) over the original 175 InputStream i; 176 OutputStream o; 177 try { 178 i = new FileInputStream(temp); 179 o = new FileOutputStream(file); 180 181 } catch ( Exception e ) { 182 throw new BuildException( "Error - Couldn't open the temp file" ); 183 } 184 185 try { 186 187 // Transfer bytes from in to out 188 byte[] buf = new byte[1024]; 189 int len; 190 while ((len = i.read(buf)) > 0) { 191 o.write(buf, 0, len); 192 } 193 194 //close them up 195 i.close(); 196 o.close(); 197 } catch ( Exception e ) { 198 throw new BuildException( "Error - Couldn't write to the specified file" ); 199 } 200 201 if ( noReplaces == 0 ) { 202 System.out.println( "No Changes Made" ); 203 } else if ( noReplaces == 1 ) { 204 System.out.println( "Successfully changed 1 line" ); 205 } else { 206 System.out.println( "Successfully changed " + noReplaces + " lines" ); 207 } 235 // Transfer bytes from in to out 236 byte[] buf = new byte[1024]; 237 int len; 238 while ((len = i.read(buf)) > 0) { 239 o.write(buf, 0, len); 240 } 241 242 //close them up 243 i.close(); 244 o.close(); 245 } catch ( Exception e ) { 246 throw new BuildException( "Error - Couldn't write to the specified file" ); 247 } 248 249 250 if ( noReplaces == 0 ) { 251 System.out.println( " " + inputFile + " : No Changes Made" ); 252 } else if ( noReplaces == 1 ) { 253 System.out.println( " " + inputFile + " : Successfully changed 1 line" ); 254 } else { 255 System.out.println( " " + inputFile + " : Successfully changed " + noReplaces + " lines" ); 256 } 257 258 } 259 System.out.println(); 208 260 209 261 } 210 262 211 263 public void setFile(File file) { 264 if ( this.fileset != null || this.file != null ) { 265 throw new BuildException( "Error - Only one file or one fileset may be given!!" ); 266 } 212 267 this.file = file; 213 268 } … … 236 291 } 237 292 293 public FileSet createFileset() { 294 if ( this.fileset != null || this.file != null ) { 295 throw new BuildException( "Error - Only one file or one fileset may be given!!" ); 296 } 297 fileset = new FileSet(); 298 return fileset; 299 } 300 238 301 }
Note:
See TracChangeset
for help on using the changeset viewer.