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* 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; }
Public Instance Methods
close()
click to toggle source
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; }
flush()
click to toggle source
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; }
string()
click to toggle source
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); }
write_attribute(p1, p2)
click to toggle source
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; }
write_element(p1, p2)
click to toggle source
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; }
write_end()
click to toggle source
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; }
write_start(p1)
click to toggle source
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; }