Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/stripproperties/StripProperties.java
32287 views
/*1* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package build.tools.stripproperties;2627import java.io.BufferedInputStream;28import java.io.BufferedWriter;29import java.io.FileInputStream;30import java.io.FileNotFoundException;31import java.io.FileOutputStream;32import java.io.OutputStream;33import java.io.OutputStreamWriter;34import java.io.IOException;35import java.io.InputStream;36import java.util.ArrayList;37import java.util.Enumeration;38import java.util.Iterator;39import java.util.List;40import java.util.Properties;4142/**43* Reads a properties file from standard input and writes an equivalent44* properties file without comments to standard output.45*/46public class StripProperties {4748private static void error(String msg, Exception e) {49System.err.println("ERROR: stripproperties: " + msg);50if ( e != null ) {51System.err.println("EXCEPTION: " + e.toString());52e.printStackTrace();53}54}5556private static List<String> infiles = new ArrayList<String>();57private static List<String> outfiles = new ArrayList<String>();5859private static boolean parseOptions(String args[]) {60boolean ok = true;6162for ( int i = 0; i < args.length ; i++ ) {63if ( "-clean".equals(args[i]) && i+2 < args.length ) {64infiles.add(args[++i]);65outfiles.add(args[++i]);66} else if ( args[i].charAt(0)=='@') {67String filename = args[i].substring(1);68FileInputStream finput = null;69byte contents[] = null;70try {71finput = new FileInputStream(filename);72int byteCount = finput.available();73if ( byteCount <= 0 ) {74error("The @file is empty", null);75ok = false;76} else {77contents = new byte[byteCount];78int bytesRead = finput.read(contents);79if ( byteCount != bytesRead ) {80error("Cannot read all of @file", null);81ok = false;82}83}84} catch ( IOException e ) {85error("cannot open " + filename, e);86ok = false;87}88if ( finput != null ) {89try {90finput.close();91} catch ( IOException e ) {92ok = false;93error("cannot close " + filename, e);94}95}96if ( ok && contents != null ) {97String tokens[] = (new String(contents)).split("\\s+");98if ( tokens.length > 0 ) {99ok = parseOptions(tokens);100}101}102if ( !ok ) {103break;104}105} else {106infiles.add(args[i]);107outfiles.add(args[i]);108}109}110return ok;111}112113private static boolean stripFiles(List<String> infiles, List<String> outfiles) {114boolean ok = true;115Iterator<String> inIter = infiles.iterator();116Iterator<String> outIter = outfiles.iterator();117118for (; inIter.hasNext(); ) {119String infile = inIter.next();120String outfile = outIter.next();121122Properties prop = new Properties();123InputStream in = null;124try {125in = new BufferedInputStream(new FileInputStream(infile));126prop.load(in);127} catch ( FileNotFoundException e ) {128error("Cannot access file " + infile, e);129ok = false;130} catch ( IOException e ) {131error("IO exception processing file " + infile, e);132ok = false;133}134if ( in != null ) {135try {136in.close();137} catch ( IOException e ) {138error("IO exception closing file " + infile, e);139ok = false;140}141}142if ( !ok ) {143break;144}145146OutputStream out = null;147try {148out = new FileOutputStream(outfile);149storeProperties(prop, out);150out.flush();151} catch ( IOException e ) {152error("IO exception processing file " + outfile, e);153ok = false;154}155if ( out != null ) {156try {157out.close();158} catch ( IOException e ) {159error("IO exception closing file " + outfile, e);160ok = false;161}162}163if ( !ok ) {164break;165}166167}168return ok;169}170171/**172* Strip the properties filenames supplied, replacing their contents.173* @param args Names of properties files to process and replace contents174*/175public static void main(String args[]) {176boolean ok = parseOptions(args);177if ( !ok || !stripFiles(infiles, outfiles) ) {178System.exit(1);179}180}181182// --- code below here is adapted from java.util.Properties ---183184private static final String specialSaveChars = "=: \t\r\n\f#!";185186/*187* Converts unicodes to encoded \uxxxx188* and writes out any of the characters in specialSaveChars189* with a preceding slash190*/191private static String saveConvert(String theString, boolean escapeSpace) {192int len = theString.length();193StringBuffer outBuffer = new StringBuffer(len*2);194195for(int x=0; x<len; x++) {196char aChar = theString.charAt(x);197switch(aChar) {198case ' ':199if (x == 0 || escapeSpace) {200outBuffer.append('\\');201}202outBuffer.append(' ');203break;204case '\\':205outBuffer.append('\\');206outBuffer.append('\\');207break;208case '\t':209outBuffer.append('\\');210outBuffer.append('t');211break;212case '\n':213outBuffer.append('\\');214outBuffer.append('n');215break;216case '\r':217outBuffer.append('\\');218outBuffer.append('r');219break;220case '\f':221outBuffer.append('\\');222outBuffer.append('f');223break;224default:225if ((aChar < 0x0020) || (aChar == 0x007e) || (aChar > 0x00ff)) {226outBuffer.append('\\');227outBuffer.append('u');228outBuffer.append(toHex((aChar >> 12) & 0xF));229outBuffer.append(toHex((aChar >> 8) & 0xF));230outBuffer.append(toHex((aChar >> 4) & 0xF));231outBuffer.append(toHex( aChar & 0xF));232} else {233if (specialSaveChars.indexOf(aChar) != -1) {234outBuffer.append('\\');235}236outBuffer.append(aChar);237}238}239}240return outBuffer.toString();241}242243/**244* Writes the content of <code>properties</code> to <code>out</code>.245* The format is that of Properties.store with the following modifications:246* <ul>247* <li>No header or date is written248* <li>Latin-1 characters are written as single bytes, not escape sequences249* <li>Line breaks are indicated by a single \n independent of platform250* <ul>251*/252private static void storeProperties(Properties properties, OutputStream out)253throws IOException {254BufferedWriter awriter;255awriter = new BufferedWriter(new OutputStreamWriter(out, "8859_1"));256for (Enumeration<Object> e = properties.keys(); e.hasMoreElements();) {257String key = (String)e.nextElement();258String val = (String)properties.get(key);259key = saveConvert(key, true);260261/* No need to escape embedded and trailing spaces for value, hence262* pass false to flag.263*/264val = saveConvert(val, false);265writeln(awriter, key + "=" + val);266}267awriter.flush();268}269270private static void writeln(BufferedWriter bw, String s) throws IOException {271bw.write(s);272bw.write("\n");273}274275/**276* Convert a nibble to a hex character277* @param nibble the nibble to convert.278*/279private static char toHex(int nibble) {280return hexDigit[(nibble & 0xF)];281}282283/** A table of hex digits */284private static final char[] hexDigit = {285'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'286};287}288289290