libxml-ruby 0.5.1.0 → 0.5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/ext/xml/libxml.c +2 -1
  2. data/ext/xml/libxml.h +5 -3
  3. data/ext/xml/libxml.rb +1 -1
  4. data/ext/xml/ruby_xml_attr.c +13 -33
  5. data/ext/xml/ruby_xml_document.c +11 -22
  6. data/ext/xml/ruby_xml_document.h +2 -1
  7. data/ext/xml/ruby_xml_html_parser.c +3 -6
  8. data/ext/xml/ruby_xml_html_parser.h +1 -1
  9. data/ext/xml/ruby_xml_node.c +87 -70
  10. data/ext/xml/ruby_xml_node.h +2 -1
  11. data/ext/xml/ruby_xml_node_set.c +32 -111
  12. data/ext/xml/ruby_xml_node_set.h +5 -11
  13. data/ext/xml/ruby_xml_ns.c +1 -1
  14. data/ext/xml/ruby_xml_ns.h +1 -1
  15. data/ext/xml/ruby_xml_parser.c +11 -11
  16. data/ext/xml/ruby_xml_parser.h +1 -1
  17. data/ext/xml/ruby_xml_parser_context.c +11 -9
  18. data/ext/xml/ruby_xml_parser_context.h +1 -1
  19. data/ext/xml/ruby_xml_sax_parser.c +1 -1
  20. data/ext/xml/ruby_xml_sax_parser.h +1 -1
  21. data/ext/xml/ruby_xml_state.c +114 -0
  22. data/ext/xml/ruby_xml_state.h +11 -0
  23. data/ext/xml/ruby_xml_tree.c +1 -1
  24. data/ext/xml/ruby_xml_tree.h +1 -1
  25. data/ext/xml/ruby_xml_xinclude.c +1 -1
  26. data/ext/xml/ruby_xml_xinclude.h +1 -1
  27. data/ext/xml/ruby_xml_xpath.c +117 -231
  28. data/ext/xml/ruby_xml_xpath.h +4 -5
  29. data/ext/xml/ruby_xml_xpath_context.c +43 -50
  30. data/ext/xml/ruby_xml_xpath_context.h +3 -7
  31. data/ext/xml/ruby_xml_xpath_object.c +246 -0
  32. data/ext/xml/ruby_xml_xpath_object.h +29 -0
  33. data/ext/xml/ruby_xml_xpointer.c +8 -14
  34. data/ext/xml/ruby_xml_xpointer.h +1 -1
  35. data/ext/xml/ruby_xml_xpointer_context.c +1 -1
  36. data/ext/xml/ruby_xml_xpointer_context.h +1 -1
  37. data/ext/xml/sax_parser_callbacks.inc +1 -1
  38. data/tests/tc_xml_document.rb +5 -4
  39. data/tests/tc_xml_html_parser.rb +7 -4
  40. data/tests/tc_xml_node.rb +6 -5
  41. data/tests/tc_xml_node_set.rb +2 -2
  42. data/tests/tc_xml_node_set2.rb +3 -3
  43. data/tests/tc_xml_xpath.rb +3 -3
  44. data/tests/tc_xml_xpointer.rb +2 -2
  45. metadata +16 -10
@@ -1,4 +1,4 @@
1
- /* $Id: ruby_xml_xpath.h 39 2006-02-21 20:40:16Z roscopeco $ */
1
+ /* $Id: ruby_xml_xpath.h 190 2007-09-28 17:13:52Z danj $ */
2
2
 
3
3
  /* Please see the LICENSE file for copyright and distribution information */
4
4
 
@@ -15,10 +15,9 @@ typedef struct ruby_xml_xpath {
15
15
  } ruby_xml_xpath;
16
16
 
17
17
  void ruby_xml_xpath_free(ruby_xml_xpath *rxxp);
18
- VALUE ruby_xml_xpath_find(int argc, VALUE *argv, VALUE class);
19
- VALUE ruby_xml_xpath_find2(int argc, VALUE *argv);
20
- VALUE ruby_xml_xpath_new(VALUE class, VALUE xd, VALUE xxpc,
21
- xmlXPathObjectPtr xpop);
18
+ VALUE ruby_xml_xpath_find(VALUE class, VALUE anode, VALUE xpath_expr, VALUE nslist);
19
+ VALUE ruby_xml_xpath_find2(VALUE anode, VALUE xpath_expr, VALUE nslist);
20
+
22
21
  void ruby_init_xml_xpath(void);
23
22
 
24
23
  #endif
@@ -1,4 +1,4 @@
1
- /* $Id: ruby_xml_xpath_context.c 138 2007-08-29 18:00:35Z danj $ */
1
+ /* $Id: ruby_xml_xpath_context.c 188 2007-09-24 01:43:21Z danj $ */
2
2
 
3
3
  /* Please see the LICENSE file for copyright and distribution information */
4
4
 
@@ -15,76 +15,68 @@ VALUE cXMLXPathContext;
15
15
  */
16
16
  VALUE
17
17
  ruby_xml_xpath_context_doc_get(VALUE self) {
18
- ruby_xml_xpath_context *rxxpc;
19
- Data_Get_Struct(self, ruby_xml_xpath_context, rxxpc);
18
+ xmlXPathContextPtr ctxt;
19
+ Data_Get_Struct(self, xmlXPathContext, ctxt);
20
20
 
21
- return(rxxpc->xd);
21
+ return ruby_xml_document_wrap2(ctxt->doc);
22
22
  }
23
23
 
24
24
 
25
25
  void
26
- ruby_xml_xpath_context_free(ruby_xml_xpath_context *rxxpc) {
27
- if (rxxpc->ctxt != NULL) {
28
- xmlXPathFreeContext(rxxpc->ctxt);
29
- rxxpc->ctxt = NULL;
26
+ ruby_xml_xpath_context_free(xmlXPathContextPtr ctxt) {
27
+ if (ctxt != NULL) {
28
+ xmlXPathFreeContext(ctxt);
29
+ ctxt = NULL;
30
30
  }
31
-
32
- free(rxxpc);
33
31
  }
34
32
 
35
33
 
36
34
  void
37
- ruby_xml_xpath_context_mark(ruby_xml_xpath_context *rxxpc) {
38
- if (rxxpc == NULL) return;
39
- if (!NIL_P(rxxpc->xd)) rb_gc_mark(rxxpc->xd);
40
- }
41
-
42
-
43
- VALUE
44
- ruby_xml_xpath_context_new(VALUE class, VALUE xd,
45
- xmlXPathContextPtr xxpc) {
46
- ruby_xml_xpath_context *rxxpc;
47
-
48
- rxxpc = ALLOC(ruby_xml_xpath_context);
49
- rxxpc->ctxt = xxpc;
50
- rxxpc->xd = xd;
51
- return(Data_Wrap_Struct(class, ruby_xml_xpath_context_mark,
52
- ruby_xml_xpath_context_free, rxxpc));
35
+ ruby_xml_xpath_context_mark(xmlXPathContextPtr ctxt) {
36
+ if (ctxt == NULL ) return;
37
+ if (ctxt->doc != NULL && ctxt->doc->_private != NULL)
38
+ rb_gc_mark((VALUE)ctxt->doc->_private);
53
39
  }
54
40
 
55
41
 
56
42
  VALUE
57
- ruby_xml_xpath_context_new2(VALUE xd, xmlXPathContextPtr xxpc) {
58
- return(ruby_xml_xpath_context_new(cXMLXPathContext, xd, xxpc));
43
+ ruby_xml_xpath_context_wrap(xmlXPathContextPtr ctxt) {
44
+ return Data_Wrap_Struct(cXMLXPathContext,
45
+ ruby_xml_xpath_context_mark,
46
+ ruby_xml_xpath_context_free,
47
+ ctxt);
59
48
  }
60
49
 
61
50
 
62
51
  VALUE
63
- ruby_xml_xpath_context_new3(VALUE xd) {
52
+ ruby_xml_xpath_context_new(VALUE anode) {
64
53
  ruby_xml_document_t *rxd;
54
+ ruby_xml_node *node;
65
55
  xmlXPathContextPtr ctxt;
56
+
57
+ if (rb_obj_is_kind_of(anode,cXMLDocument) == Qtrue ) {
58
+ Data_Get_Struct(anode,ruby_xml_document_t,rxd);
59
+ if (rxd->doc == NULL) return(Qnil);
66
60
 
67
- Data_Get_Struct(xd, ruby_xml_document_t, rxd);
68
- if (rxd->doc == NULL)
69
- return(Qnil);
61
+ ctxt = xmlXPathNewContext(rxd->doc);
62
+ if (ctxt == NULL) return(Qnil);
70
63
 
71
- ctxt = xmlXPathNewContext(rxd->doc);
72
- if (ctxt == NULL)
73
- return(Qnil);
64
+ } else if (rb_obj_is_kind_of(anode,cXMLNode) == Qtrue ) {
65
+ Data_Get_Struct(anode, ruby_xml_node, node);
66
+ if (node->node->doc == NULL)
67
+ rb_raise(rb_eTypeError,"Supplied node must be part of a document");
74
68
 
75
- return(ruby_xml_xpath_context_new2(xd, ctxt));
76
- }
69
+ ctxt = xmlXPathNewContext(node->node->doc);
70
+ if (ctxt == NULL) return(Qnil);
77
71
 
72
+ } else {
73
+ rb_raise(rb_eTypeError,"create context requires a document or node. Supplied a %s?",
74
+ rb_obj_as_string(anode));
75
+ }
78
76
 
79
- VALUE
80
- ruby_xml_xpath_context_new4(VALUE rnode) {
81
- ruby_xml_node *node;
82
-
83
- Data_Get_Struct(rnode, ruby_xml_node, node);
84
- return ruby_xml_xpath_context_new3(ruby_xml_document_wrap(cXMLDocument,node->node->doc));
77
+ return ruby_xml_xpath_context_wrap(ctxt);
85
78
  }
86
79
 
87
-
88
80
  /*
89
81
  * call-seq:
90
82
  * context.register_namespace(prefix, uri) => (true|false)
@@ -94,16 +86,17 @@ ruby_xml_xpath_context_new4(VALUE rnode) {
94
86
  */
95
87
  VALUE
96
88
  ruby_xml_xpath_context_register_namespace(VALUE self, VALUE prefix, VALUE uri) {
97
- ruby_xml_xpath_context *rxxpc;
89
+ xmlXPathContextPtr ctxt;
98
90
 
99
- Data_Get_Struct(self, ruby_xml_xpath_context, rxxpc);
100
- if (xmlXPathRegisterNs(rxxpc->ctxt,
91
+ Data_Get_Struct(self, xmlXPathContext, ctxt);
92
+ if (xmlXPathRegisterNs(ctxt,
101
93
  (xmlChar*)StringValuePtr(prefix),
102
- (xmlChar*)StringValuePtr(uri))
103
- == 0) {
94
+ (xmlChar*)StringValuePtr(uri))
95
+ == 0) {
104
96
  return(Qtrue);
105
97
  } else {
106
- /* Should raise an exception, IMHO */
98
+ /* Should raise an exception, IMHO (whose?, why shouldnt it? -danj)*/
99
+ rb_warning("register namespace failed");
107
100
  return(Qfalse);
108
101
  }
109
102
  }
@@ -1,4 +1,4 @@
1
- /* $Id: ruby_xml_xpath_context.h 41 2006-02-27 12:55:32Z roscopeco $ */
1
+ /* $Id: ruby_xml_xpath_context.h 188 2007-09-24 01:43:21Z danj $ */
2
2
 
3
3
  /* Please see the LICENSE file for copyright and distribution information */
4
4
 
@@ -12,12 +12,8 @@ typedef struct ruby_xml_xpath_context {
12
12
  xmlXPathContextPtr ctxt;
13
13
  } ruby_xml_xpath_context;
14
14
 
15
- void ruby_xml_xpath_context_free(ruby_xml_xpath_context *rxxpc);
16
- VALUE ruby_xml_xpath_context_new(VALUE class, VALUE xd,
17
- xmlXPathContextPtr ctxt);
18
- VALUE ruby_xml_xpath_context_new2(VALUE xd, xmlXPathContextPtr ctxt);
19
- VALUE ruby_xml_xpath_context_new3(VALUE xd);
20
- VALUE ruby_xml_xpath_context_new4(VALUE rnode);
15
+ VALUE ruby_xml_xpath_context_wrap(xmlXPathContextPtr ctxt);
16
+ VALUE ruby_xml_xpath_context_new(VALUE anode);
21
17
  VALUE ruby_xml_xpath_context_register_namespace(VALUE self, VALUE prefix, VALUE uri);
22
18
  void ruby_init_xml_xpath_context(void);
23
19
 
@@ -0,0 +1,246 @@
1
+ /* $Id: $ */
2
+
3
+ #include "libxml.h"
4
+
5
+ /*
6
+ * Document-class: XML::XPath::Object
7
+ *
8
+ * All XPath and XPointer evals result in this type.
9
+ * nodeset should become entirely virtual and refer back to
10
+ * this class/data structure.
11
+ *
12
+ */
13
+ VALUE cXMLXPathObject;
14
+
15
+ void
16
+ ruby_xml_xpath_object_mark(xmlXPathObjectPtr xpop)
17
+ {
18
+ void * xnp;
19
+ int i;
20
+
21
+ if ( xpop->type == XPATH_NODESET ) {
22
+ for (i=0; i<xpop->nodesetval->nodeNr; i++) {
23
+ xnp=xpop->nodesetval->nodeTab[i]->_private;
24
+ if (xnp != NULL)
25
+ rb_gc_mark((VALUE)xnp);
26
+ }
27
+ }
28
+ }
29
+
30
+ void
31
+ ruby_xml_xpath_object_free(xmlXPathObjectPtr xpop)
32
+ {
33
+ xmlXPathFreeObject(xpop);
34
+ }
35
+
36
+ VALUE
37
+ ruby_xml_xpath_object_wrap(xmlXPathObjectPtr xpop)
38
+ {
39
+ if ( xpop==NULL )
40
+ return Qnil;
41
+
42
+ return Data_Wrap_Struct(cXMLXPathObject,
43
+ ruby_xml_xpath_object_mark,
44
+ ruby_xml_xpath_object_free,
45
+ xpop);
46
+ }
47
+
48
+ /*
49
+ * call-seq:
50
+ * xpath_object.to_a => [node, ..., node]
51
+ *
52
+ * Obtain an array of the nodes in this set.
53
+ */
54
+ VALUE
55
+ ruby_xml_xpath_object_to_a(VALUE self)
56
+ {
57
+ VALUE set_ary, nodeobj;
58
+ xmlXPathObjectPtr xpop;
59
+ int i;
60
+
61
+ Data_Get_Struct(self,xmlXPathObject,xpop);
62
+
63
+ set_ary = rb_ary_new();
64
+ if (!((xpop->nodesetval == NULL) || (xpop->nodesetval->nodeNr == 0))) {
65
+ for (i = 0; i < xpop->nodesetval->nodeNr; i++) {
66
+ nodeobj = ruby_xml_node2_wrap(cXMLNode, xpop->nodesetval->nodeTab[i]);
67
+ rb_ary_push(set_ary, nodeobj);
68
+ }
69
+ }
70
+
71
+ return(set_ary);
72
+ }
73
+
74
+ /*
75
+ * call-seq:
76
+ * xpath_object.set => Node::Set
77
+ *
78
+ * Obtain the previous type object which is really
79
+ * just a proxy back to this object. Unless the
80
+ * type is not a NODESET, in which case it is nil.
81
+ */
82
+ VALUE
83
+ ruby_xml_xpath_object_set(VALUE self)
84
+ {
85
+ xmlXPathObjectPtr xpop;
86
+ VALUE r;
87
+
88
+ Data_Get_Struct(self,xmlXPathObject,xpop);
89
+ r=Qnil;
90
+
91
+ if (xpop->type == XPATH_NODESET)
92
+ r=ruby_xml_node_set_new2(self);
93
+
94
+ return r;
95
+ }
96
+
97
+ /*
98
+ * call-seq:
99
+ * xpath_object.empty? => (true|false)
100
+ *
101
+ * Determine whether this nodeset is empty (contains no nodes).
102
+ */
103
+ VALUE
104
+ ruby_xml_xpath_object_empty_q(VALUE self) {
105
+ xmlXPathObjectPtr xpop;
106
+
107
+ Data_Get_Struct(self,xmlXPathObject,xpop);
108
+
109
+ if (xpop->type != XPATH_NODESET)
110
+ return Qnil;
111
+
112
+ return ( xpop->nodesetval == NULL || xpop->nodesetval->nodeNr <= 0 ) ? Qtrue : Qfalse;
113
+ }
114
+
115
+ /*
116
+ * call-seq:
117
+ * xpath_object.each { |node| ... } => self
118
+ *
119
+ * Call the supplied block for each node in this set.
120
+ */
121
+ VALUE
122
+ ruby_xml_xpath_object_each(VALUE self)
123
+ {
124
+ xmlXPathObjectPtr xpop;
125
+ int i;
126
+ VALUE nodeobj;
127
+
128
+ if ( ruby_xml_xpath_object_empty_q(self) == Qtrue )
129
+ return Qnil;
130
+
131
+ Data_Get_Struct(self,xmlXPathObject,xpop);
132
+
133
+ for (i = 0; i < xpop->nodesetval->nodeNr; i++) {
134
+ switch(xpop->nodesetval->nodeTab[i]->type) {
135
+ case XML_ATTRIBUTE_NODE:
136
+ nodeobj = ruby_xml_attr_wrap(cXMLAttr,
137
+ (xmlAttrPtr)xpop->nodesetval->nodeTab[i]);
138
+ break;
139
+ default:
140
+ nodeobj = ruby_xml_node2_wrap(cXMLNode, xpop->nodesetval->nodeTab[i]);
141
+ }
142
+
143
+ rb_yield(nodeobj);
144
+ }
145
+ return(self);
146
+ }
147
+
148
+ /*
149
+ * call-seq:
150
+ * xpath_object.first => node
151
+ *
152
+ * Returns the first node in this node set, or nil if none exist.
153
+ */
154
+ VALUE
155
+ ruby_xml_xpath_object_first(VALUE self) {
156
+ VALUE nodeobj;
157
+
158
+ xmlXPathObjectPtr xpop;
159
+
160
+ if ( ruby_xml_xpath_object_empty_q(self) == Qtrue )
161
+ return Qnil;
162
+
163
+ Data_Get_Struct(self,xmlXPathObject,xpop);
164
+
165
+ switch(xpop->nodesetval->nodeTab[0]->type) {
166
+ case XML_ATTRIBUTE_NODE:
167
+ nodeobj = ruby_xml_attr_wrap(cXMLAttr,
168
+ (xmlAttrPtr)xpop->nodesetval->nodeTab[0]);
169
+ break;
170
+ default:
171
+ nodeobj = ruby_xml_node2_wrap(cXMLNode,
172
+ xpop->nodesetval->nodeTab[0]);
173
+ }
174
+
175
+ return(nodeobj);
176
+ }
177
+
178
+ /*
179
+ * call-seq:
180
+ * xpath_object.length => num
181
+ *
182
+ * Obtain the length of the nodesetval node list.
183
+ */
184
+ VALUE
185
+ ruby_xml_xpath_object_length(VALUE self) {
186
+ xmlXPathObjectPtr xpop;
187
+
188
+ if ( ruby_xml_xpath_object_empty_q(self) == Qtrue )
189
+ return INT2FIX(0);
190
+
191
+ Data_Get_Struct(self,xmlXPathObject,xpop);
192
+
193
+ return INT2NUM(xpop->nodesetval->nodeNr);
194
+ }
195
+
196
+ VALUE
197
+ ruby_xml_xpath_object_set_type(VALUE self)
198
+ {
199
+ xmlXPathObjectPtr xpop;
200
+
201
+ Data_Get_Struct(self,xmlXPathObject,xpop);
202
+
203
+ return INT2FIX(xpop->type);
204
+ }
205
+
206
+ VALUE
207
+ ruby_xml_xpath_object_string(VALUE self)
208
+ {
209
+ xmlXPathObjectPtr xpop;
210
+
211
+ Data_Get_Struct(self,xmlXPathObject,xpop);
212
+
213
+ if (xpop->stringval == NULL)
214
+ return Qnil;
215
+
216
+ return rb_str_new2((const char*) xpop->stringval);
217
+ }
218
+
219
+ // Rdoc needs to know
220
+ #ifdef RDOC_NEVER_DEFINED
221
+ mXML = rb_define_module("XML");
222
+ cXMLXPath = rb_define_class_under(mXML, "XPath", rb_cObject);
223
+ #endif
224
+
225
+ void
226
+ ruby_init_xml_xpath_object(void) {
227
+ cXMLXPathObject = rb_define_class_under(cXMLXPath, "Object", rb_cObject);
228
+
229
+ rb_include_module(cXMLXPathObject, rb_const_get(rb_cObject, rb_intern("Enumerable")));
230
+
231
+ rb_define_method(cXMLXPathObject, "each", ruby_xml_xpath_object_each, 0);
232
+ rb_define_method(cXMLXPathObject, "set_type", ruby_xml_xpath_object_set_type, 0);
233
+ rb_define_method(cXMLXPathObject, "empty?", ruby_xml_xpath_object_empty_q, 0);
234
+ rb_define_method(cXMLXPathObject, "first", ruby_xml_xpath_object_first, 0);
235
+ rb_define_method(cXMLXPathObject, "length", ruby_xml_xpath_object_length, 0);
236
+ rb_define_method(cXMLXPathObject, "size", ruby_xml_xpath_object_length, 0);
237
+ rb_define_method(cXMLXPathObject, "to_a", ruby_xml_xpath_object_to_a, 0);
238
+
239
+ rb_define_method(cXMLXPathObject, "string", ruby_xml_xpath_object_string, 0);
240
+
241
+ rb_include_module(cXMLNodeSet, rb_const_get(rb_cObject, rb_intern("Enumerable")));
242
+
243
+ /* Give the NodeSet type, but it is pointless */
244
+ rb_define_method(cXMLXPathObject, "set", ruby_xml_xpath_object_set, 0);
245
+
246
+ }
@@ -0,0 +1,29 @@
1
+ /* $Id $ */
2
+
3
+ #ifndef __RUBY_XML_XPATH_OBJECT__
4
+ #define __RUBY_XML_XPATH_OBJECT__
5
+
6
+ extern VALUE cXMLXPathObject;
7
+
8
+ typedef struct ruby_xml_xpath_object_s {
9
+ xmlXPathObjectPtr xpop;
10
+ } ruby_xml_xpath_object;
11
+
12
+ VALUE ruby_xml_xpath_object_wrap(xmlXPathObjectPtr xpop);
13
+
14
+ VALUE ruby_xml_xpath_object_to_a(VALUE self);
15
+ VALUE ruby_xml_xpath_object_first(VALUE self);
16
+ VALUE ruby_xml_xpath_object_set(VALUE self);
17
+ VALUE ruby_xml_xpath_object_empty_q(VALUE self);
18
+ VALUE ruby_xml_xpath_object_each(VALUE self);
19
+ VALUE ruby_xml_xpath_object_first(VALUE self);
20
+ VALUE ruby_xml_xpath_object_length(VALUE self);
21
+ VALUE ruby_xml_xpath_object_set_type(VALUE self);
22
+ VALUE ruby_xml_xpath_object_string(VALUE self);
23
+
24
+ void ruby_xml_xpath_object_mark(xmlXPathObjectPtr xpop);
25
+ void ruby_xml_xpath_object_free(xmlXPathObjectPtr xpop);
26
+
27
+ void ruby_init_xml_xpath_object(void);
28
+
29
+ #endif