src/CLIParser.h
branchv_0
changeset 0 2f783f0573fa
equal deleted inserted replaced
-1:000000000000 0:2f783f0573fa
       
     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, 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/writer/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 in {
       
    29 namespace xmltable {
       
    30 
       
    31 class CLIParser {
       
    32 private:
       
    33 
       
    34 	string_t readNext(std::vector<string_t> 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 string_t& 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 	relpipe::writer::TypeId parseTypeId(const string_t& value) {
       
    56 		using t = relpipe::writer::TypeId;
       
    57 		if (value == L"string") return t::STRING;
       
    58 		else if (value == L"integer") return t::INTEGER;
       
    59 		else if (value == L"boolean") return t::BOOLEAN;
       
    60 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    61 	}
       
    62 
       
    63 public:
       
    64 
       
    65 	static const string_t OPTION_NAMESPACE;
       
    66 	static const string_t OPTION_PARSER_OPTION;
       
    67 	static const string_t OPTION_RELATION;
       
    68 	static const string_t OPTION_NAME_IS_XPATH;
       
    69 	static const string_t OPTION_RECORDS;
       
    70 	static const string_t OPTION_ATTRIBUTE;
       
    71 	static const string_t OPTION_XINCLUDE;
       
    72 	static const string_t OPTION_MODE;
       
    73 	static const string_t OPTION_RAW_XML_NODELIST_WRAPPER;
       
    74 	static const string_t OPTION_RAW_XML_ATTRIBUTE_WRAPPER;
       
    75 
       
    76 	Configuration parse(const std::vector<string_t>& arguments) {
       
    77 		Configuration c;
       
    78 		RelationConfiguration currentRelation;
       
    79 
       
    80 		for (int i = 0; i < arguments.size();) {
       
    81 			string_t option = readNext(arguments, i);
       
    82 
       
    83 			if (option == OPTION_NAMESPACE) {
       
    84 				c.namespaceMappings.push_back(readNext(arguments, i));
       
    85 				c.namespaceMappings.push_back(readNext(arguments, i));
       
    86 			} else if (option == OPTION_PARSER_OPTION) {
       
    87 				c.parserOptions.push_back({readNext(arguments, i), readNext(arguments, i)});
       
    88 			} else if (option == OPTION_XINCLUDE) {
       
    89 				c.xinclude = parseBoolean(readNext(arguments, i));
       
    90 			} else if (option == OPTION_RELATION) {
       
    91 				addRelation(c, currentRelation); // previous relation
       
    92 				currentRelation.relation = readNext(arguments, i);
       
    93 			} else if (option == OPTION_NAME_IS_XPATH) {
       
    94 				currentRelation.nameIsXPath = true;
       
    95 			} else if (option == OPTION_RECORDS) {
       
    96 				currentRelation.xpath = readNext(arguments, i);
       
    97 			} else if (option == OPTION_ATTRIBUTE) {
       
    98 				AttributeRecipe attribute;
       
    99 				attribute.mode = currentRelation.mode;
       
   100 				attribute.rawXmlNodeListWrapper = currentRelation.rawXmlNodeListWrapper;
       
   101 				attribute.rawXmlAttributeWrapper = currentRelation.rawXmlAttributeWrapper;
       
   102 				attribute.name = readNext(arguments, i);
       
   103 				attribute.type = parseTypeId(readNext(arguments, i));
       
   104 				attribute.xpath = readNext(arguments, i);
       
   105 				currentRelation.attributes.push_back(attribute);
       
   106 			} else if (option == OPTION_MODE) {
       
   107 				string_t modeName = readNext(arguments, i);
       
   108 				Mode mode;
       
   109 				if (modeName == L"string") mode = Mode::STRING;
       
   110 				else if (modeName == L"boolean") mode = Mode::BOOLEAN;
       
   111 				else if (modeName == L"raw-xml") mode = Mode::RAW_XML;
       
   112 				else if (modeName == L"line-number") mode = Mode::LINE_NUMBER;
       
   113 				else if (modeName == L"xpath") mode = Mode::XPATH;
       
   114 				else throw relpipe::cli::RelpipeCLIException(L"Unsupported mode: " + modeName, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
   115 				if (currentRelation.attributes.size()) currentRelation.attributes.back().mode = mode;
       
   116 				else currentRelation.mode = mode;
       
   117 			} else if (option == OPTION_RAW_XML_NODELIST_WRAPPER) {
       
   118 				XmlElementSkeleton w = {readNext(arguments, i), readNext(arguments, i), readNext(arguments, i)};
       
   119 				if (currentRelation.attributes.size()) currentRelation.attributes.back().rawXmlNodeListWrapper = w;
       
   120 				else currentRelation.rawXmlNodeListWrapper = w;
       
   121 			} else if (option == OPTION_RAW_XML_ATTRIBUTE_WRAPPER) {
       
   122 				XmlElementSkeleton w = {readNext(arguments, i), readNext(arguments, i), readNext(arguments, i)};
       
   123 				if (currentRelation.attributes.size()) currentRelation.attributes.back().rawXmlAttributeWrapper = w;
       
   124 				else currentRelation.rawXmlAttributeWrapper = w;
       
   125 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
   126 		}
       
   127 		addRelation(c, currentRelation); // last relation
       
   128 
       
   129 		return c;
       
   130 	}
       
   131 
       
   132 	virtual ~CLIParser() {
       
   133 	}
       
   134 };
       
   135 
       
   136 const string_t CLIParser::OPTION_NAMESPACE = L"--namespace";
       
   137 const string_t CLIParser::OPTION_PARSER_OPTION = L"--parser-option";
       
   138 const string_t CLIParser::OPTION_RELATION = L"--relation";
       
   139 const string_t CLIParser::OPTION_NAME_IS_XPATH = L"--name-is-xpath";
       
   140 const string_t CLIParser::OPTION_RECORDS = L"--records";
       
   141 const string_t CLIParser::OPTION_ATTRIBUTE = L"--attribute";
       
   142 const string_t CLIParser::OPTION_XINCLUDE = L"--xinclude";
       
   143 const string_t CLIParser::OPTION_MODE = L"--mode";
       
   144 const string_t CLIParser::OPTION_RAW_XML_NODELIST_WRAPPER = L"--raw-xml-nodelist-wrapper";
       
   145 const string_t CLIParser::OPTION_RAW_XML_ATTRIBUTE_WRAPPER = L"--raw-xml-attribute-wrapper";
       
   146 
       
   147 }
       
   148 }
       
   149 }