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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/*
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/>.
*/
/**
* @file OsxmlEventParser.hpp
*
* The OsxmlEventParser class is responsible for parsing an XML file and calling
* the corresponding event handler functions if an XML item is found. Event
* handling is performed using a listener interface.
*
* @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
*/
#ifndef _OSXML_EVENT_PARSER_HPP_
#define _OSXML_EVENT_PARSER_HPP_
#include <memory>
#include <string>
#include <core/common/Whitespace.hpp>
namespace ousia {
// Forward declarations
class Logger;
class Variant;
class OsxmlEventParserData;
/**
* Interface which defines the callback functions which are called by the
* OsxmlEventParser whenever an event occurs.
*/
class OsxmlEvents {
public:
/**
* Virtual destructor.
*/
virtual ~OsxmlEvents();
/**
* Called whenever a command starts. Note that this implicitly always starts
* the default field of the command.
*
* @param name is a string variant containing name and location of the
* command.
* @param args is a map containing the arguments that were given to the
* command.
*/
virtual void command(const Variant &name, const Variant::mapType &args) = 0;
/**
* Called whenever an annotation starts. Note that this implicitly always
* starts the default field of the annotation.
*
* @param className is a string variant containing the name of the
* annotation class and the location of the annotation definition.
* @param args is a map variant containing the arguments that were given
* to the annotation definition.
*/
virtual void annotationStart(const Variant &className,
const Variant::mapType &args) = 0;
/**
* Called whenever the range of an annotation ends. The callee must
* disambiguate the actual annotation that is finished here.
*
* @param className is a string variant containing the name of the
* annotation class that should end here. May be empty (or nullptr), if no
* elementName has been specified at the end of the annotation.
* @param elementName is the name of the annotation element that should be
* ended here. May be empty (or nullptr), if no elementName has been
* specified at the end of the annotation.
*/
virtual void annotationEnd(const Variant &className,
const Variant &elementName) = 0;
/**
* Called whenever the default field which was implicitly started by
* commandStart or annotationStart ends. Note that this does not end the
* range of an annotation, but the default field of the annotation. To
* signal the end of the annotation this, the annotationEnd method will be
* invoked.
*/
virtual void fieldEnd() = 0;
/**
* Called whenever data is found. Whitespace data is handled as specified
* and the data has been parsed to the specified variant type. This function
* is not called if the parsing failed, the parser prints an error message
* instead.
*
* @param data is the already parsed data that should be passed to the
* handler.
*/
virtual void data(const Variant &data) = 0;
};
/**
* The OsxmlEventParser class is a wrapper around eXpat which implements the
* specialities of the osxml formats class (like annotation ranges). It notifies
* a specified event handler whenever a command, annotation or data has been
* reached.
*/
class OsxmlEventParser {
private:
/**
* Reference at the internal CharReader instance.
*/
CharReader &reader;
/**
* Set of callback functions to be called whenever an event is triggered.
*/
OsxmlEvents &events;
/**
* Reference at the Logger object to which error messages or warnings should
* be logged.
*/
Logger &logger;
/**
* Current whitespace mode.
*/
WhitespaceMode whitespaceMode;
/**
* Data to be used by the internal functions.
*/
std::unique_ptr<OsxmlEventParserData> data;
public:
/**
* Constructor fo the OsxmlEventParser. Takes a reference at the OsxmlEvents
* of which the callback functions are called.
*
* @param reader is a reference to the CharReader instance from which the
* XML should be read.
* @param events is a refence at an instance of the OsxmlEvents class. All
* events are forwarded to this class.
* @param logger is the Logger instance to which log messages should be
* written.
*/
OsxmlEventParser(CharReader &reader, OsxmlEvents &events, Logger &logger);
/**
* Destructor of OsxmlEventParser (needed for unique_ptr to incomplete type)
*/
~OsxmlEventParser();
/**
* Performs the actual parsing. Reads the XML using eXpat and calles the
* callbacks in the event listener instance whenever something interesting
* happens.
*/
void parse();
/**
* Sets the whitespace handling mode.
*
* @param whitespaceMode defines how whitespace in the data should be
* handled.
*/
void setWhitespaceMode(WhitespaceMode whitespaceMode);
/**
* Returns the current whitespace handling mode.
*
* @return the currently set whitespace handling mode.
*/
WhitespaceMode getWhitespaceMode() const;
/**
* Returns the internal CharReader reference.
*
* @return the CharReader reference.
*/
CharReader &getReader() const;
/**
* Returns the internal Logger reference.
*
* @return the internal Logger reference.
*/
Logger &getLogger() const;
/**
* Returns the internal OsxmlEvents reference.
*
* @return the internal OsxmlEvents reference.
*/
OsxmlEvents &getEvents() const;
/**
* Returns a reference at the internal data.
*/
OsxmlEventParserData &getData() const;
};
}
#endif /* _OSXML_EVENT_PARSER_HPP_ */
|