PropertiesParser.java revision 3378:d8e10040f5bd
1/* 2 * Copyright (c) 2014, 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 26package propertiesparser; 27 28import propertiesparser.parser.MessageFile; 29import propertiesparser.gen.ClassGenerator; 30 31import java.io.File; 32import java.io.PrintStream; 33import java.util.HashMap; 34import java.util.Map; 35 36/** Translates a .properties file into a .java file containing an enum-like Java class 37 * which defines static factory methods for all resource keys in a given resource file. <P> 38 * 39 * Usage: java PropertiesParser -compile [path to .properties file] [output folder where .java file will be written] 40 * 41 * @author mcimadamore 42 */ 43 44public class PropertiesParser { 45 46 public Logger logger; 47 48 public PropertiesParser(Logger logger) { 49 this.logger = logger; 50 } 51 52 public static void main(String[] args) { 53 boolean ok = run(args, System.out); 54 if ( !ok ) { 55 System.exit(1); 56 } 57 } 58 59 public static boolean run(String[] args, PrintStream out) { 60 PropertiesParser pp = new PropertiesParser(msg -> out.println(msg)); 61 return pp.run(args); 62 } 63 64 public static interface Logger { 65 void info(String msg); 66 } 67 68 public void info(String msg) { 69 logger.info(msg); 70 } 71 72 public boolean run(String[] args) { 73 Map<String, String> optionsMap = parseOptions(args); 74 if (optionsMap.isEmpty()) { 75 usage(); 76 return false; 77 } 78 try { 79 optionsMap.forEach((propfile, outfile) -> compilePropertyFile(propfile, outfile)); 80 return true; 81 } catch (RuntimeException ex) { 82 ex.printStackTrace(); 83 return false; 84 } 85 } 86 87 private void compilePropertyFile(String propertyPath, String outPath) { 88 try { 89 File propertyFile = new File(propertyPath); 90 String prefix = propertyFile.getName().split("\\.")[0]; 91 MessageFile messageFile = new MessageFile(propertyFile, prefix); 92 new ClassGenerator().generateFactory(messageFile, new File(outPath)); 93 } catch (Throwable ex) { 94 throw new RuntimeException(ex); 95 } 96 } 97 98 private Map<String, String> parseOptions(String args[]) { 99 Map<String, String> optionsMap = new HashMap<>(args.length); 100 for ( int i = 0; i < args.length ; i++ ) { 101 if ( "-compile".equals(args[i]) && i+2 < args.length ) { 102 optionsMap.put(args[++i], args[++i]); 103 } else { 104 return new HashMap<>(); 105 } 106 } 107 return optionsMap; 108 } 109 110 private void usage() { 111 info("usage:"); 112 info(" java PropertiesParser {-compile path_to_properties_file path_to_java_output_dir}"); 113 info(""); 114 info("Example:"); 115 info(" java PropertiesParser -compile resources/test.properties resources"); 116 } 117} 118