/**
* Relational pipes
* Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include <relpipe/writer/typedefs.h>
#include <relpipe/cli/CLI.h>
#include <relpipe/cli/RelpipeCLIException.h>
#include "Configuration.h"
namespace relpipe {
namespace in {
namespace xmltable {
class CLIParser {
private:
string_t readNext(std::vector<string_t> arguments, int& i) {
if (i < arguments.size()) return arguments[i++];
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);
}
/**
* TODO: use a common method
*/
bool parseBoolean(const string_t& value) {
if (value == L"true") return true;
else if (value == L"false") return false;
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);
}
void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
if (currentRelation.relation.size()) {
c.relationConfigurations.push_back(currentRelation);
currentRelation = RelationConfiguration();
}
}
relpipe::writer::TypeId parseTypeId(const string_t& value) {
using t = relpipe::writer::TypeId;
if (value == L"string") return t::STRING;
else if (value == L"integer") return t::INTEGER;
else if (value == L"boolean") return t::BOOLEAN;
else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
}
public:
static const string_t OPTION_NAMESPACE;
static const string_t OPTION_RELATION;
static const string_t OPTION_NAME_IS_XPATH;
static const string_t OPTION_RECORDS;
static const string_t OPTION_ATTRIBUTE;
static const string_t OPTION_XINCLUDE;
static const string_t OPTION_MODE;
static const string_t OPTION_RAW_XML_NODELIST_WRAPPER;
static const string_t OPTION_RAW_XML_ATTRIBUTE_WRAPPER;
Configuration parse(const std::vector<string_t>& arguments) {
Configuration c;
RelationConfiguration currentRelation;
for (int i = 0; i < arguments.size();) {
string_t option = readNext(arguments, i);
if (option == OPTION_NAMESPACE) {
c.namespaceMappings.push_back(readNext(arguments, i));
c.namespaceMappings.push_back(readNext(arguments, i));
} else if (option == OPTION_XINCLUDE) {
c.xinclude = parseBoolean(readNext(arguments, i));
} else if (option == OPTION_RELATION) {
addRelation(c, currentRelation); // previous relation
currentRelation.relation = readNext(arguments, i);
} else if (option == OPTION_NAME_IS_XPATH) {
currentRelation.nameIsXPath = true;
} else if (option == OPTION_RECORDS) {
currentRelation.xpath = readNext(arguments, i);
} else if (option == OPTION_ATTRIBUTE) {
AttributeRecipe attribute;
attribute.mode = currentRelation.mode;
attribute.rawXmlNodeListWrapper = currentRelation.rawXmlNodeListWrapper;
attribute.rawXmlAttributeWrapper = currentRelation.rawXmlAttributeWrapper;
attribute.name = readNext(arguments, i);
attribute.type = parseTypeId(readNext(arguments, i));
attribute.xpath = readNext(arguments, i);
currentRelation.attributes.push_back(attribute);
} else if (option == OPTION_MODE) {
string_t modeName = readNext(arguments, i);
Mode mode;
if (modeName == L"string") mode = Mode::STRING;
else if (modeName == L"boolean") mode = Mode::BOOLEAN;
else if (modeName == L"raw-xml") mode = Mode::RAW_XML;
else if (modeName == L"line-number") mode = Mode::LINE_NUMBER;
else if (modeName == L"xpath") mode = Mode::XPATH;
else throw relpipe::cli::RelpipeCLIException(L"Unsupported mode: " + modeName, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
if (currentRelation.attributes.size()) currentRelation.attributes.back().mode = mode;
else currentRelation.mode = mode;
} else if (option == OPTION_RAW_XML_NODELIST_WRAPPER) {
XmlElementSkeleton w = {readNext(arguments, i), readNext(arguments, i), readNext(arguments, i)};
if (currentRelation.attributes.size()) currentRelation.attributes.back().rawXmlNodeListWrapper = w;
else currentRelation.rawXmlNodeListWrapper = w;
} else if (option == OPTION_RAW_XML_ATTRIBUTE_WRAPPER) {
XmlElementSkeleton w = {readNext(arguments, i), readNext(arguments, i), readNext(arguments, i)};
if (currentRelation.attributes.size()) currentRelation.attributes.back().rawXmlAttributeWrapper = w;
else currentRelation.rawXmlAttributeWrapper = w;
} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
}
addRelation(c, currentRelation); // last relation
return c;
}
virtual ~CLIParser() {
}
};
const string_t CLIParser::OPTION_NAMESPACE = L"--namespace";
const string_t CLIParser::OPTION_RELATION = L"--relation";
const string_t CLIParser::OPTION_NAME_IS_XPATH = L"--name-is-xpath";
const string_t CLIParser::OPTION_RECORDS = L"--records";
const string_t CLIParser::OPTION_ATTRIBUTE = L"--attribute";
const string_t CLIParser::OPTION_XINCLUDE = L"--xinclude";
const string_t CLIParser::OPTION_MODE = L"--mode";
const string_t CLIParser::OPTION_RAW_XML_NODELIST_WRAPPER = L"--raw-xml-nodelist-wrapper";
const string_t CLIParser::OPTION_RAW_XML_ATTRIBUTE_WRAPPER = L"--raw-xml-attribute-wrapper";
}
}
}