jdk
118 строк · 3.9 Кб
1/*
2* Copyright (c) 2014, 2018, 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.TreeMap;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
46public Logger logger;47
48public PropertiesParser(Logger logger) {49this.logger = logger;50}51
52public static void main(String[] args) {53boolean ok = run(args, System.out);54if ( !ok ) {55System.exit(1);56}57}58
59public static boolean run(String[] args, PrintStream out) {60PropertiesParser pp = new PropertiesParser(out::println);61return pp.run(args);62}63
64public static interface Logger {65void info(String msg);66}67
68public void info(String msg) {69logger.info(msg);70}71
72public boolean run(String[] args) {73Map<String, String> optionsMap = parseOptions(args);74if (optionsMap.isEmpty()) {75usage();76return false;77}78try {79optionsMap.forEach(this::compilePropertyFile);80return true;81} catch (RuntimeException ex) {82ex.printStackTrace();83return false;84}85}86
87private void compilePropertyFile(String propertyPath, String outPath) {88try {89File propertyFile = new File(propertyPath);90String prefix = propertyFile.getName().split("\\.")[0];91MessageFile messageFile = new MessageFile(propertyFile, prefix);92new ClassGenerator().generateFactory(messageFile, new File(outPath));93} catch (Throwable ex) {94throw new RuntimeException(ex);95}96}97
98private Map<String, String> parseOptions(String args[]) {99/* Use TreeMap to guarantee stable forEach iteration */100Map<String, String> optionsMap = new TreeMap<>();101for ( int i = 0; i < args.length ; i++ ) {102if ( "-compile".equals(args[i]) && i+2 < args.length ) {103optionsMap.put(args[++i], args[++i]);104} else {105return new TreeMap<>();106}107}108return optionsMap;109}110
111private void usage() {112info("usage:");113info(" java PropertiesParser {-compile path_to_properties_file path_to_java_output_dir}");114info("");115info("Example:");116info(" java PropertiesParser -compile resources/test.properties resources");117}118}
119