class OvirtSDK4::XmlWriter
Define the class:
Public Class Methods
new(*args)
click to toggle source
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;
}
Public Instance Methods
close()
click to toggle source
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;
}
flush()
click to toggle source
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;
}
string()
click to toggle source
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, Qnil);
}
write_attribute(p1, p2)
click to toggle source
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(p1, p2)
click to toggle source
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_end()
click to toggle source
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(p1)
click to toggle source
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;
}