Subversion Repositories gelsvn

Rev

Rev 448 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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

Generated by GNU Enscript 1.6.6.
87

Generated by GNU Enscript 1.6.6.
78
 
88
 
79
 
89
 
80
 
90