1 /* Copyright 2004 The Apache Software Foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package repackage; 17 18 import java.io; 19 20 public class EditBuildScript 21 { 22 // 23 // usgae: edit buildfile token new-value 24 // 25 26 public static void main ( String[] args ) 27 throws Exception 28 { 29 if (args.length != 3) 30 throw new IllegalArgumentException( "Wrong number of arguments" ); 31 32 args[ 0 ] = args[ 0 ].replace( '/', File.separatorChar ); 33 34 File buildFile = new File( args[ 0 ] ); 35 36 StringBuffer sb = readFile( buildFile ); 37 38 String tokenStr = "<property name=\"" + args[ 1 ] + "\" value=\""; 39 40 int i = sb.indexOf( tokenStr ); 41 42 if (i < 0) 43 throw new IllegalArgumentException( "Can't find token: " + tokenStr ); 44 45 int j = i + tokenStr.length(); 46 47 while ( sb.charAt( j ) != '"' ) 48 j++; 49 50 sb.replace( i + tokenStr.length(), j, args[ 2 ] ); 51 52 writeFile( buildFile, sb ); 53 } 54 55 static StringBuffer readFile ( File f ) 56 throws IOException 57 { 58 InputStream in = new FileInputStream( f ); 59 Reader r = new InputStreamReader( in ); 60 StringWriter w = new StringWriter(); 61 62 copy( r, w ); 63 64 w.close(); 65 r.close(); 66 in.close(); 67 68 return w.getBuffer(); 69 } 70 71 static void writeFile ( File f, StringBuffer chars ) 72 throws IOException 73 { 74 OutputStream out = new FileOutputStream( f ); 75 Writer w = new OutputStreamWriter( out ); 76 Reader r = new StringReader( chars.toString() ); 77 78 copy( r, w ); 79 80 r.close(); 81 w.close(); 82 out.close(); 83 } 84 85 static void copy ( Reader r, Writer w ) throws IOException 86 { 87 char[] buffer = new char [ 1024 * 16 ]; 88 89 for ( ; ; ) 90 { 91 int n = r.read( buffer, 0, buffer.length ); 92 93 if (n < 0) 94 break; 95 96 w.write( buffer, 0, n ); 97 } 98 } 99 }