src/XmlHandler.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 09 Jan 2019 00:09:33 +0100
branchv_0
changeset 14 5094a7bf343d
parent 13 816094aa1fff
child 15 26c945ea1733
permissions -rw-r--r--
add attribute metadata: names and types
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:
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
		valueCount = 0;
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    58
		columnCount = 0;
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    59
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    60
		if (relationCount == 0) {
14
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    61
			xmlWriter->writeStartElement(L"relpipe",{L"xmlns", L"tag:globalcode.info,2018:relpipe"});
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    62
		} 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
    63
			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
    64
			xmlWriter->writeEndElement();
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    65
		}
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    66
		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
    67
		xmlWriter->writeStartElement(L"relation");
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    68
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
    69
		xmlWriter->writeTextElement(L"name",{}, name);
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    70
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    71
14
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    72
		xmlWriter->writeStartElement(L"attributes-metadata");
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		columnCount = attributes.size();
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		columnTypes.resize(columnCount);
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		columnTypeCodes.resize(columnCount);
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		columnNames.resize(columnCount);
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		for (int i = 0; i < attributes.size(); i++) {
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			columnNames[i] = attributes[i].getAttributeName();
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
			columnTypes[i] = attributes[i].getTypeId();
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
			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
    81
			xmlWriter->writeEmptyElement(L"attribute-metadata",{
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    82
				L"name", columnNames[i],
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    83
				L"type", columnTypeCodes[i]
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    84
			});
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
		}
14
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    86
		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
    87
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    88
		// TODO: print attribute metadata
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	}
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
	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
    92
		integer_t i = valueCount % columnCount;
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    93
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
    94
		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
    95
		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
    96
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    97
		valueCount++;
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    98
14
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    99
		// 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
   100
		xmlWriter->writeTextElement(L"attribute",{
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   101
			L"name", columnNames[i],
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   102
			L"type", columnTypeCodes[i]
5094a7bf343d add attribute metadata: names and types
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   103
		}, value);
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   104
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
	}
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
	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
   108
		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
   109
		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
   110
		xmlWriter->writeEndElement();
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
	}
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
}