[go: up one dir, main page]

Menu

[e79954]: / media / XML_Parser.h  Maximize  Restore  History

Download this file

235 lines (194 with data), 6.0 kB

  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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// XML_Parser.h - an XML file reader
//
// Copyright (C) 2004 Sam Varner
//
// This file is part of Vamos Automotive Simulator.
//
// Vamos 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.
//
// Vamos 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 Vamos. If not, see <http://www.gnu.org/licenses/>.
#ifndef _XML_PARSER_H_
#define _XML_PARSER_H_
#include <string>
#include <fstream>
#include <vector>
namespace Vamos_Media
{
//** Exception Classes
class XML_Exception
{
protected:
std::string m_file;
int m_line;
std::string m_message;
public:
XML_Exception (std::string file, int line = 0, std::string message = "") :
m_file (file),
m_line (line),
m_message (message)
{};
virtual ~XML_Exception () {};
virtual std::string message () const;
};
class No_XML_File : public XML_Exception
{
public:
No_XML_File (std::string file) : XML_Exception (file) {};
std::string message () const
{ return "Can't find the file \"" + m_file + '"'; }
};
class No_Declaration : public XML_Exception
{
public:
No_Declaration (std::string file, int line, std::string message) :
XML_Exception (file, line, message) {};
};
class Bad_Tag_Type : public XML_Exception
{
public:
Bad_Tag_Type (std::string file, int line, std::string message) :
XML_Exception (file, line, message) {};
};
class Tag_Mismatch : public XML_Exception
{
public:
Tag_Mismatch (std::string file, int line, std::string message) :
XML_Exception (file, line, message) {}
};
struct XML_Unterminated
{
int lines;
std::string text;
bool eof;
char delimiter;
XML_Unterminated (int lines_in,
std::string text_in,
bool eof_in,
char delimiter_in) :
lines (lines_in),
text (text_in),
eof (eof_in),
delimiter (delimiter_in)
{};
};
struct Unterminated_Tag : public XML_Unterminated
{
Unterminated_Tag (int lines_in, std::string text_in, bool eof_in)
: XML_Unterminated (lines_in, text_in, eof_in, '>') {};
};
struct Unterminated_Attribute : public XML_Unterminated
{
Unterminated_Attribute (int lines_in, std::string text_in, bool eof_in)
: XML_Unterminated (lines_in, text_in, eof_in, '"') {};
};
//** Class XML_Tag
class XML_Tag
{
public:
enum Tag_Type
{
NONE,
START,
END,
EMPTY,
PROCESSING_INSTRUCTION,
COMMENT
};
struct Attribute
{
Attribute (std::string name_in, std::string value_in)
: name (name_in), value (value_in) {}
std::string name;
std::string value;
};
typedef std::vector <Attribute> Attribute_List;
private:
typedef std::string::iterator String_Iterator;
Tag_Type m_type;
int m_lines;
std::vector <Attribute> m_attributes;
std::string m_data;
std::string m_text;
std::string m_label;
// Read everything up to the next '<'. Return true if '<' was found.
bool read_to_tag_start (std::ifstream& stream);
// Read everything up to the next `>'. Return true if '>' was found.
bool read_to_tag_end (std::ifstream& stream);
// Determine the type of the tag.
Tag_Type find_tag_type (std::ifstream& stream);
// Determine the tag's label.
std::string find_label (String_Iterator text_start,
String_Iterator text_end);
// Get the next character from the stream.
std::ifstream& get_next_char (std::ifstream& stream, char& ch);
// Parse attributes.
void find_attributes (String_Iterator attr_begin,
String_Iterator attr_end);
// Throw out characters inside a comment.
void eat_comment (std::ifstream& stream);
bool find_comment_end (std::ifstream& stream);
void skip_spaces (String_Iterator& text_start);
Attribute get_attribute (String_Iterator text_start,
String_Iterator text_end);
void get_text_boundries (String_Iterator& text_start,
String_Iterator& text_end);
public:
XML_Tag (std::ifstream& stream);
Tag_Type get_type () const { return m_type; }
int get_lines () const { return m_lines; }
const Attribute_List& get_attributes () const { return m_attributes; }
std::string get_data () const { return m_data; }
std::string get_text () const { return m_text; }
std::string get_label () const { return m_label; }
};
//** XML Tag Path Class
class XML_Path
{
std::string m_path;
public:
void push (std::string element) { m_path += '/' + element; }
void drop () { m_path = m_path.substr (0, m_path.find_last_of ("/")); }
bool empty () const { return m_path.empty (); }
bool match (std::string pattern) const;
std::string path () const { return m_path; }
std::string subpath (size_t n) const;
std::string top () const { return subpath (1); }
};
//** Parser Class
class XML_Parser
{
public:
XML_Parser ();
virtual ~XML_Parser ();
void read (std::string file);
void error (std::string message);
bool match (std::string pattern) const { return m_path.match (pattern); }
std::string path () const { return m_path.path (); }
std::string label () const { return m_path.top (); }
// Event handlers overridden by derived classes.
virtual void on_start_tag (const XML_Tag& tag) = 0;
virtual void on_end_tag (const XML_Tag& tag) = 0;
virtual void on_data (std::string data_string) = 0;
private:
std::string m_file;
std::ifstream* mp_stream;
int m_line;
XML_Path m_path;
void check_declaration ();
void read_document ();
bool run_callbacks (const XML_Tag& tag);
void add_tag (const XML_Tag& tag);
void remove_tag (const XML_Tag& tag);
void handle_unterminated (XML_Unterminated& unterminated);
};
}
#endif // not _XML_PARSER_H_