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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/* Please see the LICENSE file for copyright and distribution information */
/*
* Document-class: LibXML::XML::Attributes
*
* Provides access to an element's attributes (XML::Attr).
*
* Basic Usage:
* require 'test_helper'
*
* doc = XML::Document.new(<some_file>)
* attributes = doc.root.attributes
*
* attributes.each do |attribute|
* ..
* end
*
* attributes['foo'] = 'bar'
* attribute = attributes.get_attribute['foo']
* attribute.value == 'foo'
*
* To access a namespaced attribute:
*
* XLINK_URI = 'http://www.w3.org/1999/xlink'
*
* attribute = attributes.get_attribute_ns(XLINK_URI, 'title')
* attribute.value = 'My title'
*/
#include "ruby_libxml.h"
#include "ruby_xml_attributes.h"
VALUE cXMLAttributes;
void rxml_attributes_mark(xmlNodePtr xnode)
{
rxml_node_mark(xnode);
}
/*
* Creates a new attributes instance. Not exposed to ruby.
*/
VALUE rxml_attributes_new(xmlNodePtr xnode)
{
return Data_Wrap_Struct(cXMLAttributes, rxml_attributes_mark, NULL, xnode);
}
/*
* call-seq:
* attributes.node -> XML::Node
*
* Return the node that owns this attributes list.
*
* doc.root.attributes.node == doc.root
*/
VALUE rxml_attributes_node_get(VALUE self)
{
xmlNodePtr xnode;
Data_Get_Struct(self, xmlNode, xnode);
return rxml_node_wrap(xnode);
}
/*
* call-seq:
* attributes.get_attribute("name") -> (XML::Attr | XML::AtrrDecl)
*
* Returns the specified attribute. If the attribute does not
* exist but the document has an associated DTD that defines
* a default value for the attribute, then a XML::AttrDecl is
* returned.
*
* name: The name of the attribute, not including a namespace.
*
* doc.root.attributes.get_attribute("foo")
*/
static VALUE rxml_attributes_get_attribute(VALUE self, VALUE name)
{
xmlNodePtr xnode;
xmlAttrPtr xattr;
name = rb_obj_as_string(name);
Data_Get_Struct(self, xmlNode, xnode);
xattr = xmlHasProp(xnode, (xmlChar*) StringValuePtr(name));
if (!xattr)
return Qnil;
else if (xattr->type == XML_ATTRIBUTE_DECL)
return rxml_attr_decl_wrap((xmlAttributePtr)xattr);
else
return rxml_attr_wrap(xattr);
}
/*
* call-seq:
* attributes.get_attribute_ns("namespace", "name") -> (XML::Attr | XML::AtrrDecl)
*
* Returns the specified attribute. If the attribute does not
* exist but the document has an associated DTD that defines
* a default value for the attribute, then a XML::AttrDecl is
* returned.
*
* namespace: The URI of the attribute's namespace.
* name: The name of the attribute, not including a namespace.
*
* doc.root.attributes.get_attribute_ns('http://www.w3.org/1999/xlink', 'href')
*/
static VALUE rxml_attributes_get_attribute_ns(VALUE self, VALUE namespace,
VALUE name)
{
xmlNodePtr xnode;
xmlAttrPtr xattr;
name = rb_obj_as_string(name);
Data_Get_Struct(self, xmlNode, xnode);
xattr = xmlHasNsProp(xnode, (xmlChar*) StringValuePtr(name),
(xmlChar*) StringValuePtr(namespace));
if (!xattr)
return Qnil;
else if (xattr->type == XML_ATTRIBUTE_DECL)
return rxml_attr_decl_wrap((xmlAttributePtr)xattr);
else
return rxml_attr_wrap(xattr);
}
/*
* call-seq:
* attributes["name"] -> String
*
* Fetches an attribute value. If you want to access the underlying
* Attribute itself use get_attribute.
*
* name: The name of the attribute, not including any namespaces.
*
* doc.root.attributes['att'] -> 'some value'
*/
VALUE rxml_attributes_attribute_get(VALUE self, VALUE name)
{
VALUE xattr = rxml_attributes_get_attribute(self, name);
if (NIL_P(xattr))
return Qnil;
else
return rxml_attr_value_get(xattr);
}
/*
* call-seq:
* attributes["name"] = "value"
*
* Sets an attribute value. If you want to get the Attribute itself,
* use get_attribute.
*
* name: The name of the attribute, not including any namespaces.
* value: The new value of the namespace.
*
* doc.root.attributes['att'] = 'some value'
*/
VALUE rxml_attributes_attribute_set(VALUE self, VALUE name, VALUE value)
{
VALUE xattr = rxml_attributes_get_attribute(self, name);
if (NIL_P(xattr))
{
VALUE args[3];
args[0] = rxml_attributes_node_get(self);
args[1] = name;
args[2] = value;
return rb_class_new_instance(sizeof(args)/sizeof(VALUE), args, cXMLAttr);
}
else
{
return rxml_attr_value_set(xattr, value);
}
}
/*
* call-seq:
* attributes.each {block} -> XML::Attr
*
* Iterates over each attribute.
*
* doc.root.attributes.each {|attribute| puts attribute.name}
*/
static VALUE rxml_attributes_each(VALUE self)
{
xmlNodePtr xnode;
xmlAttrPtr xattr;
Data_Get_Struct(self, xmlNode, xnode);
xattr = xnode->properties;
while (xattr)
{
/* Get the next attribute while we still can - the user
may remove the yielded attribute. */
xmlAttrPtr next = xattr->next;
VALUE attr = rxml_attr_wrap(xattr);
rb_yield(attr);
xattr = next;
}
return self;
}
/*
* call-seq:
* attributes.length -> Integer
*
* Returns the number of attributes.
*
* doc.root.attributes.length
*/
static VALUE rxml_attributes_length(VALUE self)
{
int length = 0;
xmlNodePtr xnode;
xmlAttrPtr xattr;
Data_Get_Struct(self, xmlNode, xnode);
xattr = xnode->properties;
while (xattr)
{
length++;
xattr = xattr->next;
}
return INT2NUM(length);
}
/*
* call-seq:
* attributes.first -> XML::Attr
*
* Returns the first attribute.
*
* doc.root.attributes.first
*/
static VALUE rxml_attributes_first(VALUE self)
{
xmlNodePtr xnode;
Data_Get_Struct(self, xmlNode, xnode);
if (xnode->type == XML_ELEMENT_NODE)
{
xmlAttrPtr xattr = xnode->properties;
if (xattr)
{
return rxml_attr_wrap(xattr);
}
}
return Qnil;
}
void rxml_init_attributes(void)
{
cXMLAttributes = rb_define_class_under(mXML, "Attributes", rb_cObject);
rb_include_module(cXMLAttributes, rb_mEnumerable);
rb_define_method(cXMLAttributes, "node", rxml_attributes_node_get, 0);
rb_define_method(cXMLAttributes, "get_attribute", rxml_attributes_get_attribute, 1);
rb_define_method(cXMLAttributes, "get_attribute_ns", rxml_attributes_get_attribute_ns, 2);
rb_define_method(cXMLAttributes, "[]", rxml_attributes_attribute_get, 1);
rb_define_method(cXMLAttributes, "[]=", rxml_attributes_attribute_set, 2);
rb_define_method(cXMLAttributes, "each", rxml_attributes_each, 0);
rb_define_method(cXMLAttributes, "length", rxml_attributes_length, 0);
rb_define_method(cXMLAttributes, "first", rxml_attributes_first, 0);
}
|