Class: OvirtSDK4::XmlReader

Inherits:
Object
  • Object
show all
Defined in:
ext/ovirtsdk4c/ov_xml_reader.c

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 124

static VALUE ov_xml_reader_initialize(VALUE self, VALUE io) {
    VALUE io_class;
    int rc;
    ov_xml_reader_object* ptr;

    /* Get the pointer to the object: */
    ov_xml_reader_ptr(self, ptr);

    /* The parameter of the constructor can be a string or an IO object. If it is a string then we need to create aa
       IO object to read from it. */
    io_class = rb_class_of(io);
    if (io_class == rb_cString) {
        ptr->io = ov_xml_reader_create_string_io(io);
    }
    else if (io_class == rb_cIO) {
        ptr->io = io;
    }
    else {
        rb_raise(
            ov_error_class,
            "The type of the 'io' parameter must be 'String' or 'IO', but it is '%"PRIsVALUE"'",
            io_class
        );
    }

    /* Clear the closed flag: */
    ptr->closed = false;

    /* Create the libxml reader: */
    ptr->reader = xmlReaderForIO(ov_xml_reader_callback, NULL, ptr, NULL, NULL, 0);
    if (ptr->reader == NULL) {
        rb_raise(ov_error_class, "Can't create reader");
    }

    /* Move the cursor to the first node: */
    rc = xmlTextReaderRead(ptr->reader);
    if (rc == -1) {
        rb_raise(ov_error_class, "Can't read first node");
    }

    return self;
}

Instance Method Details

#closeObject



417
418
419
420
421
422
423
424
425
426
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 417

static VALUE ov_xml_reader_close(VALUE self) {
    ov_xml_reader_object* ptr;

    ov_xml_reader_ptr(self, ptr);
    ov_xml_reader_check_closed(ptr);
    xmlFreeTextReader(ptr->reader);
    ptr->reader = NULL;
    ptr->closed = true;
    return Qnil;
}

#empty_element?Boolean

Returns:

  • (Boolean)


227
228
229
230
231
232
233
234
235
236
237
238
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 227

static VALUE ov_xml_reader_empty_element(VALUE self) {
    int c_empty;
    ov_xml_reader_object* ptr;

    ov_xml_reader_ptr(self, ptr);
    ov_xml_reader_check_closed(ptr);
    c_empty = xmlTextReaderIsEmptyElement(ptr->reader);
    if (c_empty == -1) {
        rb_raise(ov_error_class, "Can't check if current element is empty");
    }
    return c_empty? Qtrue: Qfalse;
}

#forwardObject

Define the methods:



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 184

static VALUE ov_xml_reader_forward(VALUE self) {
    int c_type = 0;
    int rc = 0;
    ov_xml_reader_object* ptr;

    ov_xml_reader_ptr(self, ptr);
    ov_xml_reader_check_closed(ptr);

    for (;;) {
        c_type = xmlTextReaderNodeType(ptr->reader);
        if (c_type == -1) {
            rb_raise(ov_error_class, "Can't get current node type");
        }
        else if (c_type == XML_READER_TYPE_ELEMENT) {
            return Qtrue;
        }
        else if (c_type == XML_READER_TYPE_END_ELEMENT || c_type == XML_READER_TYPE_NONE) {
            return Qfalse;
        }
        else {
            rc = xmlTextReaderRead(ptr->reader);
            if (rc == -1) {
                rb_raise(ov_error_class, "Can't move to next node");
            }
        }
    }
}

#get_attribute(name) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 240

static VALUE ov_xml_reader_get_attribute(VALUE self, VALUE name) {
    VALUE value;
    ov_xml_reader_object* ptr;
    xmlChar* c_name;
    xmlChar* c_value;

    ov_xml_reader_ptr(self, ptr);
    ov_xml_reader_check_closed(ptr);
    c_name = (xmlChar*) StringValueCStr(name);
    c_value = xmlTextReaderGetAttribute(ptr->reader, c_name);
    if (c_value == NULL) {
        return Qnil;
    }
    value = rb_str_new_cstr((char*) c_value);
    xmlFree(c_value);
    return value;
}

#next_elementObject



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 401

static VALUE ov_xml_reader_next_element(VALUE self) {
    int rc;
    ov_xml_reader_object* ptr;

    ov_xml_reader_ptr(self, ptr);
    ov_xml_reader_check_closed(ptr);
    rc = xmlTextReaderNext(ptr->reader);
    if (rc == 0) {
        return Qfalse;
    }
    if (rc == 1) {
        return Qtrue;
    }
    rb_raise(ov_error_class, "Can't move to next element");
}

#node_nameObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 212

static VALUE ov_xml_reader_node_name(VALUE self) {
    VALUE name;
    const xmlChar* c_name;
    ov_xml_reader_object* ptr;

    ov_xml_reader_ptr(self, ptr);
    ov_xml_reader_check_closed(ptr);
    c_name = xmlTextReaderConstName(ptr->reader);
    if (c_name == NULL) {
        return Qnil;
    }
    name = rb_str_new_cstr((char*) c_name);
    return name;
}

#readObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 167

static VALUE ov_xml_reader_read(VALUE self) {
    int rc = 0;
    ov_xml_reader_object* ptr;

    ov_xml_reader_ptr(self, ptr);
    ov_xml_reader_check_closed(ptr);
    rc = xmlTextReaderRead(ptr->reader);
    if (rc == 0) {
        return Qfalse;
    }
    if (rc == 1) {
        return Qtrue;
    }
    rb_raise(ov_error_class, "Can't move to next node");
    return Qnil;
}

#read_elementObject



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 258

static VALUE ov_xml_reader_read_element(VALUE self) {
    VALUE value;
    int c_empty;
    int c_type;
    int rc;
    ov_xml_reader_object* ptr;
    xmlChar* c_value;

    ov_xml_reader_ptr(self, ptr);
    ov_xml_reader_check_closed(ptr);

    /* Check the type of the current node: */
    c_type = xmlTextReaderNodeType(ptr->reader);
    if (c_type == -1) {
        rb_raise(ov_error_class, "Can't get current node type");
    }
    if (c_type != XML_READER_TYPE_ELEMENT) {
        rb_raise(ov_error_class, "Current node isn't the start of an element");
    }

    /* Check if the current node is empty: */
    c_empty = xmlTextReaderIsEmptyElement(ptr->reader);
    if (c_empty == -1) {
        rb_raise(ov_error_class, "Can't check if current element is empty");
    }

    /* For empty values elements there is no need to read the value. For non empty values we need to read the value, and
       check if it is NULL, as that means that the value is an empty string. */
    if (c_empty) {
        c_value = NULL;
    }
    else {
        c_value = xmlTextReaderReadString(ptr->reader);
        if (c_value == NULL) {
            c_value = xmlCharStrdup("");
            if (c_value == NULL) {
                rb_raise(ov_error_class, "Can't allocate XML string");
            }
        }
    }

    /* Move to the next element: */
    rc = xmlTextReaderNext(ptr->reader);
    if (rc == -1) {
        if (c_value != NULL) {
            xmlFree(c_value);
        }
        rb_raise(ov_error_class, "Can't move to the next element");
    }

    /* Return the result: */
    if (c_value == NULL) {
       return Qnil;
    }
    value = rb_str_new_cstr((char*) c_value);
    xmlFree(c_value);
    return value;
}

#read_elementsObject



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 317

static VALUE ov_xml_reader_read_elements(VALUE self) {
    VALUE element;
    VALUE list;
    int c_empty;
    int c_type;
    int rc;
    ov_xml_reader_object* ptr;

    /* Get the pointer to the object and check that it isn't closed: */
    ov_xml_reader_ptr(self, ptr);
    ov_xml_reader_check_closed(ptr);

    /* This method assumes that the reader is positioned at the element that contains the values to read. For example
       if the XML document is the following:

       <list>
         <value>first</value>
         <value>second</value>
       </list>

       The reader should be positioned at the <list> element. The first thing we need to do is to check: */
    c_type = xmlTextReaderNodeType(ptr->reader);
    if (c_type == -1) {
        rb_raise(ov_error_class, "Can't get current node type");
    }
    if (c_type != XML_READER_TYPE_ELEMENT) {
        rb_raise(ov_error_class, "Current node isn't the start of an element");
    }

    /* If we are indeed positioned at the first element, then we need to check if it is empty, <list/>, as we will
       need this lter, after discarding the element: */
    c_empty = xmlTextReaderIsEmptyElement(ptr->reader);
    if (c_empty == -1) {
        rb_raise(ov_error_class, "Can't check if current element is empty");
    }

    /* Now we need to discard the current element, as we are interested only in the nested <value>...</value>
       elements: */
    rc = xmlTextReaderRead(ptr->reader);
    if (rc == -1) {
        rb_raise(ov_error_class, "Can't move to next node");
    }

    /* Create the list that will contain the result: */
    list = rb_ary_new();

    /* At this point, if the start element was empty, we don't need to do anything else: */
    if (c_empty) {
        return list;
    }

    /* Process the nested elements: */
    for (;;) {
        c_type = xmlTextReaderNodeType(ptr->reader);
        if (c_type == -1) {
            rb_raise(ov_error_class, "Can't get current node type");
        }
        else if (c_type == XML_READER_TYPE_ELEMENT) {
            element = ov_xml_reader_read_element(self);
            rb_ary_push(list, element);
        }
        else if (c_type == XML_READER_TYPE_END_ELEMENT || c_type == XML_READER_TYPE_NONE) {
            break;
        }
        else {
            rc = xmlTextReaderNext(ptr->reader);
            if (rc == -1) {
                rb_raise(ov_error_class, "Can't move to the next node");
            }
        }
    }

    /* Once all the nested <value>...</value> elements are processed the reader will be positioned at the closing
       </list> element, or at the end of the document. If it is the closing element then we need to discard it. */
    if (c_type == XML_READER_TYPE_END_ELEMENT) {
        rc = xmlTextReaderRead(ptr->reader);
        if (rc == -1) {
            rb_raise(ov_error_class, "Can't move to next node");
        }
    }

    return list;
}