|
1 /** |
|
2 * Relational pipes |
|
3 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 #pragma once |
|
19 |
|
20 #include <vector> |
|
21 |
|
22 #include <relpipe/writer/typedefs.h> |
|
23 #include <relpipe/cli/CLI.h> |
|
24 #include <relpipe/cli/RelpipeCLIException.h> |
|
25 |
|
26 #include "Configuration.h" |
|
27 |
|
28 namespace relpipe { |
|
29 namespace in { |
|
30 namespace xmltable { |
|
31 |
|
32 class CLIParser { |
|
33 private: |
|
34 |
|
35 string_t readNext(std::vector<string_t> arguments, int& i) { |
|
36 if (i < arguments.size()) return arguments[i++]; |
|
37 else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
38 } |
|
39 |
|
40 void addRelation(Configuration& c, RelationConfiguration& currentRelation) { |
|
41 if (currentRelation.relation.size()) { |
|
42 c.relationConfigurations.push_back(currentRelation); |
|
43 currentRelation = RelationConfiguration(); |
|
44 } |
|
45 } |
|
46 |
|
47 relpipe::writer::TypeId parseTypeId(const string_t& value) { |
|
48 using t = relpipe::writer::TypeId; |
|
49 if (value == L"string") return t::STRING; |
|
50 else if (value == L"integer") return t::INTEGER; |
|
51 else if (value == L"boolean") return t::BOOLEAN; |
|
52 else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
53 } |
|
54 |
|
55 public: |
|
56 |
|
57 static const string_t OPTION_NAMESPACE; |
|
58 static const string_t OPTION_RELATION; |
|
59 static const string_t OPTION_RECORDS; |
|
60 static const string_t OPTION_ATTRIBUTE; |
|
61 |
|
62 Configuration parse(const std::vector<string_t>& arguments) { |
|
63 Configuration c; |
|
64 RelationConfiguration currentRelation; |
|
65 |
|
66 for (int i = 0; i < arguments.size();) { |
|
67 string_t option = readNext(arguments, i); |
|
68 |
|
69 if (option == OPTION_NAMESPACE) { |
|
70 c.namespaceMappings.push_back(readNext(arguments, i)); |
|
71 c.namespaceMappings.push_back(readNext(arguments, i)); |
|
72 } else if (option == OPTION_RELATION) { |
|
73 addRelation(c, currentRelation); // previous relation |
|
74 currentRelation.relation = readNext(arguments, i); |
|
75 } else if (option == OPTION_RECORDS) { |
|
76 currentRelation.xpath = readNext(arguments, i); |
|
77 } else if (option == OPTION_ATTRIBUTE) { |
|
78 AttributeRecipe attribute; |
|
79 attribute.name = readNext(arguments, i); |
|
80 attribute.type = parseTypeId(readNext(arguments, i)); |
|
81 attribute.xpath = readNext(arguments, i); |
|
82 currentRelation.attributes.push_back(attribute); |
|
83 } else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
84 } |
|
85 addRelation(c, currentRelation); // last relation |
|
86 |
|
87 return c; |
|
88 } |
|
89 |
|
90 virtual ~CLIParser() { |
|
91 } |
|
92 }; |
|
93 |
|
94 const string_t CLIParser::OPTION_NAMESPACE = L"--namespace"; |
|
95 const string_t CLIParser::OPTION_RELATION = L"--relation"; |
|
96 const string_t CLIParser::OPTION_RECORDS = L"--records"; |
|
97 const string_t CLIParser::OPTION_ATTRIBUTE = L"--attribute"; |
|
98 |
|
99 } |
|
100 } |
|
101 } |