1 /* |
|
2 * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package build.tools.stripproperties; |
|
27 |
|
28 import java.io.BufferedInputStream; |
|
29 import java.io.BufferedWriter; |
|
30 import java.io.FileInputStream; |
|
31 import java.io.FileNotFoundException; |
|
32 import java.io.FileOutputStream; |
|
33 import java.io.OutputStream; |
|
34 import java.io.OutputStreamWriter; |
|
35 import java.io.IOException; |
|
36 import java.io.InputStream; |
|
37 import java.util.ArrayList; |
|
38 import java.util.Enumeration; |
|
39 import java.util.List; |
|
40 import java.util.Properties; |
|
41 |
|
42 /** |
|
43 * Reads a properties file from standard input and writes an equivalent |
|
44 * properties file without comments to standard output. |
|
45 */ |
|
46 public class StripProperties { |
|
47 |
|
48 private static void error(String msg, Exception e) { |
|
49 System.err.println("ERROR: stripproperties: " + msg); |
|
50 if ( e != null ) { |
|
51 System.err.println("EXCEPTION: " + e.toString()); |
|
52 e.printStackTrace(); |
|
53 } |
|
54 } |
|
55 |
|
56 private static List<String> parseOptions(String args[]) { |
|
57 List<String> files = new ArrayList<String>(); |
|
58 for ( int i = 0; i < args.length ; i++ ) { |
|
59 if ( "-optionsfile".equals(args[i]) && i+1 < args.length ) { |
|
60 String filename = args[++i]; |
|
61 FileInputStream finput = null; |
|
62 byte contents[] = null; |
|
63 try { |
|
64 finput = new FileInputStream(filename); |
|
65 int byteCount = finput.available(); |
|
66 if ( byteCount <= 0 ) { |
|
67 error("The -optionsfile file is empty", null); |
|
68 files = null; |
|
69 } else { |
|
70 contents = new byte[byteCount]; |
|
71 int bytesRead = finput.read(contents); |
|
72 if ( byteCount != bytesRead ) { |
|
73 error("Cannot read all of -optionsfile file", null); |
|
74 files = null; |
|
75 } |
|
76 } |
|
77 } catch ( IOException e ) { |
|
78 error("cannot open " + filename, e); |
|
79 files = null; |
|
80 } |
|
81 if ( finput != null ) { |
|
82 try { |
|
83 finput.close(); |
|
84 } catch ( IOException e ) { |
|
85 files = null; |
|
86 error("cannot close " + filename, e); |
|
87 } |
|
88 } |
|
89 if ( files != null && contents != null ) { |
|
90 String tokens[] = (new String(contents)).split("\\s+"); |
|
91 if ( tokens.length > 0 ) { |
|
92 List<String> ofiles = parseOptions(tokens); |
|
93 if ( ofiles != null ) { |
|
94 files.addAll(ofiles); |
|
95 } else { |
|
96 error("No files found in file", null); |
|
97 files = null; |
|
98 } |
|
99 } |
|
100 } |
|
101 if ( files == null ) { |
|
102 break; |
|
103 } |
|
104 } else { |
|
105 files.add(args[i]); |
|
106 } |
|
107 } |
|
108 return files; |
|
109 } |
|
110 |
|
111 private static boolean stripFiles(List<String> files) { |
|
112 boolean ok = true; |
|
113 for ( String file : files ) { |
|
114 |
|
115 Properties prop = new Properties(); |
|
116 InputStream in = null; |
|
117 try { |
|
118 in = new BufferedInputStream(new FileInputStream(file)); |
|
119 prop.load(in); |
|
120 } catch ( FileNotFoundException e ) { |
|
121 error("Cannot access file " + file, e); |
|
122 ok = false; |
|
123 } catch ( IOException e ) { |
|
124 error("IO exception processing file " + file, e); |
|
125 ok = false; |
|
126 } |
|
127 if ( in != null ) { |
|
128 try { |
|
129 in.close(); |
|
130 } catch ( IOException e ) { |
|
131 error("IO exception closing file " + file, e); |
|
132 ok = false; |
|
133 } |
|
134 } |
|
135 if ( !ok ) { |
|
136 break; |
|
137 } |
|
138 |
|
139 OutputStream out = null; |
|
140 try { |
|
141 out = new FileOutputStream(file); |
|
142 storeProperties(prop, out); |
|
143 out.flush(); |
|
144 } catch ( IOException e ) { |
|
145 error("IO exception processing file " + file, e); |
|
146 ok = false; |
|
147 } |
|
148 if ( out != null ) { |
|
149 try { |
|
150 out.close(); |
|
151 } catch ( IOException e ) { |
|
152 error("IO exception closing file " + file, e); |
|
153 ok = false; |
|
154 } |
|
155 } |
|
156 if ( !ok ) { |
|
157 break; |
|
158 } |
|
159 |
|
160 } |
|
161 return ok; |
|
162 } |
|
163 |
|
164 /** |
|
165 * Strip the properties filenames supplied, replacing their contents. |
|
166 * @param args Names of properties files to process and replace contents |
|
167 */ |
|
168 public static void main(String args[]) { |
|
169 List<String> files = parseOptions(args); |
|
170 if ( files == null || !stripFiles(files) ) { |
|
171 System.exit(1); |
|
172 } |
|
173 } |
|
174 |
|
175 // --- code below here is adapted from java.util.Properties --- |
|
176 |
|
177 private static final String specialSaveChars = "=: \t\r\n\f#!"; |
|
178 |
|
179 /* |
|
180 * Converts unicodes to encoded \uxxxx |
|
181 * and writes out any of the characters in specialSaveChars |
|
182 * with a preceding slash |
|
183 */ |
|
184 private static String saveConvert(String theString, boolean escapeSpace) { |
|
185 int len = theString.length(); |
|
186 StringBuffer outBuffer = new StringBuffer(len*2); |
|
187 |
|
188 for(int x=0; x<len; x++) { |
|
189 char aChar = theString.charAt(x); |
|
190 switch(aChar) { |
|
191 case ' ': |
|
192 if (x == 0 || escapeSpace) { |
|
193 outBuffer.append('\\'); |
|
194 } |
|
195 outBuffer.append(' '); |
|
196 break; |
|
197 case '\\': |
|
198 outBuffer.append('\\'); |
|
199 outBuffer.append('\\'); |
|
200 break; |
|
201 case '\t': |
|
202 outBuffer.append('\\'); |
|
203 outBuffer.append('t'); |
|
204 break; |
|
205 case '\n': |
|
206 outBuffer.append('\\'); |
|
207 outBuffer.append('n'); |
|
208 break; |
|
209 case '\r': |
|
210 outBuffer.append('\\'); |
|
211 outBuffer.append('r'); |
|
212 break; |
|
213 case '\f': |
|
214 outBuffer.append('\\'); |
|
215 outBuffer.append('f'); |
|
216 break; |
|
217 default: |
|
218 if ((aChar < 0x0020) || (aChar == 0x007e) || (aChar > 0x00ff)) { |
|
219 outBuffer.append('\\'); |
|
220 outBuffer.append('u'); |
|
221 outBuffer.append(toHex((aChar >> 12) & 0xF)); |
|
222 outBuffer.append(toHex((aChar >> 8) & 0xF)); |
|
223 outBuffer.append(toHex((aChar >> 4) & 0xF)); |
|
224 outBuffer.append(toHex( aChar & 0xF)); |
|
225 } else { |
|
226 if (specialSaveChars.indexOf(aChar) != -1) { |
|
227 outBuffer.append('\\'); |
|
228 } |
|
229 outBuffer.append(aChar); |
|
230 } |
|
231 } |
|
232 } |
|
233 return outBuffer.toString(); |
|
234 } |
|
235 |
|
236 /** |
|
237 * Writes the content of <code>properties</code> to <code>out</code>. |
|
238 * The format is that of Properties.store with the following modifications: |
|
239 * <ul> |
|
240 * <li>No header or date is written |
|
241 * <li>Latin-1 characters are written as single bytes, not escape sequences |
|
242 * <li>Line breaks are indicated by a single \n independent of platform |
|
243 * <ul> |
|
244 */ |
|
245 private static void storeProperties(Properties properties, OutputStream out) |
|
246 throws IOException { |
|
247 BufferedWriter awriter; |
|
248 awriter = new BufferedWriter(new OutputStreamWriter(out, "8859_1")); |
|
249 for (Enumeration e = properties.keys(); e.hasMoreElements();) { |
|
250 String key = (String)e.nextElement(); |
|
251 String val = (String)properties.get(key); |
|
252 key = saveConvert(key, true); |
|
253 |
|
254 /* No need to escape embedded and trailing spaces for value, hence |
|
255 * pass false to flag. |
|
256 */ |
|
257 val = saveConvert(val, false); |
|
258 writeln(awriter, key + "=" + val); |
|
259 } |
|
260 awriter.flush(); |
|
261 } |
|
262 |
|
263 private static void writeln(BufferedWriter bw, String s) throws IOException { |
|
264 bw.write(s); |
|
265 bw.write("\n"); |
|
266 } |
|
267 |
|
268 /** |
|
269 * Convert a nibble to a hex character |
|
270 * @param nibble the nibble to convert. |
|
271 */ |
|
272 private static char toHex(int nibble) { |
|
273 return hexDigit[(nibble & 0xF)]; |
|
274 } |
|
275 |
|
276 /** A table of hex digits */ |
|
277 private static final char[] hexDigit = { |
|
278 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' |
|
279 }; |
|
280 } |
|