nokogiri 1.1.1-java

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (142) hide show
  1. data/History.ja.txt +99 -0
  2. data/History.txt +99 -0
  3. data/Manifest.txt +141 -0
  4. data/README.ja.txt +100 -0
  5. data/README.txt +109 -0
  6. data/Rakefile +354 -0
  7. data/ext/nokogiri/extconf.rb +93 -0
  8. data/ext/nokogiri/html_document.c +86 -0
  9. data/ext/nokogiri/html_document.h +10 -0
  10. data/ext/nokogiri/html_sax_parser.c +36 -0
  11. data/ext/nokogiri/html_sax_parser.h +11 -0
  12. data/ext/nokogiri/native.c +41 -0
  13. data/ext/nokogiri/native.h +50 -0
  14. data/ext/nokogiri/xml_cdata.c +44 -0
  15. data/ext/nokogiri/xml_cdata.h +9 -0
  16. data/ext/nokogiri/xml_comment.c +42 -0
  17. data/ext/nokogiri/xml_comment.h +9 -0
  18. data/ext/nokogiri/xml_document.c +206 -0
  19. data/ext/nokogiri/xml_document.h +10 -0
  20. data/ext/nokogiri/xml_dtd.c +121 -0
  21. data/ext/nokogiri/xml_dtd.h +8 -0
  22. data/ext/nokogiri/xml_io.c +17 -0
  23. data/ext/nokogiri/xml_io.h +9 -0
  24. data/ext/nokogiri/xml_node.c +727 -0
  25. data/ext/nokogiri/xml_node.h +13 -0
  26. data/ext/nokogiri/xml_node_set.c +118 -0
  27. data/ext/nokogiri/xml_node_set.h +9 -0
  28. data/ext/nokogiri/xml_reader.c +465 -0
  29. data/ext/nokogiri/xml_reader.h +10 -0
  30. data/ext/nokogiri/xml_sax_parser.c +201 -0
  31. data/ext/nokogiri/xml_sax_parser.h +10 -0
  32. data/ext/nokogiri/xml_syntax_error.c +199 -0
  33. data/ext/nokogiri/xml_syntax_error.h +11 -0
  34. data/ext/nokogiri/xml_text.c +40 -0
  35. data/ext/nokogiri/xml_text.h +9 -0
  36. data/ext/nokogiri/xml_xpath.c +53 -0
  37. data/ext/nokogiri/xml_xpath.h +11 -0
  38. data/ext/nokogiri/xml_xpath_context.c +214 -0
  39. data/ext/nokogiri/xml_xpath_context.h +9 -0
  40. data/ext/nokogiri/xslt_stylesheet.c +123 -0
  41. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  42. data/lib/action-nokogiri.rb +30 -0
  43. data/lib/nokogiri.rb +72 -0
  44. data/lib/nokogiri/css.rb +25 -0
  45. data/lib/nokogiri/css/generated_parser.rb +721 -0
  46. data/lib/nokogiri/css/generated_tokenizer.rb +159 -0
  47. data/lib/nokogiri/css/node.rb +97 -0
  48. data/lib/nokogiri/css/parser.rb +64 -0
  49. data/lib/nokogiri/css/parser.y +216 -0
  50. data/lib/nokogiri/css/syntax_error.rb +6 -0
  51. data/lib/nokogiri/css/tokenizer.rb +9 -0
  52. data/lib/nokogiri/css/tokenizer.rex +63 -0
  53. data/lib/nokogiri/css/xpath_visitor.rb +168 -0
  54. data/lib/nokogiri/decorators.rb +2 -0
  55. data/lib/nokogiri/decorators/hpricot.rb +3 -0
  56. data/lib/nokogiri/decorators/hpricot/node.rb +56 -0
  57. data/lib/nokogiri/decorators/hpricot/node_set.rb +54 -0
  58. data/lib/nokogiri/decorators/hpricot/xpath_visitor.rb +28 -0
  59. data/lib/nokogiri/decorators/slop.rb +31 -0
  60. data/lib/nokogiri/hpricot.rb +51 -0
  61. data/lib/nokogiri/html.rb +105 -0
  62. data/lib/nokogiri/html/builder.rb +9 -0
  63. data/lib/nokogiri/html/document.rb +9 -0
  64. data/lib/nokogiri/html/sax/parser.rb +21 -0
  65. data/lib/nokogiri/version.rb +3 -0
  66. data/lib/nokogiri/xml.rb +83 -0
  67. data/lib/nokogiri/xml/after_handler.rb +18 -0
  68. data/lib/nokogiri/xml/attr.rb +10 -0
  69. data/lib/nokogiri/xml/before_handler.rb +33 -0
  70. data/lib/nokogiri/xml/builder.rb +84 -0
  71. data/lib/nokogiri/xml/cdata.rb +9 -0
  72. data/lib/nokogiri/xml/comment.rb +6 -0
  73. data/lib/nokogiri/xml/document.rb +55 -0
  74. data/lib/nokogiri/xml/dtd.rb +6 -0
  75. data/lib/nokogiri/xml/element.rb +6 -0
  76. data/lib/nokogiri/xml/entity_declaration.rb +9 -0
  77. data/lib/nokogiri/xml/node.rb +333 -0
  78. data/lib/nokogiri/xml/node_set.rb +197 -0
  79. data/lib/nokogiri/xml/notation.rb +6 -0
  80. data/lib/nokogiri/xml/reader.rb +20 -0
  81. data/lib/nokogiri/xml/sax.rb +9 -0
  82. data/lib/nokogiri/xml/sax/document.rb +59 -0
  83. data/lib/nokogiri/xml/sax/parser.rb +37 -0
  84. data/lib/nokogiri/xml/syntax_error.rb +21 -0
  85. data/lib/nokogiri/xml/text.rb +6 -0
  86. data/lib/nokogiri/xml/xpath.rb +10 -0
  87. data/lib/nokogiri/xml/xpath/syntax_error.rb +8 -0
  88. data/lib/nokogiri/xml/xpath_context.rb +14 -0
  89. data/lib/nokogiri/xslt.rb +28 -0
  90. data/lib/nokogiri/xslt/stylesheet.rb +6 -0
  91. data/test/css/test_nthiness.rb +159 -0
  92. data/test/css/test_parser.rb +237 -0
  93. data/test/css/test_tokenizer.rb +162 -0
  94. data/test/css/test_xpath_visitor.rb +64 -0
  95. data/test/files/dont_hurt_em_why.xml +422 -0
  96. data/test/files/exslt.xml +8 -0
  97. data/test/files/exslt.xslt +35 -0
  98. data/test/files/staff.xml +59 -0
  99. data/test/files/staff.xslt +32 -0
  100. data/test/files/tlm.html +850 -0
  101. data/test/helper.rb +78 -0
  102. data/test/hpricot/files/basic.xhtml +17 -0
  103. data/test/hpricot/files/boingboing.html +2266 -0
  104. data/test/hpricot/files/cy0.html +3653 -0
  105. data/test/hpricot/files/immob.html +400 -0
  106. data/test/hpricot/files/pace_application.html +1320 -0
  107. data/test/hpricot/files/tenderlove.html +16 -0
  108. data/test/hpricot/files/uswebgen.html +220 -0
  109. data/test/hpricot/files/utf8.html +1054 -0
  110. data/test/hpricot/files/week9.html +1723 -0
  111. data/test/hpricot/files/why.xml +19 -0
  112. data/test/hpricot/load_files.rb +11 -0
  113. data/test/hpricot/test_alter.rb +67 -0
  114. data/test/hpricot/test_builder.rb +27 -0
  115. data/test/hpricot/test_parser.rb +426 -0
  116. data/test/hpricot/test_paths.rb +15 -0
  117. data/test/hpricot/test_preserved.rb +77 -0
  118. data/test/hpricot/test_xml.rb +30 -0
  119. data/test/html/sax/test_parser.rb +27 -0
  120. data/test/html/test_builder.rb +89 -0
  121. data/test/html/test_document.rb +150 -0
  122. data/test/html/test_node.rb +21 -0
  123. data/test/test_convert_xpath.rb +185 -0
  124. data/test/test_css_cache.rb +57 -0
  125. data/test/test_gc.rb +15 -0
  126. data/test/test_memory_leak.rb +38 -0
  127. data/test/test_nokogiri.rb +97 -0
  128. data/test/test_reader.rb +222 -0
  129. data/test/test_xslt_transforms.rb +93 -0
  130. data/test/xml/sax/test_parser.rb +95 -0
  131. data/test/xml/test_attr.rb +15 -0
  132. data/test/xml/test_builder.rb +16 -0
  133. data/test/xml/test_cdata.rb +18 -0
  134. data/test/xml/test_comment.rb +16 -0
  135. data/test/xml/test_document.rb +195 -0
  136. data/test/xml/test_dtd.rb +43 -0
  137. data/test/xml/test_node.rb +394 -0
  138. data/test/xml/test_node_set.rb +143 -0
  139. data/test/xml/test_text.rb +13 -0
  140. data/test/xml/test_xpath.rb +105 -0
  141. data/vendor/hoe.rb +1020 -0
  142. metadata +233 -0
@@ -0,0 +1,13 @@
1
+ #ifndef NOKOGIRI_XML_NODE
2
+ #define NOKOGIRI_XML_NODE
3
+
4
+ #include <native.h>
5
+
6
+ void init_xml_node();
7
+ VALUE Nokogiri_wrap_xml_node(xmlNodePtr root);
8
+
9
+ extern VALUE cNokogiriXmlNode ;
10
+ VALUE Nokogiri_wrap_xml_node(xmlNodePtr node) ;
11
+ void Nokogiri_xml_node_properties(xmlNodePtr node, VALUE attr_hash) ;
12
+ void Nokogiri_xml_node_namespaces(xmlNodePtr node, VALUE attr_hash) ;
13
+ #endif
@@ -0,0 +1,118 @@
1
+ #include <xml_node_set.h>
2
+ #include <libxml/xpathInternals.h>
3
+ /*
4
+ * call-seq:
5
+ * length
6
+ *
7
+ * Get the length of the node set
8
+ */
9
+ static VALUE length(VALUE self)
10
+ {
11
+ xmlNodeSetPtr node_set;
12
+ Data_Get_Struct(self, xmlNodeSet, node_set);
13
+
14
+ if(node_set)
15
+ return INT2NUM(node_set->nodeNr);
16
+
17
+ return INT2NUM(0);
18
+ }
19
+
20
+ /*
21
+ * call-seq:
22
+ * push(node)
23
+ *
24
+ * Append +node+ to the NodeSet.
25
+ */
26
+ static VALUE push(VALUE self, VALUE rb_node)
27
+ {
28
+ xmlNodeSetPtr node_set;
29
+ xmlNodePtr node;
30
+
31
+ if(! rb_funcall(rb_node, rb_intern("is_a?"), 1, cNokogiriXmlNode))
32
+ rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");
33
+
34
+ Data_Get_Struct(self, xmlNodeSet, node_set);
35
+ Data_Get_Struct(rb_node, xmlNode, node);
36
+ xmlXPathNodeSetAdd(node_set, node);
37
+ return self;
38
+ }
39
+
40
+ /*
41
+ * call-seq:
42
+ * [](i)
43
+ *
44
+ * Get the node at index +i+
45
+ */
46
+ static VALUE index_at(VALUE self, VALUE number)
47
+ {
48
+ int i = NUM2INT(number);
49
+ xmlNodeSetPtr node_set;
50
+ Data_Get_Struct(self, xmlNodeSet, node_set);
51
+
52
+ if(i >= node_set->nodeNr || abs(i) > node_set->nodeNr)
53
+ return Qnil;
54
+
55
+ if(i < 0)
56
+ i = i + node_set->nodeNr;
57
+
58
+ return Nokogiri_wrap_xml_node(node_set->nodeTab[i]);
59
+ }
60
+
61
+ static void deallocate(xmlNodeSetPtr node_set)
62
+ {
63
+ /*
64
+ * xmlXPathFreeNodeSet() contains an implicit assumption that it is being
65
+ * called before any of its pointed-to nodes have been free()d. this
66
+ * assumption lies in the operation where it dereferences nodeTab pointers
67
+ * while searching for namespace nodes to free.
68
+ *
69
+ * however, since Ruby's GC mechanism cannot guarantee the strict order in
70
+ * which ruby objects will be GC'd, nodes may be garbage collected before a
71
+ * nodeset containing pointers to those nodes. (this is true regardless of
72
+ * how we declare dependencies between objects with rb_gc_mark().)
73
+ *
74
+ * as a result, xmlXPathFreeNodeSet() will perform unsafe memory operations,
75
+ * and calling it would be evil.
76
+ *
77
+ * on the bright side, though, Nokogiri's API currently does not cause
78
+ * namespace nodes to be included in node sets, ever.
79
+ *
80
+ * armed with that fact, we examined xmlXPathFreeNodeSet() and related libxml
81
+ * code and determined that, within the Nokogiri abstraction, we will not
82
+ * leak memory if we simply free the node set's memory directly. that's only
83
+ * quasi-evil!
84
+ *
85
+ * there's probably a lesson in here somewhere about intermingling, within a
86
+ * single array, structs with different memory-ownership semantics. or more
87
+ * generally, a lesson about building an API in C/C++ that does not contain
88
+ * assumptions about the strict order in which memory will be released. hey,
89
+ * that sounds like a great idea for a blog post! get to it!
90
+ *
91
+ * "In Valgrind We Trust." seriously.
92
+ */
93
+ NOKOGIRI_DEBUG_START(node_set) ;
94
+ if (node_set->nodeTab != NULL)
95
+ xmlFree(node_set->nodeTab);
96
+ xmlFree(node_set);
97
+ NOKOGIRI_DEBUG_END(node_set) ;
98
+ }
99
+
100
+ static VALUE allocate(VALUE klass)
101
+ {
102
+ return Nokogiri_wrap_xml_node_set(xmlXPathNodeSetCreate(NULL));
103
+ }
104
+
105
+ VALUE Nokogiri_wrap_xml_node_set(xmlNodeSetPtr node_set)
106
+ {
107
+ return Data_Wrap_Struct(cNokogiriXmlNodeSet, 0, deallocate, node_set);
108
+ }
109
+
110
+ VALUE cNokogiriXmlNodeSet ;
111
+ void init_xml_node_set(void)
112
+ {
113
+ VALUE klass = cNokogiriXmlNodeSet = rb_eval_string("Nokogiri::XML::NodeSet");
114
+ rb_define_alloc_func(klass, allocate);
115
+ rb_define_method(klass, "length", length, 0);
116
+ rb_define_method(klass, "[]", index_at, 1);
117
+ rb_define_method(klass, "push", push, 1);
118
+ }
@@ -0,0 +1,9 @@
1
+ #ifndef NOKOGIRI_XML_NODE_SET
2
+ #define NOKOGIRI_XML_NODE_SET
3
+
4
+ #include <native.h>
5
+ void init_xml_node_set();
6
+
7
+ extern VALUE cNokogiriXmlNodeSet ;
8
+ VALUE Nokogiri_wrap_xml_node_set(xmlNodeSetPtr node_set) ;
9
+ #endif
@@ -0,0 +1,465 @@
1
+ #include <xml_reader.h>
2
+
3
+ static void dealloc(xmlTextReaderPtr reader)
4
+ {
5
+ NOKOGIRI_DEBUG_START(reader);
6
+ xmlFreeTextReader(reader);
7
+ NOKOGIRI_DEBUG_END(reader);
8
+ }
9
+
10
+ static int has_attributes(xmlTextReaderPtr reader)
11
+ {
12
+ /*
13
+ * this implementation of xmlTextReaderHasAttributes explicitly includes
14
+ * namespaces and properties, because some earlier versions ignore
15
+ * namespaces.
16
+ */
17
+ xmlNodePtr node ;
18
+ node = xmlTextReaderCurrentNode(reader);
19
+ if (node == NULL)
20
+ return(0);
21
+
22
+ if ((node->type == XML_ELEMENT_NODE) &&
23
+ ((node->properties != NULL) || (node->nsDef != NULL)))
24
+ return(1);
25
+ return(0);
26
+ }
27
+
28
+ /*
29
+ * call-seq:
30
+ * default?
31
+ *
32
+ * Was an attribute generated from the default value in the DTD or schema?
33
+ */
34
+ static VALUE default_eh(VALUE self)
35
+ {
36
+ xmlTextReaderPtr reader;
37
+ Data_Get_Struct(self, xmlTextReader, reader);
38
+ int eh = xmlTextReaderIsDefault(reader);
39
+ if(eh == 0) return Qfalse;
40
+ if(eh == 1) return Qtrue;
41
+
42
+ return Qnil;
43
+ }
44
+
45
+ /*
46
+ * call-seq:
47
+ * value?
48
+ *
49
+ * Does this node have a text value?
50
+ */
51
+ static VALUE value_eh(VALUE self)
52
+ {
53
+ xmlTextReaderPtr reader;
54
+ Data_Get_Struct(self, xmlTextReader, reader);
55
+ int eh = xmlTextReaderHasValue(reader);
56
+ if(eh == 0) return Qfalse;
57
+ if(eh == 1) return Qtrue;
58
+
59
+ return Qnil;
60
+ }
61
+
62
+ /*
63
+ * call-seq:
64
+ * attributes?
65
+ *
66
+ * Does this node have attributes?
67
+ */
68
+ static VALUE attributes_eh(VALUE self)
69
+ {
70
+ xmlTextReaderPtr reader;
71
+ Data_Get_Struct(self, xmlTextReader, reader);
72
+ int eh = has_attributes(reader);
73
+ if(eh == 0) return Qfalse;
74
+ if(eh == 1) return Qtrue;
75
+
76
+ return Qnil;
77
+ }
78
+
79
+ /*
80
+ * call-seq:
81
+ * namespaces
82
+ *
83
+ * Get a hash of namespaces for this Node
84
+ */
85
+ static VALUE namespaces(VALUE self)
86
+ {
87
+ xmlTextReaderPtr reader;
88
+ VALUE attr ;
89
+
90
+ Data_Get_Struct(self, xmlTextReader, reader);
91
+
92
+ attr = rb_hash_new() ;
93
+
94
+ if (! has_attributes(reader))
95
+ return attr ;
96
+
97
+ xmlNodePtr ptr = xmlTextReaderExpand(reader);
98
+ if(ptr == NULL) return Qnil;
99
+
100
+ Nokogiri_xml_node_namespaces(ptr, attr);
101
+
102
+ return attr ;
103
+ }
104
+
105
+ /*
106
+ * call-seq:
107
+ * attribute_nodes
108
+ *
109
+ * Get a list of attributes for this Node
110
+ */
111
+ static VALUE attribute_nodes(VALUE self)
112
+ {
113
+ xmlTextReaderPtr reader;
114
+ VALUE attr ;
115
+
116
+ Data_Get_Struct(self, xmlTextReader, reader);
117
+
118
+ attr = rb_ary_new() ;
119
+
120
+ if (! has_attributes(reader))
121
+ return attr ;
122
+
123
+ xmlNodePtr ptr = xmlTextReaderExpand(reader);
124
+ if(ptr == NULL) return Qnil;
125
+
126
+ // FIXME I'm not sure if this is correct..... I don't really like pointing
127
+ // at this document, but I have to because of the assertions in
128
+ // the node wrapping code.
129
+ if(!ptr->doc->_private) {
130
+ VALUE rb_doc = Data_Wrap_Struct(cNokogiriXmlDocument, 0, 0, ptr->doc);
131
+ rb_iv_set(rb_doc, "@decorators", Qnil);
132
+ ptr->doc->_private = (void *)rb_doc;
133
+ }
134
+
135
+ Nokogiri_xml_node_properties(ptr, attr);
136
+
137
+ return attr ;
138
+ }
139
+
140
+ /*
141
+ * call-seq:
142
+ * attribute_at(index)
143
+ *
144
+ * Get the value of attribute at +index+
145
+ */
146
+ static VALUE attribute_at(VALUE self, VALUE index)
147
+ {
148
+ xmlTextReaderPtr reader;
149
+ Data_Get_Struct(self, xmlTextReader, reader);
150
+
151
+ if(index == Qnil) return Qnil;
152
+ index = rb_funcall(index, rb_intern("to_i"), 0);
153
+
154
+ xmlChar * value = xmlTextReaderGetAttributeNo(
155
+ reader,
156
+ NUM2INT(index)
157
+ );
158
+ if(value == NULL) return Qnil;
159
+
160
+ VALUE rb_value = rb_str_new2((const char *)value);
161
+ xmlFree(value);
162
+ return rb_value;
163
+ }
164
+
165
+ /*
166
+ * call-seq:
167
+ * attribute(name)
168
+ *
169
+ * Get the value of attribute named +name+
170
+ */
171
+ static VALUE reader_attribute(VALUE self, VALUE name)
172
+ {
173
+ xmlTextReaderPtr reader;
174
+ xmlChar *value ;
175
+ Data_Get_Struct(self, xmlTextReader, reader);
176
+
177
+ if(name == Qnil) return Qnil;
178
+ name = StringValue(name) ;
179
+
180
+ value = xmlTextReaderGetAttribute(reader, (xmlChar*)StringValuePtr(name));
181
+ if(value == NULL) {
182
+ /* this section is an attempt to workaround older versions of libxml that
183
+ don't handle namespaces properly in all attribute-and-friends functions */
184
+ xmlChar *prefix = NULL ;
185
+ xmlChar *localname = xmlSplitQName2((xmlChar*)StringValuePtr(name), &prefix);
186
+ if (localname != NULL) {
187
+ value = xmlTextReaderLookupNamespace(reader, localname);
188
+ xmlFree(localname) ;
189
+ } else {
190
+ value = xmlTextReaderLookupNamespace(reader, prefix);
191
+ }
192
+ xmlFree(prefix);
193
+ }
194
+ if(value == NULL) return Qnil;
195
+
196
+ VALUE rb_value = rb_str_new2((const char *)value);
197
+ xmlFree(value);
198
+ return rb_value;
199
+ }
200
+
201
+ /*
202
+ * call-seq:
203
+ * attribute_count
204
+ *
205
+ * Get the number of attributes for the current node
206
+ */
207
+ static VALUE attribute_count(VALUE self)
208
+ {
209
+ xmlTextReaderPtr reader;
210
+ Data_Get_Struct(self, xmlTextReader, reader);
211
+ int count = xmlTextReaderAttributeCount(reader);
212
+ if(count == -1) return Qnil;
213
+
214
+ return INT2NUM(count);
215
+ }
216
+
217
+ /*
218
+ * call-seq:
219
+ * depth
220
+ *
221
+ * Get the depth of the node
222
+ */
223
+ static VALUE depth(VALUE self)
224
+ {
225
+ xmlTextReaderPtr reader;
226
+ Data_Get_Struct(self, xmlTextReader, reader);
227
+ int depth = xmlTextReaderDepth(reader);
228
+ if(depth == -1) return Qnil;
229
+
230
+ return INT2NUM(depth);
231
+ }
232
+
233
+ /*
234
+ * call-seq:
235
+ * encoding
236
+ *
237
+ * Get the encoding for the document
238
+ */
239
+ static VALUE encoding(VALUE self)
240
+ {
241
+ xmlTextReaderPtr reader;
242
+ Data_Get_Struct(self, xmlTextReader, reader);
243
+ const char * encoding = (const char *)xmlTextReaderConstEncoding(reader);
244
+ if(encoding == NULL) return Qnil;
245
+
246
+ return rb_str_new2(encoding);
247
+ }
248
+
249
+ /*
250
+ * call-seq:
251
+ * xml_version
252
+ *
253
+ * Get the XML version of the document being read
254
+ */
255
+ static VALUE xml_version(VALUE self)
256
+ {
257
+ xmlTextReaderPtr reader;
258
+ Data_Get_Struct(self, xmlTextReader, reader);
259
+ const char * version = (const char *)xmlTextReaderConstXmlVersion(reader);
260
+ if(version == NULL) return Qnil;
261
+
262
+ return rb_str_new2(version);
263
+ }
264
+
265
+ /*
266
+ * call-seq:
267
+ * lang
268
+ *
269
+ * Get the xml:lang scope within which the node resides.
270
+ */
271
+ static VALUE lang(VALUE self)
272
+ {
273
+ xmlTextReaderPtr reader;
274
+ Data_Get_Struct(self, xmlTextReader, reader);
275
+ const char * lang = (const char *)xmlTextReaderConstXmlLang(reader);
276
+ if(lang == NULL) return Qnil;
277
+
278
+ return rb_str_new2(lang);
279
+ }
280
+
281
+ /*
282
+ * call-seq:
283
+ * value
284
+ *
285
+ * Get the text value of the node if present
286
+ */
287
+ static VALUE value(VALUE self)
288
+ {
289
+ xmlTextReaderPtr reader;
290
+ Data_Get_Struct(self, xmlTextReader, reader);
291
+ const char * value = (const char *)xmlTextReaderConstValue(reader);
292
+ if(value == NULL) return Qnil;
293
+
294
+ return rb_str_new2(value);
295
+ }
296
+
297
+ /*
298
+ * call-seq:
299
+ * prefix
300
+ *
301
+ * Get the shorthand reference to the namespace associated with the node.
302
+ */
303
+ static VALUE prefix(VALUE self)
304
+ {
305
+ xmlTextReaderPtr reader;
306
+ Data_Get_Struct(self, xmlTextReader, reader);
307
+ const char * prefix = (const char *)xmlTextReaderConstPrefix(reader);
308
+ if(prefix == NULL) return Qnil;
309
+
310
+ return rb_str_new2(prefix);
311
+ }
312
+
313
+ /*
314
+ * call-seq:
315
+ * namespace_uri
316
+ *
317
+ * Get the URI defining the namespace associated with the node
318
+ */
319
+ static VALUE namespace_uri(VALUE self)
320
+ {
321
+ xmlTextReaderPtr reader;
322
+ Data_Get_Struct(self, xmlTextReader, reader);
323
+ const char * uri = (const char *)xmlTextReaderConstNamespaceUri(reader);
324
+ if(uri == NULL) return Qnil;
325
+
326
+ return rb_str_new2(uri);
327
+ }
328
+
329
+ /*
330
+ * call-seq:
331
+ * local_name
332
+ *
333
+ * Get the local name of the node
334
+ */
335
+ static VALUE local_name(VALUE self)
336
+ {
337
+ xmlTextReaderPtr reader;
338
+ Data_Get_Struct(self, xmlTextReader, reader);
339
+ const char * name = (const char *)xmlTextReaderConstLocalName(reader);
340
+ if(name == NULL) return Qnil;
341
+
342
+ return rb_str_new2(name);
343
+ }
344
+
345
+ /*
346
+ * call-seq:
347
+ * name
348
+ *
349
+ * Get the name of the node
350
+ */
351
+ static VALUE name(VALUE self)
352
+ {
353
+ xmlTextReaderPtr reader;
354
+ Data_Get_Struct(self, xmlTextReader, reader);
355
+ const char * name = (const char *)xmlTextReaderConstName(reader);
356
+ if(name == NULL) return Qnil;
357
+
358
+ return rb_str_new2(name);
359
+ }
360
+
361
+ /*
362
+ * call-seq:
363
+ * state
364
+ *
365
+ * Get the state of the reader
366
+ */
367
+ static VALUE state(VALUE self)
368
+ {
369
+ xmlTextReaderPtr reader;
370
+ Data_Get_Struct(self, xmlTextReader, reader);
371
+ return INT2NUM(xmlTextReaderReadState(reader));
372
+ }
373
+
374
+ /*
375
+ * call-seq:
376
+ * read
377
+ *
378
+ * Move the Reader forward through the XML document.
379
+ */
380
+ static VALUE read_more(VALUE self)
381
+ {
382
+ xmlTextReaderPtr reader;
383
+ Data_Get_Struct(self, xmlTextReader, reader);
384
+
385
+ int ret = xmlTextReaderRead(reader);
386
+ if(ret == 1) return self;
387
+ if(ret == 0) return Qnil;
388
+
389
+ rb_raise(rb_eRuntimeError, "Error pulling: %d", ret);
390
+ }
391
+
392
+ /*
393
+ * call-seq:
394
+ * from_memory(string, url = nil, encoding = nil, options = 0)
395
+ *
396
+ * Create a new reader that parses +string+
397
+ */
398
+ static VALUE from_memory(int argc, VALUE *argv, VALUE klass)
399
+ {
400
+ VALUE rb_buffer, rb_url, encoding, rb_options;
401
+
402
+ const char * c_url = NULL;
403
+ const char * c_encoding = NULL;
404
+ int c_options = 0;
405
+
406
+ rb_scan_args(argc, argv, "13", &rb_buffer, &rb_url, &encoding, &rb_options);
407
+
408
+ rb_buffer = StringValue(rb_buffer) ;
409
+ if (RTEST(rb_url)) c_url = StringValuePtr(rb_url);
410
+ if (RTEST(encoding)) c_encoding = StringValuePtr(rb_url);
411
+ if (RTEST(rb_options)) c_options = NUM2INT(rb_options);
412
+
413
+ xmlTextReaderPtr reader = xmlReaderForMemory(
414
+ StringValuePtr(rb_buffer),
415
+ NUM2INT(rb_funcall(rb_buffer, rb_intern("length"), 0)),
416
+ c_url,
417
+ c_encoding,
418
+ c_options
419
+ );
420
+
421
+ if(reader == NULL) {
422
+ xmlFreeTextReader(reader);
423
+ rb_raise(rb_eRuntimeError, "couldn't create a parser");
424
+ }
425
+
426
+ return Data_Wrap_Struct(klass, NULL, dealloc, reader);
427
+ }
428
+
429
+ VALUE cNokogiriXmlReader;
430
+
431
+ void init_xml_reader()
432
+ {
433
+ VALUE module = rb_define_module("Nokogiri");
434
+ VALUE xml = rb_define_module_under(module, "XML");
435
+
436
+ /*
437
+ * The Reader parser allows you to effectively pull parse an XML document.
438
+ * Once instantiated, call Nokogiri::XML::Reader#each to iterate over each
439
+ * node. Note that you may only iterate over the document once!
440
+ */
441
+ VALUE klass = rb_define_class_under(xml, "Reader", rb_cObject);
442
+
443
+ cNokogiriXmlReader = klass;
444
+
445
+ rb_define_singleton_method(klass, "from_memory", from_memory, -1);
446
+ rb_define_method(klass, "read", read_more, 0);
447
+ rb_define_method(klass, "state", state, 0);
448
+ rb_define_method(klass, "name", name, 0);
449
+ rb_define_method(klass, "local_name", local_name, 0);
450
+ rb_define_method(klass, "namespace_uri", namespace_uri, 0);
451
+ rb_define_method(klass, "prefix", prefix, 0);
452
+ rb_define_method(klass, "value", value, 0);
453
+ rb_define_method(klass, "lang", lang, 0);
454
+ rb_define_method(klass, "xml_version", xml_version, 0);
455
+ rb_define_method(klass, "encoding", encoding, 0);
456
+ rb_define_method(klass, "depth", depth, 0);
457
+ rb_define_method(klass, "attribute_count", attribute_count, 0);
458
+ rb_define_method(klass, "attribute", reader_attribute, 1);
459
+ rb_define_method(klass, "namespaces", namespaces, 0);
460
+ rb_define_method(klass, "attribute_at", attribute_at, 1);
461
+ rb_define_method(klass, "attribute_nodes", attribute_nodes, 0);
462
+ rb_define_method(klass, "attributes?", attributes_eh, 0);
463
+ rb_define_method(klass, "value?", value_eh, 0);
464
+ rb_define_method(klass, "default?", default_eh, 0);
465
+ }