Subversion Repositories gelsvn

Rev

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