Subversion Repositories gelsvn

Rev

Rev 376 | Go to most recent revision | Details | 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
 
68
  ifstream& read_until(ifstream& in, string& s_in, const string s, const size_t bufsize = 100)
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)
190
    {
191
      if(infile >> body)
192
      {
193
 
194
      }
195
    }
196
    else
197
      cerr << filename << " is not a valid xml-file" << endl;
198
 
199
    infile.close();    
200
  }
201
 
202
  /////////////////////////////////////////////////////////////////
203
  // Methods
204
  /////////////////////////////////////////////////////////////////
205
 
206
  XmlElement::~XmlElement()
207
  {
208
    delete body;
209
  }
210
 
211
  void XmlElement::process_element(XmlElement& elem)
212
  {
213
    if(!doc) return;
214
    XmlElementHandler h = doc->handlers[elem.name];
215
    if(h) h(elem);
216
  }
217
 
218
  void XmlElement::process_elements()
219
  {
220
    if(!body) return;
221
    if(!doc) return;
222
 
223
    XmlElementHandler h = 0;
224
    for(list<XmlElement>::iterator i = body->elements.begin(); i != body->elements.end(); ++i)
225
      if(h = doc->handlers[(*i).name])
226
         h(*i);
227
  }
228
 
229
  XmlDoc::XmlDoc(const char *filename)
230
    : body(0)
231
  {
232
    body.doc = this;
233
    load_xml(filename);
234
  }
235
 
236
  void XmlDoc::process_elements()
237
  {
238
    XmlElementHandler h = 0;
239
    for(list<XmlElement>::iterator i = body.elements.begin(); i != body.elements.end(); ++i)
240
      if(h = handlers[(*i).name])
241
         h(*i);
242
  }
243
 
244
  ostream& XmlDoc::put(ostream& out) const
245
  {
246
    return out << head << body;    
247
  }
248
 
249
  /////////////////////////////////////////////////////////////////
250
  // Output handling
251
  /////////////////////////////////////////////////////////////////
252
 
253
  ostream& operator<<(ostream& out, const pair<string, string>& attrib)
254
  {
255
    return out << attrib.first << "=\"" << attrib.second << "\"";
256
  }
257
 
258
  ostream& operator<<(ostream& out, const XmlHead& head)
259
  {
260
    out << "<?xml";
261
    for(map<string, string>::const_iterator i = head.atts.begin(); i != head.atts.end(); ++i)
262
      out << " " << *i;
263
    out << "?>";
264
    return out;
265
  }
266
 
267
  ostream& operator<<(ostream& out, const XmlElement& elem)
268
  {
269
    out << "<" << elem.name;
270
    for(map<string, string>::const_iterator i = elem.atts.begin(); i != elem.atts.end(); ++i)
271
      out  << " " << *i;
272
    out << (elem.body ? ">" : "/>");
273
 
274
    if(elem.body)
275
      out << *elem.body << "</" << elem.name << ">";
276
 
277
    return out;
278
  }
279
 
280
  ostream& operator<<(ostream& out, const XmlBody& body)
281
  {
282
    list<string>::const_iterator i = body.text.begin();
283
    list<XmlElement>::const_iterator j = body.elements.begin();
284
    for(; i != body.text.end() && j != body.elements.end(); ++i, ++j)
285
      out << *i << *j;
286
    return out << (body.text.empty() ? "" : body.text.back());
287
  }
288
}