nokogiri 1.11.0.rc1-x86-linux

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 (145) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE-DEPENDENCIES.md +1614 -0
  3. data/LICENSE.md +9 -0
  4. data/README.md +200 -0
  5. data/bin/nokogiri +118 -0
  6. data/dependencies.yml +74 -0
  7. data/ext/nokogiri/depend +358 -0
  8. data/ext/nokogiri/extconf.rb +695 -0
  9. data/ext/nokogiri/html_document.c +170 -0
  10. data/ext/nokogiri/html_document.h +10 -0
  11. data/ext/nokogiri/html_element_description.c +279 -0
  12. data/ext/nokogiri/html_element_description.h +10 -0
  13. data/ext/nokogiri/html_entity_lookup.c +32 -0
  14. data/ext/nokogiri/html_entity_lookup.h +8 -0
  15. data/ext/nokogiri/html_sax_parser_context.c +116 -0
  16. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  17. data/ext/nokogiri/html_sax_push_parser.c +87 -0
  18. data/ext/nokogiri/html_sax_push_parser.h +9 -0
  19. data/ext/nokogiri/nokogiri.c +147 -0
  20. data/ext/nokogiri/nokogiri.h +122 -0
  21. data/ext/nokogiri/xml_attr.c +103 -0
  22. data/ext/nokogiri/xml_attr.h +9 -0
  23. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  24. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  25. data/ext/nokogiri/xml_cdata.c +62 -0
  26. data/ext/nokogiri/xml_cdata.h +9 -0
  27. data/ext/nokogiri/xml_comment.c +69 -0
  28. data/ext/nokogiri/xml_comment.h +9 -0
  29. data/ext/nokogiri/xml_document.c +617 -0
  30. data/ext/nokogiri/xml_document.h +23 -0
  31. data/ext/nokogiri/xml_document_fragment.c +48 -0
  32. data/ext/nokogiri/xml_document_fragment.h +10 -0
  33. data/ext/nokogiri/xml_dtd.c +202 -0
  34. data/ext/nokogiri/xml_dtd.h +10 -0
  35. data/ext/nokogiri/xml_element_content.c +123 -0
  36. data/ext/nokogiri/xml_element_content.h +10 -0
  37. data/ext/nokogiri/xml_element_decl.c +69 -0
  38. data/ext/nokogiri/xml_element_decl.h +9 -0
  39. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  40. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  41. data/ext/nokogiri/xml_entity_decl.c +110 -0
  42. data/ext/nokogiri/xml_entity_decl.h +10 -0
  43. data/ext/nokogiri/xml_entity_reference.c +52 -0
  44. data/ext/nokogiri/xml_entity_reference.h +9 -0
  45. data/ext/nokogiri/xml_io.c +61 -0
  46. data/ext/nokogiri/xml_io.h +11 -0
  47. data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
  48. data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
  49. data/ext/nokogiri/xml_namespace.c +111 -0
  50. data/ext/nokogiri/xml_namespace.h +14 -0
  51. data/ext/nokogiri/xml_node.c +1773 -0
  52. data/ext/nokogiri/xml_node.h +13 -0
  53. data/ext/nokogiri/xml_node_set.c +486 -0
  54. data/ext/nokogiri/xml_node_set.h +12 -0
  55. data/ext/nokogiri/xml_processing_instruction.c +56 -0
  56. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  57. data/ext/nokogiri/xml_reader.c +668 -0
  58. data/ext/nokogiri/xml_reader.h +10 -0
  59. data/ext/nokogiri/xml_relax_ng.c +161 -0
  60. data/ext/nokogiri/xml_relax_ng.h +9 -0
  61. data/ext/nokogiri/xml_sax_parser.c +310 -0
  62. data/ext/nokogiri/xml_sax_parser.h +39 -0
  63. data/ext/nokogiri/xml_sax_parser_context.c +262 -0
  64. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  65. data/ext/nokogiri/xml_sax_push_parser.c +159 -0
  66. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  67. data/ext/nokogiri/xml_schema.c +205 -0
  68. data/ext/nokogiri/xml_schema.h +9 -0
  69. data/ext/nokogiri/xml_syntax_error.c +64 -0
  70. data/ext/nokogiri/xml_syntax_error.h +13 -0
  71. data/ext/nokogiri/xml_text.c +52 -0
  72. data/ext/nokogiri/xml_text.h +9 -0
  73. data/ext/nokogiri/xml_xpath_context.c +298 -0
  74. data/ext/nokogiri/xml_xpath_context.h +10 -0
  75. data/ext/nokogiri/xslt_stylesheet.c +266 -0
  76. data/ext/nokogiri/xslt_stylesheet.h +14 -0
  77. data/lib/nokogiri.rb +127 -0
  78. data/lib/nokogiri/2.4/nokogiri.so +0 -0
  79. data/lib/nokogiri/2.5/nokogiri.so +0 -0
  80. data/lib/nokogiri/2.6/nokogiri.so +0 -0
  81. data/lib/nokogiri/2.7/nokogiri.so +0 -0
  82. data/lib/nokogiri/css.rb +28 -0
  83. data/lib/nokogiri/css/node.rb +53 -0
  84. data/lib/nokogiri/css/parser.rb +751 -0
  85. data/lib/nokogiri/css/parser.y +272 -0
  86. data/lib/nokogiri/css/parser_extras.rb +92 -0
  87. data/lib/nokogiri/css/syntax_error.rb +8 -0
  88. data/lib/nokogiri/css/tokenizer.rb +154 -0
  89. data/lib/nokogiri/css/tokenizer.rex +55 -0
  90. data/lib/nokogiri/css/xpath_visitor.rb +232 -0
  91. data/lib/nokogiri/decorators/slop.rb +43 -0
  92. data/lib/nokogiri/html.rb +38 -0
  93. data/lib/nokogiri/html/builder.rb +36 -0
  94. data/lib/nokogiri/html/document.rb +336 -0
  95. data/lib/nokogiri/html/document_fragment.rb +50 -0
  96. data/lib/nokogiri/html/element_description.rb +24 -0
  97. data/lib/nokogiri/html/element_description_defaults.rb +672 -0
  98. data/lib/nokogiri/html/entity_lookup.rb +14 -0
  99. data/lib/nokogiri/html/sax/parser.rb +63 -0
  100. data/lib/nokogiri/html/sax/parser_context.rb +17 -0
  101. data/lib/nokogiri/html/sax/push_parser.rb +37 -0
  102. data/lib/nokogiri/jruby/dependencies.rb +20 -0
  103. data/lib/nokogiri/syntax_error.rb +5 -0
  104. data/lib/nokogiri/version.rb +149 -0
  105. data/lib/nokogiri/xml.rb +76 -0
  106. data/lib/nokogiri/xml/attr.rb +15 -0
  107. data/lib/nokogiri/xml/attribute_decl.rb +19 -0
  108. data/lib/nokogiri/xml/builder.rb +447 -0
  109. data/lib/nokogiri/xml/cdata.rb +12 -0
  110. data/lib/nokogiri/xml/character_data.rb +8 -0
  111. data/lib/nokogiri/xml/document.rb +280 -0
  112. data/lib/nokogiri/xml/document_fragment.rb +161 -0
  113. data/lib/nokogiri/xml/dtd.rb +33 -0
  114. data/lib/nokogiri/xml/element_content.rb +37 -0
  115. data/lib/nokogiri/xml/element_decl.rb +14 -0
  116. data/lib/nokogiri/xml/entity_decl.rb +20 -0
  117. data/lib/nokogiri/xml/entity_reference.rb +19 -0
  118. data/lib/nokogiri/xml/namespace.rb +14 -0
  119. data/lib/nokogiri/xml/node.rb +916 -0
  120. data/lib/nokogiri/xml/node/save_options.rb +62 -0
  121. data/lib/nokogiri/xml/node_set.rb +372 -0
  122. data/lib/nokogiri/xml/notation.rb +7 -0
  123. data/lib/nokogiri/xml/parse_options.rb +121 -0
  124. data/lib/nokogiri/xml/pp.rb +3 -0
  125. data/lib/nokogiri/xml/pp/character_data.rb +19 -0
  126. data/lib/nokogiri/xml/pp/node.rb +57 -0
  127. data/lib/nokogiri/xml/processing_instruction.rb +9 -0
  128. data/lib/nokogiri/xml/reader.rb +116 -0
  129. data/lib/nokogiri/xml/relax_ng.rb +33 -0
  130. data/lib/nokogiri/xml/sax.rb +5 -0
  131. data/lib/nokogiri/xml/sax/document.rb +172 -0
  132. data/lib/nokogiri/xml/sax/parser.rb +123 -0
  133. data/lib/nokogiri/xml/sax/parser_context.rb +17 -0
  134. data/lib/nokogiri/xml/sax/push_parser.rb +61 -0
  135. data/lib/nokogiri/xml/schema.rb +64 -0
  136. data/lib/nokogiri/xml/searchable.rb +231 -0
  137. data/lib/nokogiri/xml/syntax_error.rb +71 -0
  138. data/lib/nokogiri/xml/text.rb +10 -0
  139. data/lib/nokogiri/xml/xpath.rb +11 -0
  140. data/lib/nokogiri/xml/xpath/syntax_error.rb +12 -0
  141. data/lib/nokogiri/xml/xpath_context.rb +17 -0
  142. data/lib/nokogiri/xslt.rb +57 -0
  143. data/lib/nokogiri/xslt/stylesheet.rb +26 -0
  144. data/lib/xsd/xmlparser/nokogiri.rb +103 -0
  145. metadata +482 -0
@@ -0,0 +1,12 @@
1
+ #ifndef NOKOGIRI_XML_NODE_SET
2
+ #define NOKOGIRI_XML_NODE_SET
3
+
4
+ #include <nokogiri.h>
5
+ void init_xml_node_set();
6
+
7
+ extern VALUE cNokogiriXmlNodeSet ;
8
+ VALUE Nokogiri_wrap_xml_node_set(xmlNodeSetPtr node_set, VALUE document) ;
9
+ VALUE Nokogiri_wrap_xml_node_set_node(xmlNodePtr node, VALUE node_set) ;
10
+ VALUE Nokogiri_wrap_xml_node_set_namespace(xmlNsPtr node, VALUE node_set) ;
11
+
12
+ #endif
@@ -0,0 +1,56 @@
1
+ #include <xml_processing_instruction.h>
2
+
3
+ /*
4
+ * call-seq:
5
+ * new(document, name, content)
6
+ *
7
+ * Create a new ProcessingInstruction element on the +document+ with +name+
8
+ * and +content+
9
+ */
10
+ static VALUE new(int argc, VALUE *argv, VALUE klass)
11
+ {
12
+ xmlDocPtr xml_doc;
13
+ xmlNodePtr node;
14
+ VALUE document;
15
+ VALUE name;
16
+ VALUE content;
17
+ VALUE rest;
18
+ VALUE rb_node;
19
+
20
+ rb_scan_args(argc, argv, "3*", &document, &name, &content, &rest);
21
+
22
+ Data_Get_Struct(document, xmlDoc, xml_doc);
23
+
24
+ node = xmlNewDocPI(
25
+ xml_doc,
26
+ (const xmlChar *)StringValueCStr(name),
27
+ (const xmlChar *)StringValueCStr(content)
28
+ );
29
+
30
+ nokogiri_root_node(node);
31
+
32
+ rb_node = Nokogiri_wrap_xml_node(klass, node);
33
+ rb_obj_call_init(rb_node, argc, argv);
34
+
35
+ if(rb_block_given_p()) rb_yield(rb_node);
36
+
37
+ return rb_node;
38
+ }
39
+
40
+ VALUE cNokogiriXmlProcessingInstruction;
41
+ void init_xml_processing_instruction()
42
+ {
43
+ VALUE nokogiri = rb_define_module("Nokogiri");
44
+ VALUE xml = rb_define_module_under(nokogiri, "XML");
45
+ VALUE node = rb_define_class_under(xml, "Node", rb_cObject);
46
+
47
+ /*
48
+ * ProcessingInstruction represents a ProcessingInstruction node in an xml
49
+ * document.
50
+ */
51
+ VALUE klass = rb_define_class_under(xml, "ProcessingInstruction", node);
52
+
53
+ cNokogiriXmlProcessingInstruction = klass;
54
+
55
+ rb_define_singleton_method(klass, "new", new, -1);
56
+ }
@@ -0,0 +1,9 @@
1
+ #ifndef NOKOGIRI_XML_PROCESSING_INSTRUCTION
2
+ #define NOKOGIRI_XML_PROCESSING_INSTRUCTION
3
+
4
+ #include <nokogiri.h>
5
+
6
+ void init_xml_processing_instruction();
7
+
8
+ extern VALUE cNokogiriXmlProcessingInstruction;
9
+ #endif
@@ -0,0 +1,668 @@
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
+ static void Nokogiri_xml_node_namespaces(xmlNodePtr node, VALUE attr_hash)
29
+ {
30
+ xmlNsPtr ns;
31
+ static char buffer[XMLNS_BUFFER_LEN] ;
32
+ char *key ;
33
+ size_t keylen ;
34
+
35
+ if (node->type != XML_ELEMENT_NODE) return ;
36
+
37
+ ns = node->nsDef;
38
+ while (ns != NULL) {
39
+
40
+ keylen = XMLNS_PREFIX_LEN + (ns->prefix ? (strlen((const char*)ns->prefix) + 1) : 0) ;
41
+ if (keylen > XMLNS_BUFFER_LEN) {
42
+ key = (char*)malloc(keylen) ;
43
+ } else {
44
+ key = buffer ;
45
+ }
46
+
47
+ if (ns->prefix) {
48
+ sprintf(key, "%s:%s", XMLNS_PREFIX, ns->prefix);
49
+ } else {
50
+ sprintf(key, "%s", XMLNS_PREFIX);
51
+ }
52
+
53
+ rb_hash_aset(attr_hash,
54
+ NOKOGIRI_STR_NEW2(key),
55
+ (ns->href ? NOKOGIRI_STR_NEW2(ns->href) : Qnil)
56
+ );
57
+ if (key != buffer) {
58
+ free(key);
59
+ }
60
+ ns = ns->next ;
61
+ }
62
+ }
63
+
64
+
65
+ /*
66
+ * call-seq:
67
+ * default?
68
+ *
69
+ * Was an attribute generated from the default value in the DTD or schema?
70
+ */
71
+ static VALUE default_eh(VALUE self)
72
+ {
73
+ xmlTextReaderPtr reader;
74
+ int eh;
75
+
76
+ Data_Get_Struct(self, xmlTextReader, reader);
77
+ eh = xmlTextReaderIsDefault(reader);
78
+ if(eh == 0) return Qfalse;
79
+ if(eh == 1) return Qtrue;
80
+
81
+ return Qnil;
82
+ }
83
+
84
+ /*
85
+ * call-seq:
86
+ * value?
87
+ *
88
+ * Does this node have a text value?
89
+ */
90
+ static VALUE value_eh(VALUE self)
91
+ {
92
+ xmlTextReaderPtr reader;
93
+ int eh;
94
+
95
+ Data_Get_Struct(self, xmlTextReader, reader);
96
+ eh = xmlTextReaderHasValue(reader);
97
+ if(eh == 0) return Qfalse;
98
+ if(eh == 1) return Qtrue;
99
+
100
+ return Qnil;
101
+ }
102
+
103
+ /*
104
+ * call-seq:
105
+ * attributes?
106
+ *
107
+ * Does this node have attributes?
108
+ */
109
+ static VALUE attributes_eh(VALUE self)
110
+ {
111
+ xmlTextReaderPtr reader;
112
+ int eh;
113
+
114
+ Data_Get_Struct(self, xmlTextReader, reader);
115
+ eh = has_attributes(reader);
116
+ if(eh == 0) return Qfalse;
117
+ if(eh == 1) return Qtrue;
118
+
119
+ return Qnil;
120
+ }
121
+
122
+ /*
123
+ * call-seq:
124
+ * namespaces
125
+ *
126
+ * Get a hash of namespaces for this Node
127
+ */
128
+ static VALUE namespaces(VALUE self)
129
+ {
130
+ xmlTextReaderPtr reader;
131
+ xmlNodePtr ptr;
132
+ VALUE attr ;
133
+
134
+ Data_Get_Struct(self, xmlTextReader, reader);
135
+
136
+ attr = rb_hash_new() ;
137
+
138
+ if (! has_attributes(reader))
139
+ return attr ;
140
+
141
+ ptr = xmlTextReaderExpand(reader);
142
+ if(ptr == NULL) return Qnil;
143
+
144
+ Nokogiri_xml_node_namespaces(ptr, attr);
145
+
146
+ return attr ;
147
+ }
148
+
149
+ /*
150
+ * call-seq:
151
+ * attribute_nodes
152
+ *
153
+ * Get a list of attributes for this Node
154
+ */
155
+ static VALUE attribute_nodes(VALUE self)
156
+ {
157
+ xmlTextReaderPtr reader;
158
+ xmlNodePtr ptr;
159
+ VALUE attr ;
160
+
161
+ Data_Get_Struct(self, xmlTextReader, reader);
162
+
163
+ attr = rb_ary_new() ;
164
+
165
+ if (! has_attributes(reader))
166
+ return attr ;
167
+
168
+ ptr = xmlTextReaderExpand(reader);
169
+ if(ptr == NULL) return Qnil;
170
+
171
+ Nokogiri_xml_node_properties(ptr, attr);
172
+
173
+ return attr ;
174
+ }
175
+
176
+ /*
177
+ * call-seq:
178
+ * attribute_at(index)
179
+ *
180
+ * Get the value of attribute at +index+
181
+ */
182
+ static VALUE attribute_at(VALUE self, VALUE index)
183
+ {
184
+ xmlTextReaderPtr reader;
185
+ xmlChar *value;
186
+ VALUE rb_value;
187
+
188
+ Data_Get_Struct(self, xmlTextReader, reader);
189
+
190
+ if(NIL_P(index)) return Qnil;
191
+ index = rb_Integer(index);
192
+
193
+ value = xmlTextReaderGetAttributeNo(
194
+ reader,
195
+ (int)NUM2INT(index)
196
+ );
197
+ if(value == NULL) return Qnil;
198
+
199
+ rb_value = NOKOGIRI_STR_NEW2(value);
200
+ xmlFree(value);
201
+ return rb_value;
202
+ }
203
+
204
+ /*
205
+ * call-seq:
206
+ * attribute(name)
207
+ *
208
+ * Get the value of attribute named +name+
209
+ */
210
+ static VALUE reader_attribute(VALUE self, VALUE name)
211
+ {
212
+ xmlTextReaderPtr reader;
213
+ xmlChar *value ;
214
+ VALUE rb_value;
215
+
216
+ Data_Get_Struct(self, xmlTextReader, reader);
217
+
218
+ if(NIL_P(name)) return Qnil;
219
+ name = StringValue(name) ;
220
+
221
+ value = xmlTextReaderGetAttribute(reader, (xmlChar*)StringValueCStr(name));
222
+ if(value == NULL) return Qnil;
223
+
224
+ rb_value = NOKOGIRI_STR_NEW2(value);
225
+ xmlFree(value);
226
+ return rb_value;
227
+ }
228
+
229
+ /*
230
+ * call-seq:
231
+ * attribute_count
232
+ *
233
+ * Get the number of attributes for the current node
234
+ */
235
+ static VALUE attribute_count(VALUE self)
236
+ {
237
+ xmlTextReaderPtr reader;
238
+ int count;
239
+
240
+ Data_Get_Struct(self, xmlTextReader, reader);
241
+ count = xmlTextReaderAttributeCount(reader);
242
+ if(count == -1) return Qnil;
243
+
244
+ return INT2NUM((long)count);
245
+ }
246
+
247
+ /*
248
+ * call-seq:
249
+ * depth
250
+ *
251
+ * Get the depth of the node
252
+ */
253
+ static VALUE depth(VALUE self)
254
+ {
255
+ xmlTextReaderPtr reader;
256
+ int depth;
257
+
258
+ Data_Get_Struct(self, xmlTextReader, reader);
259
+ depth = xmlTextReaderDepth(reader);
260
+ if(depth == -1) return Qnil;
261
+
262
+ return INT2NUM((long)depth);
263
+ }
264
+
265
+ /*
266
+ * call-seq:
267
+ * xml_version
268
+ *
269
+ * Get the XML version of the document being read
270
+ */
271
+ static VALUE xml_version(VALUE self)
272
+ {
273
+ xmlTextReaderPtr reader;
274
+ const char *version;
275
+
276
+ Data_Get_Struct(self, xmlTextReader, reader);
277
+ version = (const char *)xmlTextReaderConstXmlVersion(reader);
278
+ if(version == NULL) return Qnil;
279
+
280
+ return NOKOGIRI_STR_NEW2(version);
281
+ }
282
+
283
+ /*
284
+ * call-seq:
285
+ * lang
286
+ *
287
+ * Get the xml:lang scope within which the node resides.
288
+ */
289
+ static VALUE lang(VALUE self)
290
+ {
291
+ xmlTextReaderPtr reader;
292
+ const char *lang;
293
+
294
+ Data_Get_Struct(self, xmlTextReader, reader);
295
+ lang = (const char *)xmlTextReaderConstXmlLang(reader);
296
+ if(lang == NULL) return Qnil;
297
+
298
+ return NOKOGIRI_STR_NEW2(lang);
299
+ }
300
+
301
+ /*
302
+ * call-seq:
303
+ * value
304
+ *
305
+ * Get the text value of the node if present. Returns a utf-8 encoded string.
306
+ */
307
+ static VALUE value(VALUE self)
308
+ {
309
+ xmlTextReaderPtr reader;
310
+ const char *value;
311
+
312
+ Data_Get_Struct(self, xmlTextReader, reader);
313
+ value = (const char *)xmlTextReaderConstValue(reader);
314
+ if(value == NULL) return Qnil;
315
+
316
+ return NOKOGIRI_STR_NEW2(value);
317
+ }
318
+
319
+ /*
320
+ * call-seq:
321
+ * prefix
322
+ *
323
+ * Get the shorthand reference to the namespace associated with the node.
324
+ */
325
+ static VALUE prefix(VALUE self)
326
+ {
327
+ xmlTextReaderPtr reader;
328
+ const char *prefix;
329
+
330
+ Data_Get_Struct(self, xmlTextReader, reader);
331
+ prefix = (const char *)xmlTextReaderConstPrefix(reader);
332
+ if(prefix == NULL) return Qnil;
333
+
334
+ return NOKOGIRI_STR_NEW2(prefix);
335
+ }
336
+
337
+ /*
338
+ * call-seq:
339
+ * namespace_uri
340
+ *
341
+ * Get the URI defining the namespace associated with the node
342
+ */
343
+ static VALUE namespace_uri(VALUE self)
344
+ {
345
+ xmlTextReaderPtr reader;
346
+ const char *uri;
347
+
348
+ Data_Get_Struct(self, xmlTextReader, reader);
349
+ uri = (const char *)xmlTextReaderConstNamespaceUri(reader);
350
+ if(uri == NULL) return Qnil;
351
+
352
+ return NOKOGIRI_STR_NEW2(uri);
353
+ }
354
+
355
+ /*
356
+ * call-seq:
357
+ * local_name
358
+ *
359
+ * Get the local name of the node
360
+ */
361
+ static VALUE local_name(VALUE self)
362
+ {
363
+ xmlTextReaderPtr reader;
364
+ const char *name;
365
+
366
+ Data_Get_Struct(self, xmlTextReader, reader);
367
+ name = (const char *)xmlTextReaderConstLocalName(reader);
368
+ if(name == NULL) return Qnil;
369
+
370
+ return NOKOGIRI_STR_NEW2(name);
371
+ }
372
+
373
+ /*
374
+ * call-seq:
375
+ * name
376
+ *
377
+ * Get the name of the node. Returns a utf-8 encoded string.
378
+ */
379
+ static VALUE name(VALUE self)
380
+ {
381
+ xmlTextReaderPtr reader;
382
+ const char *name;
383
+
384
+ Data_Get_Struct(self, xmlTextReader, reader);
385
+ name = (const char *)xmlTextReaderConstName(reader);
386
+ if(name == NULL) return Qnil;
387
+
388
+ return NOKOGIRI_STR_NEW2(name);
389
+ }
390
+
391
+ /*
392
+ * call-seq:
393
+ * base_uri
394
+ *
395
+ * Get the xml:base of the node
396
+ */
397
+ static VALUE base_uri(VALUE self)
398
+ {
399
+ xmlTextReaderPtr reader;
400
+ const char * base_uri;
401
+
402
+ Data_Get_Struct(self, xmlTextReader, reader);
403
+ base_uri = (const char *)xmlTextReaderBaseUri(reader);
404
+ if (base_uri == NULL) return Qnil;
405
+
406
+ return NOKOGIRI_STR_NEW2(base_uri);
407
+ }
408
+
409
+ /*
410
+ * call-seq:
411
+ * state
412
+ *
413
+ * Get the state of the reader
414
+ */
415
+ static VALUE state(VALUE self)
416
+ {
417
+ xmlTextReaderPtr reader;
418
+ Data_Get_Struct(self, xmlTextReader, reader);
419
+ return INT2NUM((long)xmlTextReaderReadState(reader));
420
+ }
421
+
422
+ /*
423
+ * call-seq:
424
+ * node_type
425
+ *
426
+ * Get the type of readers current node
427
+ */
428
+ static VALUE node_type(VALUE self)
429
+ {
430
+ xmlTextReaderPtr reader;
431
+ Data_Get_Struct(self, xmlTextReader, reader);
432
+ return INT2NUM((long)xmlTextReaderNodeType(reader));
433
+ }
434
+
435
+ /*
436
+ * call-seq:
437
+ * read
438
+ *
439
+ * Move the Reader forward through the XML document.
440
+ */
441
+ static VALUE read_more(VALUE self)
442
+ {
443
+ xmlTextReaderPtr reader;
444
+ xmlErrorPtr error;
445
+ VALUE error_list;
446
+ int ret;
447
+
448
+ Data_Get_Struct(self, xmlTextReader, reader);
449
+
450
+ error_list = rb_funcall(self, rb_intern("errors"), 0);
451
+
452
+ xmlSetStructuredErrorFunc((void *)error_list, Nokogiri_error_array_pusher);
453
+ ret = xmlTextReaderRead(reader);
454
+ xmlSetStructuredErrorFunc(NULL, NULL);
455
+
456
+ if(ret == 1) return self;
457
+ if(ret == 0) return Qnil;
458
+
459
+ error = xmlGetLastError();
460
+ if(error)
461
+ rb_exc_raise(Nokogiri_wrap_xml_syntax_error(error));
462
+ else
463
+ rb_raise(rb_eRuntimeError, "Error pulling: %d", ret);
464
+
465
+ return Qnil;
466
+ }
467
+
468
+ /*
469
+ * call-seq:
470
+ * inner_xml
471
+ *
472
+ * Read the contents of the current node, including child nodes and markup.
473
+ * Returns a utf-8 encoded string.
474
+ */
475
+ static VALUE inner_xml(VALUE self)
476
+ {
477
+ xmlTextReaderPtr reader;
478
+ xmlChar* value;
479
+ VALUE str;
480
+
481
+ Data_Get_Struct(self, xmlTextReader, reader);
482
+
483
+ value = xmlTextReaderReadInnerXml(reader);
484
+
485
+ str = Qnil;
486
+ if(value) {
487
+ str = NOKOGIRI_STR_NEW2((char*)value);
488
+ xmlFree(value);
489
+ }
490
+
491
+ return str;
492
+ }
493
+
494
+ /*
495
+ * call-seq:
496
+ * outer_xml
497
+ *
498
+ * Read the current node and its contents, including child nodes and markup.
499
+ * Returns a utf-8 encoded string.
500
+ */
501
+ static VALUE outer_xml(VALUE self)
502
+ {
503
+ xmlTextReaderPtr reader;
504
+ xmlChar *value;
505
+ VALUE str = Qnil;
506
+
507
+ Data_Get_Struct(self, xmlTextReader, reader);
508
+
509
+ value = xmlTextReaderReadOuterXml(reader);
510
+
511
+ if(value) {
512
+ str = NOKOGIRI_STR_NEW2((char*)value);
513
+ xmlFree(value);
514
+ }
515
+ return str;
516
+ }
517
+
518
+ /*
519
+ * call-seq:
520
+ * from_memory(string, url = nil, encoding = nil, options = 0)
521
+ *
522
+ * Create a new reader that parses +string+
523
+ */
524
+ static VALUE from_memory(int argc, VALUE *argv, VALUE klass)
525
+ {
526
+ VALUE rb_buffer, rb_url, encoding, rb_options;
527
+ xmlTextReaderPtr reader;
528
+ const char * c_url = NULL;
529
+ const char * c_encoding = NULL;
530
+ int c_options = 0;
531
+ VALUE rb_reader, args[3];
532
+
533
+ rb_scan_args(argc, argv, "13", &rb_buffer, &rb_url, &encoding, &rb_options);
534
+
535
+ if (!RTEST(rb_buffer)) rb_raise(rb_eArgError, "string cannot be nil");
536
+ if (RTEST(rb_url)) c_url = StringValueCStr(rb_url);
537
+ if (RTEST(encoding)) c_encoding = StringValueCStr(encoding);
538
+ if (RTEST(rb_options)) c_options = (int)NUM2INT(rb_options);
539
+
540
+ reader = xmlReaderForMemory(
541
+ StringValuePtr(rb_buffer),
542
+ (int)RSTRING_LEN(rb_buffer),
543
+ c_url,
544
+ c_encoding,
545
+ c_options
546
+ );
547
+
548
+ if(reader == NULL) {
549
+ xmlFreeTextReader(reader);
550
+ rb_raise(rb_eRuntimeError, "couldn't create a parser");
551
+ }
552
+
553
+ rb_reader = Data_Wrap_Struct(klass, NULL, dealloc, reader);
554
+ args[0] = rb_buffer;
555
+ args[1] = rb_url;
556
+ args[2] = encoding;
557
+ rb_obj_call_init(rb_reader, 3, args);
558
+
559
+ return rb_reader;
560
+ }
561
+
562
+ /*
563
+ * call-seq:
564
+ * from_io(io, url = nil, encoding = nil, options = 0)
565
+ *
566
+ * Create a new reader that parses +io+
567
+ */
568
+ static VALUE from_io(int argc, VALUE *argv, VALUE klass)
569
+ {
570
+ VALUE rb_io, rb_url, encoding, rb_options;
571
+ xmlTextReaderPtr reader;
572
+ const char * c_url = NULL;
573
+ const char * c_encoding = NULL;
574
+ int c_options = 0;
575
+ VALUE rb_reader, args[3];
576
+
577
+ rb_scan_args(argc, argv, "13", &rb_io, &rb_url, &encoding, &rb_options);
578
+
579
+ if (!RTEST(rb_io)) rb_raise(rb_eArgError, "io cannot be nil");
580
+ if (RTEST(rb_url)) c_url = StringValueCStr(rb_url);
581
+ if (RTEST(encoding)) c_encoding = StringValueCStr(encoding);
582
+ if (RTEST(rb_options)) c_options = (int)NUM2INT(rb_options);
583
+
584
+ reader = xmlReaderForIO(
585
+ (xmlInputReadCallback)io_read_callback,
586
+ (xmlInputCloseCallback)io_close_callback,
587
+ (void *)rb_io,
588
+ c_url,
589
+ c_encoding,
590
+ c_options
591
+ );
592
+
593
+ if(reader == NULL) {
594
+ xmlFreeTextReader(reader);
595
+ rb_raise(rb_eRuntimeError, "couldn't create a parser");
596
+ }
597
+
598
+ rb_reader = Data_Wrap_Struct(klass, NULL, dealloc, reader);
599
+ args[0] = rb_io;
600
+ args[1] = rb_url;
601
+ args[2] = encoding;
602
+ rb_obj_call_init(rb_reader, 3, args);
603
+
604
+ return rb_reader;
605
+ }
606
+
607
+ /*
608
+ * call-seq:
609
+ * reader.empty_element? # => true or false
610
+ *
611
+ * Returns true if the current node is empty, otherwise false.
612
+ */
613
+ static VALUE empty_element_p(VALUE self)
614
+ {
615
+ xmlTextReaderPtr reader;
616
+
617
+ Data_Get_Struct(self, xmlTextReader, reader);
618
+
619
+ if(xmlTextReaderIsEmptyElement(reader))
620
+ return Qtrue;
621
+
622
+ return Qfalse;
623
+ }
624
+
625
+ VALUE cNokogiriXmlReader;
626
+
627
+ void init_xml_reader()
628
+ {
629
+ VALUE module = rb_define_module("Nokogiri");
630
+ VALUE xml = rb_define_module_under(module, "XML");
631
+
632
+ /*
633
+ * The Reader parser allows you to effectively pull parse an XML document.
634
+ * Once instantiated, call Nokogiri::XML::Reader#each to iterate over each
635
+ * node. Note that you may only iterate over the document once!
636
+ */
637
+ VALUE klass = rb_define_class_under(xml, "Reader", rb_cObject);
638
+
639
+ cNokogiriXmlReader = klass;
640
+
641
+ rb_define_singleton_method(klass, "from_memory", from_memory, -1);
642
+ rb_define_singleton_method(klass, "from_io", from_io, -1);
643
+
644
+ rb_define_method(klass, "read", read_more, 0);
645
+ rb_define_method(klass, "inner_xml", inner_xml, 0);
646
+ rb_define_method(klass, "outer_xml", outer_xml, 0);
647
+ rb_define_method(klass, "state", state, 0);
648
+ rb_define_method(klass, "node_type", node_type, 0);
649
+ rb_define_method(klass, "name", name, 0);
650
+ rb_define_method(klass, "local_name", local_name, 0);
651
+ rb_define_method(klass, "namespace_uri", namespace_uri, 0);
652
+ rb_define_method(klass, "prefix", prefix, 0);
653
+ rb_define_method(klass, "value", value, 0);
654
+ rb_define_method(klass, "lang", lang, 0);
655
+ rb_define_method(klass, "xml_version", xml_version, 0);
656
+ rb_define_method(klass, "depth", depth, 0);
657
+ rb_define_method(klass, "attribute_count", attribute_count, 0);
658
+ rb_define_method(klass, "attribute", reader_attribute, 1);
659
+ rb_define_method(klass, "namespaces", namespaces, 0);
660
+ rb_define_method(klass, "attribute_at", attribute_at, 1);
661
+ rb_define_method(klass, "empty_element?", empty_element_p, 0);
662
+ rb_define_method(klass, "attributes?", attributes_eh, 0);
663
+ rb_define_method(klass, "value?", value_eh, 0);
664
+ rb_define_method(klass, "default?", default_eh, 0);
665
+ rb_define_method(klass, "base_uri", base_uri, 0);
666
+
667
+ rb_define_private_method(klass, "attr_nodes", attribute_nodes, 0);
668
+ }