1/*
2 * Copyright (c) 2015, 2016, 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
24package sampleapi;
25
26import java.nio.file.Files;
27import java.nio.file.Path;
28import java.nio.file.Paths;
29
30public class SampleApiDefaultRunner {
31
32    public static final String MSG_NO_OUTDIR =
33        "SampleApi: no outdir set";
34    public static final String MSG_NO_RESDIR =
35        "SampleApi: no resourcedir set";
36    public static final String MSG_DUP_OUTDIR =
37        "SampleApi: duplicated outdir detected: ";
38    public static final String MSG_DUP_RESDIR =
39        "SampleApi: duplicated resourcedir detected: ";
40    public static final String MSG_USE_FIRST =
41        "           will use first occurance: ";
42    public static final String MSG_INVAL_OUTDIR =
43        "SampleApi: outdir is not valid: ";
44    public static final String MSG_INVAL_RESDIR =
45        "SampleApi: outdir is not valid: ";
46    public static final String MSG_CANNOT_GEN =
47        "SampleApi: cannot generate output: ";
48    public static final String MSG_WRONG_OPTION =
49        "SampleApi: incorrect option: ";
50    public static final String MSG_USE_HELP =
51        "           use -? for help";
52    public static final String[] MSG_HELP = {
53        "SampleApi options:",
54        "    -?|-h|--help                                 - print help",
55        "    -r=<dir>|--resdir=<dir>|--resourcedir=<dir>  - set <dir> to find xml resources",
56        "    -o=<dir>|--outdir=<dir>                      - set <dir> to generate output"
57    };
58
59    public static void main(String... args) throws Exception {
60        System.exit(execute(args));
61    }
62
63    public static int execute(String... args) throws Exception {
64        if (args.length == 0) {
65            printHelp();
66            return 1;
67        }
68
69        String outDirName = "";
70        String resDirName = "";
71
72        boolean isOutDirSet = false;
73        boolean isResDirSet = false;
74        boolean isHelpPrinted = false;
75        for (String arg : args) {
76            Option option = new Option(arg);
77            switch (option.getOptionName()) {
78                case "-?":
79                case "-h":
80                case "--help":
81                    if (!isHelpPrinted) {
82                        printHelp();
83                        isHelpPrinted = true;
84                    }
85                    break;
86                case "-o":
87                case "--outdir":
88                    if (!isOutDirSet) {
89                        outDirName = option.getOptionValue();
90                        isOutDirSet = true;
91                    } else {
92                        System.err.println(MSG_DUP_OUTDIR + option.getOptionValue());
93                        System.err.println(MSG_USE_FIRST + outDirName);
94                    }
95                    break;
96                case "-r":
97                case "--resdir":
98                case "--resourcedir":
99                    if (!isResDirSet) {
100                        resDirName = option.getOptionValue();
101                        isResDirSet = true;
102                    } else {
103                        System.err.println(MSG_DUP_RESDIR + option.getOptionValue());
104                        System.err.println(MSG_USE_FIRST + resDirName);
105                    }
106                    break;
107                default:
108                    System.err.println(MSG_WRONG_OPTION + arg);
109                    System.err.println(MSG_USE_HELP);
110                    break;
111            }
112
113        }
114
115        if (!isOutDirSet) {
116            System.err.println(MSG_NO_OUTDIR);
117            return 1;
118        }
119
120        if (outDirName.length() == 0) {
121            System.err.println(MSG_INVAL_OUTDIR + outDirName);
122            return 1;
123        }
124
125        if (!isResDirSet) {
126            System.err.println(MSG_NO_RESDIR);
127            return 1;
128        }
129
130        if (resDirName.length() == 0) {
131            System.err.println(MSG_INVAL_RESDIR + resDirName);
132            return 1;
133        }
134
135        Path resDir = Paths.get(resDirName);
136        Path outDir = Paths.get(outDirName);
137        Files.createDirectories(outDir);
138        SampleApi apiGen = new SampleApi();
139
140        apiGen.load(resDir).generate(outDir);
141
142        return 0;
143    }
144
145    private static void printHelp() {
146        for (String line : MSG_HELP)
147            System.out.println(line);
148    }
149
150    private static class Option {
151
152        private final String optionName;
153        private final String optionValue;
154
155        public Option(String arg) {
156            int delimPos = arg.indexOf('=');
157
158            if (delimPos == -1) {
159                optionName = arg;
160                optionValue = "";
161            } else {
162                optionName = arg.substring(0, delimPos);
163                optionValue = arg.substring(delimPos + 1, arg.length());
164            }
165        }
166
167        public String getOptionName() {
168            return optionName;
169        }
170
171        public String getOptionValue() {
172            return optionValue;
173        }
174    }
175}
176