1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package com.rapiddweller.benerator.main;
28
29 import com.rapiddweller.benerator.template.TemplateInputReader;
30 import com.rapiddweller.common.BeanUtil;
31 import com.rapiddweller.common.ConfigurationError;
32 import com.rapiddweller.common.Context;
33 import com.rapiddweller.common.StringUtil;
34 import com.rapiddweller.common.context.DefaultContext;
35 import com.rapiddweller.common.ui.ConsoleInfoPrinter;
36 import com.rapiddweller.format.script.Script;
37 import com.rapiddweller.format.script.freemarker.FreeMarkerScriptFactory;
38
39 import java.io.FileWriter;
40 import java.io.IOException;
41 import java.io.Writer;
42 import java.util.ArrayList;
43 import java.util.List;
44
45
46
47
48
49
50
51
52 public class TemplateRunner {
53
54
55
56
57
58
59
60 public static void main(String[] args) throws Exception {
61
62 List<String> params = processCmdLineArgs(args);
63
64
65
66 if (params.size() < 3 || params.size() > 4) {
67 printHelpAndExit();
68 }
69
70
71 String configFile = params.get(0);
72 String configParserClass = params.get(1);
73 String templateFile = params.get(2);
74 String generatedFile = (params.size() == 4 ? params.get(3) : "benerator.xml");
75
76
77 TemplateInputReaderom/rapiddweller/benerator/template/TemplateInputReader.html#TemplateInputReader">TemplateInputReader reader = (TemplateInputReader) BeanUtil.newInstance(configParserClass);
78 Context context = new DefaultContext();
79 reader.parse(configFile, context);
80 createDescriptor(context, templateFile, generatedFile);
81
82
83 runDescriptor(generatedFile);
84 }
85
86 private static List<String> processCmdLineArgs(String[] args) {
87 List<String> params = new ArrayList<>();
88 for (String arg : args) {
89 if (arg.startsWith("-D")) {
90 String[] tokens = StringUtil.splitOnFirstSeparator(arg.substring(2), '=');
91 if (tokens.length == 2) {
92 System.setProperty(tokens[0], tokens[1]);
93 }
94 } else {
95 params.add(arg);
96 }
97 }
98 return params;
99 }
100
101 @SuppressWarnings("checkstyle:FileTabCharacter")
102 private static void printHelpAndExit() {
103 ConsoleInfoPrinter.printHelp(
104 "The class " + TemplateRunner.class.getName(),
105 "creates and runs Benerator descriptor files from custom templates. It has the following parameters:",
106 "<config_file> <config_parser_class> <template_file> [<generated_file>]",
107 " config_file: Path of an individual data file to provide configuration",
108 " config_parser_class: Fully qualified name of a Java class which is able to parse the config_file",
109 " template file: File path of a FreeMarker template to generate a Benerator descriptor file",
110 " generated_file: File path of the generated Benerator descriptor file"
111 );
112 System.exit(-1);
113 }
114
115 private static void createDescriptor(Context context, String templateUri, String targetUri) {
116 try {
117 Script script = new FreeMarkerScriptFactory().readFile(templateUri);
118 Writer out = new FileWriter(targetUri);
119 script.execute(context, out);
120 } catch (Exception e) {
121 throw new ConfigurationError("Error generating descriptor file " + targetUri, e);
122 }
123 }
124
125 private static void runDescriptor(String descriptorUri) throws IOException {
126 Benerator.main(new String[] {descriptorUri});
127 }
128
129 }