Subversion Repositories gelsvn

Rev

Rev 377 | Rev 381 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 377 Rev 380
Line 112... Line 112...
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;
Line 127... Line 135...
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
  /////////////////////////////////////////////////////////////////
Line 258... Line 235...
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