| 1 |
#ifndef CRASH_XML |
|---|
| 2 |
#define CRASH_XML |
|---|
| 3 |
|
|---|
| 4 |
#include <algorithm> |
|---|
| 5 |
#include <cstring> |
|---|
| 6 |
#include <stdexcept> |
|---|
| 7 |
#include <vector> |
|---|
| 8 |
#include <map> |
|---|
| 9 |
#include <string> |
|---|
| 10 |
#include "Strings.h" |
|---|
| 11 |
#include "Lexer.h" |
|---|
| 12 |
|
|---|
| 13 |
using namespace std; |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
class XML { |
|---|
| 17 |
public : |
|---|
| 18 |
enum Type { |
|---|
| 19 |
Element = 256, |
|---|
| 20 |
Body, |
|---|
| 21 |
Data |
|---|
| 22 |
}; |
|---|
| 23 |
|
|---|
| 24 |
#ifdef CRASH_SIGNAL |
|---|
| 25 |
static Signal2<string const &, map<string, string> const &> onElementBegin; |
|---|
| 26 |
static Signal1<string const &> onElementEnd; |
|---|
| 27 |
static Signal2<XML const &, string const &> onBody; |
|---|
| 28 |
static Signal2<XML const &, string const &> onData; |
|---|
| 29 |
#endif |
|---|
| 30 |
|
|---|
| 31 |
class exception : public runtime_error { |
|---|
| 32 |
public : |
|---|
| 33 |
exception(string const &what, int line) : |
|---|
| 34 |
runtime_error(what.c_str()), _line(line) {} |
|---|
| 35 |
int line() const { return _line; } |
|---|
| 36 |
private : |
|---|
| 37 |
int _line; |
|---|
| 38 |
}; |
|---|
| 39 |
|
|---|
| 40 |
XML(); |
|---|
| 41 |
XML(char const *input); |
|---|
| 42 |
virtual ~XML(); |
|---|
| 43 |
|
|---|
| 44 |
void parse(char const *input); |
|---|
| 45 |
|
|---|
| 46 |
Type type() const { return _type; } |
|---|
| 47 |
|
|---|
| 48 |
string const &name() const { return _data; } |
|---|
| 49 |
string const &body() const { return _data; } |
|---|
| 50 |
string const &data() const { return _data; } |
|---|
| 51 |
|
|---|
| 52 |
vector<XML*> const &child() const { return _child; } |
|---|
| 53 |
map<string, string> const &attrib() const { return _attrib; } |
|---|
| 54 |
protected : |
|---|
| 55 |
XML(Type type, XML *parent, Lexer::iterator &token); |
|---|
| 56 |
|
|---|
| 57 |
void init(); |
|---|
| 58 |
|
|---|
| 59 |
void skip(Lexer::iterator &token); |
|---|
| 60 |
void next(Lexer::iterator &token); |
|---|
| 61 |
|
|---|
| 62 |
void parseElement(Lexer::iterator &token); |
|---|
| 63 |
void parseBody(Lexer::iterator &token); |
|---|
| 64 |
void parseData(Lexer::iterator &token); |
|---|
| 65 |
|
|---|
| 66 |
// Lexer constants |
|---|
| 67 |
enum { XmlDecl = 256, XmlCommentBegin, XmlBegin, XmlEnd, XmlDataBegin, XmlContent }; |
|---|
| 68 |
enum { ElementWS = 256, ElementValue, ElementKey, ElementAssignment, ElementTerminator}; |
|---|
| 69 |
enum { CommentEnd = 256, CommentBody }; |
|---|
| 70 |
enum { DataEnd = 256, DataBody }; |
|---|
| 71 |
enum { ProcessBegin = 256, ProcessBody, ProcessEnd }; |
|---|
| 72 |
|
|---|
| 73 |
static bool initialised; |
|---|
| 74 |
static Lexer xmlScan, tagScan, commentScan, dataScan, processScan; |
|---|
| 75 |
|
|---|
| 76 |
XML *_parent; |
|---|
| 77 |
Type _type; |
|---|
| 78 |
string _data; |
|---|
| 79 |
|
|---|
| 80 |
map<string, string> _attrib; |
|---|
| 81 |
vector<XML*> _child; |
|---|
| 82 |
}; |
|---|
| 83 |
|
|---|
| 84 |
#endif |
|---|