src/INIDispatchHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 13 Dec 2020 16:47:25 +0100
branchv_0
changeset 7 bda9d3c83cb7
parent 5 bee7acb57330
permissions -rw-r--r--
improved basic escaping
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <regex>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <stdexcept>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/common/type/typedefs.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/TypeId.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/reader/handlers/AttributeMetadata.h>
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include "Configuration.h"
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include "INIWriter.h"
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include "INIStandardHandler.h"
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
#include "INILiteralHandler.h"
1
e04e5bbc147b add --style section-first
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    38
#include "INISectionFirstHandler.h"
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    39
#include "JavaPropertiesDialect.h"
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    40
#include "JavaManifestMFDialect.h"
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    41
#include "JavaPropertiesEscapingProcessor.h"
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    42
#include "BasicEscapingProcessor.h"
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    43
#include "uri.h"
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
namespace relpipe {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
namespace out {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
namespace ini {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
class INIDispatchHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
private:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	std::ostream& output;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	INIWriter writer;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	Configuration& configuration;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	std::shared_ptr<relpipe::reader::handlers::RelationalReaderStringHandler> currentHandler;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	relpipe::reader::handlers::RelationalReaderStringHandler* createHandlerAuto(const relpipe::common::type::StringX& name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		bool hasKey = false;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		bool hasValue = false;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		for (auto a : attributes) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
			if (a.getAttributeName() == L"key") hasKey = true;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
			else if (a.getAttributeName() == L"value") hasValue = true;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		if (hasKey && hasValue) return new INIStandardHandler(writer);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		else return new INILiteralHandler(writer);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	relpipe::reader::handlers::RelationalReaderStringHandler* createHandler(const relpipe::common::type::StringX& name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		for (auto rc : configuration.relationConfigurations) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
			if (std::regex_match(name, std::wregex(rc.relation))) {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
				if (rc.style == Style::Standard) return new INIStandardHandler(writer);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
				else if (rc.style == Style::Literal) return new INILiteralHandler(writer);
1
e04e5bbc147b add --style section-first
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    74
				else if (rc.style == Style::SectionFirst) return new INISectionFirstHandler(writer);
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
				else if (rc.style == Style::Dropped) return nullptr;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
				else if (rc.style == Style::Automatic) return createHandlerAuto(name, attributes);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
				else throw std::invalid_argument("Unsupported style: " + std::to_string((int) rc.style));
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		if (configuration.relationConfigurations.size()) return nullptr;
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
		else return createHandlerAuto(name, attributes);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
public:
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	INIDispatchHandler(std::ostream& output, Configuration& configuration) : output(output), writer(output), configuration(configuration) {
7
bda9d3c83cb7 improved basic escaping
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    88
		// TODO: dialects might be moved to configuration (after switching to alt2xml writers)
3
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    89
		writer.addDialect(std::make_shared<JavaPropertiesDialect>(), dialect::JavaProperties, false);
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    90
		writer.addDialect(std::make_shared<JavaManifestMFDialect>(), dialect::JavaManifestMF, false);
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    91
		writer.addEscapingProcessor(std::make_shared<BasicEscapingProcessor>(), escaping::Basic, true);
ae8775e0bc7a configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    92
		writer.addEscapingProcessor(std::make_shared<JavaPropertiesEscapingProcessor>(), escaping::JavaProperties, false);
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		for (auto o : configuration.writerOptions) writer.setOption(o.uri, o.value);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
		if (currentHandler) currentHandler->endOfPipe();
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
		currentHandler.reset(createHandler(name, attributes));
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
		if (currentHandler) currentHandler->startRelation(name, attributes);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
	void attribute(const relpipe::common::type::StringX& value) override {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
		if (currentHandler) currentHandler->attribute(value);
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
	void endOfPipe() {
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
		if (currentHandler) currentHandler->endOfPipe();
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
		currentHandler.reset();
5
bee7acb57330 implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   109
		writer.endDocument();
0
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
		output.flush();
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
	}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
};
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
}
1bb084f84eb9 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
}