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.BeneratorConstants;
30  import com.rapiddweller.benerator.BeneratorFactory;
31  import com.rapiddweller.benerator.engine.BeneratorContext;
32  import com.rapiddweller.benerator.file.XMLFileGenerator;
33  import com.rapiddweller.benerator.wrapper.ProductWrapper;
34  import com.rapiddweller.common.ArrayUtil;
35  import com.rapiddweller.common.IOUtil;
36  import com.rapiddweller.common.ui.ConsoleInfoPrinter;
37  import org.apache.logging.log4j.LogManager;
38  import org.apache.logging.log4j.Logger;
39  
40  import java.io.File;
41  import java.text.MessageFormat;
42  
43  /**
44   * Main class for generating XML files from the command line.<br/><br/>
45   * Created: 28.03.2008 16:52:49
46   *
47   * @author Volker Bergmann
48   * @since 0.5.0
49   */
50  public class XmlCreator {
51  
52    private static final Logger logger = LogManager.getLogger(XmlCreator.class);
53  
54    /**
55     * The entry point of application.
56     *
57     * @param args additional args you can add
58     */
59    public static void main(String[] args) {
60      if (args.length < 3) {
61        printHelp();
62        System.exit(BeneratorConstants.EXIT_CODE_ERROR);
63      }
64      String schemaUri = args[0];
65      String root = args[1];
66      String pattern = args[2];
67      long fileCount = 1;
68      if (args.length >= 4) {
69        fileCount = Long.parseLong(args[3]);
70      }
71      String[] propertiesFiles = (args.length > 4 ? ArrayUtil.copyOfRange(args, 4, args.length - 4) : new String[0]);
72      createXMLFiles(schemaUri, root, pattern, fileCount, propertiesFiles);
73    }
74  
75    /**
76     * Create xml files.
77     *
78     * @param schemaUri       the schema uri
79     * @param root            the root
80     * @param pattern         the pattern
81     * @param fileCount       the file count
82     * @param propertiesFiles the properties files
83     */
84    public static void createXMLFiles(String schemaUri, String root,
85                                      String pattern, long fileCount, String[] propertiesFiles) {
86      logParams(schemaUri, root, pattern, fileCount);
87      long start = System.currentTimeMillis();
88      BeneratorContext context = BeneratorFactory.getInstance().createContext(IOUtil.getParentUri(schemaUri));
89      XMLFileGeneratortor.html#XMLFileGenerator">XMLFileGenerator fileGenerator = new XMLFileGenerator(schemaUri, root, pattern, propertiesFiles);
90      try (fileGenerator) {
91        fileGenerator.init(context);
92        for (long i = 0; i < fileCount; i++) {
93          ProductWrapper<File> file = fileGenerator.generate(new ProductWrapper<>());
94          if (file == null) {
95            throw new RuntimeException("Unable to create the expected number of files. " +
96                "Created " + i + " of " + fileCount + " files");
97          }
98          logger.info("created file: " + file);
99        }
100     }
101     long duration = System.currentTimeMillis() - start;
102     logger.info("Finished after " + duration + " ms");
103   }
104 
105   private static void logParams(String schemaUri, String root, String pattern, long fileCount) {
106     if (logger.isDebugEnabled()) {
107       if (fileCount > 1) {
108         logger.debug("Creating " + fileCount + " XML files for schema " + schemaUri + " with root " + root + " and pattern " + pattern);
109       } else {
110         logger.debug("Creating XML file " + MessageFormat.format(pattern, fileCount) + " for schema " + schemaUri + " with root " + root);
111       }
112     }
113   }
114 
115   private static void printHelp() {
116     ConsoleInfoPrinter.printHelp(
117         "Invalid parameters",
118         "parameters: schemaUri root fileNamePattern [count [propertiesFilenames]]"
119     );
120   }
121 
122 }