Subversion Repositories gelsvn

Rev

Rev 376 | Rev 380 | 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;
117
 
118
    bool has_body = true;
119
    if(head[head.size() - 1] == '/')
120
    {
121
      has_body = false;
122
      head.erase(head.size() - 1);
123
    }
124
 
125
    get_first(head, elem.name);
126
    parse_attribs(head, elem.atts);
127
 
128
    if(has_body)
129
    {
130
      delete elem.body;
131
      elem.body = new XmlBody(elem.doc);
132
      string body;
133
      while(read_until(in, body, "<"))
134
      {
135
        elem.body->text.push_back(body);
136
 
137
        char c;
138
        in >> c;
139
        if(c == '/')
140
        {
141
          string closing;
142
          read_until(in, closing, ">");
143
          if(trim(closing) == elem.name)
144
            break;
377 jrf 145
          else
146
            continue;
375 jrf 147
        }
148
        in.putback(c);
149
        in.putback('<');
150
 
151
        elem.body->elements.push_back(XmlElement(elem.doc, &elem));
152
        if(!(in >> elem.body->elements.back()))
153
        {
154
          elem.body->elements.pop_back();
155
          break;
156
        }
157
      }      
158
    }
159
    return in;
160
  }
161
 
162
  ifstream& operator>>(ifstream& in, XmlBody& body)
163
  {
164
    string s;
165
    while(read_until(in, s, "<"))
166
    {
167
      body.text.push_back(s);
168
 
169
      in.putback('<');
170
 
171
      body.elements.push_back(XmlElement(body.doc));
172
      if(!(in >> body.elements.back()))
173
      {
174
        body.elements.pop_back();
175
        break;
176
      }
177
    }      
178
    return in;
179
  }
180
 
181
  void XmlDoc::load_xml(const char *filename)
182
  {
183
    ifstream infile(filename);
184
 
185
    if(!infile)
186
    {
187
       cerr << "cannot open input file" << filename << endl;
188
       return;
189
    }
190
 
191
    if(infile >> head)
376 jrf 192
      infile >> body;
375 jrf 193
    else
194
      cerr << filename << " is not a valid xml-file" << endl;
195
 
196
    infile.close();    
197
  }
198
 
199
  /////////////////////////////////////////////////////////////////
200
  // Methods
201
  /////////////////////////////////////////////////////////////////
202
 
203
  XmlElement::~XmlElement()
204
  {
205
    delete body;
206
  }
207
 
208
  void XmlElement::process_element(XmlElement& elem)
209
  {
210
    if(!doc) return;
211
    XmlElementHandler h = doc->handlers[elem.name];
212
    if(h) h(elem);
213
  }
214
 
215
  void XmlElement::process_elements()
216
  {
217
    if(!body) return;
218
    if(!doc) return;
219
 
220
    XmlElementHandler h = 0;
221
    for(list<XmlElement>::iterator i = body->elements.begin(); i != body->elements.end(); ++i)
222
      if(h = doc->handlers[(*i).name])
223
         h(*i);
224
  }
225
 
226
  XmlDoc::XmlDoc(const char *filename)
227
    : body(0)
228
  {
229
    body.doc = this;
230
    load_xml(filename);
231
  }
232
 
233
  void XmlDoc::process_elements()
234
  {
235
    XmlElementHandler h = 0;
236
    for(list<XmlElement>::iterator i = body.elements.begin(); i != body.elements.end(); ++i)
237
      if(h = handlers[(*i).name])
238
         h(*i);
239
  }
240
 
241
  ostream& XmlDoc::put(ostream& out) const
242
  {
243
    return out << head << body;    
244
  }
245
 
246
  /////////////////////////////////////////////////////////////////
247
  // Output handling
248
  /////////////////////////////////////////////////////////////////
249
 
250
  ostream& operator<<(ostream& out, const pair<string, string>& attrib)
251
  {
252
    return out << attrib.first << "=\"" << attrib.second << "\"";
253
  }
254
 
255
  ostream& operator<<(ostream& out, const XmlHead& head)
256
  {
257
    out << "<?xml";
258
    for(map<string, string>::const_iterator i = head.atts.begin(); i != head.atts.end(); ++i)
259
      out << " " << *i;
260
    out << "?>";
261
    return out;
262
  }
263
 
264
  ostream& operator<<(ostream& out, const XmlElement& elem)
265
  {
266
    out << "<" << elem.name;
267
    for(map<string, string>::const_iterator i = elem.atts.begin(); i != elem.atts.end(); ++i)
268
      out  << " " << *i;
269
    out << (elem.body ? ">" : "/>");
270
 
271
    if(elem.body)
272
      out << *elem.body << "</" << elem.name << ">";
273
 
274
    return out;
275
  }
276
 
277
  ostream& operator<<(ostream& out, const XmlBody& body)
278
  {
279
    list<string>::const_iterator i = body.text.begin();
280
    list<XmlElement>::const_iterator j = body.elements.begin();
281
    for(; i != body.text.end() && j != body.elements.end(); ++i, ++j)
282
      out << *i << *j;
283
    return out << (body.text.empty() ? "" : body.text.back());
284
  }
285
}