Subversion Repositories gelsvn

Rev

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

Rev Author Line No. Line
595 jab 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
 */
448 jab 11
#ifndef __UTIL_XMLPARSER_H
12
#define __UTIL_XMLPARSER_H
375 jrf 13
 
14
#include <iostream>
380 jrf 15
#include <fstream>
375 jrf 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
  {
380 jrf 50
    XmlBody(XmlDoc* parent) : doc(parent), element(parent) { }
375 jrf 51
 
380 jrf 52
    void process_element();
53
 
54
    std::string text;
55
    XmlElement element;
375 jrf 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();
380 jrf 69
    void close() { infile.close(); }
375 jrf 70
 
380 jrf 71
    XmlHead head;
72
 
375 jrf 73
  private:
382 jab 74
    friend struct XmlElement;
75
    friend struct XmlBody;
375 jrf 76
 
380 jrf 77
    std::ifstream infile;
375 jrf 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