1.1 --- a/make/tools/src/build/tools/stripproperties/StripProperties.java Fri Mar 09 11:56:14 2012 -0800 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,280 +0,0 @@ 1.4 -/* 1.5 - * Copyright (c) 2001, Oracle and/or its affiliates. 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. Oracle designates this 1.11 - * particular file as subject to the "Classpath" exception as provided 1.12 - * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 - * or visit www.oracle.com if you need additional information or have any 1.26 - * questions. 1.27 - */ 1.28 - 1.29 -package build.tools.stripproperties; 1.30 - 1.31 -import java.io.BufferedInputStream; 1.32 -import java.io.BufferedWriter; 1.33 -import java.io.FileInputStream; 1.34 -import java.io.FileNotFoundException; 1.35 -import java.io.FileOutputStream; 1.36 -import java.io.OutputStream; 1.37 -import java.io.OutputStreamWriter; 1.38 -import java.io.IOException; 1.39 -import java.io.InputStream; 1.40 -import java.util.ArrayList; 1.41 -import java.util.Enumeration; 1.42 -import java.util.List; 1.43 -import java.util.Properties; 1.44 - 1.45 -/** 1.46 - * Reads a properties file from standard input and writes an equivalent 1.47 - * properties file without comments to standard output. 1.48 - */ 1.49 -public class StripProperties { 1.50 - 1.51 - private static void error(String msg, Exception e) { 1.52 - System.err.println("ERROR: stripproperties: " + msg); 1.53 - if ( e != null ) { 1.54 - System.err.println("EXCEPTION: " + e.toString()); 1.55 - e.printStackTrace(); 1.56 - } 1.57 - } 1.58 - 1.59 - private static List<String> parseOptions(String args[]) { 1.60 - List<String> files = new ArrayList<String>(); 1.61 - for ( int i = 0; i < args.length ; i++ ) { 1.62 - if ( "-optionsfile".equals(args[i]) && i+1 < args.length ) { 1.63 - String filename = args[++i]; 1.64 - FileInputStream finput = null; 1.65 - byte contents[] = null; 1.66 - try { 1.67 - finput = new FileInputStream(filename); 1.68 - int byteCount = finput.available(); 1.69 - if ( byteCount <= 0 ) { 1.70 - error("The -optionsfile file is empty", null); 1.71 - files = null; 1.72 - } else { 1.73 - contents = new byte[byteCount]; 1.74 - int bytesRead = finput.read(contents); 1.75 - if ( byteCount != bytesRead ) { 1.76 - error("Cannot read all of -optionsfile file", null); 1.77 - files = null; 1.78 - } 1.79 - } 1.80 - } catch ( IOException e ) { 1.81 - error("cannot open " + filename, e); 1.82 - files = null; 1.83 - } 1.84 - if ( finput != null ) { 1.85 - try { 1.86 - finput.close(); 1.87 - } catch ( IOException e ) { 1.88 - files = null; 1.89 - error("cannot close " + filename, e); 1.90 - } 1.91 - } 1.92 - if ( files != null && contents != null ) { 1.93 - String tokens[] = (new String(contents)).split("\\s+"); 1.94 - if ( tokens.length > 0 ) { 1.95 - List<String> ofiles = parseOptions(tokens); 1.96 - if ( ofiles != null ) { 1.97 - files.addAll(ofiles); 1.98 - } else { 1.99 - error("No files found in file", null); 1.100 - files = null; 1.101 - } 1.102 - } 1.103 - } 1.104 - if ( files == null ) { 1.105 - break; 1.106 - } 1.107 - } else { 1.108 - files.add(args[i]); 1.109 - } 1.110 - } 1.111 - return files; 1.112 - } 1.113 - 1.114 - private static boolean stripFiles(List<String> files) { 1.115 - boolean ok = true; 1.116 - for ( String file : files ) { 1.117 - 1.118 - Properties prop = new Properties(); 1.119 - InputStream in = null; 1.120 - try { 1.121 - in = new BufferedInputStream(new FileInputStream(file)); 1.122 - prop.load(in); 1.123 - } catch ( FileNotFoundException e ) { 1.124 - error("Cannot access file " + file, e); 1.125 - ok = false; 1.126 - } catch ( IOException e ) { 1.127 - error("IO exception processing file " + file, e); 1.128 - ok = false; 1.129 - } 1.130 - if ( in != null ) { 1.131 - try { 1.132 - in.close(); 1.133 - } catch ( IOException e ) { 1.134 - error("IO exception closing file " + file, e); 1.135 - ok = false; 1.136 - } 1.137 - } 1.138 - if ( !ok ) { 1.139 - break; 1.140 - } 1.141 - 1.142 - OutputStream out = null; 1.143 - try { 1.144 - out = new FileOutputStream(file); 1.145 - storeProperties(prop, out); 1.146 - out.flush(); 1.147 - } catch ( IOException e ) { 1.148 - error("IO exception processing file " + file, e); 1.149 - ok = false; 1.150 - } 1.151 - if ( out != null ) { 1.152 - try { 1.153 - out.close(); 1.154 - } catch ( IOException e ) { 1.155 - error("IO exception closing file " + file, e); 1.156 - ok = false; 1.157 - } 1.158 - } 1.159 - if ( !ok ) { 1.160 - break; 1.161 - } 1.162 - 1.163 - } 1.164 - return ok; 1.165 - } 1.166 - 1.167 - /** 1.168 - * Strip the properties filenames supplied, replacing their contents. 1.169 - * @param args Names of properties files to process and replace contents 1.170 - */ 1.171 - public static void main(String args[]) { 1.172 - List<String> files = parseOptions(args); 1.173 - if ( files == null || !stripFiles(files) ) { 1.174 - System.exit(1); 1.175 - } 1.176 - } 1.177 - 1.178 - // --- code below here is adapted from java.util.Properties --- 1.179 - 1.180 - private static final String specialSaveChars = "=: \t\r\n\f#!"; 1.181 - 1.182 - /* 1.183 - * Converts unicodes to encoded \uxxxx 1.184 - * and writes out any of the characters in specialSaveChars 1.185 - * with a preceding slash 1.186 - */ 1.187 - private static String saveConvert(String theString, boolean escapeSpace) { 1.188 - int len = theString.length(); 1.189 - StringBuffer outBuffer = new StringBuffer(len*2); 1.190 - 1.191 - for(int x=0; x<len; x++) { 1.192 - char aChar = theString.charAt(x); 1.193 - switch(aChar) { 1.194 - case ' ': 1.195 - if (x == 0 || escapeSpace) { 1.196 - outBuffer.append('\\'); 1.197 - } 1.198 - outBuffer.append(' '); 1.199 - break; 1.200 - case '\\': 1.201 - outBuffer.append('\\'); 1.202 - outBuffer.append('\\'); 1.203 - break; 1.204 - case '\t': 1.205 - outBuffer.append('\\'); 1.206 - outBuffer.append('t'); 1.207 - break; 1.208 - case '\n': 1.209 - outBuffer.append('\\'); 1.210 - outBuffer.append('n'); 1.211 - break; 1.212 - case '\r': 1.213 - outBuffer.append('\\'); 1.214 - outBuffer.append('r'); 1.215 - break; 1.216 - case '\f': 1.217 - outBuffer.append('\\'); 1.218 - outBuffer.append('f'); 1.219 - break; 1.220 - default: 1.221 - if ((aChar < 0x0020) || (aChar == 0x007e) || (aChar > 0x00ff)) { 1.222 - outBuffer.append('\\'); 1.223 - outBuffer.append('u'); 1.224 - outBuffer.append(toHex((aChar >> 12) & 0xF)); 1.225 - outBuffer.append(toHex((aChar >> 8) & 0xF)); 1.226 - outBuffer.append(toHex((aChar >> 4) & 0xF)); 1.227 - outBuffer.append(toHex( aChar & 0xF)); 1.228 - } else { 1.229 - if (specialSaveChars.indexOf(aChar) != -1) { 1.230 - outBuffer.append('\\'); 1.231 - } 1.232 - outBuffer.append(aChar); 1.233 - } 1.234 - } 1.235 - } 1.236 - return outBuffer.toString(); 1.237 - } 1.238 - 1.239 - /** 1.240 - * Writes the content of <code>properties</code> to <code>out</code>. 1.241 - * The format is that of Properties.store with the following modifications: 1.242 - * <ul> 1.243 - * <li>No header or date is written 1.244 - * <li>Latin-1 characters are written as single bytes, not escape sequences 1.245 - * <li>Line breaks are indicated by a single \n independent of platform 1.246 - * <ul> 1.247 - */ 1.248 - private static void storeProperties(Properties properties, OutputStream out) 1.249 - throws IOException { 1.250 - BufferedWriter awriter; 1.251 - awriter = new BufferedWriter(new OutputStreamWriter(out, "8859_1")); 1.252 - for (Enumeration e = properties.keys(); e.hasMoreElements();) { 1.253 - String key = (String)e.nextElement(); 1.254 - String val = (String)properties.get(key); 1.255 - key = saveConvert(key, true); 1.256 - 1.257 - /* No need to escape embedded and trailing spaces for value, hence 1.258 - * pass false to flag. 1.259 - */ 1.260 - val = saveConvert(val, false); 1.261 - writeln(awriter, key + "=" + val); 1.262 - } 1.263 - awriter.flush(); 1.264 - } 1.265 - 1.266 - private static void writeln(BufferedWriter bw, String s) throws IOException { 1.267 - bw.write(s); 1.268 - bw.write("\n"); 1.269 - } 1.270 - 1.271 - /** 1.272 - * Convert a nibble to a hex character 1.273 - * @param nibble the nibble to convert. 1.274 - */ 1.275 - private static char toHex(int nibble) { 1.276 - return hexDigit[(nibble & 0xF)]; 1.277 - } 1.278 - 1.279 - /** A table of hex digits */ 1.280 - private static final char[] hexDigit = { 1.281 - '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' 1.282 - }; 1.283 -}