Subversion Repositories gelsvn

Rev

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

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

Generated by GNU Enscript 1.6.6.
242

Generated by GNU Enscript 1.6.6.
288
 
243
 
289
 
244
 
290
 
245