1.1 --- a/make/tools/StripProperties/StripProperties.java Thu Sep 17 13:46:52 2009 -0700 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,329 +0,0 @@ 1.4 -/* 1.5 - * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved. 1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 - * 1.8 - * This code is free software; you can redistribute it and/or modify it 1.9 - * under the terms of the GNU General Public License version 2 only, as 1.10 - * published by the Free Software Foundation. Sun designates this 1.11 - * particular file as subject to the "Classpath" exception as provided 1.12 - * by Sun in the LICENSE file that accompanied this code. 1.13 - * 1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 - * version 2 for more details (a copy is included in the LICENSE file that 1.18 - * accompanied this code). 1.19 - * 1.20 - * You should have received a copy of the GNU General Public License version 1.21 - * 2 along with this work; if not, write to the Free Software Foundation, 1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 - * 1.24 - * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 - * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 - * have any questions. 1.27 - */ 1.28 - 1.29 -import java.io.BufferedInputStream; 1.30 -import java.io.BufferedWriter; 1.31 -import java.io.FileInputStream; 1.32 -import java.io.FileNotFoundException; 1.33 -import java.io.FileOutputStream; 1.34 -import java.io.OutputStream; 1.35 -import java.io.OutputStreamWriter; 1.36 -import java.io.IOException; 1.37 -import java.io.InputStream; 1.38 -import java.util.ArrayList; 1.39 -import java.util.Enumeration; 1.40 -import java.util.List; 1.41 -import java.util.Properties; 1.42 - 1.43 -/** 1.44 - * Reads a properties file from standard input and writes an equivalent 1.45 - * properties file without comments to standard output. 1.46 - */ 1.47 -public class StripProperties { 1.48 - 1.49 - public static void main(String[] args) { 1.50 - StripProperties sp = new StripProperties(); 1.51 - boolean ok = sp.run(args); 1.52 - if ( !ok ) { 1.53 - System.exit(1); 1.54 - } 1.55 - } 1.56 - 1.57 - static interface Log { 1.58 - void info(String msg); 1.59 - void verbose(String msg); 1.60 - void error(String msg, Exception e); 1.61 - } 1.62 - 1.63 - private String propfiles[]; 1.64 - private String outfiles[] ; 1.65 - private int stripCount = 0; 1.66 - private boolean quiet = false; 1.67 - private Log log; 1.68 - 1.69 - public void setLog(Log log) { 1.70 - this.log = log; 1.71 - } 1.72 - 1.73 - private boolean parseOptions(String args[]) { 1.74 - boolean ok = true; 1.75 - if ( stripCount > 0 ) { 1.76 - String new_propfiles[] = new String[stripCount + args.length]; 1.77 - String new_outfiles[] = new String[stripCount + args.length]; 1.78 - System.arraycopy(propfiles, 0, new_propfiles, 0, stripCount); 1.79 - System.arraycopy(outfiles, 0, new_outfiles, 0, stripCount); 1.80 - propfiles = new_propfiles; 1.81 - outfiles = new_outfiles; 1.82 - } else { 1.83 - propfiles = new String[args.length]; 1.84 - outfiles = new String[args.length]; 1.85 - } 1.86 - 1.87 - for ( int i = 0; i < args.length ; i++ ) { 1.88 - if ( "-strip".equals(args[i]) && i+2 < args.length ) { 1.89 - propfiles[stripCount] = args[++i]; 1.90 - outfiles[stripCount] = args[++i]; 1.91 - stripCount++; 1.92 - } else if ( "-optionsfile".equals(args[i]) && i+1 < args.length ) { 1.93 - String filename = args[++i]; 1.94 - FileInputStream finput = null; 1.95 - byte contents[] = null; 1.96 - try { 1.97 - finput = new FileInputStream(filename); 1.98 - int byteCount = finput.available(); 1.99 - if ( byteCount <= 0 ) { 1.100 - log.error("The -optionsfile file is empty", null); 1.101 - ok = false; 1.102 - } else { 1.103 - contents = new byte[byteCount]; 1.104 - int bytesRead = finput.read(contents); 1.105 - if ( byteCount != bytesRead ) { 1.106 - log.error("Cannot read all of -optionsfile file", null); 1.107 - ok = false; 1.108 - } 1.109 - } 1.110 - } catch ( IOException e ) { 1.111 - log.error("cannot open " + filename, e); 1.112 - ok = false; 1.113 - } 1.114 - if ( finput != null ) { 1.115 - try { 1.116 - finput.close(); 1.117 - } catch ( IOException e ) { 1.118 - ok = false; 1.119 - log.error("cannot close " + filename, e); 1.120 - } 1.121 - } 1.122 - if ( ok = true && contents != null ) { 1.123 - String tokens[] = (new String(contents)).split("\\s+"); 1.124 - if ( tokens.length > 0 ) { 1.125 - ok = parseOptions(tokens); 1.126 - } 1.127 - } 1.128 - if ( !ok ) { 1.129 - break; 1.130 - } 1.131 - } else if ( "-quiet".equals(args[i]) ) { 1.132 - quiet = true; 1.133 - } else { 1.134 - log.error("argument error", null); 1.135 - ok = false; 1.136 - } 1.137 - } 1.138 - return ok; 1.139 - } 1.140 - 1.141 - private boolean stripFiles(String propertiesPath, String outputPath) { 1.142 - boolean ok = true; 1.143 - Properties prop = new Properties(); 1.144 - InputStream in = null; 1.145 - try { 1.146 - in = new BufferedInputStream(new FileInputStream(propertiesPath)); 1.147 - prop.load(in); 1.148 - } catch ( FileNotFoundException e ) { 1.149 - log.error("Cannot access file " + propertiesPath, e); 1.150 - ok = false; 1.151 - } catch ( IOException e ) { 1.152 - log.error("IO exception processing file " + propertiesPath, e); 1.153 - ok = false; 1.154 - } 1.155 - if ( in != null ) { 1.156 - try { 1.157 - in.close(); 1.158 - } catch ( IOException e ) { 1.159 - log.error("IO exception closing file " + propertiesPath, e); 1.160 - ok = false; 1.161 - } 1.162 - } 1.163 - 1.164 - OutputStream out = null; 1.165 - try { 1.166 - out = new FileOutputStream(outputPath); 1.167 - storeProperties(prop, out); 1.168 - out.flush(); 1.169 - } catch ( IOException e ) { 1.170 - log.error("IO exception processing file " + outputPath, e); 1.171 - e.printStackTrace(); 1.172 - ok = false; 1.173 - } 1.174 - if ( out != null ) { 1.175 - try { 1.176 - out.close(); 1.177 - } catch ( IOException e ) { 1.178 - log.error("IO exception closing file " + outputPath, e); 1.179 - ok = false; 1.180 - } 1.181 - } 1.182 - return ok; 1.183 - } 1.184 - 1.185 - /** 1.186 - * Strip the properties filenames supplied, replacing their contents. 1.187 - * @param args Names of properties files to process and replace contents 1.188 - */ 1.189 - public boolean run(String args[]) { 1.190 - if (log == null) { 1.191 - log = new Log() { 1.192 - public void error(String msg, Exception e) { 1.193 - System.err.println("ERROR: StripProperties: " + msg); 1.194 - if ( e != null ) { 1.195 - System.err.println("EXCEPTION: " + e.toString()); 1.196 - e.printStackTrace(); 1.197 - } 1.198 - } 1.199 - public void info(String msg) { 1.200 - System.out.println(msg); 1.201 - } 1.202 - public void verbose(String msg) { 1.203 - if (!quiet) 1.204 - System.out.println(msg); 1.205 - } 1.206 - }; 1.207 - } 1.208 - 1.209 - boolean ok = true; 1.210 - ok = parseOptions(args); 1.211 - if ( ok && stripCount == 0 ) { 1.212 - log.error("options parsed but no files to compile", null); 1.213 - ok = false; 1.214 - } 1.215 - /* Need at least one file. */ 1.216 - if ( !ok ) { 1.217 - //usage(log); 1.218 - } else { 1.219 - /* Process files */ 1.220 - for ( int i = 0; i < stripCount && ok ; i++ ) { 1.221 - ok = stripFiles(propfiles[i], outfiles[i]); 1.222 - } 1.223 - } 1.224 - return ok; 1.225 - } 1.226 - 1.227 - // --- code below here is adapted from java.util.Properties --- 1.228 - 1.229 - private static final String specialSaveChars = "=: \t\r\n\f#!"; 1.230 - 1.231 - /* 1.232 - * Converts unicodes to encoded \uxxxx 1.233 - * and writes out any of the characters in specialSaveChars 1.234 - * with a preceding slash 1.235 - */ 1.236 - private static String saveConvert(String theString, boolean escapeSpace) { 1.237 - int len = theString.length(); 1.238 - StringBuffer outBuffer = new StringBuffer(len*2); 1.239 - 1.240 - for(int x=0; x<len; x++) { 1.241 - char aChar = theString.charAt(x); 1.242 - switch(aChar) { 1.243 - case ' ': 1.244 - if (x == 0 || escapeSpace) { 1.245 - outBuffer.append('\\'); 1.246 - } 1.247 - outBuffer.append(' '); 1.248 - break; 1.249 - case '\\': 1.250 - outBuffer.append('\\'); 1.251 - outBuffer.append('\\'); 1.252 - break; 1.253 - case '\t': 1.254 - outBuffer.append('\\'); 1.255 - outBuffer.append('t'); 1.256 - break; 1.257 - case '\n': 1.258 - outBuffer.append('\\'); 1.259 - outBuffer.append('n'); 1.260 - break; 1.261 - case '\r': 1.262 - outBuffer.append('\\'); 1.263 - outBuffer.append('r'); 1.264 - break; 1.265 - case '\f': 1.266 - outBuffer.append('\\'); 1.267 - outBuffer.append('f'); 1.268 - break; 1.269 - default: 1.270 - if ((aChar < 0x0020) || (aChar == 0x007e) || (aChar > 0x00ff)) { 1.271 - outBuffer.append('\\'); 1.272 - outBuffer.append('u'); 1.273 - outBuffer.append(toHex((aChar >> 12) & 0xF)); 1.274 - outBuffer.append(toHex((aChar >> 8) & 0xF)); 1.275 - outBuffer.append(toHex((aChar >> 4) & 0xF)); 1.276 - outBuffer.append(toHex( aChar & 0xF)); 1.277 - } else { 1.278 - if (specialSaveChars.indexOf(aChar) != -1) { 1.279 - outBuffer.append('\\'); 1.280 - } 1.281 - outBuffer.append(aChar); 1.282 - } 1.283 - } 1.284 - } 1.285 - return outBuffer.toString(); 1.286 - } 1.287 - 1.288 - /** 1.289 - * Writes the content of <code>properties</code> to <code>out</code>. 1.290 - * The format is that of Properties.store with the following modifications: 1.291 - * <ul> 1.292 - * <li>No header or date is written 1.293 - * <li>Latin-1 characters are written as single bytes, not escape sequences 1.294 - * <li>Line breaks are indicated by a single \n independent of platform 1.295 - * <ul> 1.296 - */ 1.297 - private static void storeProperties(Properties properties, OutputStream out) 1.298 - throws IOException { 1.299 - BufferedWriter awriter; 1.300 - awriter = new BufferedWriter(new OutputStreamWriter(out, "8859_1")); 1.301 - for (Enumeration e = properties.keys(); e.hasMoreElements();) { 1.302 - String key = (String)e.nextElement(); 1.303 - String val = (String)properties.get(key); 1.304 - key = saveConvert(key, true); 1.305 - 1.306 - /* No need to escape embedded and trailing spaces for value, hence 1.307 - * pass false to flag. 1.308 - */ 1.309 - val = saveConvert(val, false); 1.310 - writeln(awriter, key + "=" + val); 1.311 - } 1.312 - awriter.flush(); 1.313 - } 1.314 - 1.315 - private static void writeln(BufferedWriter bw, String s) throws IOException { 1.316 - bw.write(s); 1.317 - bw.write("\n"); 1.318 - } 1.319 - 1.320 - /** 1.321 - * Convert a nibble to a hex character 1.322 - * @param nibble the nibble to convert. 1.323 - */ 1.324 - private static char toHex(int nibble) { 1.325 - return hexDigit[(nibble & 0xF)]; 1.326 - } 1.327 - 1.328 - /** A table of hex digits */ 1.329 - private static final char[] hexDigit = { 1.330 - '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' 1.331 - }; 1.332 -}