author | František Kučera <franta-hg@frantovo.cz> |
Fri, 11 Jan 2019 12:04:45 +0100 | |
branch | v_0 |
changeset 4 | 1363ec0879ca |
parent 3 | 4b566dee1a57 |
child 5 | e5cf88ce91ac |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
#pragma once |
|
19 |
||
20 |
#include <cstdlib> |
|
21 |
#include <iostream> |
|
22 |
#include <string> |
|
23 |
#include <vector> |
|
24 |
#include <algorithm> |
|
25 |
||
1
9aed8c2ea97d
add Xerces-C++ dependency
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
26 |
#include <xercesc/sax2/SAX2XMLReader.hpp> |
9aed8c2ea97d
add Xerces-C++ dependency
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
27 |
#include <xercesc/sax2/XMLReaderFactory.hpp> |
9aed8c2ea97d
add Xerces-C++ dependency
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
28 |
#include <xercesc/sax2/DefaultHandler.hpp> |
9aed8c2ea97d
add Xerces-C++ dependency
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
29 |
#include <xercesc/util/XMLString.hpp> |
9aed8c2ea97d
add Xerces-C++ dependency
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
30 |
|
0 | 31 |
#include <relpipe/writer/typedefs.h> |
32 |
||
2
3ab78bf63467
read XML from istream using Xerces (just parse/validate, no processing yet)
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
33 |
#include "StreamInputSource.h" |
3ab78bf63467
read XML from istream using Xerces (just parse/validate, no processing yet)
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
34 |
|
0 | 35 |
namespace relpipe { |
36 |
namespace in { |
|
37 |
namespace xml { |
|
38 |
||
3
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
39 |
using namespace relpipe::writer; |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
40 |
using namespace xercesc; |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
41 |
|
0 | 42 |
class XMLCommand { |
3
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
43 |
private: |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
44 |
|
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
45 |
class RelpipeSaxHandler : public xercesc::DefaultHandler { |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
46 |
private: |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
47 |
unique_ptr<RelationalWriter> writer; |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
48 |
|
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
49 |
public: |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
50 |
|
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
51 |
RelpipeSaxHandler(std::ostream& output) : DefaultHandler(), writer(Factory::create(output)) { |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
52 |
} |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
53 |
|
4
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
54 |
void startDocument() override { |
3
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
55 |
//XMLString:: |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
56 |
// TODO: remove demo |
4
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
57 |
writer->startRelation(L"xml",{ |
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
58 |
{L"event", TypeId::STRING}, |
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
59 |
{L"data", TypeId::STRING}, |
3
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
60 |
}, true); |
4
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
61 |
} |
3
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
62 |
|
4
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
63 |
void startElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname, const Attributes& attrs) override { |
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
64 |
writer->writeAttribute(L"startElement"); |
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
65 |
writer->writeAttribute(L""); |
3
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
66 |
} |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
67 |
|
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
68 |
void endElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname) override { |
4
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
69 |
writer->writeAttribute(L"endElement"); |
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
70 |
writer->writeAttribute(L""); |
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
71 |
} |
3
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
72 |
|
4
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
73 |
void characters(const XMLCh * const chars, const XMLSize_t length) override { |
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
74 |
writer->writeAttribute(L"characters"); |
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
75 |
writer->writeAttribute(to_wstring(length)); |
3
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
76 |
} |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
77 |
|
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
78 |
void endDocument() override { |
4
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
79 |
writer->writeAttribute(L"endDocument"); |
1363ec0879ca
report SAX events in a single relation
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
80 |
writer->writeAttribute(L""); |
3
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
81 |
} |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
82 |
|
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
83 |
}; |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
84 |
|
0 | 85 |
public: |
86 |
||
87 |
void process(std::istream& input, std::ostream& output) { |
|
2
3ab78bf63467
read XML from istream using Xerces (just parse/validate, no processing yet)
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
88 |
XMLPlatformUtils::Initialize(); |
3ab78bf63467
read XML from istream using Xerces (just parse/validate, no processing yet)
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
89 |
unique_ptr<SAX2XMLReader> parser(XMLReaderFactory::createXMLReader()); |
3ab78bf63467
read XML from istream using Xerces (just parse/validate, no processing yet)
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
90 |
parser->setFeature(XMLUni::fgSAX2CoreValidation, true); |
3ab78bf63467
read XML from istream using Xerces (just parse/validate, no processing yet)
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
91 |
parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true); |
3ab78bf63467
read XML from istream using Xerces (just parse/validate, no processing yet)
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
92 |
|
3
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
93 |
RelpipeSaxHandler saxHandler(output); |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
94 |
parser->setContentHandler(&saxHandler); |
4b566dee1a57
generate random relation on SAX events
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
95 |
parser->setErrorHandler(&saxHandler); |
2
3ab78bf63467
read XML from istream using Xerces (just parse/validate, no processing yet)
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
96 |
|
3ab78bf63467
read XML from istream using Xerces (just parse/validate, no processing yet)
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
97 |
StreamInputSource inputSource(input); |
3ab78bf63467
read XML from istream using Xerces (just parse/validate, no processing yet)
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
98 |
|
3ab78bf63467
read XML from istream using Xerces (just parse/validate, no processing yet)
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
99 |
parser->parse(inputSource); |
0 | 100 |
} |
101 |
}; |
|
102 |
||
103 |
} |
|
104 |
} |
|
105 |
} |