Coverage Report - org.deduced.generator.java.CommandLineJavaGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
CommandLineJavaGenerator
100%
99/99
92%
13/14
1.611
 
 1  
 /**
 2  
  *    Copyright 2005-2011 Steve McDuff d-duff@users.sourceforge.net
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.deduced.generator.java;
 17  
 
 18  
 import org.apache.commons.cli.CommandLine;
 19  
 import org.apache.commons.cli.CommandLineParser;
 20  
 import org.apache.commons.cli.HelpFormatter;
 21  
 import org.apache.commons.cli.Option;
 22  
 import org.apache.commons.cli.Options;
 23  
 import org.apache.commons.cli.ParseException;
 24  
 import org.apache.commons.cli.PosixParser;
 25  
 import org.apache.commons.lang.StringUtils;
 26  
 import org.deduced.AbstractPropertyCollection;
 27  
 import org.deduced.DeducedModelLayerExtension;
 28  
 import org.deduced.PropertyCollection;
 29  
 import org.deduced.framework.FrameworkLoader;
 30  
 import org.deduced.utilities.LogUtilities;
 31  
 import org.deduced.utilities.SystemExit;
 32  
 import org.deduced.utilities.SystemExitImplementation;
 33  
 
 34  
 /**
 35  
  * Class used go generate java files from the command line
 36  
  * 
 37  
  * @author Steve McDuff
 38  
  */
 39  1
 public class CommandLineJavaGenerator
 40  
 {
 41  
         /** system exit wrapper used for tests */
 42  1
         private static SystemExit EXIT = new SystemExitImplementation();
 43  
 
 44  
         /** help option short name */
 45  
         public static final String HELP_OPTION_SHORT_NAME = "h";
 46  
 
 47  
         /** output folder option short name */
 48  
         public static final String OUTPUT_OPTION_SHORT_NAME = "o";
 49  
 
 50  
         /** input file name option short name */
 51  
         public static final String INPUT_OPTION_SHORT_NAME = "i";
 52  
 
 53  
         /** ignore XML loading errors short name */
 54  
         public static final String IGNORING_XML_LOADING_ERROR_NAME = "x";
 55  
 
 56  
         /** dependent file list option short name */
 57  
         public static final String DEPENDENCY_OPTION_SHORT_NAME = "d";
 58  
 
 59  
         /**
 60  
          * main method used to start the generator
 61  
          * 
 62  
          * @param arguments command line arguments
 63  
          */
 64  
         public static void main(
 65  
                 String[] arguments)
 66  
         {
 67  2
                 LogUtilities.readLoggerConfigurationOnce(true, new String[]{
 68  
                         "logging.properties", "org/deduced/generator/logging.properties" });
 69  2
                 if (!runGeneratorBasedOnArguments(arguments))
 70  
                 {
 71  1
                         getExit().exit(-1);
 72  
                 }
 73  2
         }
 74  
 
 75  
         /**
 76  
          * run Generator Based On Arguments
 77  
          * 
 78  
          * @param arguments the arguments
 79  
          * @return true if the execution was successful
 80  
          */
 81  
         public static boolean runGeneratorBasedOnArguments(
 82  
                 String[] arguments)
 83  
         {
 84  2
                 boolean returnValue = false;
 85  2
                 FrameworkLoader.createBaseSchemaModel();
 86  2
                 AbstractPropertyCollection
 87  
                         .setCheckDeleteMode(AbstractPropertyCollection.CHECK_DELETE_MODE_MANUAL);
 88  
 
 89  2
                 Options createOptions = createOptions();
 90  
 
 91  2
                 GeneratorOptions options = new GeneratorOptions();
 92  2
                 if (readOptions(arguments, createOptions, options))
 93  
                 {
 94  1
                         returnValue = executeActionBasedOnOption(options);
 95  
                 }
 96  2
                 return returnValue;
 97  
         }
 98  
 
 99  
         /**
 100  
          * execute the required action on the generator based on the selected
 101  
          * options
 102  
          * 
 103  
          * @param options the command line option configuration. This parameter is
 104  
          *            used to print the help guide if required.
 105  
          * @return true if the execution is successful
 106  
          */
 107  
         public static boolean executeActionBasedOnOption(
 108  
                 GeneratorOptions options)
 109  
         {
 110  4
                 boolean retVal = true;
 111  4
                 if (options.isShowHelp())
 112  
                 {
 113  2
                         printHelp();
 114  
                 }
 115  
                 else
 116  
                 {
 117  2
                         retVal = runGenerator(options);
 118  
                 }
 119  4
                 return retVal;
 120  
         }
 121  
 
 122  
         /**
 123  
          * run the java file generator based on the selected options
 124  
          * 
 125  
          * @param options the selected options
 126  
          * @return true if the generator ran successfully
 127  
          */
 128  
         public static boolean runGenerator(
 129  
                 GeneratorOptions options)
 130  
         {
 131  12
                 boolean retVal = false;
 132  
                 try
 133  
                 {
 134  12
                         DeducedModelLayerExtension hub =
 135  
                                 FrameworkLoader
 136  
                                         .createApplicationModel("Deduced Generator Application");
 137  
 
 138  12
                         if (StringUtils.isBlank(options.getInputFileName()))
 139  
                         {
 140  2
                                 throw new IllegalArgumentException("No input file specified");
 141  
                         }
 142  
 
 143  10
                         String[] dependencyFileNameList =
 144  
                                 options.getDependencyFileNameList();
 145  10
                         boolean ignoringXmlLoadingErrors =
 146  
                                 options.isIgnoringXmlLoadingErrors();
 147  10
                         if (dependencyFileNameList != null)
 148  
                         {
 149  8
                                 for (String currentDependency : dependencyFileNameList)
 150  
                                 {
 151  5
                                         if (!StringUtils.isEmpty(currentDependency))
 152  
                                         {
 153  5
                                                 JavaGeneratorUtilities.loadModelFromXmlFile(
 154  
                                                         currentDependency, hub, ignoringXmlLoadingErrors);
 155  
                                         }
 156  
                                 }
 157  
                         }
 158  8
                         PropertyCollection<?, ?> schemaModelToGenerate = null;
 159  8
                         String inputFileName = options.getInputFileName();
 160  
 
 161  8
                         schemaModelToGenerate =
 162  
                                 JavaGeneratorUtilities.loadModelFromXmlFile(inputFileName, hub,
 163  
                                         ignoringXmlLoadingErrors);
 164  
 
 165  4
                         JavaSchemaGenerator javaGenerator =
 166  
                                 new JavaSchemaGeneratorImplementation();
 167  
 
 168  4
                         javaGenerator.generateSchemaFromModel(
 169  
                                 options.getOutputFolderName(), schemaModelToGenerate);
 170  
 
 171  4
                         retVal = true;
 172  
                 }
 173  8
                 catch (Exception e)
 174  
                 {
 175  8
                         handleError(e, "Failed to generate files");
 176  8
                         printHelp();
 177  4
                 }
 178  
 
 179  12
                 return retVal;
 180  
         }
 181  
 
 182  
         /**
 183  
          * handle Error
 184  
          * 
 185  
          * @param error the exception that occurred
 186  
          * @param errorContext the error context description
 187  
          */
 188  
         private static void handleError(
 189  
                 Exception error, String errorContext)
 190  
         {
 191  12
                 String errorMessage = errorContext + " : " + error.getMessage();
 192  12
                 setLastError(errorMessage);
 193  12
                 System.err.println(errorMessage);
 194  12
                 error.printStackTrace();
 195  12
         }
 196  
 
 197  
         /**
 198  
          * last error message
 199  
          */
 200  1
         private static String LAST_ERROR = null;
 201  
 
 202  
         /**
 203  
          * read options from the input arguments and fill the generator options
 204  
          * file. If the parameters fail to be parsed, the error message will printed
 205  
          * in the System.err output stream and the method will return false.
 206  
          * 
 207  
          * @param arguments the command line arguments
 208  
          * @param createOptions the command line options
 209  
          * @param options the generator options to fill
 210  
          * @return true if parameters were successfully parsed.
 211  
          */
 212  
         public static boolean readOptions(
 213  
                 String[] arguments, Options createOptions, GeneratorOptions options)
 214  
         {
 215  
                 try
 216  
                 {
 217  10
                         CommandLineParser parser = new PosixParser();
 218  10
                         CommandLine cmd = parser.parse(createOptions, arguments);
 219  
 
 220  6
                         options.setShowHelp(cmd.hasOption(HELP_OPTION_SHORT_NAME));
 221  6
                         options.setOutputFolderName(cmd
 222  
                                 .getOptionValue(OUTPUT_OPTION_SHORT_NAME));
 223  6
                         options.setInputFileName(cmd
 224  
                                 .getOptionValue(INPUT_OPTION_SHORT_NAME));
 225  6
                         options.setDependencyFileNameList(cmd
 226  
                                 .getOptionValues(DEPENDENCY_OPTION_SHORT_NAME));
 227  6
                         options.setIgnoringXmlLoadingErrors(cmd
 228  
                                 .hasOption(IGNORING_XML_LOADING_ERROR_NAME));
 229  
                 }
 230  4
                 catch (ParseException ex)
 231  
                 {
 232  4
                         handleError(ex, "Parsing command line arguments failed.");
 233  4
                         printHelp();
 234  
 
 235  4
                         return false;
 236  6
                 }
 237  6
                 return true;
 238  
         }
 239  
 
 240  
         /**
 241  
          * print help on the console
 242  
          */
 243  
         public static void printHelp()
 244  
         {
 245  14
                 printHelp(createOptions());
 246  14
         }
 247  
 
 248  
         /**
 249  
          * print help on the console
 250  
          * 
 251  
          * @param options the generator options
 252  
          */
 253  
         public static void printHelp(
 254  
                 Options options)
 255  
         {
 256  15
                 HelpFormatter formatter = new HelpFormatter();
 257  15
                 formatter.printHelp("javagenerator", options);
 258  15
         }
 259  
 
 260  
         /**
 261  
          * create command line options used to parse arguments
 262  
          * 
 263  
          * @return the command line options
 264  
          */
 265  
         public static Options createOptions()
 266  
         {
 267  18
                 Options retVal = new Options();
 268  
 
 269  18
                 retVal.addOption(createInputOption());
 270  18
                 retVal.addOption(createOutputOption());
 271  18
                 retVal.addOption(createDependenciesOption());
 272  18
                 retVal.addOption(createHelpOption());
 273  18
                 retVal.addOption(createIgnoringXmlLoadingErrorOption());
 274  
 
 275  18
                 return retVal;
 276  
         }
 277  
 
 278  
         /**
 279  
          * create the help option
 280  
          * 
 281  
          * @return the help option
 282  
          */
 283  
         public static Option createHelpOption()
 284  
         {
 285  18
                 Option helpOption =
 286  
                         new Option(HELP_OPTION_SHORT_NAME, "Print this message.");
 287  
 
 288  
                 // set the option properties
 289  18
                 helpOption.setLongOpt("help");
 290  
 
 291  18
                 return helpOption;
 292  
         }
 293  
 
 294  
         /**
 295  
          * create the output folder option
 296  
          * 
 297  
          * @return the output folder option
 298  
          */
 299  
         public static Option createOutputOption()
 300  
         {
 301  18
                 Option dependenciesOption =
 302  
                         new Option(
 303  
                                 OUTPUT_OPTION_SHORT_NAME,
 304  
                                 "Output folder name where the generated files will be created. "
 305  
                                         + "If the folder doesn't exist, the generator will create it. "
 306  
                                         + "This option isn't mandatory. If it isn't specified, the output will be "
 307  
                                         + "in the current folder.");
 308  
 
 309  
                 // set the option properties
 310  18
                 dependenciesOption.setLongOpt("output");
 311  18
                 dependenciesOption.setArgs(1);
 312  18
                 dependenciesOption.setArgName("FOLDER");
 313  
 
 314  18
                 return dependenciesOption;
 315  
         }
 316  
 
 317  
         /**
 318  
          * create the input file option
 319  
          * 
 320  
          * @return the input file option
 321  
          */
 322  
         public static Option createInputOption()
 323  
         {
 324  18
                 Option inputOption =
 325  
                         new Option(
 326  
                                 INPUT_OPTION_SHORT_NAME,
 327  
                                 "Input file name. This should point to a valid XML file that contains "
 328  
                                         + "a deduced schema layer. Use quotes to enter file names with blank spaces. "
 329  
                                         + "This option is mandatory.");
 330  
 
 331  
                 // set the option properties
 332  18
                 inputOption.setLongOpt("input");
 333  18
                 inputOption.setArgs(1);
 334  18
                 inputOption.setArgName("FILE");
 335  
 
 336  18
                 return inputOption;
 337  
         }
 338  
 
 339  
         /**
 340  
          * create the input file option
 341  
          * 
 342  
          * @return the input file option
 343  
          */
 344  
         public static Option createIgnoringXmlLoadingErrorOption()
 345  
         {
 346  18
                 Option inputOption =
 347  
                         new Option(IGNORING_XML_LOADING_ERROR_NAME,
 348  
                                 "Ignore XML loading errors encountered while loading the input file or "
 349  
                                         + "it's dependencies. Default setting is false.");
 350  
 
 351  
                 // set the option properties
 352  18
                 inputOption.setLongOpt("ignoreXmlLoadingErrors");
 353  
 
 354  18
                 return inputOption;
 355  
         }
 356  
 
 357  
         /**
 358  
          * create the dependencies file list option
 359  
          * 
 360  
          * @return the dependencies file list option
 361  
          */
 362  
         public static Option createDependenciesOption()
 363  
         {
 364  18
                 Option dependenciesOption =
 365  
                         new Option(
 366  
                                 DEPENDENCY_OPTION_SHORT_NAME,
 367  
                                 "List of dependent schema files required to generate the current schema. "
 368  
                                         + "Use a list of file names separated by a semicolon. The files have to "
 369  
                                         + "be in order of dependency. Example : "
 370  
                                         + "--dependencies=\"file1.xml;file2.xml\"");
 371  
 
 372  
                 // set the option properties
 373  18
                 dependenciesOption.setLongOpt("dependencies");
 374  18
                 dependenciesOption.setArgs(Option.UNLIMITED_VALUES);
 375  18
                 dependenciesOption.setValueSeparator(';');
 376  18
                 dependenciesOption.setArgName("FILES");
 377  
 
 378  18
                 return dependenciesOption;
 379  
         }
 380  
 
 381  
         /**
 382  
          * @param setExit the system exit implementation to set
 383  
          */
 384  
         public static void setExit(
 385  
                 SystemExit setExit)
 386  
         {
 387  9
                 EXIT = setExit;
 388  9
         }
 389  
 
 390  
         /**
 391  
          * @return the system exit implementation
 392  
          */
 393  
         public static SystemExit getExit()
 394  
         {
 395  1
                 return EXIT;
 396  
         }
 397  
 
 398  
         /**
 399  
          * @param setLastError the last error to set
 400  
          */
 401  
         public static void setLastError(
 402  
                 String setLastError)
 403  
         {
 404  12
                 LAST_ERROR = setLastError;
 405  12
         }
 406  
 
 407  
         /**
 408  
          * @return the last error
 409  
          */
 410  
         public static String getLastError()
 411  
         {
 412  2
                 return LAST_ERROR;
 413  
         }
 414  
 }