Subversion Repositories gelsvn

Rev

Rev 375 | Rev 382 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
375 jrf 1
#ifndef XMLPARSER_H
2
#define XMLPARSER_H
3
 
4
#include <iostream>
380 jrf 5
#include <fstream>
375 jrf 6
#include <string>
7
#include <list>
8
#include <map>
9
 
10
namespace Util
11
{
12
  struct XmlHead
13
  {
14
    XmlHead() : is_xml(false) { }
15
 
16
    bool is_xml;
17
    std::map<std::string, std::string> atts;
18
  };
19
 
20
  struct XmlBody;
21
  class XmlDoc;
22
 
23
  struct XmlElement
24
  {
25
    XmlElement(XmlDoc* in_doc, XmlElement* parent_elem) : body(0), doc(in_doc), parent(parent_elem) { }
26
    XmlElement(XmlDoc* in_doc) : body(0), doc(in_doc), parent(0) { }
27
    ~XmlElement();
28
 
29
    void process_elements();
30
 
31
    std::string name;
32
    std::map<std::string, std::string> atts;
33
    XmlBody* body;
34
    XmlDoc* doc;
35
    XmlElement* parent;
36
  };
37
 
38
  struct XmlBody
39
  {
380 jrf 40
    XmlBody(XmlDoc* parent) : doc(parent), element(parent) { }
375 jrf 41
 
380 jrf 42
    void process_element();
43
 
44
    std::string text;
45
    XmlElement element;
375 jrf 46
    XmlDoc* doc;
47
  };
48
 
49
  typedef void (*XmlElementHandler)(XmlElement&);
50
 
51
  class XmlDoc
52
  {
53
  public:
54
    XmlDoc(const char* filename);
55
 
56
    bool is_valid() const { return head.is_xml; }
57
    void add_handler(const std::string& element_name, const XmlElementHandler& h) { handlers[element_name] = h; }
58
    void process_elements();
380 jrf 59
    void close() { infile.close(); }
375 jrf 60
 
380 jrf 61
    XmlHead head;
62
 
375 jrf 63
  private:
64
    friend XmlElement;
380 jrf 65
    friend XmlBody;
375 jrf 66
 
380 jrf 67
    std::ifstream infile;
375 jrf 68
    XmlBody body;
69
    std::map<std::string, XmlElementHandler> handlers;
70
  };
71
 
72
  std::ostream& operator<<(std::ostream& out, const XmlHead& head);
73
}
74
 
75
#endif // XMLPARSER_H