1 /* 2 * Copyright (c) 2002, 2013, 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.compileproperties; 27 28 import java.io.BufferedWriter; 29 import java.io.File; 30 import java.io.FileInputStream; 31 import java.io.FileNotFoundException; 32 import java.io.FileOutputStream; 33 import java.io.IOException; 34 import java.io.OutputStreamWriter; 35 import java.io.Writer; 36 import java.text.MessageFormat; 37 import java.util.ArrayList; 38 import java.util.Collections; 39 import java.util.List; 40 import java.util.Properties; 41 42 /** Translates a .properties file into a .java file containing the 43 * definition of a java.util.Properties subclass which can then be 44 * compiled with javac. <P> 45 * 46 * Usage: java -jar compileproperties.jar [path to .properties file] [path to .java file to be output] [super class] 47 * 48 * Infers the package by looking at the common suffix of the two 49 * inputs, eliminating "classes" from it. 50 * 51 * @author Scott Violet 52 * @author Kenneth Russell 53 */ 54 55 public class CompileProperties { 56 private static final String FORMAT = 57 "{0}" + 58 "import java.util.ListResourceBundle;\n\n" + 59 "public final class {1} extends {2} '{'\n" + 60 " protected final Object[][] getContents() '{'\n" + 61 " return new Object[][] '{'\n" + 62 "{3}" + 63 " };\n" + 64 " }\n" + 65 "}\n"; 66 67 68 // This comes from Properties 69 private static final char[] hexDigit = { 70 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' 71 }; 72 73 // Note: different from that in Properties 74 private static final String specialSaveChars = "\""; 75 76 // This comes from Properties toHex(int nibble)77 private static char toHex(int nibble) { 78 return hexDigit[(nibble & 0xF)]; 79 } 80 error(String msg, Exception e)81 private static void error(String msg, Exception e) { 82 System.err.println("ERROR: compileproperties: " + msg); 83 if ( e != null ) { 84 System.err.println("EXCEPTION: " + e.toString()); 85 e.printStackTrace(); 86 } 87 } 88 89 private static String propfiles[]; 90 private static String outfiles[] ; 91 private static String supers[] ; 92 private static int compileCount = 0; 93 private static boolean quiet = false; 94 parseOptions(String args[])95 private static boolean parseOptions(String args[]) { 96 boolean ok = true; 97 if ( compileCount > 0 ) { 98 String new_propfiles[] = new String[compileCount + args.length]; 99 String new_outfiles[] = new String[compileCount + args.length]; 100 String new_supers[] = new String[compileCount + args.length]; 101 System.arraycopy(propfiles, 0, new_propfiles, 0, compileCount); 102 System.arraycopy(outfiles, 0, new_outfiles, 0, compileCount); 103 System.arraycopy(supers, 0, new_supers, 0, compileCount); 104 propfiles = new_propfiles; 105 outfiles = new_outfiles; 106 supers = new_supers; 107 } else { 108 propfiles = new String[args.length]; 109 outfiles = new String[args.length]; 110 supers = new String[args.length]; 111 } 112 for ( int i = 0; i < args.length ; i++ ) { 113 if ( "-compile".equals(args[i]) && i+3 < args.length ) { 114 propfiles[compileCount] = args[++i]; 115 outfiles[compileCount] = args[++i]; 116 supers[compileCount] = args[++i]; 117 compileCount++; 118 } else if ( args[i].charAt(0) == '@') { 119 String filename = args[i].substring(1); 120 FileInputStream finput = null; 121 byte contents[] = null; 122 try { 123 finput = new FileInputStream(filename); 124 int byteCount = finput.available(); 125 if ( byteCount <= 0 ) { 126 error("The @file is empty", null); 127 ok = false; 128 } else { 129 contents = new byte[byteCount]; 130 int bytesRead = finput.read(contents); 131 if ( byteCount != bytesRead ) { 132 error("Cannot read all of @file", null); 133 ok = false; 134 } 135 } 136 } catch ( IOException e ) { 137 error("cannot open " + filename, e); 138 ok = false; 139 } 140 if ( finput != null ) { 141 try { 142 finput.close(); 143 } catch ( IOException e ) { 144 ok = false; 145 error("cannot close " + filename, e); 146 } 147 } 148 if ( ok && contents != null ) { 149 String tokens[] = (new String(contents)).split("\\s+"); 150 if ( tokens.length > 0 ) { 151 ok = parseOptions(tokens); 152 } 153 } 154 if ( !ok ) { 155 break; 156 } 157 } else { 158 error("argument error", null); 159 ok = false; 160 } 161 } 162 return ok; 163 } 164 main(String[] args)165 public static void main(String[] args) { 166 boolean ok = true; 167 if (args.length >= 1 && args[0].equals("-quiet")) 168 { 169 quiet = true; 170 String[] newargs = new String[args.length-1]; 171 System.arraycopy(args, 1, newargs, 0, newargs.length); 172 args = newargs; 173 } 174 /* Original usage */ 175 if (args.length == 2 && args[0].charAt(0) != '-' ) { 176 ok = createFile(args[0], args[1], "ListResourceBundle"); 177 } else if (args.length == 3) { 178 ok = createFile(args[0], args[1], args[2]); 179 } else if (args.length == 0) { 180 usage(); 181 ok = false; 182 } else { 183 /* New batch usage */ 184 ok = parseOptions(args); 185 if ( ok && compileCount == 0 ) { 186 error("options parsed but no files to compile", null); 187 ok = false; 188 } 189 /* Need at least one file. */ 190 if ( !ok ) { 191 usage(); 192 } else { 193 /* Process files */ 194 for ( int i = 0; i < compileCount && ok ; i++ ) { 195 ok = createFile(propfiles[i], outfiles[i], supers[i]); 196 } 197 } 198 } 199 if ( !ok ) { 200 System.exit(1); 201 } 202 } 203 usage()204 private static void usage() { 205 System.err.println("usage:"); 206 System.err.println(" java -jar compileproperties.jar path_to_properties_file path_to_java_output_file [super_class]"); 207 System.err.println(" -OR-"); 208 System.err.println(" java -jar compileproperties.jar {-compile path_to_properties_file path_to_java_output_file super_class} -or- @filename"); 209 System.err.println(""); 210 System.err.println("Example:"); 211 System.err.println(" java -jar compileproperties.jar -compile test.properties test.java ListResourceBundle"); 212 System.err.println(" java -jar compileproperties.jar @option_file"); 213 System.err.println("option_file contains: -compile test.properties test.java ListResourceBundle"); 214 } 215 createFile(String propertiesPath, String outputPath, String superClass)216 private static boolean createFile(String propertiesPath, String outputPath, 217 String superClass) { 218 boolean ok = true; 219 if (!quiet) { 220 System.out.println("parsing: " + propertiesPath); 221 } 222 Properties p = new Properties(); 223 try { 224 p.load(new FileInputStream(propertiesPath)); 225 } catch ( FileNotFoundException e ) { 226 ok = false; 227 error("Cannot find file " + propertiesPath, e); 228 } catch ( IOException e ) { 229 ok = false; 230 error("IO error on file " + propertiesPath, e); 231 } 232 if ( ok ) { 233 String packageName = inferPackageName(propertiesPath, outputPath); 234 if (!quiet) { 235 System.out.println("inferred package name: " + packageName); 236 } 237 List<String> sortedKeys = new ArrayList<>(); 238 for ( Object key : p.keySet() ) { 239 sortedKeys.add((String)key); 240 } 241 Collections.sort(sortedKeys); 242 243 StringBuffer data = new StringBuffer(); 244 245 for (String key : sortedKeys) { 246 data.append(" { \"" + escape(key) + "\", \"" + 247 escape((String)p.get(key)) + "\" },\n"); 248 } 249 250 // Get class name from java filename, not the properties filename. 251 // (zh_TW properties might be used to create zh_HK files) 252 File file = new File(outputPath); 253 String name = file.getName(); 254 int dotIndex = name.lastIndexOf('.'); 255 String className; 256 if (dotIndex == -1) { 257 className = name; 258 } else { 259 className = name.substring(0, dotIndex); 260 } 261 262 String packageString = ""; 263 if (packageName != null && !packageName.equals("")) { 264 packageString = "package " + packageName + ";\n\n"; 265 } 266 267 Writer writer = null; 268 try { 269 writer = new BufferedWriter( 270 new OutputStreamWriter(new FileOutputStream(outputPath), "8859_1")); 271 MessageFormat format = new MessageFormat(FORMAT); 272 writer.write(format.format(new Object[] { packageString, className, superClass, data })); 273 } catch ( IOException e ) { 274 ok = false; 275 error("IO error writing to file " + outputPath, e); 276 } 277 if ( writer != null ) { 278 try { 279 writer.flush(); 280 } catch ( IOException e ) { 281 ok = false; 282 error("IO error flush " + outputPath, e); 283 } 284 try { 285 writer.close(); 286 } catch ( IOException e ) { 287 ok = false; 288 error("IO error close " + outputPath, e); 289 } 290 } 291 if (!quiet) { 292 System.out.println("wrote: " + outputPath); 293 } 294 } 295 return ok; 296 } 297 escape(String theString)298 private static String escape(String theString) { 299 // This is taken from Properties.saveConvert with changes for Java strings 300 int len = theString.length(); 301 StringBuffer outBuffer = new StringBuffer(len*2); 302 303 for(int x=0; x<len; x++) { 304 char aChar = theString.charAt(x); 305 switch(aChar) { 306 case '\\':outBuffer.append('\\'); outBuffer.append('\\'); 307 break; 308 case '\t':outBuffer.append('\\'); outBuffer.append('t'); 309 break; 310 case '\n':outBuffer.append('\\'); outBuffer.append('n'); 311 break; 312 case '\r':outBuffer.append('\\'); outBuffer.append('r'); 313 break; 314 case '\f':outBuffer.append('\\'); outBuffer.append('f'); 315 break; 316 default: 317 if ((aChar < 0x0020) || (aChar > 0x007e)) { 318 outBuffer.append('\\'); 319 outBuffer.append('u'); 320 outBuffer.append(toHex((aChar >> 12) & 0xF)); 321 outBuffer.append(toHex((aChar >> 8) & 0xF)); 322 outBuffer.append(toHex((aChar >> 4) & 0xF)); 323 outBuffer.append(toHex( aChar & 0xF)); 324 } else { 325 if (specialSaveChars.indexOf(aChar) != -1) { 326 outBuffer.append('\\'); 327 } 328 outBuffer.append(aChar); 329 } 330 } 331 } 332 return outBuffer.toString(); 333 } 334 inferPackageName(String inputPath, String outputPath)335 private static String inferPackageName(String inputPath, String outputPath) { 336 // Normalize file names 337 inputPath = new File(inputPath).getPath(); 338 outputPath = new File(outputPath).getPath(); 339 // Split into components 340 String sep; 341 if (File.separatorChar == '\\') { 342 sep = "\\\\"; 343 } else { 344 sep = File.separator; 345 } 346 String[] inputs = inputPath.split(sep); 347 String[] outputs = outputPath.split(sep); 348 // Match common names, eliminating first "classes" entry from 349 // each if present 350 int inStart = 0; 351 int inEnd = inputs.length - 2; 352 int outEnd = outputs.length - 2; 353 int i = inEnd; 354 int j = outEnd; 355 while (i >= 0 && j >= 0) { 356 if (!inputs[i].equals(outputs[j]) || 357 (inputs[i].equals("gensrc") && inputs[j].equals("gensrc"))) { 358 ++i; 359 ++j; 360 break; 361 } 362 --i; 363 --j; 364 } 365 String result; 366 if (i < 0 || j < 0 || i >= inEnd || j >= outEnd) { 367 result = ""; 368 } else { 369 if (inputs[i].equals("classes") && outputs[j].equals("classes")) { 370 ++i; 371 } 372 if (i > 0 && inputs[i-1].equals("modules")) { 373 ++i; 374 } 375 inStart = i; 376 StringBuffer buf = new StringBuffer(); 377 for (i = inStart; i <= inEnd; i++) { 378 buf.append(inputs[i]); 379 if (i < inEnd) { 380 buf.append('.'); 381 } 382 } 383 result = buf.toString(); 384 } 385 return result; 386 } 387 } 388