View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
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   * Anonymizes XML files homogeneously based on generator and XPath definitions in an Excel sheet.<br/><br/>
47   * Created: 27.02.2014 10:04:58
48   *
49   * @author Volker Bergmann
50   * @since 0.9.0
51   */
52  public class TemplateRunner {
53  
54    /**
55     * The entry point of application.
56     *
57     * @param args the input arguments
58     * @throws Exception the exception
59     */
60    public static void main(String[] args) throws Exception {
61      // extract possible VM params from argument list
62      List<String> params = processCmdLineArgs(args);
63  
64  
65      // check preconditions
66      if (params.size() < 3 || params.size() > 4) {
67        printHelpAndExit();
68      }
69  
70      // parse parameters
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      // generate descriptor file
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      // run descriptor file
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 }