1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/*
Ousía
Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <expat.h>
#include "XmlParser.hpp"
namespace ousia {
namespace parser {
namespace xml {
/**
* Wrapper class around the XML_Parser pointer which safely frees it whenever
* the scope is left (e.g. because an exception was thrown).
*/
class ScopedExpatXmlParser {
private:
/**
* Internal pointer to the XML_Parser instance.
*/
XML_Parser parser;
public:
/**
* Constructor of the ScopedExpatXmlParser class. Calls XML_ParserCreateNS
* from the expat library. Throws a parser exception if the XML parser
* cannot be initialized.
*
* @param encoding is the protocol-defined encoding passed to expat (or
* nullptr if expat should determine the encoding by itself).
*/
ScopedExpatXmlParser(const XML_Char *encoding)
: parser(nullptr)
{
parser = XML_ParserCreate(encoding);
if (!parser) {
throw ParserException{
"Internal error: Could not create expat XML parser!"};
}
}
/**
* Destuctor of the ScopedExpatXmlParser, frees the XML parser instance.
*/
~ScopedExpatXmlParser()
{
if (parser) {
XML_ParserFree(parser);
parser = nullptr;
}
}
/**
* Returns the XML_Parser pointer.
*/
XML_Parser operator&() { return parser; }
};
static void xmlStartElementHandler(void *userData, const XML_Char *name,
const XML_Char **attrs)
{
std::cout << "start tag: " << name << std::endl;
const XML_Char **attr = attrs;
while (*attr) {
std::cout << "\t" << *attr;
attr++;
std::cout << " -> " << *attr << std::endl;
attr++;
}
}
static void xmlEndElementHandler(void *userData, const XML_Char *name) {
std::cout << "end tag: " << name << std::endl;
}
static void xmlCharacterDataHandler(void *userData, const XML_Char *s, int len) {
std::cout << "\tdata: " << std::string(s, len) << std::endl;
}
std::set<std::string> XmlParser::mimetypes()
{
return std::set<std::string>{{"text/vnd.ousia.oxm", "text/vnd.ousia.oxd"}};
}
Rooted<Node> XmlParser::parse(std::istream &is, ParserContext &ctx)
{
// Create the parser object
ScopedExpatXmlParser p{"UTF-8"};
XML_SetUserData(&p, &ctx);
// Set the callback functions
XML_SetStartElementHandler(&p, xmlStartElementHandler);
XML_SetEndElementHandler(&p, xmlEndElementHandler);
XML_SetCharacterDataHandler(&p, xmlCharacterDataHandler);
// Feed data into expat while there is data to process
const std::streamsize BUFFER_SIZE = 4096; // TODO: Move to own header?
while (true) {
// Fetch a buffer from expat for the input data
char *buf = static_cast<char *>(XML_GetBuffer(&p, BUFFER_SIZE));
if (!buf) {
throw ParserException{"Internal error: XML parser out of memory!"};
}
// Read the input data from the stream
const std::streamsize bytesRead = is.read(buf, BUFFER_SIZE).gcount();
// Parse the data and handle any XML error
if (!XML_ParseBuffer(&p, bytesRead, bytesRead == 0)) {
const int line = XML_GetCurrentLineNumber(&p);
const int column = XML_GetCurrentColumnNumber(&p);
const XML_Error code = XML_GetErrorCode(&p);
const std::string msg = std::string{XML_ErrorString(code)};
throw ParserException{"XML Syntax Error: " + msg, line, column,
false};
}
// Abort once there are no more bytes in the stream
if (bytesRead == 0) {
break;
}
}
return nullptr;
}
}
}
}
|