src/XmlHandler.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 18 Jan 2019 20:26:31 +0100
branchv_0
changeset 17 1ad18e8b4048
parent 15 26c945ea1733
child 19 3636510513d5
permissions -rw-r--r--
Added tag v0.9 for changeset 6b88bc57ae91
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
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     8
 * (at your option) any later version.
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     9
 *
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    10
 * 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
    11
 * 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
    12
 * 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
    13
 * GNU General Public License for more details.
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    14
 *
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    15
 * 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
    16
 * 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
    17
 */
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <regex>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/typedefs.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/TypeId.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#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
    32
#include <relpipe/xmlwriter/XMLWriter.h>
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
namespace relpipe {
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace out {
4
7e0211f00413 fix names
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    36
namespace xml {
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
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
    39
using namespace relpipe::xmlwriter;
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
class XmlHandler : public handlers::RelationalReaderStringHadler {
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
private:
15
26c945ea1733 constant for XMLNS
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    43
	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
    44
	shared_ptr<XMLWriter> xmlWriter;
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	std::vector<TypeId> columnTypes;
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	std::vector<string_t> columnTypeCodes;
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	std::vector<string_t> columnNames;
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    48
	integer_t valueCount = 0;
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	integer_t columnCount = 0;
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    50
	integer_t relationCount = 0;
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
public:
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
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
    54
	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
    55
	}
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	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
    58
		valueCount = 0;
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    59
		columnCount = 0;
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    60
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    61
		if (relationCount == 0) {
15
26c945ea1733 constant for XMLNS
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    62
			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
    63
		} else {
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
    64
			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
    65
			xmlWriter->writeEndElement();
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    66
		}
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    67
		relationCount++;
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
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
    95
		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
    96
		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
    97
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    98
		valueCount++;
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    99
14
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   100
		// 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
   101
		xmlWriter->writeTextElement(L"attribute",{
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   102
			L"name", columnNames[i],
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   103
			L"type", columnTypeCodes[i]
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   104
		}, value);
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   105
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
	}
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
	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
   109
		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
   110
		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
   111
		xmlWriter->writeEndElement();
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
	}
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
}