src/XmlHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 04 Dec 2021 21:14:52 +0100
branchv_0
changeset 34 03a167952dcd
parent 32 1164d5bc5640
permissions -rw-r--r--
Added tag v0.18 for changeset a1024068410c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     1
/**
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     2
 * Relational pipes
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     4
 *
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
23
47a7d86ad810 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
3
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     8
 *
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    12
 * GNU General Public License for more details.
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    13
 *
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    14
 * You should have received a copy of the GNU General Public License
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    16
 */
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <string>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <vector>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <iostream>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <sstream>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <locale>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <codecvt>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <regex>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/reader/typedefs.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/TypeId.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/AttributeMetadata.h>
13
816094aa1fff move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    31
#include <relpipe/xmlwriter/XMLWriter.h>
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
namespace relpipe {
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
namespace out {
4
7e0211f00413 fix names
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    35
namespace xml {
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
using namespace relpipe::reader;
13
816094aa1fff move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    38
using namespace relpipe::xmlwriter;
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
19
3636510513d5 fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    40
class XmlHandler : public handlers::RelationalReaderStringHandler {
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
private:
15
26c945ea1733 constant for XMLNS
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    42
	const wstring XMLNS = L"tag:globalcode.info,2018:relpipe";
13
816094aa1fff move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    43
	shared_ptr<XMLWriter> xmlWriter;
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	std::vector<TypeId> columnTypes;
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	std::vector<string_t> columnTypeCodes;
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	std::vector<string_t> columnNames;
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    47
	integer_t valueCount = 0;
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	integer_t columnCount = 0;
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    49
	integer_t relationCount = 0;
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
public:
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
13
816094aa1fff move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    53
	XmlHandler(std::ostream& output) : xmlWriter(new XMLWriter(output)) {
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	}
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override {
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    57
		if (relationCount == 0) {
15
26c945ea1733 constant for XMLNS
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    58
			xmlWriter->writeStartElement(L"relpipe",{L"xmlns", XMLNS});
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    59
		} else {
32
1164d5bc5640 properly close elements when the relation is empty
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    60
			if (valueCount) xmlWriter->writeEndElement();
13
816094aa1fff move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    61
			xmlWriter->writeEndElement();
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    62
		}
32
1164d5bc5640 properly close elements when the relation is empty
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    63
1164d5bc5640 properly close elements when the relation is empty
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    64
		valueCount = 0;
1164d5bc5640 properly close elements when the relation is empty
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    65
		columnCount = 0;
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    66
		relationCount++;
32
1164d5bc5640 properly close elements when the relation is empty
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    67
13
816094aa1fff move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    68
		xmlWriter->writeStartElement(L"relation");
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    69
13
816094aa1fff move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    70
		xmlWriter->writeTextElement(L"name",{}, name);
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    71
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    72
14
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    73
		xmlWriter->writeStartElement(L"attributes-metadata");
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		columnCount = attributes.size();
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		columnTypes.resize(columnCount);
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		columnTypeCodes.resize(columnCount);
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		columnNames.resize(columnCount);
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
		for (int i = 0; i < attributes.size(); i++) {
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
			columnNames[i] = attributes[i].getAttributeName();
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
			columnTypes[i] = attributes[i].getTypeId();
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
			columnTypeCodes[i] = attributes[i].getTypeName();
14
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    82
			xmlWriter->writeEmptyElement(L"attribute-metadata",{
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    83
				L"name", columnNames[i],
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    84
				L"type", columnTypeCodes[i]
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    85
			});
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
		}
14
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    87
		xmlWriter->writeEndElement();
13
816094aa1fff move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    88
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    89
		// TODO: print attribute metadata
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	}
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
	void attribute(const string_t& value) override {
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    93
		integer_t i = valueCount % columnCount;
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    94
32
1164d5bc5640 properly close elements when the relation is empty
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    95
		// TODO: end element on last attribute instead of first + on end of pipe
13
816094aa1fff move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    96
		if (i == 0 && valueCount) xmlWriter->writeEndElement();
816094aa1fff move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    97
		if (i == 0) xmlWriter->writeStartElement(L"record");
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    98
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    99
		valueCount++;
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   100
14
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   101
		// TODO: print attribute metadata (only optional, not default)
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   102
		xmlWriter->writeTextElement(L"attribute",{
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   103
			L"name", columnNames[i],
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   104
			L"type", columnTypeCodes[i]
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   105
		}, value);
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   106
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	}
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
	void endOfPipe() {
13
816094aa1fff move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   110
		if (valueCount) xmlWriter->writeEndElement();
816094aa1fff move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   111
		if (relationCount) xmlWriter->writeEndElement();
816094aa1fff move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   112
		xmlWriter->writeEndElement();
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
	}
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
};
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
}
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
}
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
}