FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
xml_scanner.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
10#include <kernel/util/string.hpp>
11
12// includes, system
13#include <memory>
14#include <iostream>
15#include <fstream>
16#include <deque>
17#include <map>
18
19namespace FEAT
20{
24 namespace Xml
25 {
34 class Error :
35 public FEAT::Exception
36 {
37 private:
46
47 public:
48 Error(int iline, const String& sline, const String& msg, const String& err_type) :
49 FEAT::Exception("XML Error"),
50 my_iline(iline),
51 my_sline(sline),
52 my_msg(msg),
53 my_what(err_type + ": " + msg + " in line " + stringify(iline) + ": '" + sline + "'")
54 {
55 }
56
58 int get_line_number() const
59 {
60 return my_iline;
61 }
62
64 const String& get_line_string() const
65 {
66 return my_sline;
67 }
68
70 const String& get_message() const
71 {
72 return my_msg;
73 }
74
76 virtual const char * what() const noexcept override
77 {
78 return my_what.c_str();
79 }
80 }; // class Error
81
93 : public Error
94 {
95 public:
96 SyntaxError(int iline, const String& sline, const String& msg) :
97 Error(iline, sline, msg, "XML Syntax Error")
98 {
99 }
100 }; // class SyntaxError
101
113 : public Error
114 {
115 public:
116 GrammarError(int iline, const String& sline, const String& msg) :
117 Error(iline, sline, msg, "XML Grammar Error")
118 {
119 }
120 }; // class GrammarError
121
132 : public Error
133 {
134 public:
135 ContentError(int iline, const String& sline, const String& msg) :
136 Error(iline, sline, msg, "XML Content Error")
137 {
138 }
139 }; // class ContentError
140
149 {
150 public:
152 virtual ~MarkupParser() {}
153
164 virtual bool attribs(std::map<String, bool>& attrs) const = 0;
165
184 virtual void create(
185 int iline,
186 const String& sline,
187 const String& name,
188 const std::map<String, String>& attrs,
189 bool closed) = 0;
190
200 virtual void close(int iline, const String& sline) = 0;
201
224 virtual std::shared_ptr<MarkupParser> markup(int iline, const String& sline, const String& name) = 0;
225
244 virtual bool content(int iline, const String& sline) = 0;
245 }; // class MarkupParser
246
259 public MarkupParser
260 {
261 public:
262 explicit DummyParser();
263 virtual bool attribs(std::map<String, bool>&) const override;
264 virtual void create(int, const String&, const String&, const std::map<String, String>&, bool) override;
265 virtual void close(int, const String&) override;
266 virtual std::shared_ptr<MarkupParser> markup(int, const String&, const String&) override;
267 virtual bool content(int, const String&) override;
268 }; // class DummyParser
269
280 public MarkupParser
281 {
282 protected:
286 bool _lines;
288 std::size_t _indent;
289
290 public:
291 explicit DumpParser(const String& name, bool lines = true, std::size_t indent = 0);
292 virtual bool attribs(std::map<String, bool>&) const override;
293 virtual void create(int iline, const String&, const String&, const std::map<String, String>& attrs, bool closed) override;
294 virtual void close(int iline, const String&) override;
295 virtual std::shared_ptr<MarkupParser> markup(int, const String&, const String& name) override;
296 virtual bool content(int iline, const String& sline) override;
297 }; // class DumpParser
298
307 {
308 private:
315 {
316 private:
317 int my_line;
318 String my_name;
319 std::shared_ptr<MarkupParser> my_parser;
320
321 public:
322 explicit MarkupInfo(int _line, const String& _name, std::shared_ptr<MarkupParser> _parser) :
323 my_line(_line), my_name(_name), my_parser(_parser)
324 {
325 }
326
327 int line() const
328 {
329 return my_line;
330 }
331
332 const String& name() const
333 {
334 return my_name;
335 }
336
337 std::shared_ptr<MarkupParser> parser()
338 {
339 return my_parser;
340 }
341 }; // class MarkupInfo
342
343 private:
345 std::istream& _stream;
347 std::vector<MarkupInfo> _markups;
363 std::map<String,String> _cur_attribs;
364
365 public:
375 explicit Scanner(std::istream& stream, int lines_read = 0) :
376 _stream(stream),
377 _markups(),
378 _have_read_root(false),
379 _cur_iline(lines_read),
380 _cur_sline(),
381 _cur_is_markup(false),
382 _cur_is_closed(false),
383 _cur_is_termin(false),
384 _cur_name(),
386 {
387 _markups.reserve(32);
388 _cur_sline.reserve(1024);
389 }
390
392 virtual ~Scanner()
393 {
394 }
395
397 int get_cur_iline() const
398 {
399 return _cur_iline;
400 }
401
403 const String& get_cur_sline() const
404 {
405 return _cur_sline;
406 }
407
409 bool is_cur_markup() const
410 {
411 return _cur_is_markup;
412 }
413
415 bool is_cur_closed() const
416 {
417 return _cur_is_closed;
418 }
419
421 bool is_cur_termin() const
422 {
423 return _cur_is_termin;
424 }
425
427 const String& get_cur_name() const
428 {
429 return _cur_name;
430 }
431
433 const std::map<String, String>& get_cur_attribs() const
434 {
435 return _cur_attribs;
436 }
437
448 void read_root();
449
462 void set_root_parser(std::shared_ptr<MarkupParser> parser);
463
475 void scan();
476
485 void scan(std::shared_ptr<MarkupParser> root_parser);
486
493 void throw_syntax(const String& msg) const
494 {
495 throw SyntaxError(_cur_iline, _cur_sline, msg);
496 }
497
504 void throw_grammar(const String& msg) const
505 {
507 }
508
515 void throw_content(const String& msg) const
516 {
518 }
519
520 protected:
528 bool read_next_line();
529
544 bool scan_markup();
545
555 void process_markup();
556
563 void create_top_parser();
564
570 void process_content();
571 }; // class Scanner
572 } // namespace Xml
573} // namespace FEAT
FEAT Kernel base header.
Base exception class.
Definition: exception.hpp:27
String class implementation.
Definition: string.hpp:46
Xml content error class.
Dummy XML markup parser class.
virtual bool attribs(std::map< String, bool > &) const override
Specifies the mandatory and optional attributes.
Definition: xml_scanner.cpp:18
virtual bool content(int, const String &) override
Called to process a content line.
Definition: xml_scanner.cpp:37
virtual void create(int, const String &, const String &, const std::map< String, String > &, bool) override
Creates this markup parser node.
Definition: xml_scanner.cpp:23
virtual void close(int, const String &) override
Closes this markup parser node.
Definition: xml_scanner.cpp:27
virtual std::shared_ptr< MarkupParser > markup(int, const String &, const String &) override
Called to process a child markup node.
Definition: xml_scanner.cpp:31
Dump XML markup parser class.
virtual std::shared_ptr< MarkupParser > markup(int, const String &, const String &name) override
Called to process a child markup node.
Definition: xml_scanner.cpp:75
virtual void create(int iline, const String &, const String &, const std::map< String, String > &attrs, bool closed) override
Creates this markup parser node.
Definition: xml_scanner.cpp:56
bool _lines
specifies whether to write line numbers
std::size_t _indent
indentation for this parser node
String _name
the name of this markup parser node
virtual bool attribs(std::map< String, bool > &) const override
Specifies the mandatory and optional attributes.
Definition: xml_scanner.cpp:51
virtual bool content(int iline, const String &sline) override
Called to process a content line.
Definition: xml_scanner.cpp:80
virtual void close(int iline, const String &) override
Closes this markup parser node.
Definition: xml_scanner.cpp:68
Xml Error base class.
Definition: xml_scanner.hpp:36
int get_line_number() const
Definition: xml_scanner.hpp:58
const String & get_message() const
Definition: xml_scanner.hpp:70
String my_msg
error message
Definition: xml_scanner.hpp:43
String my_what
what string
Definition: xml_scanner.hpp:45
int my_iline
erroneous line number
Definition: xml_scanner.hpp:39
const String & get_line_string() const
Definition: xml_scanner.hpp:64
String my_sline
erroneous line string
Definition: xml_scanner.hpp:41
virtual const char * what() const noexcept override
Definition: xml_scanner.hpp:76
Xml grammar error class.
XML Markup Parser interface.
virtual ~MarkupParser()
virtual destructor
virtual bool content(int iline, const String &sline)=0
Called to process a content line.
virtual void close(int iline, const String &sline)=0
Closes this markup parser node.
virtual std::shared_ptr< MarkupParser > markup(int iline, const String &sline, const String &name)=0
Called to process a child markup node.
virtual void create(int iline, const String &sline, const String &name, const std::map< String, String > &attrs, bool closed)=0
Creates this markup parser node.
virtual bool attribs(std::map< String, bool > &attrs) const =0
Specifies the mandatory and optional attributes.
XML Scanner class.
void set_root_parser(std::shared_ptr< MarkupParser > parser)
Sets the root markup parser node.
String _cur_sline
current line string
bool read_next_line()
Reads the next non-empty line from the stream.
bool _cur_is_termin
specifies whether the markup is a terminator
bool is_cur_markup() const
bool scan_markup()
Tries to interpret the current line as a XML markup line.
int _cur_iline
current line number
std::map< String, String > _cur_attribs
specifies the markup attributes of the current line
void read_root()
Tries to read the XML root markup.
Definition: xml_scanner.cpp:92
void throw_content(const String &msg) const
Throws a ContentError for the current line.
bool _have_read_root
specifies whether the root has been read
void throw_grammar(const String &msg) const
Throws a GrammarError for the current line.
bool is_cur_termin() const
String _cur_name
specifies the markup name of the current line
void create_top_parser()
Creates the top parser node.
void process_markup()
Tries to process the current XML markup line.
void throw_syntax(const String &msg) const
Throws a SyntaxError for the current line.
bool _cur_is_markup
specifies whether this line is a markup
std::vector< MarkupInfo > _markups
the markup stack
const String & get_cur_sline() const
bool is_cur_closed() const
const std::map< String, String > & get_cur_attribs() const
void process_content()
Tries to process the current XML content line.
const String & get_cur_name() const
Scanner(std::istream &stream, int lines_read=0)
Creates a XML scanner for an input stream.
bool _cur_is_closed
specifies whether the markup is closed
void scan()
Scans the stream.
virtual ~Scanner()
virtual destructor
std::istream & _stream
the input stream
int get_cur_iline() const
Xml syntax error class.
Definition: xml_scanner.hpp:94
FEAT namespace.
Definition: adjactor.hpp:12
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:944