1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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 152 153
|
/* $Id: ruby_xml_ns.c 134 2007-08-29 17:30:19Z danj $ */
/* Please see the LICENSE file for copyright and distribution information */
#include "libxml.h"
#include "ruby_xml_ns.h"
VALUE cXMLNS;
/*
* call-seq:
* ns.href => "href"
*
* Obtain the namespace's href.
*/
VALUE
ruby_xml_ns_href_get(VALUE self) {
ruby_xml_ns *rxns;
Data_Get_Struct(self, ruby_xml_ns, rxns);
if (rxns->ns == NULL || rxns->ns->href == NULL)
return(Qnil);
else
return(rb_str_new2((const char*)rxns->ns->href));
}
/*
* call-seq:
* ns.href? => (true|false)
*
* Determine whether this namespace has an href.
*/
VALUE
ruby_xml_ns_href_q(VALUE self) {
ruby_xml_ns *rxns;
Data_Get_Struct(self, ruby_xml_ns, rxns);
if (rxns->ns == NULL || rxns->ns->href == NULL)
return(Qfalse);
else
return(Qtrue);
}
void
ruby_xml_ns_free(ruby_xml_ns *rxns) {
if (rxns->ns != NULL && !rxns->is_ptr) {
xmlFreeNs(rxns->ns);
rxns->ns = NULL;
}
free(rxns);
}
static void
ruby_xml_ns_mark(ruby_xml_ns *rxns) {
if (rxns == NULL) return;
if (!NIL_P(rxns->xd)) rb_gc_mark(rxns->xd);
}
VALUE
ruby_xml_ns_new(VALUE class, VALUE xd, xmlNsPtr ns) {
ruby_xml_ns *rxns;
rxns = ALLOC(ruby_xml_ns);
rxns->is_ptr = 0;
rxns->ns = ns;
rxns->xd = xd;
return(Data_Wrap_Struct(class, ruby_xml_ns_mark,
ruby_xml_ns_free, rxns));
}
VALUE
ruby_xml_ns_new2(VALUE class, VALUE xd, xmlNsPtr ns) {
ruby_xml_ns *rxns;
rxns = ALLOC(ruby_xml_ns);
rxns->is_ptr = 1;
rxns->ns = ns;
rxns->xd = xd;
return(Data_Wrap_Struct(class, ruby_xml_ns_mark,
ruby_xml_ns_free, rxns));
}
/*
* call-seq:
* ns.next => ns
*
* Obtain the next namespace.
*/
VALUE
ruby_xml_ns_next(VALUE self) {
ruby_xml_ns *rxns;
Data_Get_Struct(self, ruby_xml_ns, rxns);
if (rxns->ns == NULL || rxns->ns->next == NULL)
return(Qnil);
else
return(ruby_xml_ns_new2(cXMLNS, rxns->xd, rxns->ns->next));
}
/*
* call-seq:
* ns.prefix => "prefix"
* ns.to_s => "prefix"
*
* Obtain the namespace's prefix.
*/
VALUE
ruby_xml_ns_prefix_get(VALUE self) {
ruby_xml_ns *rxns;
Data_Get_Struct(self, ruby_xml_ns, rxns);
if (rxns->ns == NULL || rxns->ns->prefix == NULL)
return(Qnil);
else
return(rb_str_new2((const char*)rxns->ns->prefix));
}
/*
* call-seq:
* ns.prefix? => (true|false)
*
* Determine whether this namespace has a prefix.
*/
VALUE
ruby_xml_ns_prefix_q(VALUE self) {
ruby_xml_ns *rxns;
Data_Get_Struct(self, ruby_xml_ns, rxns);
if (rxns->ns == NULL || rxns->ns->prefix == NULL)
return(Qfalse);
else
return(Qtrue);
}
// Rdoc needs to know
#ifdef RDOC_NEVER_DEFINED
mXML = rb_define_module("XML");
#endif
void
ruby_init_xml_ns(void) {
cXMLNS = rb_define_class_under(mXML, "NS", rb_cObject);
rb_define_method(cXMLNS, "href", ruby_xml_ns_href_get, 0);
rb_define_method(cXMLNS, "href?", ruby_xml_ns_href_q, 0);
rb_define_method(cXMLNS, "next", ruby_xml_ns_next, 0);
rb_define_method(cXMLNS, "prefix", ruby_xml_ns_prefix_get, 0);
rb_define_method(cXMLNS, "prefix?", ruby_xml_ns_prefix_q, 0);
rb_define_method(cXMLNS, "to_s", ruby_xml_ns_prefix_get, 0);
}
|