Class: OvirtSDK4::XmlReader

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

Instance Method Summary (collapse)

Constructor Details

- (Object) initialize



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 105

static VALUE ov_xml_reader_initialize(VALUE self, VALUE io) {
    VALUE io_class;
    int rc = 0;
    ov_xml_reader_object* object = NULL;

    /* Get the pointer to the object: */
    Data_Get_Struct(self, ov_xml_reader_object, object);

    /* 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) {
        object->io = ov_xml_reader_create_string_io(io);
    }
    else if (io_class == rb_cIO) {
        object->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: */
    object->closed = false;

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

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

    return self;
}

Instance Method Details

- (Object) close



341
342
343
344
345
346
347
348
349
350
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 341

static VALUE ov_xml_reader_close(VALUE self) {
    ov_xml_reader_object* object = NULL;

    Data_Get_Struct(self, ov_xml_reader_object, object);
    ov_xml_reader_check_closed(object);
    xmlFreeTextReader(object->reader);
    object->reader = NULL;
    object->closed = true;
    return Qnil;
}

- (Boolean) empty_element?

Returns:

  • (Boolean)


208
209
210
211
212
213
214
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 208

static VALUE ov_xml_reader_empty_element(VALUE self) {
    ov_xml_reader_object* object = NULL;

    Data_Get_Struct(self, ov_xml_reader_object, object);
    ov_xml_reader_check_closed(object);
    return xmlTextReaderIsEmptyElement(object->reader)? Qtrue: Qfalse;
}

- (Object) forward

Define the methods:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 165

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

    Data_Get_Struct(self, ov_xml_reader_object, object);
    ov_xml_reader_check_closed(object);

    for (;;) {
        c_type = xmlTextReaderNodeType(object->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(object->reader);
            if (rc == -1) {
                rb_raise(ov_error_class, "Can't move to next node");
            }
        }
    }
}

- (Object) get_attribute



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 216

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

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

- (Object) next_element



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 325

static VALUE ov_xml_reader_next_element(VALUE self) {
    int rc = 0;
    ov_xml_reader_object* object = NULL;

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

- (Object) node_name



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 193

static VALUE ov_xml_reader_node_name(VALUE self) {
    VALUE name;
    const xmlChar* c_name = NULL;
    ov_xml_reader_object* object = NULL;

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

- (Object) read



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 148

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

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

- (Object) read_element



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 234

static VALUE ov_xml_reader_read_element(VALUE self) {
    VALUE value;
    int c_empty = 0;
    int c_type = 0;
    int rc = 0;
    ov_xml_reader_object *object = NULL;
    xmlChar *c_value = NULL;

    Data_Get_Struct(self, ov_xml_reader_object, object);
    ov_xml_reader_check_closed(object);

    /* Check the type of the current node: */
    c_type = xmlTextReaderNodeType(object->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(object->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(object->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(object->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;
}

- (Object) read_elements



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'ext/ovirtsdk4c/ov_xml_reader.c', line 293

static VALUE ov_xml_reader_read_elements(VALUE self) {
    VALUE element;
    VALUE list;
    int c_type = 0;
    int rc = 0;
    ov_xml_reader_object* object = NULL;

    Data_Get_Struct(self, ov_xml_reader_object, object);
    ov_xml_reader_check_closed(object);
    list = rb_ary_new();
    for (;;) {
        c_type = xmlTextReaderNodeType(object->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(object->reader);
            if (rc == -1) {
                rb_raise(ov_error_class, "Can't move to the next element");
            }
        }
    }
    return list;
}