author | coleenp |
Tue, 21 May 2019 09:53:16 -0400 | |
changeset 54961 | 30c8a21ce002 |
parent 52804 | 28094715ae71 |
permissions | -rw-r--r-- |
28334
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
1 |
/* |
52804
28094715ae71
8214718: Update missing copyright year in build system
ihse
parents:
51737
diff
changeset
|
2 |
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved. |
28334
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
4 |
* |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
10 |
* |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
15 |
* accompanied this code). |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
16 |
* |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
20 |
* |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
23 |
* questions. |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
24 |
*/ |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
25 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
26 |
package propertiesparser; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
27 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
28 |
import propertiesparser.parser.MessageFile; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
29 |
import propertiesparser.gen.ClassGenerator; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
30 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
31 |
import java.io.File; |
37850
24e70308f4ea
8156000: tools/all/RunCodingRules.java fails if build dir exists, but generated sources do not
jlahoda
parents:
28334
diff
changeset
|
32 |
import java.io.PrintStream; |
51737
30e6a0b9d691
8210731: PropertiesParser does not produce reproducible output
ihse
parents:
47216
diff
changeset
|
33 |
import java.util.TreeMap; |
28334
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
34 |
import java.util.Map; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
35 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
36 |
/** Translates a .properties file into a .java file containing an enum-like Java class |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
37 |
* which defines static factory methods for all resource keys in a given resource file. <P> |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
38 |
* |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
39 |
* Usage: java PropertiesParser -compile [path to .properties file] [output folder where .java file will be written] |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
40 |
* |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
41 |
* @author mcimadamore |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
42 |
*/ |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
43 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
44 |
public class PropertiesParser { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
45 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
46 |
public Logger logger; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
47 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
48 |
public PropertiesParser(Logger logger) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
49 |
this.logger = logger; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
50 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
51 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
52 |
public static void main(String[] args) { |
37850
24e70308f4ea
8156000: tools/all/RunCodingRules.java fails if build dir exists, but generated sources do not
jlahoda
parents:
28334
diff
changeset
|
53 |
boolean ok = run(args, System.out); |
28334
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
54 |
if ( !ok ) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
55 |
System.exit(1); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
56 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
57 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
58 |
|
37850
24e70308f4ea
8156000: tools/all/RunCodingRules.java fails if build dir exists, but generated sources do not
jlahoda
parents:
28334
diff
changeset
|
59 |
public static boolean run(String[] args, PrintStream out) { |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
37850
diff
changeset
|
60 |
PropertiesParser pp = new PropertiesParser(out::println); |
37850
24e70308f4ea
8156000: tools/all/RunCodingRules.java fails if build dir exists, but generated sources do not
jlahoda
parents:
28334
diff
changeset
|
61 |
return pp.run(args); |
24e70308f4ea
8156000: tools/all/RunCodingRules.java fails if build dir exists, but generated sources do not
jlahoda
parents:
28334
diff
changeset
|
62 |
} |
24e70308f4ea
8156000: tools/all/RunCodingRules.java fails if build dir exists, but generated sources do not
jlahoda
parents:
28334
diff
changeset
|
63 |
|
28334
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
64 |
public static interface Logger { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
65 |
void info(String msg); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
66 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
67 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
68 |
public void info(String msg) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
69 |
logger.info(msg); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
70 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
71 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
72 |
public boolean run(String[] args) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
73 |
Map<String, String> optionsMap = parseOptions(args); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
74 |
if (optionsMap.isEmpty()) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
75 |
usage(); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
76 |
return false; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
77 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
78 |
try { |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
37850
diff
changeset
|
79 |
optionsMap.forEach(this::compilePropertyFile); |
28334
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
80 |
return true; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
81 |
} catch (RuntimeException ex) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
82 |
ex.printStackTrace(); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
83 |
return false; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
84 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
85 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
86 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
87 |
private void compilePropertyFile(String propertyPath, String outPath) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
88 |
try { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
89 |
File propertyFile = new File(propertyPath); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
90 |
String prefix = propertyFile.getName().split("\\.")[0]; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
91 |
MessageFile messageFile = new MessageFile(propertyFile, prefix); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
92 |
new ClassGenerator().generateFactory(messageFile, new File(outPath)); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
93 |
} catch (Throwable ex) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
94 |
throw new RuntimeException(ex); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
95 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
96 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
97 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
98 |
private Map<String, String> parseOptions(String args[]) { |
51737
30e6a0b9d691
8210731: PropertiesParser does not produce reproducible output
ihse
parents:
47216
diff
changeset
|
99 |
/* Use TreeMap to guarantee stable forEach iteration */ |
30e6a0b9d691
8210731: PropertiesParser does not produce reproducible output
ihse
parents:
47216
diff
changeset
|
100 |
Map<String, String> optionsMap = new TreeMap<>(); |
28334
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
101 |
for ( int i = 0; i < args.length ; i++ ) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
102 |
if ( "-compile".equals(args[i]) && i+2 < args.length ) { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
103 |
optionsMap.put(args[++i], args[++i]); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
104 |
} else { |
51737
30e6a0b9d691
8210731: PropertiesParser does not produce reproducible output
ihse
parents:
47216
diff
changeset
|
105 |
return new TreeMap<>(); |
28334
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
106 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
107 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
108 |
return optionsMap; |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
109 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
110 |
|
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
111 |
private void usage() { |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
112 |
info("usage:"); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
113 |
info(" java PropertiesParser {-compile path_to_properties_file path_to_java_output_dir}"); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
114 |
info(""); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
115 |
info("Example:"); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
116 |
info(" java PropertiesParser -compile resources/test.properties resources"); |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
117 |
} |
1633de6070ae
8058542: Devise scheme for better diagnostic creation
mcimadamore
parents:
diff
changeset
|
118 |
} |