Class: OvirtSDK4::XmlWriter

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

Instance Method Summary (collapse)

Constructor Details

- (Object) initialize



95
96
97
98
99
100
101
102
103
104
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
147
148
149
150
151
# File 'ext/ovirtsdk4c/ov_xml_writer.c', line 95

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

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

    /* 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)) {
        object->io = ov_xml_writer_create_string_io();
    }
    else {
        io_class = rb_class_of(io);
        if (io_class == rb_cIO) {
            object->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, object, NULL);
    if (buffer == NULL) {
        rb_raise(ov_error_class, "Can't create XML buffer");
    }

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

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

    return self;
}

Instance Method Details

- (Object) close

Define the methods:



246
247
248
249
250
251
252
253
254
# File 'ext/ovirtsdk4c/ov_xml_writer.c', line 246

static VALUE ov_xml_writer_close(VALUE self) {
    ov_xml_writer_object* object = NULL;

    Data_Get_Struct(self, ov_xml_writer_object, object);
    ov_xml_writer_check_closed(object);
    xmlFreeTextWriter(object->writer);
    object->writer = NULL;
    return Qnil;
}

- (Object) flush



233
234
235
236
237
238
239
240
241
242
243
244
# File 'ext/ovirtsdk4c/ov_xml_writer.c', line 233

static VALUE ov_xml_writer_flush(VALUE self) {
    ov_xml_writer_object* object = NULL;
    int rc = 0;

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

- (Object) string



153
154
155
156
157
158
159
160
161
162
163
164
# File 'ext/ovirtsdk4c/ov_xml_writer.c', line 153

static VALUE ov_xml_writer_string(VALUE self) {
    int rc = 0;
    ov_xml_writer_object* object = NULL;

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

- (Object) write_attribute



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'ext/ovirtsdk4c/ov_xml_writer.c', line 195

static VALUE ov_xml_writer_write_attribute(VALUE self, VALUE name, VALUE value) {
    char* c_name = NULL;
    char* c_value = NULL;
    int rc = 0;
    ov_xml_writer_object* object = NULL;

    Data_Get_Struct(self, ov_xml_writer_object, object);
    ov_xml_writer_check_closed(object);
    Check_Type(name, T_STRING);
    Check_Type(value, T_STRING);
    c_name = StringValueCStr(name);
    c_value = StringValueCStr(value);
    rc = xmlTextWriterWriteAttribute(object->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;
}

- (Object) write_element



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

static VALUE ov_xml_writer_write_element(VALUE self, VALUE name, VALUE value) {
    char* c_name = NULL;
    char* c_value = NULL;
    int rc = 0;
    ov_xml_writer_object* object = NULL;

    Data_Get_Struct(self, ov_xml_writer_object, object);
    ov_xml_writer_check_closed(object);
    Check_Type(name, T_STRING);
    Check_Type(value, T_STRING);
    c_name = StringValueCStr(name);
    c_value = StringValueCStr(value);
    rc = xmlTextWriterWriteElement(object->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;
}

- (Object) write_end



182
183
184
185
186
187
188
189
190
191
192
193
# File 'ext/ovirtsdk4c/ov_xml_writer.c', line 182

static VALUE ov_xml_writer_write_end(VALUE self) {
    int rc = 0;
    ov_xml_writer_object* object = NULL;

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

- (Object) write_start



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

static VALUE ov_xml_writer_write_start(VALUE self, VALUE name) {
    char* c_name = NULL;
    int rc = 0;
    ov_xml_writer_object* object = NULL;

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