| 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:" ); |
|---|
| 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") ); |
|---|
| 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; |
|---|
| 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 | } |
|---|
| 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 { |
|---|
| 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(); |
|---|