nokogiri 1.3.0-x86-mswin32

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 (256) hide show
  1. data/.autotest +27 -0
  2. data/CHANGELOG.ja.rdoc +233 -0
  3. data/CHANGELOG.rdoc +222 -0
  4. data/Manifest.txt +247 -0
  5. data/README.ja.rdoc +103 -0
  6. data/README.rdoc +117 -0
  7. data/Rakefile +205 -0
  8. data/bin/nokogiri +47 -0
  9. data/ext/nokogiri/extconf.rb +89 -0
  10. data/ext/nokogiri/html_document.c +183 -0
  11. data/ext/nokogiri/html_document.h +10 -0
  12. data/ext/nokogiri/html_element_description.c +272 -0
  13. data/ext/nokogiri/html_element_description.h +10 -0
  14. data/ext/nokogiri/html_entity_lookup.c +30 -0
  15. data/ext/nokogiri/html_entity_lookup.h +8 -0
  16. data/ext/nokogiri/html_sax_parser.c +57 -0
  17. data/ext/nokogiri/html_sax_parser.h +11 -0
  18. data/ext/nokogiri/iconv.dll +0 -0
  19. data/ext/nokogiri/libexslt.dll +0 -0
  20. data/ext/nokogiri/libxml2.dll +0 -0
  21. data/ext/nokogiri/libxslt.dll +0 -0
  22. data/ext/nokogiri/nokogiri.c +81 -0
  23. data/ext/nokogiri/nokogiri.h +149 -0
  24. data/ext/nokogiri/xml_attr.c +92 -0
  25. data/ext/nokogiri/xml_attr.h +9 -0
  26. data/ext/nokogiri/xml_cdata.c +53 -0
  27. data/ext/nokogiri/xml_cdata.h +9 -0
  28. data/ext/nokogiri/xml_comment.c +51 -0
  29. data/ext/nokogiri/xml_comment.h +9 -0
  30. data/ext/nokogiri/xml_document.c +308 -0
  31. data/ext/nokogiri/xml_document.h +21 -0
  32. data/ext/nokogiri/xml_document_fragment.c +48 -0
  33. data/ext/nokogiri/xml_document_fragment.h +10 -0
  34. data/ext/nokogiri/xml_dtd.c +102 -0
  35. data/ext/nokogiri/xml_dtd.h +8 -0
  36. data/ext/nokogiri/xml_entity_reference.c +50 -0
  37. data/ext/nokogiri/xml_entity_reference.h +9 -0
  38. data/ext/nokogiri/xml_io.c +24 -0
  39. data/ext/nokogiri/xml_io.h +10 -0
  40. data/ext/nokogiri/xml_namespace.c +69 -0
  41. data/ext/nokogiri/xml_namespace.h +12 -0
  42. data/ext/nokogiri/xml_node.c +928 -0
  43. data/ext/nokogiri/xml_node.h +14 -0
  44. data/ext/nokogiri/xml_node_set.c +386 -0
  45. data/ext/nokogiri/xml_node_set.h +9 -0
  46. data/ext/nokogiri/xml_processing_instruction.c +54 -0
  47. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  48. data/ext/nokogiri/xml_reader.c +572 -0
  49. data/ext/nokogiri/xml_reader.h +10 -0
  50. data/ext/nokogiri/xml_relax_ng.c +106 -0
  51. data/ext/nokogiri/xml_relax_ng.h +9 -0
  52. data/ext/nokogiri/xml_sax_parser.c +336 -0
  53. data/ext/nokogiri/xml_sax_parser.h +10 -0
  54. data/ext/nokogiri/xml_sax_push_parser.c +86 -0
  55. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  56. data/ext/nokogiri/xml_schema.c +107 -0
  57. data/ext/nokogiri/xml_schema.h +9 -0
  58. data/ext/nokogiri/xml_syntax_error.c +203 -0
  59. data/ext/nokogiri/xml_syntax_error.h +12 -0
  60. data/ext/nokogiri/xml_text.c +47 -0
  61. data/ext/nokogiri/xml_text.h +9 -0
  62. data/ext/nokogiri/xml_xpath.c +53 -0
  63. data/ext/nokogiri/xml_xpath.h +11 -0
  64. data/ext/nokogiri/xml_xpath_context.c +252 -0
  65. data/ext/nokogiri/xml_xpath_context.h +9 -0
  66. data/ext/nokogiri/xslt_stylesheet.c +131 -0
  67. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  68. data/ext/nokogiri/zlib1.dll +0 -0
  69. data/lib/action-nokogiri.rb +36 -0
  70. data/lib/nokogiri.rb +110 -0
  71. data/lib/nokogiri/1.8/nokogiri.so +0 -0
  72. data/lib/nokogiri/1.9/nokogiri.so +0 -0
  73. data/lib/nokogiri/css.rb +25 -0
  74. data/lib/nokogiri/css/generated_parser.rb +748 -0
  75. data/lib/nokogiri/css/generated_tokenizer.rb +144 -0
  76. data/lib/nokogiri/css/node.rb +107 -0
  77. data/lib/nokogiri/css/parser.rb +82 -0
  78. data/lib/nokogiri/css/parser.y +227 -0
  79. data/lib/nokogiri/css/syntax_error.rb +7 -0
  80. data/lib/nokogiri/css/tokenizer.rb +11 -0
  81. data/lib/nokogiri/css/tokenizer.rex +54 -0
  82. data/lib/nokogiri/css/xpath_visitor.rb +172 -0
  83. data/lib/nokogiri/decorators.rb +2 -0
  84. data/lib/nokogiri/decorators/hpricot.rb +3 -0
  85. data/lib/nokogiri/decorators/hpricot/node.rb +56 -0
  86. data/lib/nokogiri/decorators/hpricot/node_set.rb +54 -0
  87. data/lib/nokogiri/decorators/hpricot/xpath_visitor.rb +30 -0
  88. data/lib/nokogiri/decorators/slop.rb +33 -0
  89. data/lib/nokogiri/ffi/html/document.rb +37 -0
  90. data/lib/nokogiri/ffi/html/element_description.rb +85 -0
  91. data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
  92. data/lib/nokogiri/ffi/html/sax/parser.rb +21 -0
  93. data/lib/nokogiri/ffi/io_callbacks.rb +32 -0
  94. data/lib/nokogiri/ffi/libxml.rb +314 -0
  95. data/lib/nokogiri/ffi/structs/common_node.rb +26 -0
  96. data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
  97. data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
  98. data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
  99. data/lib/nokogiri/ffi/structs/xml_attr.rb +19 -0
  100. data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
  101. data/lib/nokogiri/ffi/structs/xml_document.rb +108 -0
  102. data/lib/nokogiri/ffi/structs/xml_dtd.rb +26 -0
  103. data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
  104. data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
  105. data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
  106. data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
  107. data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
  108. data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
  109. data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +14 -0
  110. data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
  111. data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
  112. data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
  113. data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +37 -0
  114. data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
  115. data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
  116. data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
  117. data/lib/nokogiri/ffi/xml/attr.rb +41 -0
  118. data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
  119. data/lib/nokogiri/ffi/xml/comment.rb +18 -0
  120. data/lib/nokogiri/ffi/xml/document.rb +107 -0
  121. data/lib/nokogiri/ffi/xml/document_fragment.rb +26 -0
  122. data/lib/nokogiri/ffi/xml/dtd.rb +42 -0
  123. data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
  124. data/lib/nokogiri/ffi/xml/namespace.rb +38 -0
  125. data/lib/nokogiri/ffi/xml/node.rb +380 -0
  126. data/lib/nokogiri/ffi/xml/node_set.rb +130 -0
  127. data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
  128. data/lib/nokogiri/ffi/xml/reader.rb +217 -0
  129. data/lib/nokogiri/ffi/xml/relax_ng.rb +51 -0
  130. data/lib/nokogiri/ffi/xml/sax/parser.rb +148 -0
  131. data/lib/nokogiri/ffi/xml/sax/push_parser.rb +38 -0
  132. data/lib/nokogiri/ffi/xml/schema.rb +55 -0
  133. data/lib/nokogiri/ffi/xml/syntax_error.rb +76 -0
  134. data/lib/nokogiri/ffi/xml/text.rb +18 -0
  135. data/lib/nokogiri/ffi/xml/xpath.rb +19 -0
  136. data/lib/nokogiri/ffi/xml/xpath_context.rb +135 -0
  137. data/lib/nokogiri/ffi/xslt/stylesheet.rb +47 -0
  138. data/lib/nokogiri/hpricot.rb +62 -0
  139. data/lib/nokogiri/html.rb +34 -0
  140. data/lib/nokogiri/html/builder.rb +35 -0
  141. data/lib/nokogiri/html/document.rb +71 -0
  142. data/lib/nokogiri/html/document_fragment.rb +15 -0
  143. data/lib/nokogiri/html/element_description.rb +23 -0
  144. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  145. data/lib/nokogiri/html/sax/parser.rb +47 -0
  146. data/lib/nokogiri/nokogiri.rb +1 -0
  147. data/lib/nokogiri/syntax_error.rb +4 -0
  148. data/lib/nokogiri/version.rb +29 -0
  149. data/lib/nokogiri/version_warning.rb +11 -0
  150. data/lib/nokogiri/xml.rb +62 -0
  151. data/lib/nokogiri/xml/attr.rb +9 -0
  152. data/lib/nokogiri/xml/builder.rb +254 -0
  153. data/lib/nokogiri/xml/cdata.rb +11 -0
  154. data/lib/nokogiri/xml/document.rb +100 -0
  155. data/lib/nokogiri/xml/document_fragment.rb +49 -0
  156. data/lib/nokogiri/xml/dtd.rb +11 -0
  157. data/lib/nokogiri/xml/entity_declaration.rb +11 -0
  158. data/lib/nokogiri/xml/fragment_handler.rb +55 -0
  159. data/lib/nokogiri/xml/namespace.rb +7 -0
  160. data/lib/nokogiri/xml/node.rb +745 -0
  161. data/lib/nokogiri/xml/node/save_options.rb +42 -0
  162. data/lib/nokogiri/xml/node_set.rb +238 -0
  163. data/lib/nokogiri/xml/notation.rb +6 -0
  164. data/lib/nokogiri/xml/parse_options.rb +80 -0
  165. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  166. data/lib/nokogiri/xml/reader.rb +66 -0
  167. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  168. data/lib/nokogiri/xml/sax.rb +3 -0
  169. data/lib/nokogiri/xml/sax/document.rb +143 -0
  170. data/lib/nokogiri/xml/sax/parser.rb +101 -0
  171. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  172. data/lib/nokogiri/xml/schema.rb +65 -0
  173. data/lib/nokogiri/xml/syntax_error.rb +34 -0
  174. data/lib/nokogiri/xml/xpath.rb +10 -0
  175. data/lib/nokogiri/xml/xpath/syntax_error.rb +8 -0
  176. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  177. data/lib/nokogiri/xslt.rb +48 -0
  178. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  179. data/lib/xsd/xmlparser/nokogiri.rb +64 -0
  180. data/tasks/test.rb +161 -0
  181. data/test/css/test_nthiness.rb +160 -0
  182. data/test/css/test_parser.rb +277 -0
  183. data/test/css/test_tokenizer.rb +176 -0
  184. data/test/css/test_xpath_visitor.rb +76 -0
  185. data/test/ffi/test_document.rb +35 -0
  186. data/test/files/address_book.rlx +12 -0
  187. data/test/files/address_book.xml +10 -0
  188. data/test/files/dont_hurt_em_why.xml +422 -0
  189. data/test/files/exslt.xml +8 -0
  190. data/test/files/exslt.xslt +35 -0
  191. data/test/files/po.xml +32 -0
  192. data/test/files/po.xsd +66 -0
  193. data/test/files/staff.xml +59 -0
  194. data/test/files/staff.xslt +32 -0
  195. data/test/files/tlm.html +850 -0
  196. data/test/helper.rb +123 -0
  197. data/test/hpricot/files/basic.xhtml +17 -0
  198. data/test/hpricot/files/boingboing.html +2266 -0
  199. data/test/hpricot/files/cy0.html +3653 -0
  200. data/test/hpricot/files/immob.html +400 -0
  201. data/test/hpricot/files/pace_application.html +1320 -0
  202. data/test/hpricot/files/tenderlove.html +16 -0
  203. data/test/hpricot/files/uswebgen.html +220 -0
  204. data/test/hpricot/files/utf8.html +1054 -0
  205. data/test/hpricot/files/week9.html +1723 -0
  206. data/test/hpricot/files/why.xml +19 -0
  207. data/test/hpricot/load_files.rb +11 -0
  208. data/test/hpricot/test_alter.rb +68 -0
  209. data/test/hpricot/test_builder.rb +20 -0
  210. data/test/hpricot/test_parser.rb +426 -0
  211. data/test/hpricot/test_paths.rb +15 -0
  212. data/test/hpricot/test_preserved.rb +77 -0
  213. data/test/hpricot/test_xml.rb +30 -0
  214. data/test/html/sax/test_parser.rb +52 -0
  215. data/test/html/test_builder.rb +156 -0
  216. data/test/html/test_document.rb +361 -0
  217. data/test/html/test_document_encoding.rb +46 -0
  218. data/test/html/test_document_fragment.rb +97 -0
  219. data/test/html/test_element_description.rb +95 -0
  220. data/test/html/test_named_characters.rb +14 -0
  221. data/test/html/test_node.rb +165 -0
  222. data/test/test_convert_xpath.rb +186 -0
  223. data/test/test_css_cache.rb +56 -0
  224. data/test/test_gc.rb +15 -0
  225. data/test/test_memory_leak.rb +77 -0
  226. data/test/test_nokogiri.rb +127 -0
  227. data/test/test_reader.rb +316 -0
  228. data/test/test_xslt_transforms.rb +131 -0
  229. data/test/xml/node/test_save_options.rb +20 -0
  230. data/test/xml/node/test_subclass.rb +44 -0
  231. data/test/xml/sax/test_parser.rb +169 -0
  232. data/test/xml/sax/test_push_parser.rb +92 -0
  233. data/test/xml/test_attr.rb +38 -0
  234. data/test/xml/test_builder.rb +73 -0
  235. data/test/xml/test_cdata.rb +38 -0
  236. data/test/xml/test_comment.rb +23 -0
  237. data/test/xml/test_document.rb +397 -0
  238. data/test/xml/test_document_encoding.rb +26 -0
  239. data/test/xml/test_document_fragment.rb +76 -0
  240. data/test/xml/test_dtd.rb +42 -0
  241. data/test/xml/test_dtd_encoding.rb +31 -0
  242. data/test/xml/test_entity_reference.rb +21 -0
  243. data/test/xml/test_namespace.rb +43 -0
  244. data/test/xml/test_node.rb +808 -0
  245. data/test/xml/test_node_attributes.rb +34 -0
  246. data/test/xml/test_node_encoding.rb +84 -0
  247. data/test/xml/test_node_set.rb +368 -0
  248. data/test/xml/test_parse_options.rb +52 -0
  249. data/test/xml/test_processing_instruction.rb +30 -0
  250. data/test/xml/test_reader_encoding.rb +126 -0
  251. data/test/xml/test_relax_ng.rb +60 -0
  252. data/test/xml/test_schema.rb +65 -0
  253. data/test/xml/test_text.rb +18 -0
  254. data/test/xml/test_unparented_node.rb +381 -0
  255. data/test/xml/test_xpath.rb +106 -0
  256. metadata +409 -0
@@ -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,572 @@
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
+ #define XMLNS_PREFIX "xmlns"
29
+ #define XMLNS_PREFIX_LEN 6 /* including either colon or \0 */
30
+ #define XMLNS_BUFFER_LEN 128
31
+ static void Nokogiri_xml_node_namespaces(xmlNodePtr node, VALUE attr_hash)
32
+ {
33
+ xmlNsPtr ns;
34
+ static char buffer[XMLNS_BUFFER_LEN] ;
35
+ char *key ;
36
+ size_t keylen ;
37
+
38
+ if (node->type != XML_ELEMENT_NODE) return ;
39
+
40
+ ns = node->nsDef;
41
+ while (ns != NULL) {
42
+
43
+ keylen = XMLNS_PREFIX_LEN + (ns->prefix ? (strlen((const char*)ns->prefix) + 1) : 0) ;
44
+ if (keylen > XMLNS_BUFFER_LEN) {
45
+ key = (char*)malloc(keylen) ;
46
+ } else {
47
+ key = buffer ;
48
+ }
49
+
50
+ if (ns->prefix) {
51
+ sprintf(key, "%s:%s", XMLNS_PREFIX, ns->prefix);
52
+ } else {
53
+ sprintf(key, "%s", XMLNS_PREFIX);
54
+ }
55
+
56
+ rb_hash_aset(attr_hash,
57
+ NOKOGIRI_STR_NEW2(key, node->doc->encoding),
58
+ (ns->href ? NOKOGIRI_STR_NEW2(ns->href, node->doc->encoding) : Qnil)
59
+ );
60
+ if (key != buffer) {
61
+ free(key);
62
+ }
63
+ ns = ns->next ;
64
+ }
65
+ }
66
+
67
+
68
+ /*
69
+ * call-seq:
70
+ * default?
71
+ *
72
+ * Was an attribute generated from the default value in the DTD or schema?
73
+ */
74
+ static VALUE default_eh(VALUE self)
75
+ {
76
+ xmlTextReaderPtr reader;
77
+ Data_Get_Struct(self, xmlTextReader, reader);
78
+ int eh = xmlTextReaderIsDefault(reader);
79
+ if(eh == 0) return Qfalse;
80
+ if(eh == 1) return Qtrue;
81
+
82
+ return Qnil;
83
+ }
84
+
85
+ /*
86
+ * call-seq:
87
+ * value?
88
+ *
89
+ * Does this node have a text value?
90
+ */
91
+ static VALUE value_eh(VALUE self)
92
+ {
93
+ xmlTextReaderPtr reader;
94
+ Data_Get_Struct(self, xmlTextReader, reader);
95
+ int eh = xmlTextReaderHasValue(reader);
96
+ if(eh == 0) return Qfalse;
97
+ if(eh == 1) return Qtrue;
98
+
99
+ return Qnil;
100
+ }
101
+
102
+ /*
103
+ * call-seq:
104
+ * attributes?
105
+ *
106
+ * Does this node have attributes?
107
+ */
108
+ static VALUE attributes_eh(VALUE self)
109
+ {
110
+ xmlTextReaderPtr reader;
111
+ Data_Get_Struct(self, xmlTextReader, reader);
112
+ int eh = has_attributes(reader);
113
+ if(eh == 0) return Qfalse;
114
+ if(eh == 1) return Qtrue;
115
+
116
+ return Qnil;
117
+ }
118
+
119
+ /*
120
+ * call-seq:
121
+ * namespaces
122
+ *
123
+ * Get a hash of namespaces for this Node
124
+ */
125
+ static VALUE namespaces(VALUE self)
126
+ {
127
+ xmlTextReaderPtr reader;
128
+ VALUE attr ;
129
+
130
+ Data_Get_Struct(self, xmlTextReader, reader);
131
+
132
+ attr = rb_hash_new() ;
133
+
134
+ if (! has_attributes(reader))
135
+ return attr ;
136
+
137
+ xmlNodePtr ptr = xmlTextReaderExpand(reader);
138
+ if(ptr == NULL) return Qnil;
139
+
140
+ Nokogiri_xml_node_namespaces(ptr, attr);
141
+
142
+ return attr ;
143
+ }
144
+
145
+ /*
146
+ * call-seq:
147
+ * attribute_nodes
148
+ *
149
+ * Get a list of attributes for this Node
150
+ */
151
+ static VALUE attribute_nodes(VALUE self)
152
+ {
153
+ xmlTextReaderPtr reader;
154
+ VALUE attr ;
155
+
156
+ Data_Get_Struct(self, xmlTextReader, reader);
157
+
158
+ attr = rb_ary_new() ;
159
+
160
+ if (! has_attributes(reader))
161
+ return attr ;
162
+
163
+ xmlNodePtr ptr = xmlTextReaderExpand(reader);
164
+ if(ptr == NULL) return Qnil;
165
+
166
+ // FIXME I'm not sure if this is correct..... I don't really like pointing
167
+ // at this document, but I have to because of the assertions in
168
+ // the node wrapping code.
169
+ if(! DOC_RUBY_OBJECT_TEST(ptr->doc)) {
170
+ VALUE rb_doc = Nokogiri_wrap_xml_document(cNokogiriXmlDocument, ptr->doc);
171
+ RDATA(rb_doc)->dfree = NULL;
172
+ }
173
+ VALUE enc = rb_iv_get(self, "@encoding");
174
+
175
+ if(enc != Qnil && NULL == ptr->doc->encoding) {
176
+ ptr->doc->encoding = calloc((size_t)RSTRING_LEN(enc), sizeof(char));
177
+ strncpy(
178
+ (char *)ptr->doc->encoding,
179
+ StringValuePtr(enc),
180
+ (size_t)RSTRING_LEN(enc)
181
+ );
182
+ }
183
+
184
+ Nokogiri_xml_node_properties(ptr, attr);
185
+
186
+ return attr ;
187
+ }
188
+
189
+ /*
190
+ * call-seq:
191
+ * attribute_at(index)
192
+ *
193
+ * Get the value of attribute at +index+
194
+ */
195
+ static VALUE attribute_at(VALUE self, VALUE index)
196
+ {
197
+ xmlTextReaderPtr reader;
198
+ Data_Get_Struct(self, xmlTextReader, reader);
199
+
200
+ if(index == Qnil) return Qnil;
201
+ index = rb_funcall(index, rb_intern("to_i"), 0);
202
+
203
+ xmlChar * value = xmlTextReaderGetAttributeNo(
204
+ reader,
205
+ NUM2INT(index)
206
+ );
207
+ if(value == NULL) return Qnil;
208
+
209
+ VALUE MAYBE_UNUSED(enc) = rb_iv_get(self, "@encoding");
210
+ VALUE rb_value = NOKOGIRI_STR_NEW2(value,
211
+ RTEST(enc) ? StringValuePtr(enc) : NULL);
212
+ xmlFree(value);
213
+ return rb_value;
214
+ }
215
+
216
+ /*
217
+ * call-seq:
218
+ * attribute(name)
219
+ *
220
+ * Get the value of attribute named +name+
221
+ */
222
+ static VALUE reader_attribute(VALUE self, VALUE name)
223
+ {
224
+ xmlTextReaderPtr reader;
225
+ xmlChar *value ;
226
+ Data_Get_Struct(self, xmlTextReader, reader);
227
+
228
+ if(name == Qnil) return Qnil;
229
+ name = StringValue(name) ;
230
+
231
+ value = xmlTextReaderGetAttribute(reader, (xmlChar*)StringValuePtr(name));
232
+ if(value == NULL) {
233
+ /* this section is an attempt to workaround older versions of libxml that
234
+ don't handle namespaces properly in all attribute-and-friends functions */
235
+ xmlChar *prefix = NULL ;
236
+ xmlChar *localname = xmlSplitQName2((xmlChar*)StringValuePtr(name), &prefix);
237
+ if (localname != NULL) {
238
+ value = xmlTextReaderLookupNamespace(reader, localname);
239
+ xmlFree(localname) ;
240
+ } else {
241
+ value = xmlTextReaderLookupNamespace(reader, prefix);
242
+ }
243
+ xmlFree(prefix);
244
+ }
245
+ if(value == NULL) return Qnil;
246
+
247
+ VALUE MAYBE_UNUSED(enc) = rb_iv_get(self, "@encoding");
248
+ VALUE rb_value = NOKOGIRI_STR_NEW2(value,
249
+ RTEST(enc) ? StringValuePtr(enc) : NULL);
250
+ xmlFree(value);
251
+ return rb_value;
252
+ }
253
+
254
+ /*
255
+ * call-seq:
256
+ * attribute_count
257
+ *
258
+ * Get the number of attributes for the current node
259
+ */
260
+ static VALUE attribute_count(VALUE self)
261
+ {
262
+ xmlTextReaderPtr reader;
263
+ Data_Get_Struct(self, xmlTextReader, reader);
264
+ int count = xmlTextReaderAttributeCount(reader);
265
+ if(count == -1) return Qnil;
266
+
267
+ return INT2NUM(count);
268
+ }
269
+
270
+ /*
271
+ * call-seq:
272
+ * depth
273
+ *
274
+ * Get the depth of the node
275
+ */
276
+ static VALUE depth(VALUE self)
277
+ {
278
+ xmlTextReaderPtr reader;
279
+ Data_Get_Struct(self, xmlTextReader, reader);
280
+ int depth = xmlTextReaderDepth(reader);
281
+ if(depth == -1) return Qnil;
282
+
283
+ return INT2NUM(depth);
284
+ }
285
+
286
+ /*
287
+ * call-seq:
288
+ * xml_version
289
+ *
290
+ * Get the XML version of the document being read
291
+ */
292
+ static VALUE xml_version(VALUE self)
293
+ {
294
+ xmlTextReaderPtr reader;
295
+ Data_Get_Struct(self, xmlTextReader, reader);
296
+ const char * version = (const char *)xmlTextReaderConstXmlVersion(reader);
297
+ if(version == NULL) return Qnil;
298
+
299
+ return NOKOGIRI_STR_NEW2(version, "UTF-8");
300
+ }
301
+
302
+ /*
303
+ * call-seq:
304
+ * lang
305
+ *
306
+ * Get the xml:lang scope within which the node resides.
307
+ */
308
+ static VALUE lang(VALUE self)
309
+ {
310
+ xmlTextReaderPtr reader;
311
+ Data_Get_Struct(self, xmlTextReader, reader);
312
+ const char * lang = (const char *)xmlTextReaderConstXmlLang(reader);
313
+ if(lang == NULL) return Qnil;
314
+
315
+ VALUE MAYBE_UNUSED(enc) = rb_iv_get(self, "@encoding");
316
+ return NOKOGIRI_STR_NEW2(lang,
317
+ RTEST(enc) ? StringValuePtr(enc) : NULL);
318
+ }
319
+
320
+ /*
321
+ * call-seq:
322
+ * value
323
+ *
324
+ * Get the text value of the node if present
325
+ */
326
+ static VALUE value(VALUE self)
327
+ {
328
+ xmlTextReaderPtr reader;
329
+ Data_Get_Struct(self, xmlTextReader, reader);
330
+ const char * value = (const char *)xmlTextReaderConstValue(reader);
331
+ if(value == NULL) return Qnil;
332
+
333
+ VALUE MAYBE_UNUSED(enc) = rb_iv_get(self, "@encoding");
334
+ return NOKOGIRI_STR_NEW2(value,
335
+ RTEST(enc) ? StringValuePtr(enc) : NULL);
336
+ }
337
+
338
+ /*
339
+ * call-seq:
340
+ * prefix
341
+ *
342
+ * Get the shorthand reference to the namespace associated with the node.
343
+ */
344
+ static VALUE prefix(VALUE self)
345
+ {
346
+ xmlTextReaderPtr reader;
347
+ Data_Get_Struct(self, xmlTextReader, reader);
348
+ const char * prefix = (const char *)xmlTextReaderConstPrefix(reader);
349
+ if(prefix == NULL) return Qnil;
350
+
351
+ VALUE MAYBE_UNUSED(enc) = rb_iv_get(self, "@encoding");
352
+ return NOKOGIRI_STR_NEW2(prefix,
353
+ RTEST(enc) ? StringValuePtr(enc) : NULL);
354
+ }
355
+
356
+ /*
357
+ * call-seq:
358
+ * namespace_uri
359
+ *
360
+ * Get the URI defining the namespace associated with the node
361
+ */
362
+ static VALUE namespace_uri(VALUE self)
363
+ {
364
+ xmlTextReaderPtr reader;
365
+ Data_Get_Struct(self, xmlTextReader, reader);
366
+ const char * uri = (const char *)xmlTextReaderConstNamespaceUri(reader);
367
+ if(uri == NULL) return Qnil;
368
+
369
+ VALUE MAYBE_UNUSED(enc) = rb_iv_get(self, "@encoding");
370
+ return NOKOGIRI_STR_NEW2(uri,
371
+ RTEST(enc) ? StringValuePtr(enc) : NULL);
372
+ }
373
+
374
+ /*
375
+ * call-seq:
376
+ * local_name
377
+ *
378
+ * Get the local name of the node
379
+ */
380
+ static VALUE local_name(VALUE self)
381
+ {
382
+ xmlTextReaderPtr reader;
383
+ Data_Get_Struct(self, xmlTextReader, reader);
384
+ const char * name = (const char *)xmlTextReaderConstLocalName(reader);
385
+ if(name == NULL) return Qnil;
386
+
387
+ VALUE MAYBE_UNUSED(enc) = rb_iv_get(self, "@encoding");
388
+ return NOKOGIRI_STR_NEW2(name,
389
+ RTEST(enc) ? StringValuePtr(enc) : NULL);
390
+ }
391
+
392
+ /*
393
+ * call-seq:
394
+ * name
395
+ *
396
+ * Get the name of the node
397
+ */
398
+ static VALUE name(VALUE self)
399
+ {
400
+ xmlTextReaderPtr reader;
401
+ Data_Get_Struct(self, xmlTextReader, reader);
402
+ const char * name = (const char *)xmlTextReaderConstName(reader);
403
+ if(name == NULL) return Qnil;
404
+
405
+ VALUE MAYBE_UNUSED(enc) = rb_iv_get(self, "@encoding");
406
+ return NOKOGIRI_STR_NEW2(name,
407
+ RTEST(enc) ? StringValuePtr(enc) : NULL);
408
+ }
409
+
410
+ /*
411
+ * call-seq:
412
+ * state
413
+ *
414
+ * Get the state of the reader
415
+ */
416
+ static VALUE state(VALUE self)
417
+ {
418
+ xmlTextReaderPtr reader;
419
+ Data_Get_Struct(self, xmlTextReader, reader);
420
+ return INT2NUM(xmlTextReaderReadState(reader));
421
+ }
422
+
423
+ /*
424
+ * call-seq:
425
+ * read
426
+ *
427
+ * Move the Reader forward through the XML document.
428
+ */
429
+ static VALUE read_more(VALUE self)
430
+ {
431
+ xmlTextReaderPtr reader;
432
+ Data_Get_Struct(self, xmlTextReader, reader);
433
+
434
+ VALUE error_list = rb_funcall(self, rb_intern("errors"), 0);
435
+
436
+ xmlSetStructuredErrorFunc((void *)error_list, Nokogiri_error_array_pusher);
437
+ int ret = xmlTextReaderRead(reader);
438
+ xmlSetStructuredErrorFunc(NULL, NULL);
439
+
440
+ if(ret == 1) return self;
441
+ if(ret == 0) return Qnil;
442
+
443
+ xmlErrorPtr error = xmlGetLastError();
444
+ if(error)
445
+ rb_funcall(rb_mKernel, rb_intern("raise"), 1,
446
+ Nokogiri_wrap_xml_syntax_error((VALUE)NULL, error)
447
+ );
448
+ else
449
+ rb_raise(rb_eRuntimeError, "Error pulling: %d", ret);
450
+
451
+ return Qnil;
452
+ }
453
+
454
+ /*
455
+ * call-seq:
456
+ * from_memory(string, url = nil, encoding = nil, options = 0)
457
+ *
458
+ * Create a new reader that parses +string+
459
+ */
460
+ static VALUE from_memory(int argc, VALUE *argv, VALUE klass)
461
+ {
462
+ VALUE rb_buffer, rb_url, encoding, rb_options;
463
+
464
+ const char * c_url = NULL;
465
+ const char * c_encoding = NULL;
466
+ int c_options = 0;
467
+
468
+ rb_scan_args(argc, argv, "13", &rb_buffer, &rb_url, &encoding, &rb_options);
469
+
470
+ if (!RTEST(rb_buffer)) rb_raise(rb_eArgError, "string cannot be nil");
471
+ if (RTEST(rb_url)) c_url = StringValuePtr(rb_url);
472
+ if (RTEST(encoding)) c_encoding = StringValuePtr(encoding);
473
+ if (RTEST(rb_options)) c_options = NUM2INT(rb_options);
474
+
475
+ xmlTextReaderPtr reader = xmlReaderForMemory(
476
+ StringValuePtr(rb_buffer),
477
+ RSTRING_LEN(rb_buffer),
478
+ c_url,
479
+ c_encoding,
480
+ c_options
481
+ );
482
+
483
+ if(reader == NULL) {
484
+ xmlFreeTextReader(reader);
485
+ rb_raise(rb_eRuntimeError, "couldn't create a parser");
486
+ }
487
+
488
+ VALUE rb_reader = Data_Wrap_Struct(klass, NULL, dealloc, reader);
489
+ rb_funcall(rb_reader, rb_intern("initialize"), 3, rb_buffer, rb_url, encoding);
490
+
491
+ return rb_reader;
492
+ }
493
+
494
+ /*
495
+ * call-seq:
496
+ * from_io(io, url = nil, encoding = nil, options = 0)
497
+ *
498
+ * Create a new reader that parses +io+
499
+ */
500
+ static VALUE from_io(int argc, VALUE *argv, VALUE klass)
501
+ {
502
+ VALUE rb_io, rb_url, encoding, rb_options;
503
+
504
+ const char * c_url = NULL;
505
+ const char * c_encoding = NULL;
506
+ int c_options = 0;
507
+
508
+ rb_scan_args(argc, argv, "13", &rb_io, &rb_url, &encoding, &rb_options);
509
+
510
+ if (!RTEST(rb_io)) rb_raise(rb_eArgError, "io cannot be nil");
511
+ if (RTEST(rb_url)) c_url = StringValuePtr(rb_url);
512
+ if (RTEST(encoding)) c_encoding = StringValuePtr(encoding);
513
+ if (RTEST(rb_options)) c_options = NUM2INT(rb_options);
514
+
515
+ xmlTextReaderPtr reader = xmlReaderForIO(
516
+ (xmlInputReadCallback)io_read_callback,
517
+ (xmlInputCloseCallback)io_close_callback,
518
+ (void *)rb_io,
519
+ c_url,
520
+ c_encoding,
521
+ c_options
522
+ );
523
+
524
+ if(reader == NULL) {
525
+ xmlFreeTextReader(reader);
526
+ rb_raise(rb_eRuntimeError, "couldn't create a parser");
527
+ }
528
+
529
+ VALUE rb_reader = Data_Wrap_Struct(klass, NULL, dealloc, reader);
530
+ rb_funcall(rb_reader, rb_intern("initialize"), 3, rb_io, rb_url, encoding);
531
+
532
+ return rb_reader;
533
+ }
534
+
535
+ VALUE cNokogiriXmlReader;
536
+
537
+ void init_xml_reader()
538
+ {
539
+ VALUE module = rb_define_module("Nokogiri");
540
+ VALUE xml = rb_define_module_under(module, "XML");
541
+
542
+ /*
543
+ * The Reader parser allows you to effectively pull parse an XML document.
544
+ * Once instantiated, call Nokogiri::XML::Reader#each to iterate over each
545
+ * node. Note that you may only iterate over the document once!
546
+ */
547
+ VALUE klass = rb_define_class_under(xml, "Reader", rb_cObject);
548
+
549
+ cNokogiriXmlReader = klass;
550
+
551
+ rb_define_singleton_method(klass, "from_memory", from_memory, -1);
552
+ rb_define_singleton_method(klass, "from_io", from_io, -1);
553
+
554
+ rb_define_method(klass, "read", read_more, 0);
555
+ rb_define_method(klass, "state", state, 0);
556
+ rb_define_method(klass, "name", name, 0);
557
+ rb_define_method(klass, "local_name", local_name, 0);
558
+ rb_define_method(klass, "namespace_uri", namespace_uri, 0);
559
+ rb_define_method(klass, "prefix", prefix, 0);
560
+ rb_define_method(klass, "value", value, 0);
561
+ rb_define_method(klass, "lang", lang, 0);
562
+ rb_define_method(klass, "xml_version", xml_version, 0);
563
+ rb_define_method(klass, "depth", depth, 0);
564
+ rb_define_method(klass, "attribute_count", attribute_count, 0);
565
+ rb_define_method(klass, "attribute", reader_attribute, 1);
566
+ rb_define_method(klass, "namespaces", namespaces, 0);
567
+ rb_define_method(klass, "attribute_at", attribute_at, 1);
568
+ rb_define_method(klass, "attribute_nodes", attribute_nodes, 0);
569
+ rb_define_method(klass, "attributes?", attributes_eh, 0);
570
+ rb_define_method(klass, "value?", value_eh, 0);
571
+ rb_define_method(klass, "default?", default_eh, 0);
572
+ }