Skip to content

Method: verifyArgumentCount(OptionSet)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.owl2java;
16:
17: import joptsimple.OptionParser;
18: import joptsimple.OptionSet;
19: import org.slf4j.Logger;
20: import org.slf4j.LoggerFactory;
21:
22: import java.util.Arrays;
23: import java.util.HashMap;
24: import java.util.Map;
25:
26: public class OWL2Java {
27:
28: private static final Logger LOG = LoggerFactory.getLogger(OWL2Java.class);
29:
30: // CLI map
31: private static final Map<Command, OptionParser> map = new HashMap<>();
32:
33: static {
34: map.put(Command.help, new OptionParser() {
35: {
36: // no options
37: }
38: });
39: map.put(Command.transform, new OptionParser() {
40: {
41: accepts(Param.MAPPING_FILE.arg, Param.MAPPING_FILE.description).withRequiredArg().ofType(String.class);
42: accepts(Param.PACKAGE.arg, Param.PACKAGE.description).withRequiredArg().ofType(String.class)
43: .defaultsTo("generated");
44: accepts(Param.CONTEXT.arg, Param.CONTEXT.description).withRequiredArg().ofType(String.class);
45: accepts(Param.WITH_IRIS.arg, Param.WITH_IRIS.description).withRequiredArg().ofType(Boolean.class)
46: .defaultsTo(false);
47: accepts(Param.TARGET_DIR.arg, Param.TARGET_DIR.description).withRequiredArg().ofType(String.class)
48: .defaultsTo("");
49: }
50: });
51: map.put(Command.vocabulary, new OptionParser() {
52: {
53: accepts(Param.MAPPING_FILE.arg, Param.MAPPING_FILE.description).withRequiredArg().ofType(String.class);
54: accepts(Param.CONTEXT.arg, Param.CONTEXT.description).withRequiredArg().ofType(String.class);
55: accepts(Param.WITH_IRIS.arg, Param.WITH_IRIS.description).withRequiredArg().ofType(Boolean.class)
56: .defaultsTo(false);
57: accepts(Param.TARGET_DIR.arg, Param.TARGET_DIR.description).withRequiredArg().ofType(String.class)
58: .defaultsTo("");
59: }
60: });
61: map.put(Command.list, new OptionParser() {
62: {
63: accepts(Param.MAPPING_FILE.arg, Param.MAPPING_FILE.description).withRequiredArg().ofType(String.class);
64: }
65: });
66: map.put(Command.version, new OptionParser() {
67: {
68: // no options
69: }
70: });
71: }
72:
73: private enum Command {
74: help, list, transform, vocabulary, version
75: }
76:
77: private enum Param {
78: MAPPING_FILE("m", "mapping file"), CONTEXT("c", "context name"), WITH_IRIS("w", "with OWLAPI IRIs"), TARGET_DIR(
79: "d", "output directory"), PACKAGE("p", "package");
80:
81: private final String arg;
82: private final String description;
83:
84: Param(String arg, String description) {
85: this.arg = arg;
86: this.description = description;
87: }
88: }
89:
90: private static void printHelp(Command cc) {
91: switch (cc) {
92: case help:
93: System.out
94: .println(
95: "Help command gives hints on how to use other commands. Try 'OWL2Java help <command>' for more specific info.");
96: System.out.println("");
97: System.out.println("Syntax: OWL2Java help <command>");
98: System.out.println("");
99: break;
100: case list:
101: System.out.println("Lists all available IC contexts.");
102: System.out.println("");
103: System.out
104: .println("Syntax: OWL2Java list <ontology_iri> [ <options> ].");
105: System.out.println("");
106: break;
107: case transform:
108: System.out
109: .println("Transforms all ICs into annotated Java classes.");
110: System.out.println("");
111: System.out
112: .println("Syntax: OWL2Java transform <ontology_iri> [ <options> ].");
113: System.out.println("");
114: break;
115: case vocabulary:
116: System.out
117: .println("Generates vocabulary based on the ICs.");
118: System.out.println("");
119: System.out
120: .println("Syntax: OWL2Java vocabulary <ontology_iri> [ <options> ].");
121: System.out.println("");
122: break;
123: case version:
124: System.out.println("Prints the version of the OWL2Java tool.");
125: break;
126: }
127:
128: try {
129: map.get(cc).printHelpOn(System.out);
130: } catch (Exception e) {
131: LOG.error(e.getMessage(), e);
132: }
133: }
134:
135: private static Command getCommandOrNull(String s) {
136: try {
137: return Command.valueOf(s);
138: } catch (IllegalArgumentException e) {
139: return null;
140: }
141: }
142:
143: public static void main(String[] args) {
144:
145: if (args.length == 0) {
146: System.out.println("Syntax: OWL2Java <command> <args>. Run 'OWL2Java help' for more details");
147: return;
148: }
149:
150: final Command c;
151:
152: if ((c = getCommandOrNull(args[0])) == null) {
153: System.err
154: .println("Invalid command " + args[0] + ", try 'OWL2Java help' for the list of available commands");
155: return;
156: }
157:
158: final OptionParser op = map.get(c);
159: final OptionSet os = op.parse(args);
160:
161: final OWL2JavaTransformer oj;
162:
163: switch (c) {
164: case help:
165: if (args.length != 1) {
166: final Command cc;
167: if ((cc = getCommandOrNull(args[1])) != null) {
168: printHelp(cc);
169: } else {
170: System.err.println("Invalid command " + args[0] + " " + args[1] +
171: ", try 'OWL2Java help' for the list of available commands.");
172: return;
173: }
174: } else {
175: System.out.println("Available commands : " + Arrays.asList(Command.values()));
176: }
177: break;
178: case list:
179: if (!verifyArgumentCount(os)) {
180: break;
181: }
182: oj = getTransformer(os);
183:
184: System.out.println("Available contexts: " + oj.listContexts());
185: break;
186: case transform:
187: transformOwlToJava(os);
188: break;
189: case vocabulary:
190: generateVocabulary(os);
191: break;
192: case version:
193: System.out.println("OWL2Java version " + Constants.VERSION);
194: break;
195: default:
196: System.err.println("Unknown command '" + args[0] + "', try 'OWL2Java help.'");
197: }
198: }
199:
200: private static OWL2JavaTransformer getTransformer(OptionSet os) {
201: OWL2JavaTransformer oj;
202: oj = new OWL2JavaTransformer();
203: if (os.has(Param.MAPPING_FILE.arg)) {
204: oj.setOntology(os.nonOptionArguments().get(1), os.valueOf(Param.MAPPING_FILE.arg).toString(), true);
205: } else {
206: oj.setOntology(os.nonOptionArguments().get(1), null, true);
207: }
208: return oj;
209: }
210:
211: private static boolean verifyArgumentCount(OptionSet os) {
212:• if (os.nonOptionArguments().size() != 2) {
213: System.err
214: .println("Exactly one ontology IRI has to be specified, got "
215: + (os.nonOptionArguments().size() - 1)
216: + ", try 'OWL2Java help' for the list of available commands");
217: return false;
218: }
219: return true;
220: }
221:
222: private static void transformOwlToJava(OptionSet os) {
223: if (!verifyTransformOptions(os)) {
224: return;
225: }
226:
227: final OWL2JavaTransformer oj = getTransformer(os);
228:
229: oj.transform(os.valueOf(Param.CONTEXT.arg).toString(),
230: os.valueOf(Param.PACKAGE.arg).toString(), os.valueOf(Param.TARGET_DIR.arg).toString(),
231: (Boolean) os.valueOf(Param.WITH_IRIS.arg));
232: }
233:
234: private static boolean verifyTransformOptions(OptionSet os) {
235: if (!verifyArgumentCount(os)) {
236: return false;
237: }
238:
239: if (!os.has(Param.CONTEXT.arg)) {
240: System.err.println("The parameter '-" + Param.CONTEXT.arg +
241: "' is obligatory. Try the 'help' command for more details.");
242: return false;
243: }
244: return true;
245: }
246:
247: private static void generateVocabulary(OptionSet os) {
248: if (!verifyTransformOptions(os)) {
249: return;
250: }
251: final OWL2JavaTransformer transformer = getTransformer(os);
252:
253: transformer.generateVocabulary(os.valueOf(Param.CONTEXT.arg).toString(),
254: os.valueOf(Param.TARGET_DIR.arg).toString(), (Boolean) os.valueOf(Param.WITH_IRIS.arg));
255: }
256: }