|
1 /** |
|
2 * Relational pipes |
|
3 * Copyright © 2020 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, version 3 of the License. |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 */ |
|
17 #pragma once |
|
18 |
|
19 #include <vector> |
|
20 |
|
21 #include <relpipe/common/type/typedefs.h> |
|
22 #include <relpipe/cli/CLI.h> |
|
23 #include <relpipe/cli/RelpipeCLIException.h> |
|
24 |
|
25 #include "Configuration.h" |
|
26 |
|
27 namespace relpipe { |
|
28 namespace tr { |
|
29 namespace xpath { |
|
30 |
|
31 class CLIParser { |
|
32 private: |
|
33 |
|
34 relpipe::common::type::StringX readNext(std::vector<relpipe::common::type::StringX> arguments, int& i) { |
|
35 if (i < arguments.size()) return arguments[i++]; |
|
36 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); |
|
37 } |
|
38 |
|
39 /** |
|
40 * TODO: use a common method |
|
41 */ |
|
42 bool parseBoolean(const relpipe::common::type::StringX& value) { |
|
43 if (value == L"true") return true; |
|
44 else if (value == L"false") return false; |
|
45 else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
46 } |
|
47 |
|
48 void addRelation(Configuration& c, RelationConfiguration& currentRelation) { |
|
49 if (currentRelation.relation.size()) { |
|
50 c.relationConfigurations.push_back(currentRelation); |
|
51 currentRelation = RelationConfiguration(); |
|
52 } |
|
53 } |
|
54 |
|
55 /** |
|
56 * TODO: use a common method |
|
57 */ |
|
58 relpipe::writer::TypeId parseTypeId(const relpipe::common::type::StringX& value) { |
|
59 using t = relpipe::writer::TypeId; |
|
60 if (value == L"string") return t::STRING; |
|
61 else if (value == L"integer") return t::INTEGER; |
|
62 else if (value == L"boolean") return t::BOOLEAN; |
|
63 else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
64 } |
|
65 |
|
66 public: |
|
67 |
|
68 static const relpipe::common::type::StringX OPTION_NAMESPACE; |
|
69 static const relpipe::common::type::StringX OPTION_RELATION; |
|
70 static const relpipe::common::type::StringX OPTION_WHERE; |
|
71 static const relpipe::common::type::StringX OPTION_INPUT_ATTRIBUTES; |
|
72 static const relpipe::common::type::StringX OPTION_OUTPUT_ATTRIBUTE; |
|
73 static const relpipe::common::type::StringX OPTION_XML_ATTRIBUTE; |
|
74 |
|
75 Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) { |
|
76 Configuration c; |
|
77 RelationConfiguration currentRelation; |
|
78 |
|
79 for (int i = 0; i < arguments.size();) { |
|
80 relpipe::common::type::StringX option = readNext(arguments, i); |
|
81 |
|
82 if (option == OPTION_NAMESPACE) { |
|
83 c.namespaceMappings.push_back(readNext(arguments, i)); |
|
84 c.namespaceMappings.push_back(readNext(arguments, i)); |
|
85 } else if (option == OPTION_RELATION) { |
|
86 addRelation(c, currentRelation); // previous relation |
|
87 currentRelation.relation = readNext(arguments, i); |
|
88 } else if (option == OPTION_WHERE) { |
|
89 currentRelation.where = readNext(arguments, i); |
|
90 } else if (option == OPTION_XML_ATTRIBUTE) { |
|
91 currentRelation.xmlAttributes.push_back(readNext(arguments, i)); |
|
92 } else if (option == OPTION_OUTPUT_ATTRIBUTE) { |
|
93 AttributeRecipe attribute; |
|
94 attribute.name = readNext(arguments, i); |
|
95 attribute.type = parseTypeId(readNext(arguments, i)); |
|
96 attribute.xpath = readNext(arguments, i); |
|
97 currentRelation.outputAttributes.push_back(attribute); |
|
98 } else if (option == OPTION_INPUT_ATTRIBUTES) { |
|
99 relpipe::common::type::StringX policyName = readNext(arguments, i); |
|
100 InputAttributePolicy policy; |
|
101 if (policyName == L"append") policy = InputAttributePolicy::Append; |
|
102 else if (policyName == L"prepend") policy = InputAttributePolicy::Prepend; |
|
103 else if (policyName == L"auto") policy = InputAttributePolicy::Auto; |
|
104 else throw relpipe::cli::RelpipeCLIException(L"Unsupported input attributes policy: " + policyName, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
105 currentRelation.inputAttributePolicy = policy; |
|
106 } else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
107 } |
|
108 addRelation(c, currentRelation); // last relation |
|
109 |
|
110 return c; |
|
111 } |
|
112 |
|
113 virtual ~CLIParser() { |
|
114 } |
|
115 }; |
|
116 |
|
117 const relpipe::common::type::StringX CLIParser::OPTION_NAMESPACE = L"--namespace"; |
|
118 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation"; |
|
119 const relpipe::common::type::StringX CLIParser::OPTION_WHERE = L"--where"; |
|
120 const relpipe::common::type::StringX CLIParser::OPTION_INPUT_ATTRIBUTES = L"--input-attributes"; |
|
121 const relpipe::common::type::StringX CLIParser::OPTION_OUTPUT_ATTRIBUTE = L"--output-attribute"; |
|
122 const relpipe::common::type::StringX CLIParser::OPTION_XML_ATTRIBUTE = L"--xml-attribute"; |
|
123 |
|
124 } |
|
125 } |
|
126 } |