Class: OvirtSDK4::XmlWriter

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object


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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'ext/ovirtsdk4c/ov_xml_writer.c', line 116

static VALUE ov_xml_writer_initialize(int argc, VALUE* argv, VALUE self) {
    VALUE indent;
    VALUE io;
    VALUE io_class;
    ov_xml_writer_object* ptr;
    xmlOutputBufferPtr buffer;

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

    /* Get the values of the parameters: */
    if (argc > 2) {
      rb_raise(ov_error_class, "Expected at most two arguments, 'io' and 'indent', but received %d", argc);
    }
    io = argc > 0? argv[0]: Qnil;
    indent = argc > 1? argv[1]: Qnil;

    /* The first parameter can be an IO object or nil. If it is nil then we need to create a IO object where we can
       write the generated XML. */
    if (NIL_P(io)) {
        ptr->io = ov_xml_writer_create_string_io();
    }
    else {
        io_class = rb_class_of(io);
        if (io_class == rb_cIO) {
            ptr->io = io;
        }
        else {
            rb_raise(
                ov_error_class,
                "The type of the 'io' parameter must be 'IO', but it is '%"PRIsVALUE"'",
                io_class
            );
        }
    }

    /* Create the libxml buffer that writes to the IO object: */
    buffer = xmlOutputBufferCreateIO(ov_xml_writer_callback, NULL, ptr, NULL);
    if (buffer == NULL) {
        rb_raise(ov_error_class, "Can't create XML buffer");
    }

    /* Create the libxml writer: */
    ptr->writer = xmlNewTextWriter(buffer);
    if (ptr->writer == NULL) {
        xmlOutputBufferClose(buffer);
        rb_raise(ov_error_class, "Can't create XML writer");
    }

    /* Enable indentation: */
    if (RTEST(indent)) {
        xmlTextWriterSetIndent(ptr->writer, 1);
        xmlTextWriterSetIndentString(ptr->writer, BAD_CAST "  ");
    }

    return self;
}

Instance Method Details

#closeObject

Define the methods:


267
268
269
270
271
272
273
274
275
# File 'ext/ovirtsdk4c/ov_xml_writer.c', line 267

static VALUE ov_xml_writer_close(VALUE self) {
    ov_xml_writer_object* ptr;

    ov_xml_writer_ptr(self, ptr);
    ov_xml_writer_check_closed(ptr);
    xmlFreeTextWriter(ptr->writer);
    ptr->writer = NULL;
    return Qnil;
}

#flushObject


254
255
256
257
258
259
260
261
262
263
264
265
# File 'ext/ovirtsdk4c/ov_xml_writer.c', line 254

static VALUE ov_xml_writer_flush(VALUE self) {
    int rc;
    ov_xml_writer_object* ptr;

    ov_xml_writer_ptr(self, ptr);
    ov_xml_writer_check_closed(ptr);
    rc = xmlTextWriterFlush(ptr->writer);
    if (rc < 0) {
        rb_raise(ov_error_class, "Can't flush XML writer");
    }
    return Qnil;
}

#stringObject


174
175
176
177
178
179
180
181
182
183
184
185
# File 'ext/ovirtsdk4c/ov_xml_writer.c', line 174

static VALUE ov_xml_writer_string(VALUE self) {
    int rc;
    ov_xml_writer_object* ptr;

    ov_xml_writer_ptr(self, ptr);
    ov_xml_writer_check_closed(ptr);
    rc = xmlTextWriterFlush(ptr->writer);
    if (rc < 0) {
        rb_raise(ov_error_class, "Can't flush XML writer");
    }
    return rb_funcall(ptr->io, STRING_ID, 0, NULL);
}

#write_attribute(name, value) ⇒ Object


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

static VALUE ov_xml_writer_write_attribute(VALUE self, VALUE name, VALUE value) {
    char* c_name;
    char* c_value;
    int rc;
    ov_xml_writer_object* ptr;

    ov_xml_writer_ptr(self, ptr);
    ov_xml_writer_check_closed(ptr);
    Check_Type(name, T_STRING);
    Check_Type(value, T_STRING);
    c_name = StringValueCStr(name);
    c_value = StringValueCStr(value);
    rc = xmlTextWriterWriteAttribute(ptr->writer, BAD_CAST c_name, BAD_CAST c_value);
    if (rc < 0) {
        rb_raise(ov_error_class, "Can't write attribute with name \"%s\" and value \"%s\"", c_name, c_value);
    }
    return Qnil;
}

#write_element(name, value) ⇒ Object


235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/ovirtsdk4c/ov_xml_writer.c', line 235

static VALUE ov_xml_writer_write_element(VALUE self, VALUE name, VALUE value) {
    char* c_name;
    char* c_value;
    int rc;
    ov_xml_writer_object* ptr;

    ov_xml_writer_ptr(self, ptr);
    ov_xml_writer_check_closed(ptr);
    Check_Type(name, T_STRING);
    Check_Type(value, T_STRING);
    c_name = StringValueCStr(name);
    c_value = StringValueCStr(value);
    rc = xmlTextWriterWriteElement(ptr->writer, BAD_CAST c_name, BAD_CAST c_value);
    if (rc < 0) {
        rb_raise(ov_error_class, "Can't write element with name \"%s\" and value \"%s\"", c_name, c_value);
    }
    return Qnil;
}

#write_endObject


203
204
205
206
207
208
209
210
211
212
213
214
# File 'ext/ovirtsdk4c/ov_xml_writer.c', line 203

static VALUE ov_xml_writer_write_end(VALUE self) {
    int rc;
    ov_xml_writer_object* ptr;

    ov_xml_writer_ptr(self, ptr);
    ov_xml_writer_check_closed(ptr);
    rc = xmlTextWriterEndElement(ptr->writer);
    if (rc < 0) {
        rb_raise(ov_error_class, "Can't end XML element");
    }
    return Qnil;
}

#write_start(name) ⇒ Object


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'ext/ovirtsdk4c/ov_xml_writer.c', line 187

static VALUE ov_xml_writer_write_start(VALUE self, VALUE name) {
    char* c_name;
    int rc;
    ov_xml_writer_object* ptr;

    ov_xml_writer_ptr(self, ptr);
    ov_xml_writer_check_closed(ptr);
    Check_Type(name, T_STRING);
    c_name = StringValueCStr(name);
    rc = xmlTextWriterStartElement(ptr->writer, BAD_CAST c_name);
    if (rc < 0) {
        rb_raise(ov_error_class, "Can't start XML element");
    }
    return Qnil;
}