libxml-ruby 4.1.0 → 4.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,275 +1,276 @@
1
- /* Please see the LICENSE file for copyright and distribution information */
2
-
3
- /*
4
- * Document-class: LibXML::XML::Attributes
5
- *
6
- * Provides access to an element's attributes (XML::Attr).
7
- *
8
- * Basic Usage:
9
- * require 'test_helper'
10
- *
11
- * doc = XML::Document.new(<some_file>)
12
- * attributes = doc.root.attributes
13
- *
14
- * attributes.each do |attribute|
15
- * ..
16
- * end
17
- *
18
- * attributes['foo'] = 'bar'
19
- * attribute = attributes.get_attribute['foo']
20
- * attribute.value == 'foo'
21
- *
22
- * To access a namespaced attribute:
23
- *
24
- * XLINK_URI = 'http://www.w3.org/1999/xlink'
25
- *
26
- * attribute = attributes.get_attribute_ns(XLINK_URI, 'title')
27
- * attribute.value = 'My title'
28
- */
29
-
30
- #include "ruby_libxml.h"
31
- #include "ruby_xml_attributes.h"
32
-
33
- VALUE cXMLAttributes;
34
-
35
- void rxml_attributes_mark(xmlNodePtr xnode)
36
- {
37
- rxml_node_mark(xnode);
38
- }
39
-
40
- /*
41
- * Creates a new attributes instance. Not exposed to ruby.
42
- */
43
- VALUE rxml_attributes_new(xmlNodePtr xnode)
44
- {
45
- return Data_Wrap_Struct(cXMLAttributes, rxml_attributes_mark, NULL, xnode);
46
- }
47
-
48
- /*
49
- * call-seq:
50
- * attributes.node -> XML::Node
51
- *
52
- * Return the node that owns this attributes list.
53
- *
54
- * doc.root.attributes.node == doc.root
55
- */
56
- VALUE rxml_attributes_node_get(VALUE self)
57
- {
58
- xmlNodePtr xnode;
59
- Data_Get_Struct(self, xmlNode, xnode);
60
- return rxml_node_wrap(xnode);
61
- }
62
-
63
- /*
64
- * call-seq:
65
- * attributes.get_attribute("name") -> (XML::Attr | XML::AtrrDecl)
66
- *
67
- * Returns the specified attribute. If the attribute does not
68
- * exist but the document has an associated DTD that defines
69
- * a default value for the attribute, then a XML::AttrDecl is
70
- * returned.
71
- *
72
- * name: The name of the attribute, not including a namespace.
73
- *
74
- * doc.root.attributes.get_attribute("foo")
75
- */
76
- static VALUE rxml_attributes_get_attribute(VALUE self, VALUE name)
77
- {
78
- xmlNodePtr xnode;
79
- xmlAttrPtr xattr;
80
-
81
- name = rb_obj_as_string(name);
82
-
83
- Data_Get_Struct(self, xmlNode, xnode);
84
-
85
- xattr = xmlHasProp(xnode, (xmlChar*) StringValuePtr(name));
86
-
87
- if (!xattr)
88
- return Qnil;
89
- else if (xattr->type == XML_ATTRIBUTE_DECL)
90
- return rxml_attr_decl_wrap((xmlAttributePtr)xattr);
91
- else
92
- return rxml_attr_wrap(xattr);
93
- }
94
-
95
- /*
96
- * call-seq:
97
- * attributes.get_attribute_ns("namespace", "name") -> (XML::Attr | XML::AtrrDecl)
98
- *
99
- * Returns the specified attribute. If the attribute does not
100
- * exist but the document has an associated DTD that defines
101
- * a default value for the attribute, then a XML::AttrDecl is
102
- * returned.
103
- *
104
- * namespace: The URI of the attribute's namespace.
105
- * name: The name of the attribute, not including a namespace.
106
- *
107
- * doc.root.attributes.get_attribute_ns('http://www.w3.org/1999/xlink', 'href')
108
- */
109
- static VALUE rxml_attributes_get_attribute_ns(VALUE self, VALUE namespace,
110
- VALUE name)
111
- {
112
- xmlNodePtr xnode;
113
- xmlAttrPtr xattr;
114
-
115
- name = rb_obj_as_string(name);
116
-
117
- Data_Get_Struct(self, xmlNode, xnode);
118
-
119
- xattr = xmlHasNsProp(xnode, (xmlChar*) StringValuePtr(name),
120
- (xmlChar*) StringValuePtr(namespace));
121
-
122
- if (!xattr)
123
- return Qnil;
124
- else if (xattr->type == XML_ATTRIBUTE_DECL)
125
- return rxml_attr_decl_wrap((xmlAttributePtr)xattr);
126
- else
127
- return rxml_attr_wrap(xattr);
128
- }
129
-
130
- /*
131
- * call-seq:
132
- * attributes["name"] -> String
133
- *
134
- * Fetches an attribute value. If you want to access the underlying
135
- * Attribute itself use get_attribute.
136
- *
137
- * name: The name of the attribute, not including any namespaces.
138
- *
139
- * doc.root.attributes['att'] -> 'some value'
140
- */
141
- VALUE rxml_attributes_attribute_get(VALUE self, VALUE name)
142
- {
143
- VALUE xattr = rxml_attributes_get_attribute(self, name);
144
-
145
- if (NIL_P(xattr))
146
- return Qnil;
147
- else
148
- return rxml_attr_value_get(xattr);
149
- }
150
-
151
- /*
152
- * call-seq:
153
- * attributes["name"] = "value"
154
- *
155
- * Sets an attribute value. If you want to get the Attribute itself,
156
- * use get_attribute.
157
- *
158
- * name: The name of the attribute, not including any namespaces.
159
- * value: The new value of the namespace.
160
- *
161
- * doc.root.attributes['att'] = 'some value'
162
- */
163
- VALUE rxml_attributes_attribute_set(VALUE self, VALUE name, VALUE value)
164
- {
165
- VALUE xattr = rxml_attributes_get_attribute(self, name);
166
- if (NIL_P(xattr))
167
- {
168
- VALUE args[3];
169
-
170
- args[0] = rxml_attributes_node_get(self);
171
- args[1] = name;
172
- args[2] = value;
173
-
174
- return rb_class_new_instance(sizeof(args)/sizeof(VALUE), args, cXMLAttr);
175
- }
176
- else
177
- {
178
- return rxml_attr_value_set(xattr, value);
179
- }
180
- }
181
-
182
- /*
183
- * call-seq:
184
- * attributes.each {block} -> XML::Attr
185
- *
186
- * Iterates over each attribute.
187
- *
188
- * doc.root.attributes.each {|attribute| puts attribute.name}
189
- */
190
- static VALUE rxml_attributes_each(VALUE self)
191
- {
192
- xmlNodePtr xnode;
193
- xmlAttrPtr xattr;
194
- Data_Get_Struct(self, xmlNode, xnode);
195
-
196
- xattr = xnode->properties;
197
-
198
- while (xattr)
199
- {
200
- /* Get the next attribute while we still can - the user
201
- may remove the yielded attribute. */
202
- xmlAttrPtr next = xattr->next;
203
-
204
- VALUE attr = rxml_attr_wrap(xattr);
205
- rb_yield(attr);
206
- xattr = next;
207
- }
208
-
209
- return self;
210
- }
211
-
212
- /*
213
- * call-seq:
214
- * attributes.length -> Integer
215
- *
216
- * Returns the number of attributes.
217
- *
218
- * doc.root.attributes.length
219
- */
220
- static VALUE rxml_attributes_length(VALUE self)
221
- {
222
- int length = 0;
223
- xmlNodePtr xnode;
224
- xmlAttrPtr xattr;
225
- Data_Get_Struct(self, xmlNode, xnode);
226
-
227
- xattr = xnode->properties;
228
-
229
- while (xattr)
230
- {
231
- length++;
232
- xattr = xattr->next;
233
- }
234
-
235
- return INT2NUM(length);
236
- }
237
-
238
- /*
239
- * call-seq:
240
- * attributes.first -> XML::Attr
241
- *
242
- * Returns the first attribute.
243
- *
244
- * doc.root.attributes.first
245
- */
246
- static VALUE rxml_attributes_first(VALUE self)
247
- {
248
- xmlNodePtr xnode;
249
- Data_Get_Struct(self, xmlNode, xnode);
250
-
251
- if (xnode->type == XML_ELEMENT_NODE)
252
- {
253
- xmlAttrPtr xattr = xnode->properties;
254
-
255
- if (xattr)
256
- {
257
- return rxml_attr_wrap(xattr);
258
- }
259
- }
260
- return Qnil;
261
- }
262
-
263
- void rxml_init_attributes(void)
264
- {
265
- cXMLAttributes = rb_define_class_under(mXML, "Attributes", rb_cObject);
266
- rb_include_module(cXMLAttributes, rb_mEnumerable);
267
- rb_define_method(cXMLAttributes, "node", rxml_attributes_node_get, 0);
268
- rb_define_method(cXMLAttributes, "get_attribute", rxml_attributes_get_attribute, 1);
269
- rb_define_method(cXMLAttributes, "get_attribute_ns", rxml_attributes_get_attribute_ns, 2);
270
- rb_define_method(cXMLAttributes, "[]", rxml_attributes_attribute_get, 1);
271
- rb_define_method(cXMLAttributes, "[]=", rxml_attributes_attribute_set, 2);
272
- rb_define_method(cXMLAttributes, "each", rxml_attributes_each, 0);
273
- rb_define_method(cXMLAttributes, "length", rxml_attributes_length, 0);
274
- rb_define_method(cXMLAttributes, "first", rxml_attributes_first, 0);
275
- }
1
+ /* Please see the LICENSE file for copyright and distribution information */
2
+
3
+ /*
4
+ * Document-class: LibXML::XML::Attributes
5
+ *
6
+ * Provides access to an element's attributes (XML::Attr).
7
+ *
8
+ * Basic Usage:
9
+ * require 'test_helper'
10
+ *
11
+ * doc = XML::Document.new(<some_file>)
12
+ * attributes = doc.root.attributes
13
+ *
14
+ * attributes.each do |attribute|
15
+ * ..
16
+ * end
17
+ *
18
+ * attributes['foo'] = 'bar'
19
+ * attribute = attributes.get_attribute['foo']
20
+ * attribute.value == 'foo'
21
+ *
22
+ * To access a namespaced attribute:
23
+ *
24
+ * XLINK_URI = 'http://www.w3.org/1999/xlink'
25
+ *
26
+ * attribute = attributes.get_attribute_ns(XLINK_URI, 'title')
27
+ * attribute.value = 'My title'
28
+ */
29
+
30
+ #include "ruby_libxml.h"
31
+ #include "ruby_xml_attributes.h"
32
+
33
+ VALUE cXMLAttributes;
34
+
35
+ void rxml_attributes_mark(xmlNodePtr xnode)
36
+ {
37
+ rxml_node_mark(xnode);
38
+ }
39
+
40
+ /*
41
+ * Creates a new attributes instance. Not exposed to ruby.
42
+ */
43
+ VALUE rxml_attributes_new(xmlNodePtr xnode)
44
+ {
45
+ return Data_Wrap_Struct(cXMLAttributes, rxml_attributes_mark, NULL, xnode);
46
+ }
47
+
48
+ /*
49
+ * call-seq:
50
+ * attributes.node -> XML::Node
51
+ *
52
+ * Return the node that owns this attributes list.
53
+ *
54
+ * doc.root.attributes.node == doc.root
55
+ */
56
+ VALUE rxml_attributes_node_get(VALUE self)
57
+ {
58
+ xmlNodePtr xnode;
59
+ Data_Get_Struct(self, xmlNode, xnode);
60
+ return rxml_node_wrap(xnode);
61
+ }
62
+
63
+ /*
64
+ * call-seq:
65
+ * attributes.get_attribute("name") -> (XML::Attr | XML::AtrrDecl)
66
+ *
67
+ * Returns the specified attribute. If the attribute does not
68
+ * exist but the document has an associated DTD that defines
69
+ * a default value for the attribute, then a XML::AttrDecl is
70
+ * returned.
71
+ *
72
+ * name: The name of the attribute, not including a namespace.
73
+ *
74
+ * doc.root.attributes.get_attribute("foo")
75
+ */
76
+ static VALUE rxml_attributes_get_attribute(VALUE self, VALUE name)
77
+ {
78
+ xmlNodePtr xnode;
79
+ xmlAttrPtr xattr;
80
+
81
+ name = rb_obj_as_string(name);
82
+
83
+ Data_Get_Struct(self, xmlNode, xnode);
84
+
85
+ xattr = xmlHasProp(xnode, (xmlChar*) StringValuePtr(name));
86
+
87
+ if (!xattr)
88
+ return Qnil;
89
+ else if (xattr->type == XML_ATTRIBUTE_DECL)
90
+ return rxml_attr_decl_wrap((xmlAttributePtr)xattr);
91
+ else
92
+ return rxml_attr_wrap(xattr);
93
+ }
94
+
95
+ /*
96
+ * call-seq:
97
+ * attributes.get_attribute_ns("namespace", "name") -> (XML::Attr | XML::AtrrDecl)
98
+ *
99
+ * Returns the specified attribute. If the attribute does not
100
+ * exist but the document has an associated DTD that defines
101
+ * a default value for the attribute, then a XML::AttrDecl is
102
+ * returned.
103
+ *
104
+ * namespace: The URI of the attribute's namespace.
105
+ * name: The name of the attribute, not including a namespace.
106
+ *
107
+ * doc.root.attributes.get_attribute_ns('http://www.w3.org/1999/xlink', 'href')
108
+ */
109
+ static VALUE rxml_attributes_get_attribute_ns(VALUE self, VALUE namespace,
110
+ VALUE name)
111
+ {
112
+ xmlNodePtr xnode;
113
+ xmlAttrPtr xattr;
114
+
115
+ name = rb_obj_as_string(name);
116
+
117
+ Data_Get_Struct(self, xmlNode, xnode);
118
+
119
+ xattr = xmlHasNsProp(xnode, (xmlChar*) StringValuePtr(name),
120
+ (xmlChar*) StringValuePtr(namespace));
121
+
122
+ if (!xattr)
123
+ return Qnil;
124
+ else if (xattr->type == XML_ATTRIBUTE_DECL)
125
+ return rxml_attr_decl_wrap((xmlAttributePtr)xattr);
126
+ else
127
+ return rxml_attr_wrap(xattr);
128
+ }
129
+
130
+ /*
131
+ * call-seq:
132
+ * attributes["name"] -> String
133
+ *
134
+ * Fetches an attribute value. If you want to access the underlying
135
+ * Attribute itself use get_attribute.
136
+ *
137
+ * name: The name of the attribute, not including any namespaces.
138
+ *
139
+ * doc.root.attributes['att'] -> 'some value'
140
+ */
141
+ VALUE rxml_attributes_attribute_get(VALUE self, VALUE name)
142
+ {
143
+ VALUE xattr = rxml_attributes_get_attribute(self, name);
144
+
145
+ if (NIL_P(xattr))
146
+ return Qnil;
147
+ else
148
+ return rxml_attr_value_get(xattr);
149
+ }
150
+
151
+ /*
152
+ * call-seq:
153
+ * attributes["name"] = "value"
154
+ *
155
+ * Sets an attribute value. If you want to get the Attribute itself,
156
+ * use get_attribute.
157
+ *
158
+ * name: The name of the attribute, not including any namespaces.
159
+ * value: The new value of the namespace.
160
+ *
161
+ * doc.root.attributes['att'] = 'some value'
162
+ */
163
+ VALUE rxml_attributes_attribute_set(VALUE self, VALUE name, VALUE value)
164
+ {
165
+ VALUE xattr = rxml_attributes_get_attribute(self, name);
166
+ if (NIL_P(xattr))
167
+ {
168
+ VALUE args[3];
169
+
170
+ args[0] = rxml_attributes_node_get(self);
171
+ args[1] = name;
172
+ args[2] = value;
173
+
174
+ return rb_class_new_instance(sizeof(args)/sizeof(VALUE), args, cXMLAttr);
175
+ }
176
+ else
177
+ {
178
+ return rxml_attr_value_set(xattr, value);
179
+ }
180
+ }
181
+
182
+ /*
183
+ * call-seq:
184
+ * attributes.each {block} -> XML::Attr
185
+ *
186
+ * Iterates over each attribute.
187
+ *
188
+ * doc.root.attributes.each {|attribute| puts attribute.name}
189
+ */
190
+ static VALUE rxml_attributes_each(VALUE self)
191
+ {
192
+ xmlNodePtr xnode;
193
+ xmlAttrPtr xattr;
194
+ Data_Get_Struct(self, xmlNode, xnode);
195
+
196
+ xattr = xnode->properties;
197
+
198
+ while (xattr)
199
+ {
200
+ /* Get the next attribute while we still can - the user
201
+ may remove the yielded attribute. */
202
+ xmlAttrPtr next = xattr->next;
203
+
204
+ VALUE attr = rxml_attr_wrap(xattr);
205
+ rb_yield(attr);
206
+ xattr = next;
207
+ }
208
+
209
+ return self;
210
+ }
211
+
212
+ /*
213
+ * call-seq:
214
+ * attributes.length -> Integer
215
+ *
216
+ * Returns the number of attributes.
217
+ *
218
+ * doc.root.attributes.length
219
+ */
220
+ static VALUE rxml_attributes_length(VALUE self)
221
+ {
222
+ int length = 0;
223
+ xmlNodePtr xnode;
224
+ xmlAttrPtr xattr;
225
+ Data_Get_Struct(self, xmlNode, xnode);
226
+
227
+ xattr = xnode->properties;
228
+
229
+ while (xattr)
230
+ {
231
+ length++;
232
+ xattr = xattr->next;
233
+ }
234
+
235
+ return INT2NUM(length);
236
+ }
237
+
238
+ /*
239
+ * call-seq:
240
+ * attributes.first -> XML::Attr
241
+ *
242
+ * Returns the first attribute.
243
+ *
244
+ * doc.root.attributes.first
245
+ */
246
+ static VALUE rxml_attributes_first(VALUE self)
247
+ {
248
+ xmlNodePtr xnode;
249
+ Data_Get_Struct(self, xmlNode, xnode);
250
+
251
+ if (xnode->type == XML_ELEMENT_NODE)
252
+ {
253
+ xmlAttrPtr xattr = xnode->properties;
254
+
255
+ if (xattr)
256
+ {
257
+ return rxml_attr_wrap(xattr);
258
+ }
259
+ }
260
+ return Qnil;
261
+ }
262
+
263
+ void rxml_init_attributes(void)
264
+ {
265
+ cXMLAttributes = rb_define_class_under(mXML, "Attributes", rb_cObject);
266
+ rb_undef_alloc_func(cXMLAttributes);
267
+ rb_include_module(cXMLAttributes, rb_mEnumerable);
268
+ rb_define_method(cXMLAttributes, "node", rxml_attributes_node_get, 0);
269
+ rb_define_method(cXMLAttributes, "get_attribute", rxml_attributes_get_attribute, 1);
270
+ rb_define_method(cXMLAttributes, "get_attribute_ns", rxml_attributes_get_attribute_ns, 2);
271
+ rb_define_method(cXMLAttributes, "[]", rxml_attributes_attribute_get, 1);
272
+ rb_define_method(cXMLAttributes, "[]=", rxml_attributes_attribute_set, 2);
273
+ rb_define_method(cXMLAttributes, "each", rxml_attributes_each, 0);
274
+ rb_define_method(cXMLAttributes, "length", rxml_attributes_length, 0);
275
+ rb_define_method(cXMLAttributes, "first", rxml_attributes_first, 0);
276
+ }
@@ -44,7 +44,7 @@ static ID ERROR_HANDLER_ID;
44
44
  * Returns the proc that will be called when libxml generates
45
45
  * warning, error or fatal error messages.
46
46
  */
47
- static VALUE rxml_error_get_handler(void)
47
+ static VALUE rxml_error_get_handler(VALUE self)
48
48
  {
49
49
  VALUE block = rb_cvar_get(eXMLError, ERROR_HANDLER_ID);
50
50
  return block;
@@ -102,7 +102,7 @@ static void structuredErrorFunc(void *userData, xmlErrorPtr xerror)
102
102
  VALUE error = rxml_error_wrap(xerror);
103
103
 
104
104
  /* Wrap error up as Ruby object and send it off to ruby */
105
- VALUE block = rxml_error_get_handler();
105
+ VALUE block = rxml_error_get_handler(error);
106
106
 
107
107
  /* Now call global handler */
108
108
  if (block != Qnil)
@@ -243,7 +243,6 @@ static VALUE rxml_html_parser_context_string(VALUE klass, VALUE string)
243
243
  sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
244
244
  htmlCtxtUseOptions(ctxt, rxml_libxml_default_options());
245
245
 
246
- htmlDefaultSAXHandlerInit();
247
246
  if (ctxt->sax != NULL)
248
247
  memcpy(ctxt->sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
249
248
 
@@ -88,7 +88,7 @@ int ic_close(void *context)
88
88
  *
89
89
  * Register a new set of I/O callback for handling parser input.
90
90
  */
91
- static VALUE input_callbacks_register_input_callbacks(void)
91
+ static VALUE input_callbacks_register_input_callbacks(VALUE self)
92
92
  {
93
93
  xmlRegisterInputCallbacks(ic_match, ic_open, ic_read, ic_close);
94
94
  return (Qtrue);
@@ -182,10 +182,7 @@ void rxml_init_input_callbacks(void)
182
182
  cInputCallbacks = rb_define_class_under(mXML, "InputCallbacks", rb_cObject);
183
183
 
184
184
  /* Class Methods */
185
- rb_define_singleton_method(cInputCallbacks, "register",
186
- input_callbacks_register_input_callbacks, 0);
187
- rb_define_singleton_method(cInputCallbacks, "add_scheme",
188
- input_callbacks_add_scheme, 2);
189
- rb_define_singleton_method(cInputCallbacks, "remove_scheme",
190
- input_callbacks_remove_scheme, 1);
185
+ rb_define_singleton_method(cInputCallbacks, "register", input_callbacks_register_input_callbacks, 0);
186
+ rb_define_singleton_method(cInputCallbacks, "add_scheme", input_callbacks_add_scheme, 2);
187
+ rb_define_singleton_method(cInputCallbacks, "remove_scheme", input_callbacks_remove_scheme, 1);
191
188
  }