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