Subversion Repositories gelsvn

Rev

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

Rev Author Line No. Line
375 jrf 1
#include <fstream>
2
#include <string>
3
#include <sstream>
4
#include <cstring>
5
#include <algorithm>
6
#include <list>
7
#include "XmlParser.h"
8
#include "string_utils.h"
9
 
10
using namespace std;
11
 
12
namespace Util
13
{
14
  /////////////////////////////////////////////////////////////////
15
  // String handling
16
  /////////////////////////////////////////////////////////////////
17
 
18
  void parse_attribs(const string& s, map<string, string>& result)
19
  {
20
    string name1, name2;
21
    list<string> atts;
22
    trim_split(s, atts, "=");
23
    if(atts.empty())
24
      return;
25
 
26
    get_last(atts.front(), name1);
27
    list<string>::iterator i = atts.begin();
28
    list<string>::iterator end = atts.end();
29
    if(i == --end)
30
      return;
31
 
32
    for(++i; i != end; ++i)
33
    { 
34
      get_last(*i, name2);
35
      result[trim(name1)] = trim(*i, " \"");
36
      name1 = name2;
37
    }
38
    result[trim(name1)] = trim(*end, " \"");
39
  }
40
 
41
  /////////////////////////////////////////////////////////////////
42
  // File handling
43
  /////////////////////////////////////////////////////////////////
44
 
45
  ifstream& seek_string(ifstream& in, const string& s, const size_t bufsize = 100)
46
  {
47
    const int bsize = static_cast<int>(max(s.size(), bufsize));
48
    const int n = static_cast<int>(s.size());
49
    char* buf = new char[bsize + 1];
50
    char s0 = s[0];
51
 
52
    in.get(buf, bsize, s0);
53
    in.clear();
54
    in.read(buf, n);
55
    buf[n] = '\0'; 
56
    while(in && strcmp(s.c_str(), buf) != 0)
57
    {
58
      in.get(buf, bsize, s0);
59
      in.clear();
60
      in.read(buf, n);
61
      buf[n] = '\0'; 
62
    }
63
 
64
    delete [] buf;
65
    return in;
66
  }
67
 
376 jrf 68
  ifstream& read_until(ifstream& in, string& s_in, const string s, const size_t bufsize = 500)
375 jrf 69
  {
70
    const int bsize = static_cast<int>(max(s.size(), bufsize));
71
    const int n = static_cast<int>(s.size());
72
    char* buf = new char[bsize + 1];
73
    char s0 = s[0];
74
    ostringstream ostr;
75
 
76
    in.get(buf, bsize, s0);
77
    ostr << buf;
78
    in.clear();
79
    in.read(buf, n);
80
    buf[n] = '\0'; 
81
    while(in && strcmp(s.c_str(), buf) != 0)
82
    {
83
      ostr << buf;
84
      in.get(buf, bsize, s0);
85
      ostr << buf;
86
      in.clear();
87
      in.read(buf, n);
88
      buf[n] = '\0'; 
89
    }
90
    s_in = ostr.str();
91
 
92
    delete [] buf;
93
    return in;
94
  }
95
 
96
  ifstream& operator>>(ifstream& in, XmlHead& fhead)
97
  {
98
    seek_string(in, "<?xml");
99
 
100
    string head;
101
    read_until(in, head, "?>");
102
 
103
    fhead.is_xml = in.good();
104
    parse_attribs(head, fhead.atts);
105
 
106
    return in;
107
  }
108
 
109
  ifstream& operator>>(ifstream& in, XmlElement& elem)
110
  {
111
    seek_string(in, "<");
112
 
113
    string head;
114
    read_until(in, head, ">");
115
 
116
    if(head[0] == '!') return in;
380 jrf 117
    if(head[0] == '/')
118
    {
119
      if(elem.parent && elem.parent->name == head.substr(1))
120
        in.setstate(ios_base::eofbit);
121
      else
122
        elem.name = "";
123
      return in;
124
    }
375 jrf 125
 
126
    bool has_body = true;
127
    if(head[head.size() - 1] == '/')
128
    {
129
      has_body = false;
130
      head.erase(head.size() - 1);
131
    }
132
 
133
    get_first(head, elem.name);
134
    parse_attribs(head, elem.atts);
135
 
136
    if(has_body)
137
    {
138
      delete elem.body;
139
      elem.body = new XmlBody(elem.doc);
380 jrf 140
      elem.body->element.parent = &elem;
141
      read_until(in, elem.body->text, "<");
142
      in.putback('<');
375 jrf 143
    }
144
    return in;
145
  }
146
 
147
  ifstream& operator>>(ifstream& in, XmlBody& body)
148
  {
380 jrf 149
    read_until(in, body.text, "<");
150
    in.putback('<');
375 jrf 151
    return in;
152
  }
153
 
154
  /////////////////////////////////////////////////////////////////
155
  // Methods
156
  /////////////////////////////////////////////////////////////////
157
 
158
  XmlElement::~XmlElement()
159
  {
160
    delete body;
161
  }
162
 
380 jrf 163
  void XmlBody::process_element()
375 jrf 164
  {
165
    if(!doc) return;
380 jrf 166
    if((doc->infile >> element) && !doc->infile.eof())
167
    {
168
      XmlElementHandler h = doc->handlers[element.name];
169
      if(h) 
170
        h(element);
171
      else 
172
        element.process_elements();
173
    }
375 jrf 174
  }
175
 
176
  void XmlElement::process_elements()
177
  {
178
    if(!doc) return;
380 jrf 179
    if(!body) return; 
375 jrf 180
 
380 jrf 181
    while((doc->infile >> body->element) && !doc->infile.eof())
182
    {
183
      XmlElementHandler h = doc->handlers[body->element.name];
184
      if(h) 
185
        h(body->element);
186
      else 
187
        body->element.process_elements();
188
    }
189
    doc->infile.clear();
375 jrf 190
  }
191
 
192
  XmlDoc::XmlDoc(const char *filename)
380 jrf 193
    : body(0), infile(filename)
375 jrf 194
  {
380 jrf 195
    if(!infile)
196
    {
197
       cerr << "cannot open input file" << filename << endl;
198
       return;
199
    }
200
 
201
    if(infile >> head)
202
      infile >> body;
203
    else
204
      cerr << filename << " is not a valid xml-file" << endl;
205
 
375 jrf 206
    body.doc = this;
380 jrf 207
    body.element.doc = this;
375 jrf 208
  }
209
 
210
  void XmlDoc::process_elements()
211
  {
380 jrf 212
    while((infile >> body.element) && !infile.eof())
213
    {
214
      XmlElementHandler h = handlers[body.element.name];
215
      if(h) 
216
        h(body.element);
217
      else 
218
        body.element.process_elements();
219
    }
220
    infile.clear();
375 jrf 221
  }
222
 
223
  /////////////////////////////////////////////////////////////////
224
  // Output handling
225
  /////////////////////////////////////////////////////////////////
226
 
227
  ostream& operator<<(ostream& out, const pair<string, string>& attrib)
228
  {
229
    return out << attrib.first << "=\"" << attrib.second << "\"";
230
  }
231
 
232
  ostream& operator<<(ostream& out, const XmlHead& head)
233
  {
234
    out << "<?xml";
235
    for(map<string, string>::const_iterator i = head.atts.begin(); i != head.atts.end(); ++i)
236
      out << " " << *i;
237
    out << "?>";
238
    return out;
239
  }
240
}