Subversion Repositories gelsvn

Rev

Rev 380 | Go to most recent revision | Details | 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>
5
#include <string>
6
#include <list>
7
#include <map>
8
 
9
namespace Util
10
{
11
  struct XmlHead
12
  {
13
    XmlHead() : is_xml(false) { }
14
 
15
    bool is_xml;
16
    std::map<std::string, std::string> atts;
17
  };
18
 
19
  struct XmlBody;
20
  class XmlDoc;
21
 
22
  struct XmlElement
23
  {
24
    XmlElement(XmlDoc* in_doc, XmlElement* parent_elem) : body(0), doc(in_doc), parent(parent_elem) { }
25
    XmlElement(XmlDoc* in_doc) : body(0), doc(in_doc), parent(0) { }
26
    ~XmlElement();
27
 
28
    void process_element(XmlElement& elem);
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
  {
40
    XmlBody(XmlDoc* parent) : doc(parent) { }
41
 
42
    std::list<std::string> text;
43
    std::list<XmlElement> elements;
44
    XmlDoc* doc;
45
  };
46
 
47
  typedef void (*XmlElementHandler)(XmlElement&);
48
 
49
  class XmlDoc
50
  {
51
  public:
52
    XmlDoc(const char* filename);
53
 
54
    bool is_valid() const { return head.is_xml; }
55
    void add_handler(const std::string& element_name, const XmlElementHandler& h) { handlers[element_name] = h; }
56
    void process_elements();
57
    std::ostream& put(std::ostream& out) const;
58
 
59
  private:
60
    friend XmlElement;
61
 
62
    void load_xml(const char* filename);
63
 
64
    XmlHead head;
65
    XmlBody body;
66
    std::map<std::string, XmlElementHandler> handlers;
67
  };
68
 
69
  std::ostream& operator<<(std::ostream& out, const XmlHead& head);
70
  std::ostream& operator<<(std::ostream& out, const XmlElement& elem);
71
  std::ostream& operator<<(std::ostream& out, const XmlBody& body);
72
  inline std::ostream& operator<<(std::ostream& out, const XmlDoc& doc) { return doc.put(out); } 
73
}
74
 
75
#endif // XMLPARSER_H