Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
667 khor 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
6
 
7
/**
8
 * @file XmlParser.h
9
 * @brief Simple XML parser.
10
 */
11
#ifndef __UTIL_XMLPARSER_H
12
#define __UTIL_XMLPARSER_H
13
 
14
#include <iostream>
15
#include <fstream>
16
#include <string>
17
#include <list>
18
#include <map>
19
 
20
namespace Util
21
{
22
  struct XmlHead
23
  {
24
    XmlHead() : is_xml(false) { }
25
 
26
    bool is_xml;
27
    std::map<std::string, std::string> atts;
28
  };
29
 
30
  struct XmlBody;
31
  class XmlDoc;
32
 
33
  struct XmlElement
34
  {
35
    XmlElement(XmlDoc* in_doc, XmlElement* parent_elem) : body(0), doc(in_doc), parent(parent_elem) { }
36
    XmlElement(XmlDoc* in_doc) : body(0), doc(in_doc), parent(0) { }
37
    ~XmlElement();
38
 
39
    void process_elements();
40
 
41
    std::string name;
42
    std::map<std::string, std::string> atts;
43
    XmlBody* body;
44
    XmlDoc* doc;
45
    XmlElement* parent;
46
  };
47
 
48
  struct XmlBody
49
  {
50
    XmlBody(XmlDoc* parent) : element(parent), doc(parent) { }
51
 
52
    void process_element();
53
 
54
    std::string text;
55
    XmlElement element;
56
    XmlDoc* doc;
57
  };
58
 
59
  typedef void (*XmlElementHandler)(XmlElement&);
60
 
61
  class XmlDoc
62
  {
63
  public:
64
    XmlDoc(const char* filename);
65
 
66
    bool is_valid() const { return head.is_xml; }
67
    void add_handler(const std::string& element_name, const XmlElementHandler& h) { handlers[element_name] = h; }
68
    void process_elements();
69
    void close() { infile.close(); }
70
 
71
    XmlHead head;
72
 
73
  private:
74
    friend struct XmlElement;
75
    friend struct XmlBody;
76
 
77
    std::ifstream infile;
78
    XmlBody body;
79
    std::map<std::string, XmlElementHandler> handlers;
80
  };
81
 
82
  std::ostream& operator<<(std::ostream& out, const XmlHead& head);
83
}
84
 
85
#endif // XMLPARSER_H