Subversion Repositories gelsvn

Rev

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

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

Generated by GNU Enscript 1.6.6.
287

Generated by GNU Enscript 1.6.6.
286
 
288
 
287
 
289
 
288
 
290