1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package sampleapi;
  25 
  26 import java.io.File;
  27 
  28 import sampleapi.SampleApi.Fault;
  29 
  30 public class SampleApiDefaultRunner {
  31 
  32     public static final String MSG_DUP_OUTDIR =
  33         "SampleApi: duplicated outdir detected: ";
  34     public static final String MSG_USE_FIRST =
  35         "           will use first occurance: ";
  36     public static final String MSG_INVAL_OUTDIR =
  37         "SampleApi: outdir is not valid: ";
  38     public static final String MSG_CANNOT_GEN =
  39         "SampleApi: cannot generate output: ";
  40     public static final String MSG_WRONG_OPTION =
  41         "SampleApi: incorrect option: ";
  42     public static final String MSG_USE_HELP =
  43         "           use -? for help";
  44 
  45     public static void main(String... args) throws Exception {
  46         if (args.length == 0) {
  47             printHelp();
  48             System.exit(1);
  49         }
  50 
  51         String outDirName = "";
  52 
  53         boolean isOutDirSet = false;
  54         boolean isHelpPrinted = false;
  55         for (String arg : args) {
  56             Option option = new Option(arg);
  57             switch (option.getOptionName()) {
  58                 case "-?":
  59                 case "-h":
  60                 case "--help":
  61                     if (!isHelpPrinted) {
  62                         printHelp();
  63                         isHelpPrinted = true;
  64                     }
  65                     break;
  66                 case "-o":
  67                 case "--outdir":
  68                     if (!isOutDirSet) {
  69                         outDirName = option.getOptionValue();
  70                         isOutDirSet = true;
  71                     } else {
  72                         System.err.println(MSG_DUP_OUTDIR + option.getOptionValue());
  73                         System.err.println(MSG_USE_FIRST + outDirName);
  74                     }
  75                     break;
  76                 default:
  77                     System.err.println(MSG_WRONG_OPTION + arg);
  78                     System.err.println(MSG_USE_HELP);
  79                     break;
  80             }
  81 
  82         }
  83 
  84         if (!isOutDirSet) {
  85             System.exit(1);
  86         }
  87 
  88         if (outDirName.length() == 0) {
  89             System.err.println(MSG_INVAL_OUTDIR + outDirName);
  90             System.exit(1);
  91         }
  92 
  93         File outDir = new File(outDirName);
  94         outDir.mkdirs();
  95         SampleApi apiGen = new SampleApi();
  96 
  97         try {
  98             apiGen.generate(outDir);
  99         } catch (Fault e) {
 100             System.err.println(MSG_CANNOT_GEN + e.getMessage());
 101             e.printStackTrace();
 102         }
 103     }
 104 
 105     private static void printHelp() {
 106         System.out.println("SampleApi:");
 107         System.out.println("    options: [-?|-h|--help] [-o:<dir>|--outdir:<dir>]");
 108         System.out.println("    -?|-h|--help             - print help");
 109         System.out.println("    -o:<dir>|--outdir:<dir>  - set <dir> to generate output");
 110     }
 111 
 112     private static class Option {
 113 
 114         private String optionName;
 115         private String optionValue;
 116 
 117         public Option(String arg) {
 118             int delimPos = arg.indexOf(':');
 119 
 120             if (delimPos == -1) {
 121                 optionName = arg;
 122                 optionValue = "";
 123             } else {
 124                 optionName = arg.substring(0, delimPos);
 125                 optionValue = arg.substring(delimPos + 1, arg.length());
 126             }
 127         }
 128 
 129         public String getOptionName() {
 130             return optionName;
 131         }
 132 
 133         public String getOptionValue() {
 134             return optionValue;
 135         }
 136     }
 137 }