libxml-ruby 4.1.1 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/HISTORY +22 -0
  3. data/ext/libxml/extconf.rb +67 -61
  4. data/ext/libxml/ruby_libxml.h +43 -44
  5. data/ext/libxml/ruby_xml.c +0 -343
  6. data/ext/libxml/ruby_xml.h +9 -10
  7. data/ext/libxml/ruby_xml_attr_decl.c +154 -153
  8. data/ext/libxml/ruby_xml_attributes.c +276 -275
  9. data/ext/libxml/ruby_xml_attributes.h +2 -0
  10. data/ext/libxml/ruby_xml_document.c +6 -6
  11. data/ext/libxml/ruby_xml_document.h +11 -11
  12. data/ext/libxml/ruby_xml_dtd.c +3 -3
  13. data/ext/libxml/ruby_xml_encoding.h +20 -18
  14. data/ext/libxml/ruby_xml_error.c +9 -6
  15. data/ext/libxml/ruby_xml_error.h +2 -2
  16. data/ext/libxml/ruby_xml_html_parser_context.c +35 -21
  17. data/ext/libxml/ruby_xml_namespace.c +0 -3
  18. data/ext/libxml/ruby_xml_node.c +1394 -1398
  19. data/ext/libxml/ruby_xml_parser.h +1 -1
  20. data/ext/libxml/ruby_xml_parser_context.c +47 -39
  21. data/ext/libxml/ruby_xml_parser_options.c +9 -1
  22. data/ext/libxml/ruby_xml_parser_options.h +1 -1
  23. data/ext/libxml/ruby_xml_reader.c +1244 -1242
  24. data/ext/libxml/ruby_xml_relaxng.c +113 -112
  25. data/ext/libxml/ruby_xml_sax2_handler.c +1 -1
  26. data/ext/libxml/ruby_xml_sax_parser.c +1 -9
  27. data/ext/libxml/ruby_xml_schema.c +422 -420
  28. data/ext/libxml/ruby_xml_schema_attribute.c +108 -107
  29. data/ext/libxml/ruby_xml_schema_element.c +70 -69
  30. data/ext/libxml/ruby_xml_schema_type.c +252 -251
  31. data/ext/libxml/ruby_xml_version.h +5 -5
  32. data/ext/libxml/ruby_xml_writer.c +1138 -1137
  33. data/ext/libxml/ruby_xml_xpath.c +1 -1
  34. data/ext/libxml/ruby_xml_xpath_context.c +2 -2
  35. data/ext/libxml/ruby_xml_xpath_expression.c +81 -81
  36. data/ext/libxml/ruby_xml_xpath_object.c +340 -339
  37. data/lib/libxml/document.rb +13 -13
  38. data/lib/libxml/html_parser.rb +23 -23
  39. data/lib/libxml/parser.rb +26 -24
  40. data/lib/libxml/schema/element.rb +27 -19
  41. data/test/test.rb +5 -0
  42. data/test/test_document_write.rb +1 -4
  43. data/test/test_dtd.rb +1 -4
  44. data/test/test_encoding.rb +1 -4
  45. data/test/test_helper.rb +9 -2
  46. data/test/test_html_parser.rb +162 -162
  47. data/test/test_namespace.rb +1 -3
  48. data/test/test_node.rb +1 -3
  49. data/test/test_node_write.rb +1 -4
  50. data/test/test_parser.rb +26 -17
  51. data/test/test_reader.rb +4 -4
  52. data/test/test_sax_parser.rb +1 -1
  53. data/test/test_schema.rb +237 -231
  54. data/test/test_xml.rb +0 -99
  55. metadata +4 -3
@@ -1,1398 +1,1394 @@
1
- #include "ruby_libxml.h"
2
- #include "ruby_xml_node.h"
3
- #include <assert.h>
4
-
5
- #include <libxml/debugXML.h>
6
- #include <libxml/parserInternals.h>
7
- #include <libxml/xlink.h>
8
-
9
- VALUE cXMLNode;
10
-
11
- /* Document-class: LibXML::XML::Node
12
- *
13
- * Nodes are the primary objects that make up an XML document.
14
- * The node class represents most node types that are found in
15
- * an XML document (but not LibXML::XML::Attributes, see LibXML::XML::Attr).
16
- * It exposes libxml's full API for creating, querying
17
- * moving and deleting node objects. Many of these methods are
18
- * documented in the DOM Level 3 specification found at:
19
- * http://www.w3.org/TR/DOM-Level-3-Core/. */
20
-
21
-
22
- /* Memory management:
23
- *
24
- * The bindings create a one-to-one mapping between ruby objects and
25
- * libxml documents and libxml parent nodes (ie, nodes that do not
26
- * have a parent and do not belong to a document). In these cases,
27
- * the bindings manage the memory. They do this by installing a free
28
- * function and storing a back pointer to the Ruby object from the xmlnode
29
- * using the _private member on libxml structures. When the Ruby object
30
- * goes out of scope, the underlying libxml structure is freed. Libxml
31
- * itself then frees all child node (recursively).
32
- *
33
- * For all other nodes (the vast majority), the bindings create temporary
34
- * Ruby objects that get freed once they go out of scope. Thus there can be
35
- * more than one ruby object pointing to the same xml node. To mostly hide
36
- * this from programmers on the ruby side, the #eql? and #== methods are
37
- * overriden to check if two ruby objects wrap the same xmlnode. If they do,
38
- * then the methods return true. During the mark phase, each of these temporary
39
- * objects marks its owning document, thereby keeping the Ruby document object
40
- * alive and thus the xmldoc tree.
41
- *
42
- * In the sweep phase of the garbage collector, or when a program ends,
43
- * there is no order to how Ruby objects are freed. In fact, the ruby document
44
- * object is almost always freed before any ruby objects that wrap child nodes.
45
- * However, this is ok because those ruby objects do not have a free function
46
- * and are no longer in scope (since if they were the document would not be freed).
47
- */
48
-
49
- static void rxml_node_free(xmlNodePtr xnode)
50
- {
51
- /* The ruby object wrapping the xml object no longer exists and this
52
- is a standalone node without a document or parent so ruby is
53
- responsible for freeing the underlying node.*/
54
- if (xnode->doc == NULL && xnode->parent == NULL)
55
- {
56
- // Remove the back linkage from libxml to Ruby
57
- xnode->_private = NULL;
58
- xmlFreeNode(xnode);
59
- }
60
- }
61
-
62
- void rxml_node_manage(xmlNodePtr xnode, VALUE node)
63
- {
64
- RDATA(node)->dfree = (RUBY_DATA_FUNC)rxml_node_free;
65
- xnode->_private = (void*)node;
66
- }
67
-
68
- void rxml_node_unmanage(xmlNodePtr xnode, VALUE node)
69
- {
70
- RDATA(node)->dfree = NULL;
71
- xnode->_private = NULL;
72
- }
73
-
74
- xmlNodePtr rxml_node_root(xmlNodePtr xnode)
75
- {
76
- xmlNodePtr current = xnode;
77
-
78
- while (current->parent)
79
- {
80
- current = current->parent;
81
- }
82
-
83
- return current;
84
- }
85
-
86
- void rxml_node_mark(xmlNodePtr xnode)
87
- {
88
- if (xnode->doc)
89
- {
90
- if (xnode->doc->_private)
91
- {
92
- VALUE doc = (VALUE)xnode->doc->_private;
93
- rb_gc_mark(doc);
94
- }
95
- }
96
- else if (xnode->parent)
97
- {
98
- xmlNodePtr root = rxml_node_root(xnode);
99
- if (root->_private)
100
- {
101
- VALUE node = (VALUE)root->_private;
102
- rb_gc_mark(node);
103
- }
104
- }
105
- }
106
-
107
- VALUE rxml_node_wrap(xmlNodePtr xnode)
108
- {
109
- VALUE result = Qnil;
110
-
111
- // Is this node already wrapped?
112
- if (xnode->_private)
113
- {
114
- result = (VALUE)xnode->_private;
115
- }
116
- else
117
- {
118
- result = Data_Wrap_Struct(cXMLNode, rxml_node_mark, NULL, xnode);
119
- }
120
-
121
- if (!xnode->doc && !xnode->parent)
122
- {
123
- rxml_node_manage(xnode, result);
124
- }
125
- return result;
126
- }
127
-
128
- static VALUE rxml_node_alloc(VALUE klass)
129
- {
130
- // This node was created from Ruby so we are responsible for freeing it not libxml
131
- return Data_Wrap_Struct(klass, rxml_node_mark, rxml_node_free, NULL);
132
- }
133
-
134
- static xmlNodePtr rxml_get_xnode(VALUE node)
135
- {
136
- xmlNodePtr result;
137
- Data_Get_Struct(node, xmlNode, result);
138
-
139
- if (!result)
140
- rb_raise(rb_eRuntimeError, "This node has already been freed.");
141
-
142
- return result;
143
- }
144
-
145
- /*
146
- * call-seq:
147
- * XML::Node.new_cdata(content = nil) -> XML::Node
148
- *
149
- * Create a new #CDATA node, optionally setting
150
- * the node's content.
151
- */
152
- static VALUE rxml_node_new_cdata(int argc, VALUE *argv, VALUE klass)
153
- {
154
- VALUE content = Qnil;
155
- xmlNodePtr xnode;
156
-
157
- rb_scan_args(argc, argv, "01", &content);
158
-
159
- if (NIL_P(content))
160
- {
161
- xnode = xmlNewCDataBlock(NULL, NULL, 0);
162
- }
163
- else
164
- {
165
- content = rb_obj_as_string(content);
166
- xnode = xmlNewCDataBlock(NULL, (xmlChar*) StringValuePtr(content), (int)RSTRING_LEN(content));
167
- }
168
-
169
- if (xnode == NULL)
170
- rxml_raise(&xmlLastError);
171
-
172
- return rxml_node_wrap(xnode);
173
- }
174
-
175
- /*
176
- * call-seq:
177
- * XML::Node.new_comment(content = nil) -> XML::Node
178
- *
179
- * Create a new comment node, optionally setting
180
- * the node's content.
181
- *
182
- */
183
- static VALUE rxml_node_new_comment(int argc, VALUE *argv, VALUE klass)
184
- {
185
- VALUE content = Qnil;
186
- xmlNodePtr xnode;
187
-
188
- rb_scan_args(argc, argv, "01", &content);
189
-
190
- if (NIL_P(content))
191
- {
192
- xnode = xmlNewComment(NULL);
193
- }
194
- else
195
- {
196
- content = rb_obj_as_string(content);
197
- xnode = xmlNewComment((xmlChar*) StringValueCStr(content));
198
- }
199
-
200
- if (xnode == NULL)
201
- rxml_raise(&xmlLastError);
202
-
203
- return rxml_node_wrap(xnode);
204
- }
205
-
206
- /*
207
- * call-seq:
208
- * XML::Node.new_pi(name, content = nil) -> XML::Node
209
- *
210
- * Create a new pi node, optionally setting
211
- * the node's content.
212
- *
213
- */
214
- static VALUE rxml_node_new_pi(int argc, VALUE *argv, VALUE klass)
215
- {
216
- VALUE name = Qnil;
217
- VALUE content = Qnil;
218
- xmlNodePtr xnode;
219
-
220
- rb_scan_args(argc, argv, "11", &name, &content);
221
-
222
- if (NIL_P(name))
223
- {
224
- rb_raise(rb_eRuntimeError, "You must provide me with a name for a PI.");
225
- }
226
- name = rb_obj_as_string(name);
227
- if (NIL_P(content))
228
- {
229
- xnode = xmlNewPI((xmlChar*) StringValuePtr(name), NULL);
230
- }
231
- else
232
- {
233
- content = rb_obj_as_string(content);
234
- xnode = xmlNewPI((xmlChar*) StringValuePtr(name), (xmlChar*) StringValueCStr(content));
235
- }
236
-
237
- if (xnode == NULL)
238
- rxml_raise(&xmlLastError);
239
-
240
- return rxml_node_wrap(xnode);
241
- }
242
-
243
- /*
244
- * call-seq:
245
- * XML::Node.new_text(content) -> XML::Node
246
- *
247
- * Create a new text node.
248
- *
249
- */
250
- static VALUE rxml_node_new_text(VALUE klass, VALUE content)
251
- {
252
- xmlNodePtr xnode;
253
- Check_Type(content, T_STRING);
254
- content = rb_obj_as_string(content);
255
-
256
- xnode = xmlNewText((xmlChar*) StringValueCStr(content));
257
-
258
- if (xnode == NULL)
259
- rxml_raise(&xmlLastError);
260
-
261
- return rxml_node_wrap(xnode);
262
- }
263
-
264
- static VALUE rxml_node_content_set(VALUE self, VALUE content);
265
-
266
- /*
267
- * call-seq:
268
- * XML::Node.initialize(name, content = nil, namespace = nil) -> XML::Node
269
- *
270
- * Creates a new element with the specified name, content and
271
- * namespace. The content and namespace may be nil.
272
- */
273
- static VALUE rxml_node_initialize(int argc, VALUE *argv, VALUE self)
274
- {
275
- VALUE name;
276
- VALUE content;
277
- VALUE ns;
278
- xmlNodePtr xnode = NULL;
279
- xmlNsPtr xns = NULL;
280
-
281
- rb_scan_args(argc, argv, "12", &name, &content, &ns);
282
-
283
- name = rb_obj_as_string(name);
284
-
285
- if (!NIL_P(ns))
286
- Data_Get_Struct(ns, xmlNs, xns);
287
-
288
- xnode = xmlNewNode(xns, (xmlChar*) StringValuePtr(name));
289
-
290
- if (xnode == NULL)
291
- rxml_raise(&xmlLastError);
292
-
293
- // Link the ruby wrapper to the underlying libxml node
294
- RDATA(self)->data = xnode;
295
-
296
- // Ruby is in charge of managing this node's memory
297
- rxml_node_manage(xnode, self);
298
-
299
- if (!NIL_P(content))
300
- rxml_node_content_set(self, content);
301
-
302
- return self;
303
- }
304
-
305
- static VALUE rxml_node_modify_dom(VALUE self, VALUE target,
306
- xmlNodePtr (*xmlFunc)(xmlNodePtr, xmlNodePtr))
307
- {
308
- xmlNodePtr xnode, xtarget, xresult;
309
-
310
- if (rb_obj_is_kind_of(target, cXMLNode) == Qfalse)
311
- rb_raise(rb_eTypeError, "Must pass an XML::Node object");
312
-
313
- xnode = rxml_get_xnode(self);
314
- xtarget = rxml_get_xnode(target);
315
-
316
- if (xtarget->doc != NULL && xtarget->doc != xnode->doc)
317
- rb_raise(eXMLError, "Nodes belong to different documents. You must first import the node by calling LibXML::XML::Document.import");
318
-
319
- xmlUnlinkNode(xtarget);
320
-
321
- // Target is about to have a parent, so stop having ruby manage it.
322
- rxml_node_unmanage(xtarget, target);
323
-
324
- // This target node could be freed here and be replaced by a different node
325
- xresult = xmlFunc(xnode, xtarget);
326
-
327
- if (!xresult)
328
- rxml_raise(&xmlLastError);
329
-
330
- /* Assume the target was freed, we need to fix up the ruby object to point to the
331
- newly returned node. */
332
- RDATA(target)->data = xresult;
333
-
334
- return target;
335
- }
336
-
337
- /*
338
- * call-seq:
339
- * node.base_uri -> "uri"
340
- *
341
- * Obtain this node's base URI.
342
- */
343
- static VALUE rxml_node_base_uri_get(VALUE self)
344
- {
345
- xmlNodePtr xnode;
346
- xmlChar* base_uri;
347
- VALUE result = Qnil;
348
-
349
- xnode = rxml_get_xnode(self);
350
-
351
- if (xnode->doc == NULL)
352
- return (result);
353
-
354
- base_uri = xmlNodeGetBase(xnode->doc, xnode);
355
- if (base_uri)
356
- {
357
- result = rxml_new_cstr( base_uri, NULL);
358
- xmlFree(base_uri);
359
- }
360
-
361
- return (result);
362
- }
363
-
364
- // TODO node_base_set should support setting back to nil
365
-
366
- /*
367
- * call-seq:
368
- * node.base_uri = "uri"
369
- *
370
- * Set this node's base URI.
371
- */
372
- static VALUE rxml_node_base_uri_set(VALUE self, VALUE uri)
373
- {
374
- xmlNodePtr xnode;
375
-
376
- Check_Type(uri, T_STRING);
377
- xnode = rxml_get_xnode(self);
378
- if (xnode->doc == NULL)
379
- return (Qnil);
380
-
381
- xmlNodeSetBase(xnode, (xmlChar*) StringValuePtr(uri));
382
- return (Qtrue);
383
- }
384
-
385
- /*
386
- * call-seq:
387
- * node.content -> "string"
388
- *
389
- * Obtain this node's content as a string.
390
- */
391
- static VALUE rxml_node_content_get(VALUE self)
392
- {
393
- xmlNodePtr xnode;
394
- xmlChar *content;
395
- VALUE result = Qnil;
396
-
397
- xnode = rxml_get_xnode(self);
398
- content = xmlNodeGetContent(xnode);
399
- if (content)
400
- {
401
- result = rxml_new_cstr(content, NULL);
402
- xmlFree(content);
403
- }
404
-
405
- return result;
406
- }
407
-
408
- /*
409
- * call-seq:
410
- * node.content = "string"
411
- *
412
- * Set this node's content to the specified string.
413
- */
414
- static VALUE rxml_node_content_set(VALUE self, VALUE content)
415
- {
416
- xmlNodePtr xnode;
417
- xmlChar* encoded_content;
418
-
419
- Check_Type(content, T_STRING);
420
- xnode = rxml_get_xnode(self);
421
- encoded_content = xmlEncodeSpecialChars(xnode->doc, (xmlChar*) StringValuePtr(content));
422
- xmlNodeSetContent(xnode, encoded_content);
423
- xmlFree(encoded_content);
424
- return (Qtrue);
425
- }
426
-
427
- /*
428
- * call-seq:
429
- * node.debug -> true|false
430
- *
431
- * Print libxml debugging information to stdout.
432
- * Requires that libxml was compiled with debugging enabled.
433
- */
434
- static VALUE rxml_node_debug(VALUE self)
435
- {
436
- #ifdef LIBXML_DEBUG_ENABLED
437
- xmlNodePtr xnode;
438
- xnode = rxml_get_xnode(self);
439
- xmlDebugDumpNode(NULL, xnode, 2);
440
- return Qtrue;
441
- #else
442
- rb_warn("libxml was compiled without debugging support.");
443
- return Qfalse;
444
- #endif
445
- }
446
-
447
- /*
448
- * call-seq:
449
- * node.first -> XML::Node
450
- *
451
- * Returns this node's first child node if any.
452
- */
453
- static VALUE rxml_node_first_get(VALUE self)
454
- {
455
- xmlNodePtr xnode;
456
-
457
- xnode = rxml_get_xnode(self);
458
-
459
- if (xnode->children)
460
- return (rxml_node_wrap(xnode->children));
461
- else
462
- return (Qnil);
463
- }
464
-
465
-
466
- /*
467
- * call-seq:
468
- * curr_node << "Some text"
469
- * curr_node << node
470
- *
471
- * Add the specified text or XML::Node as a new child node to the
472
- * current node.
473
- *
474
- * If the specified argument is a string, it should be a raw string
475
- * that contains unescaped XML special characters. Entity references
476
- * are not supported.
477
- *
478
- * The method will return the current node.
479
- */
480
- static VALUE rxml_node_content_add(VALUE self, VALUE obj)
481
- {
482
- xmlNodePtr xnode;
483
- VALUE str;
484
-
485
- xnode = rxml_get_xnode(self);
486
-
487
- /* XXX This should only be legal for a CDATA type node, I think,
488
- * resulting in a merge of content, as if a string were passed
489
- * danj 070827
490
- */
491
- if (rb_obj_is_kind_of(obj, cXMLNode))
492
- {
493
- rxml_node_modify_dom(self, obj, xmlAddChild);
494
- }
495
- else
496
- {
497
- str = rb_obj_as_string(obj);
498
- if (NIL_P(str) || TYPE(str) != T_STRING)
499
- rb_raise(rb_eTypeError, "invalid argument: must be string or XML::Node");
500
-
501
- xmlNodeAddContent(xnode, (xmlChar*) StringValuePtr(str));
502
- }
503
- return self;
504
- }
505
-
506
- /*
507
- * call-seq:
508
- * node.doc -> document
509
- *
510
- * Obtain the XML::Document this node belongs to.
511
- */
512
- static VALUE rxml_node_doc(VALUE self)
513
- {
514
- xmlDocPtr xdoc = NULL;
515
- xmlNodePtr xnode = rxml_get_xnode(self);
516
-
517
- switch (xnode->type)
518
- {
519
- case XML_DOCUMENT_NODE:
520
- #ifdef LIBXML_DOCB_ENABLED
521
- case XML_DOCB_DOCUMENT_NODE:
522
- #endif
523
- case XML_HTML_DOCUMENT_NODE:
524
- case XML_NAMESPACE_DECL:
525
- break;
526
- case XML_ATTRIBUTE_NODE:
527
- xdoc = (xmlDocPtr)((xmlAttrPtr) xnode->doc);
528
- break;
529
- default:
530
- xdoc = xnode->doc;
531
- }
532
-
533
- if (xdoc == NULL)
534
- return (Qnil);
535
-
536
- return (VALUE)xdoc->_private;
537
- }
538
-
539
- /*
540
- * call-seq:
541
- * node.to_s -> "string"
542
- * node.to_s(:indent => true, :encoding => 'UTF-8', :level => 0) -> "string"
543
- *
544
- * Converts a node, and all of its children, to a string representation.
545
- * To include only the node's children, use the the XML::Node#inner_xml
546
- * method.
547
- *
548
- * You may provide an optional hash table to control how the string is
549
- * generated. Valid options are:
550
- *
551
- * :indent - Specifies if the string should be indented. The default value
552
- * is true. Note that indentation is only added if both :indent is
553
- * true and XML.indent_tree_output is true. If :indent is set to false,
554
- * then both indentation and line feeds are removed from the result.
555
- *
556
- * :level - Specifies the indentation level. The amount of indentation
557
- * is equal to the (level * number_spaces) + number_spaces, where libxml
558
- * defaults the number of spaces to 2. Thus a level of 0 results in
559
- * 2 spaces, level 1 results in 4 spaces, level 2 results in 6 spaces, etc.
560
- *
561
- * :encoding - Specifies the output encoding of the string. It
562
- * defaults to XML::Encoding::UTF8. To change it, use one of the
563
- * XML::Encoding encoding constants. */
564
-
565
- static VALUE rxml_node_to_s(int argc, VALUE *argv, VALUE self)
566
- {
567
- VALUE result = Qnil;
568
- VALUE options = Qnil;
569
- xmlNodePtr xnode;
570
- xmlCharEncodingHandlerPtr encodingHandler;
571
- xmlOutputBufferPtr output;
572
-
573
- int level = 0;
574
- int indent = 1;
575
- const xmlChar *xencoding = (const xmlChar*)"UTF-8";
576
-
577
- rb_scan_args(argc, argv, "01", &options);
578
-
579
- if (!NIL_P(options))
580
- {
581
- VALUE rencoding, rindent, rlevel;
582
- Check_Type(options, T_HASH);
583
- rencoding = rb_hash_aref(options, ID2SYM(rb_intern("encoding")));
584
- rindent = rb_hash_aref(options, ID2SYM(rb_intern("indent")));
585
- rlevel = rb_hash_aref(options, ID2SYM(rb_intern("level")));
586
-
587
- if (rindent == Qfalse)
588
- indent = 0;
589
-
590
- if (rlevel != Qnil)
591
- level = NUM2INT(rlevel);
592
-
593
- if (rencoding != Qnil)
594
- {
595
- xencoding = (const xmlChar*)xmlGetCharEncodingName((xmlCharEncoding)NUM2INT(rencoding));
596
- if (!xencoding)
597
- rb_raise(rb_eArgError, "Unknown encoding value: %d", NUM2INT(rencoding));
598
- }
599
- }
600
-
601
- encodingHandler = xmlFindCharEncodingHandler((const char*)xencoding);
602
- output = xmlAllocOutputBuffer(encodingHandler);
603
-
604
- xnode = rxml_get_xnode(self);
605
-
606
- xmlNodeDumpOutput(output, xnode->doc, xnode, level, indent, (const char*)xencoding);
607
- xmlOutputBufferFlush(output);
608
-
609
- #ifdef LIBXML2_NEW_BUFFER
610
- if (output->conv)
611
- result = rxml_new_cstr(xmlBufContent(output->conv), xencoding);
612
- else
613
- result = rxml_new_cstr(xmlBufContent(output->buffer), xencoding);
614
- #else
615
- if (output->conv)
616
- result = rxml_new_cstr(xmlBufferContent(output->conv), xencoding);
617
- else
618
- result = rxml_new_cstr(xmlBufferContent(output->buffer), xencoding);
619
- #endif
620
-
621
- xmlOutputBufferClose(output);
622
-
623
- return result;
624
- }
625
-
626
-
627
- /*
628
- * call-seq:
629
- * node.each -> XML::Node
630
- *
631
- * Iterates over this node's children, including text
632
- * nodes, element nodes, etc. If you wish to iterate
633
- * only over child elements, use XML::Node#each_element.
634
- *
635
- * doc = XML::Document.new('model/books.xml')
636
- * doc.root.each {|node| puts node}
637
- */
638
- static VALUE rxml_node_each(VALUE self)
639
- {
640
- xmlNodePtr xnode;
641
- xmlNodePtr xcurrent;
642
- xnode = rxml_get_xnode(self);
643
-
644
- xcurrent = xnode->children;
645
-
646
- while (xcurrent)
647
- {
648
- /* The user could remove this node, so first stache
649
- away the next node. */
650
- xmlNodePtr xnext = xcurrent->next;
651
-
652
- rb_yield(rxml_node_wrap(xcurrent));
653
- xcurrent = xnext;
654
- }
655
- return Qnil;
656
- }
657
-
658
- /*
659
- * call-seq:
660
- * node.empty? -> (true|false)
661
- *
662
- * Determine whether this node is an empty or whitespace only text-node.
663
- */
664
- static VALUE rxml_node_empty_q(VALUE self)
665
- {
666
- xmlNodePtr xnode;
667
- xnode = rxml_get_xnode(self);
668
- if (xnode == NULL)
669
- return (Qnil);
670
-
671
- return ((xmlIsBlankNode(xnode) == 1) ? Qtrue : Qfalse);
672
- }
673
-
674
-
675
- /*
676
- * call-seq:
677
- * node.eql?(other_node) => (true|false)
678
- *
679
- * Test equality between the two nodes. Two nodes are equal
680
- * if they are the same node.*/
681
- static VALUE rxml_node_eql_q(VALUE self, VALUE other)
682
- {
683
- if (self == other)
684
- {
685
- return Qtrue;
686
- }
687
- else if (NIL_P(other))
688
- {
689
- return Qfalse;
690
- }
691
- else
692
- {
693
- xmlNodePtr xnode = rxml_get_xnode(self);
694
- xmlNodePtr xnode_other = rxml_get_xnode(other);
695
- return xnode == xnode_other ? Qtrue : Qfalse;
696
- }
697
- }
698
-
699
- /*
700
- * call-seq:
701
- * node.lang -> "string"
702
- *
703
- * Obtain the language set for this node, if any.
704
- * This is set in XML via the xml:lang attribute.
705
- */
706
- static VALUE rxml_node_lang_get(VALUE self)
707
- {
708
- xmlNodePtr xnode;
709
- xmlChar *lang;
710
- VALUE result = Qnil;
711
-
712
- xnode = rxml_get_xnode(self);
713
- lang = xmlNodeGetLang(xnode);
714
-
715
- if (lang)
716
- {
717
- result = rxml_new_cstr( lang, NULL);
718
- xmlFree(lang);
719
- }
720
-
721
- return (result);
722
- }
723
-
724
- // TODO node_lang_set should support setting back to nil
725
-
726
- /*
727
- * call-seq:
728
- * node.lang = "string"
729
- *
730
- * Set the language for this node. This affects the value
731
- * of the xml:lang attribute.
732
- */
733
- static VALUE rxml_node_lang_set(VALUE self, VALUE lang)
734
- {
735
- xmlNodePtr xnode;
736
-
737
- Check_Type(lang, T_STRING);
738
- xnode = rxml_get_xnode(self);
739
- xmlNodeSetLang(xnode, (xmlChar*) StringValuePtr(lang));
740
-
741
- return (Qtrue);
742
- }
743
-
744
- /*
745
- * call-seq:
746
- * node.last -> XML::Node
747
- *
748
- * Obtain the last child node of this node, if any.
749
- */
750
- static VALUE rxml_node_last_get(VALUE self)
751
- {
752
- xmlNodePtr xnode;
753
-
754
- xnode = rxml_get_xnode(self);
755
-
756
- if (xnode->last)
757
- return (rxml_node_wrap(xnode->last));
758
- else
759
- return (Qnil);
760
- }
761
-
762
- /*
763
- * call-seq:
764
- * node.line_num -> num
765
- *
766
- * Obtain the line number (in the XML document) that this
767
- * node was read from. If +default_line_numbers+ is set
768
- * false (the default), this method returns zero.
769
- */
770
- static VALUE rxml_node_line_num(VALUE self)
771
- {
772
- xmlNodePtr xnode;
773
- long line_num;
774
- xnode = rxml_get_xnode(self);
775
-
776
- if (!xmlLineNumbersDefaultValue)
777
- rb_warn(
778
- "Line numbers were not retained: use XML::Parser::default_line_numbers=true");
779
-
780
- line_num = xmlGetLineNo(xnode);
781
- if (line_num == -1)
782
- return (Qnil);
783
- else
784
- return (INT2NUM((long) line_num));
785
- }
786
-
787
- /*
788
- * call-seq:
789
- * node.xlink? -> (true|false)
790
- *
791
- * Determine whether this node is an xlink node.
792
- */
793
- static VALUE rxml_node_xlink_q(VALUE self)
794
- {
795
- xmlNodePtr xnode;
796
- xlinkType xlt;
797
-
798
- xnode = rxml_get_xnode(self);
799
- xlt = xlinkIsLink(xnode->doc, xnode);
800
-
801
- if (xlt == XLINK_TYPE_NONE)
802
- return (Qfalse);
803
- else
804
- return (Qtrue);
805
- }
806
-
807
- /*
808
- * call-seq:
809
- * node.xlink_type -> num
810
- *
811
- * Obtain the type identifier for this xlink, if applicable.
812
- * If this is not an xlink node (see +xlink?+), will return
813
- * nil.
814
- */
815
- static VALUE rxml_node_xlink_type(VALUE self)
816
- {
817
- xmlNodePtr xnode;
818
- xlinkType xlt;
819
-
820
- xnode = rxml_get_xnode(self);
821
- xlt = xlinkIsLink(xnode->doc, xnode);
822
-
823
- if (xlt == XLINK_TYPE_NONE)
824
- return (Qnil);
825
- else
826
- return (INT2NUM(xlt));
827
- }
828
-
829
- /*
830
- * call-seq:
831
- * node.xlink_type_name -> "string"
832
- *
833
- * Obtain the type name for this xlink, if applicable.
834
- * If this is not an xlink node (see +xlink?+), will return
835
- * nil.
836
- */
837
- static VALUE rxml_node_xlink_type_name(VALUE self)
838
- {
839
- xmlNodePtr xnode;
840
- xlinkType xlt;
841
-
842
- xnode = rxml_get_xnode(self);
843
- xlt = xlinkIsLink(xnode->doc, xnode);
844
-
845
- switch (xlt)
846
- {
847
- case XLINK_TYPE_NONE:
848
- return (Qnil);
849
- case XLINK_TYPE_SIMPLE:
850
- return (rxml_new_cstr((const xmlChar*)"simple", NULL));
851
- case XLINK_TYPE_EXTENDED:
852
- return (rxml_new_cstr((const xmlChar*)"extended", NULL));
853
- case XLINK_TYPE_EXTENDED_SET:
854
- return (rxml_new_cstr((const xmlChar*)"extended_set", NULL));
855
- default:
856
- rb_fatal("Unknowng xlink type, %d", xlt);
857
- }
858
- }
859
-
860
- /*
861
- * call-seq:
862
- * node.name -> "string"
863
- *
864
- * Obtain this node's name.
865
- */
866
- static VALUE rxml_node_name_get(VALUE self)
867
- {
868
- xmlNodePtr xnode;
869
- const xmlChar *name;
870
-
871
- xnode = rxml_get_xnode(self);
872
-
873
- switch (xnode->type)
874
- {
875
- case XML_DOCUMENT_NODE:
876
- #ifdef LIBXML_DOCB_ENABLED
877
- case XML_DOCB_DOCUMENT_NODE:
878
- #endif
879
- case XML_HTML_DOCUMENT_NODE:
880
- {
881
- xmlDocPtr doc = (xmlDocPtr) xnode;
882
- name = doc->URL;
883
- break;
884
- }
885
- case XML_ATTRIBUTE_NODE:
886
- {
887
- xmlAttrPtr attr = (xmlAttrPtr) xnode;
888
- name = attr->name;
889
- break;
890
- }
891
- case XML_NAMESPACE_DECL:
892
- {
893
- xmlNsPtr ns = (xmlNsPtr) xnode;
894
- name = ns->prefix;
895
- break;
896
- }
897
- default:
898
- name = xnode->name;
899
- break;
900
- }
901
-
902
- if (xnode->name == NULL)
903
- return (Qnil);
904
- else
905
- return (rxml_new_cstr( name, NULL));
906
- }
907
-
908
- /*
909
- * call-seq:
910
- * node.name = "string"
911
- *
912
- * Set this node's name.
913
- */
914
- static VALUE rxml_node_name_set(VALUE self, VALUE name)
915
- {
916
- xmlNodePtr xnode;
917
- const xmlChar *xname;
918
-
919
- Check_Type(name, T_STRING);
920
- xnode = rxml_get_xnode(self);
921
- xname = (const xmlChar*)StringValuePtr(name);
922
-
923
- /* Note: calling xmlNodeSetName() for a text node is ignored by libXML. */
924
- xmlNodeSetName(xnode, xname);
925
-
926
- return (Qtrue);
927
- }
928
-
929
- /*
930
- * call-seq:
931
- * node.next -> XML::Node
932
- *
933
- * Returns the next sibling node if one exists.
934
- */
935
- static VALUE rxml_node_next_get(VALUE self)
936
- {
937
- xmlNodePtr xnode;
938
-
939
- xnode = rxml_get_xnode(self);
940
-
941
- if (xnode->next)
942
- return (rxml_node_wrap(xnode->next));
943
- else
944
- return (Qnil);
945
- }
946
-
947
- /*
948
- * call-seq:
949
- * curr_node.next = node
950
- *
951
- * Adds the specified node as the next sibling of the current node.
952
- * If the node already exists in the document, it is first removed
953
- * from its existing context. Any adjacent text nodes will be
954
- * merged together, meaning the returned node may be different
955
- * than the original node.
956
- */
957
- static VALUE rxml_node_next_set(VALUE self, VALUE next)
958
- {
959
- return rxml_node_modify_dom(self, next, xmlAddNextSibling);
960
- }
961
-
962
- /*
963
- * call-seq:
964
- * node.parent -> XML::Node
965
- *
966
- * Obtain this node's parent node, if any.
967
- */
968
- static VALUE rxml_node_parent_get(VALUE self)
969
- {
970
- xmlNodePtr xnode;
971
-
972
- xnode = rxml_get_xnode(self);
973
-
974
- if (xnode->parent)
975
- return (rxml_node_wrap(xnode->parent));
976
- else
977
- return (Qnil);
978
- }
979
-
980
- /*
981
- * call-seq:
982
- * node.path -> path
983
- *
984
- * Obtain this node's path.
985
- */
986
- static VALUE rxml_node_path(VALUE self)
987
- {
988
- xmlNodePtr xnode;
989
- xmlChar *path;
990
- VALUE result = Qnil;
991
-
992
- xnode = rxml_get_xnode(self);
993
- path = xmlGetNodePath(xnode);
994
-
995
- if (path)
996
- {
997
- result = rxml_new_cstr( path, NULL);
998
- xmlFree(path);
999
- }
1000
-
1001
- return result;
1002
- }
1003
-
1004
- /*
1005
- * call-seq:
1006
- * node.prev -> XML::Node
1007
- *
1008
- * Obtain the previous sibling, if any.
1009
- */
1010
- static VALUE rxml_node_prev_get(VALUE self)
1011
- {
1012
- xmlNodePtr xnode;
1013
- xmlNodePtr node;
1014
- xnode = rxml_get_xnode(self);
1015
-
1016
- switch (xnode->type)
1017
- {
1018
- case XML_DOCUMENT_NODE:
1019
- #ifdef LIBXML_DOCB_ENABLED
1020
- case XML_DOCB_DOCUMENT_NODE:
1021
- #endif
1022
- case XML_HTML_DOCUMENT_NODE:
1023
- case XML_NAMESPACE_DECL:
1024
- node = NULL;
1025
- break;
1026
- case XML_ATTRIBUTE_NODE:
1027
- {
1028
- xmlAttrPtr attr = (xmlAttrPtr) xnode;
1029
- node = (xmlNodePtr) attr->prev;
1030
- }
1031
- break;
1032
- default:
1033
- node = xnode->prev;
1034
- break;
1035
- }
1036
-
1037
- if (node == NULL)
1038
- return (Qnil);
1039
- else
1040
- return (rxml_node_wrap(node));
1041
- }
1042
-
1043
- /*
1044
- * call-seq:
1045
- * curr_node.prev = node
1046
- *
1047
- * Adds the specified node as the previous sibling of the current node.
1048
- * If the node already exists in the document, it is first removed
1049
- * from its existing context. Any adjacent text nodes will be
1050
- * merged together, meaning the returned node may be different
1051
- * than the original node.
1052
- */
1053
- static VALUE rxml_node_prev_set(VALUE self, VALUE prev)
1054
- {
1055
- return rxml_node_modify_dom(self, prev, xmlAddPrevSibling);
1056
- }
1057
-
1058
- /*
1059
- * call-seq:
1060
- * node.attributes -> attributes
1061
- *
1062
- * Returns the XML::Attributes for this node.
1063
- */
1064
- static VALUE rxml_node_attributes_get(VALUE self)
1065
- {
1066
- xmlNodePtr xnode;
1067
-
1068
- xnode = rxml_get_xnode(self);
1069
- return rxml_attributes_new(xnode);
1070
- }
1071
-
1072
- /*
1073
- * call-seq:
1074
- * node.property("name") -> "string"
1075
- * node["name"] -> "string"
1076
- *
1077
- * Obtain the named property.
1078
- */
1079
- static VALUE rxml_node_attribute_get(VALUE self, VALUE name)
1080
- {
1081
- VALUE attributes = rxml_node_attributes_get(self);
1082
- return rxml_attributes_attribute_get(attributes, name);
1083
- }
1084
-
1085
- /*
1086
- * call-seq:
1087
- * node["name"] = "string"
1088
- *
1089
- * Set the named property.
1090
- */
1091
- static VALUE rxml_node_property_set(VALUE self, VALUE name, VALUE value)
1092
- {
1093
- VALUE attributes = rxml_node_attributes_get(self);
1094
- return rxml_attributes_attribute_set(attributes, name, value);
1095
- }
1096
-
1097
- /*
1098
- * call-seq:
1099
- * node.remove! -> node
1100
- *
1101
- * Removes this node and its children from the document tree by setting its document,
1102
- * parent and siblings to nil. You can add the returned node back into a document.
1103
- * Otherwise, the node will be freed once any references to it go out of scope.
1104
- */
1105
-
1106
- static VALUE rxml_node_remove_ex(VALUE self)
1107
- {
1108
- xmlNodePtr xnode = rxml_get_xnode(self);
1109
-
1110
- // Now unlink the node from its parent
1111
- xmlUnlinkNode(xnode);
1112
-
1113
- // Ruby now manages this node
1114
- rxml_node_manage(xnode, self);
1115
-
1116
- // Now return the removed node so the user can do something with it
1117
- return self;
1118
- }
1119
-
1120
- /*
1121
- * call-seq:
1122
- * curr_node.sibling = node
1123
- *
1124
- * Adds the specified node as the end of the current node's list
1125
- * of siblings. If the node already exists in the document, it
1126
- * is first removed from its existing context. Any adjacent text
1127
- * nodes will be merged together, meaning the returned node may
1128
- * be different than the original node.
1129
- */
1130
- static VALUE rxml_node_sibling_set(VALUE self, VALUE sibling)
1131
- {
1132
- return rxml_node_modify_dom(self, sibling, xmlAddSibling);
1133
- }
1134
-
1135
- /*
1136
- * call-seq:
1137
- * text_node.output_escaping? -> (true|false)
1138
- * element_node.output_escaping? -> (true|false|nil)
1139
- * attribute_node.output_escaping? -> (true|false|nil)
1140
- * other_node.output_escaping? -> (nil)
1141
- *
1142
- * Determine whether this node escapes it's output or not.
1143
- *
1144
- * Text nodes return only +true+ or +false+. Element and attribute nodes
1145
- * examine their immediate text node children to determine the value.
1146
- * Any other type of node always returns +nil+.
1147
- *
1148
- * If an element or attribute node has at least one immediate child text node
1149
- * and all the immediate text node children have the same +output_escaping?+
1150
- * value, that value is returned. Otherwise, +nil+ is returned.
1151
- */
1152
- static VALUE rxml_node_output_escaping_q(VALUE self)
1153
- {
1154
- xmlNodePtr xnode;
1155
- xnode = rxml_get_xnode(self);
1156
-
1157
- switch (xnode->type) {
1158
- case XML_TEXT_NODE:
1159
- return xnode->name==xmlStringTextNoenc ? Qfalse : Qtrue;
1160
- case XML_ELEMENT_NODE:
1161
- case XML_ATTRIBUTE_NODE:
1162
- {
1163
- xmlNodePtr tmp = xnode->children;
1164
- const xmlChar *match = NULL;
1165
-
1166
- /* Find the first text node and use it as the reference. */
1167
- while (tmp && tmp->type != XML_TEXT_NODE)
1168
- tmp = tmp->next;
1169
- if (! tmp)
1170
- return Qnil;
1171
- match = tmp->name;
1172
-
1173
- /* Walk the remaining text nodes until we run out or one doesn't match. */
1174
- while (tmp && (tmp->type != XML_TEXT_NODE || match == tmp->name))
1175
- tmp = tmp->next;
1176
-
1177
- /* We're left with either the mismatched node or the aggregate result. */
1178
- return tmp ? Qnil : (match==xmlStringTextNoenc ? Qfalse : Qtrue);
1179
- }
1180
- break;
1181
- default:
1182
- return Qnil;
1183
- }
1184
- }
1185
-
1186
- /*
1187
- * call-seq:
1188
- * text_node.output_escaping = true|false
1189
- * element_node.output_escaping = true|false
1190
- * attribute_node.output_escaping = true|false
1191
- *
1192
- * Controls whether this text node or the immediate text node children of an
1193
- * element or attribute node escapes their output. Any other type of node
1194
- * will simply ignore this operation.
1195
- *
1196
- * Text nodes which are added to an element or attribute node will be affected
1197
- * by any previous setting of this property.
1198
- */
1199
- static VALUE rxml_node_output_escaping_set(VALUE self, VALUE value)
1200
- {
1201
- xmlNodePtr xnode;
1202
- xnode = rxml_get_xnode(self);
1203
-
1204
- switch (xnode->type) {
1205
- case XML_TEXT_NODE:
1206
- xnode->name = (value != Qfalse && value != Qnil) ? xmlStringText : xmlStringTextNoenc;
1207
- break;
1208
- case XML_ELEMENT_NODE:
1209
- case XML_ATTRIBUTE_NODE:
1210
- {
1211
- const xmlChar *name = (value != Qfalse && value != Qnil) ? xmlStringText : xmlStringTextNoenc;
1212
- xmlNodePtr tmp;
1213
- for (tmp = xnode->children; tmp; tmp = tmp->next)
1214
- if (tmp->type == XML_TEXT_NODE)
1215
- tmp->name = name;
1216
- }
1217
- break;
1218
- default:
1219
- return Qnil;
1220
- }
1221
-
1222
- return (value!=Qfalse && value!=Qnil) ? Qtrue : Qfalse;
1223
- }
1224
-
1225
- /*
1226
- * call-seq:
1227
- * node.space_preserve -> (true|false)
1228
- *
1229
- * Determine whether this node preserves whitespace.
1230
- */
1231
- static VALUE rxml_node_space_preserve_get(VALUE self)
1232
- {
1233
- xmlNodePtr xnode;
1234
-
1235
- xnode = rxml_get_xnode(self);
1236
- return (INT2NUM(xmlNodeGetSpacePreserve(xnode)));
1237
- }
1238
-
1239
- /*
1240
- * call-seq:
1241
- * node.space_preserve = true|false
1242
- *
1243
- * Control whether this node preserves whitespace.
1244
- */
1245
- static VALUE rxml_node_space_preserve_set(VALUE self, VALUE value)
1246
- {
1247
- xmlNodePtr xnode;
1248
- xnode = rxml_get_xnode(self);
1249
-
1250
- if (value == Qfalse)
1251
- xmlNodeSetSpacePreserve(xnode, 0);
1252
- else
1253
- xmlNodeSetSpacePreserve(xnode, 1);
1254
-
1255
- return (Qnil);
1256
- }
1257
-
1258
- /*
1259
- * call-seq:
1260
- * node.type -> num
1261
- *
1262
- * Obtain this node's type identifier.
1263
- */
1264
- static VALUE rxml_node_type(VALUE self)
1265
- {
1266
- xmlNodePtr xnode;
1267
- xnode = rxml_get_xnode(self);
1268
- return (INT2NUM(xnode->type));
1269
- }
1270
-
1271
- /*
1272
- * call-seq:
1273
- * node.copy -> XML::Node
1274
- *
1275
- * Creates a copy of this node. To create a
1276
- * shallow copy set the deep parameter to false.
1277
- * To create a deep copy set the deep parameter
1278
- * to true.
1279
- *
1280
- */
1281
- static VALUE rxml_node_copy(VALUE self, VALUE deep)
1282
- {
1283
- xmlNodePtr xnode;
1284
- xmlNodePtr xcopy;
1285
- int recursive = (deep == Qnil || deep == Qfalse) ? 0 : 1;
1286
- xnode = rxml_get_xnode(self);
1287
-
1288
- xcopy = xmlCopyNode(xnode, recursive);
1289
-
1290
- if (xcopy)
1291
- return rxml_node_wrap(xcopy);
1292
- else
1293
- return Qnil;
1294
- }
1295
-
1296
- void rxml_init_node(void)
1297
- {
1298
- cXMLNode = rb_define_class_under(mXML, "Node", rb_cObject);
1299
-
1300
- rb_define_const(cXMLNode, "SPACE_DEFAULT", INT2NUM(0));
1301
- rb_define_const(cXMLNode, "SPACE_PRESERVE", INT2NUM(1));
1302
- rb_define_const(cXMLNode, "SPACE_NOT_INHERIT", INT2NUM(-1));
1303
- rb_define_const(cXMLNode, "XLINK_ACTUATE_AUTO", INT2NUM(1));
1304
- rb_define_const(cXMLNode, "XLINK_ACTUATE_NONE", INT2NUM(0));
1305
- rb_define_const(cXMLNode, "XLINK_ACTUATE_ONREQUEST", INT2NUM(2));
1306
- rb_define_const(cXMLNode, "XLINK_SHOW_EMBED", INT2NUM(2));
1307
- rb_define_const(cXMLNode, "XLINK_SHOW_NEW", INT2NUM(1));
1308
- rb_define_const(cXMLNode, "XLINK_SHOW_NONE", INT2NUM(0));
1309
- rb_define_const(cXMLNode, "XLINK_SHOW_REPLACE", INT2NUM(3));
1310
- rb_define_const(cXMLNode, "XLINK_TYPE_EXTENDED", INT2NUM(2));
1311
- rb_define_const(cXMLNode, "XLINK_TYPE_EXTENDED_SET", INT2NUM(3));
1312
- rb_define_const(cXMLNode, "XLINK_TYPE_NONE", INT2NUM(0));
1313
- rb_define_const(cXMLNode, "XLINK_TYPE_SIMPLE", INT2NUM(1));
1314
-
1315
- rb_define_const(cXMLNode, "ELEMENT_NODE", INT2FIX(XML_ELEMENT_NODE));
1316
- rb_define_const(cXMLNode, "ATTRIBUTE_NODE", INT2FIX(XML_ATTRIBUTE_NODE));
1317
- rb_define_const(cXMLNode, "TEXT_NODE", INT2FIX(XML_TEXT_NODE));
1318
- rb_define_const(cXMLNode, "CDATA_SECTION_NODE", INT2FIX(XML_CDATA_SECTION_NODE));
1319
- rb_define_const(cXMLNode, "ENTITY_REF_NODE", INT2FIX(XML_ENTITY_REF_NODE));
1320
- rb_define_const(cXMLNode, "ENTITY_NODE", INT2FIX(XML_ENTITY_NODE));
1321
- rb_define_const(cXMLNode, "PI_NODE", INT2FIX(XML_PI_NODE));
1322
- rb_define_const(cXMLNode, "COMMENT_NODE", INT2FIX(XML_COMMENT_NODE));
1323
- rb_define_const(cXMLNode, "DOCUMENT_NODE", INT2FIX(XML_DOCUMENT_NODE));
1324
- rb_define_const(cXMLNode, "DOCUMENT_TYPE_NODE", INT2FIX(XML_DOCUMENT_TYPE_NODE));
1325
- rb_define_const(cXMLNode, "DOCUMENT_FRAG_NODE", INT2FIX(XML_DOCUMENT_FRAG_NODE));
1326
- rb_define_const(cXMLNode, "NOTATION_NODE", INT2FIX(XML_NOTATION_NODE));
1327
- rb_define_const(cXMLNode, "HTML_DOCUMENT_NODE", INT2FIX(XML_HTML_DOCUMENT_NODE));
1328
- rb_define_const(cXMLNode, "DTD_NODE", INT2FIX(XML_DTD_NODE));
1329
- rb_define_const(cXMLNode, "ELEMENT_DECL", INT2FIX(XML_ELEMENT_DECL));
1330
- rb_define_const(cXMLNode, "ATTRIBUTE_DECL", INT2FIX(XML_ATTRIBUTE_DECL));
1331
- rb_define_const(cXMLNode, "ENTITY_DECL", INT2FIX(XML_ENTITY_DECL));
1332
- rb_define_const(cXMLNode, "NAMESPACE_DECL", INT2FIX(XML_NAMESPACE_DECL));
1333
- rb_define_const(cXMLNode, "XINCLUDE_START", INT2FIX(XML_XINCLUDE_START));
1334
- rb_define_const(cXMLNode, "XINCLUDE_END", INT2FIX(XML_XINCLUDE_END));
1335
-
1336
- #ifdef LIBXML_DOCB_ENABLED
1337
- rb_define_const(cXMLNode, "DOCB_DOCUMENT_NODE", INT2FIX(XML_DOCB_DOCUMENT_NODE));
1338
- #else
1339
- rb_define_const(cXMLNode, "DOCB_DOCUMENT_NODE", Qnil);
1340
- #endif
1341
-
1342
- rb_define_singleton_method(cXMLNode, "new_cdata", rxml_node_new_cdata, -1);
1343
- rb_define_singleton_method(cXMLNode, "new_comment", rxml_node_new_comment, -1);
1344
- rb_define_singleton_method(cXMLNode, "new_pi", rxml_node_new_pi, -1);
1345
- rb_define_singleton_method(cXMLNode, "new_text", rxml_node_new_text, 1);
1346
-
1347
- /* Initialization */
1348
- rb_define_alloc_func(cXMLNode, rxml_node_alloc);
1349
- rb_define_method(cXMLNode, "initialize", rxml_node_initialize, -1);
1350
-
1351
- /* Traversal */
1352
- rb_include_module(cXMLNode, rb_mEnumerable);
1353
- rb_define_method(cXMLNode, "[]", rxml_node_attribute_get, 1);
1354
- rb_define_method(cXMLNode, "each", rxml_node_each, 0);
1355
- rb_define_method(cXMLNode, "first", rxml_node_first_get, 0);
1356
- rb_define_method(cXMLNode, "last", rxml_node_last_get, 0);
1357
- rb_define_method(cXMLNode, "next", rxml_node_next_get, 0);
1358
- rb_define_method(cXMLNode, "parent", rxml_node_parent_get, 0);
1359
- rb_define_method(cXMLNode, "prev", rxml_node_prev_get, 0);
1360
-
1361
- /* Modification */
1362
- rb_define_method(cXMLNode, "[]=", rxml_node_property_set, 2);
1363
- rb_define_method(cXMLNode, "<<", rxml_node_content_add, 1);
1364
- rb_define_method(cXMLNode, "sibling=", rxml_node_sibling_set, 1);
1365
- rb_define_method(cXMLNode, "next=", rxml_node_next_set, 1);
1366
- rb_define_method(cXMLNode, "prev=", rxml_node_prev_set, 1);
1367
-
1368
- /* Rest of the node api */
1369
- rb_define_method(cXMLNode, "attributes", rxml_node_attributes_get, 0);
1370
- rb_define_method(cXMLNode, "base_uri", rxml_node_base_uri_get, 0);
1371
- rb_define_method(cXMLNode, "base_uri=", rxml_node_base_uri_set, 1);
1372
- rb_define_method(cXMLNode, "blank?", rxml_node_empty_q, 0);
1373
- rb_define_method(cXMLNode, "copy", rxml_node_copy, 1);
1374
- rb_define_method(cXMLNode, "content", rxml_node_content_get, 0);
1375
- rb_define_method(cXMLNode, "content=", rxml_node_content_set, 1);
1376
- rb_define_method(cXMLNode, "debug", rxml_node_debug, 0);
1377
- rb_define_method(cXMLNode, "doc", rxml_node_doc, 0);
1378
- rb_define_method(cXMLNode, "empty?", rxml_node_empty_q, 0);
1379
- rb_define_method(cXMLNode, "eql?", rxml_node_eql_q, 1);
1380
- rb_define_method(cXMLNode, "lang", rxml_node_lang_get, 0);
1381
- rb_define_method(cXMLNode, "lang=", rxml_node_lang_set, 1);
1382
- rb_define_method(cXMLNode, "line_num", rxml_node_line_num, 0);
1383
- rb_define_method(cXMLNode, "name", rxml_node_name_get, 0);
1384
- rb_define_method(cXMLNode, "name=", rxml_node_name_set, 1);
1385
- rb_define_method(cXMLNode, "node_type", rxml_node_type, 0);
1386
- rb_define_method(cXMLNode, "output_escaping?", rxml_node_output_escaping_q, 0);
1387
- rb_define_method(cXMLNode, "output_escaping=", rxml_node_output_escaping_set, 1);
1388
- rb_define_method(cXMLNode, "path", rxml_node_path, 0);
1389
- rb_define_method(cXMLNode, "remove!", rxml_node_remove_ex, 0);
1390
- rb_define_method(cXMLNode, "space_preserve", rxml_node_space_preserve_get, 0);
1391
- rb_define_method(cXMLNode, "space_preserve=", rxml_node_space_preserve_set, 1);
1392
- rb_define_method(cXMLNode, "to_s", rxml_node_to_s, -1);
1393
- rb_define_method(cXMLNode, "xlink?", rxml_node_xlink_q, 0);
1394
- rb_define_method(cXMLNode, "xlink_type", rxml_node_xlink_type, 0);
1395
- rb_define_method(cXMLNode, "xlink_type_name", rxml_node_xlink_type_name, 0);
1396
-
1397
- rb_define_alias(cXMLNode, "==", "eql?");
1398
- }
1
+ #include "ruby_libxml.h"
2
+ #include "ruby_xml_node.h"
3
+ #include <assert.h>
4
+
5
+ #include <libxml/debugXML.h>
6
+ #include <libxml/parserInternals.h>
7
+ #include <libxml/xlink.h>
8
+
9
+ VALUE cXMLNode;
10
+
11
+ /* Document-class: LibXML::XML::Node
12
+ *
13
+ * Nodes are the primary objects that make up an XML document.
14
+ * The node class represents most node types that are found in
15
+ * an XML document (but not LibXML::XML::Attributes, see LibXML::XML::Attr).
16
+ * It exposes libxml's full API for creating, querying
17
+ * moving and deleting node objects. Many of these methods are
18
+ * documented in the DOM Level 3 specification found at:
19
+ * http://www.w3.org/TR/DOM-Level-3-Core/. */
20
+
21
+
22
+ /* Memory management:
23
+ *
24
+ * The bindings create a one-to-one mapping between ruby objects and
25
+ * libxml documents and libxml parent nodes (ie, nodes that do not
26
+ * have a parent and do not belong to a document). In these cases,
27
+ * the bindings manage the memory. They do this by installing a free
28
+ * function and storing a back pointer to the Ruby object from the xmlnode
29
+ * using the _private member on libxml structures. When the Ruby object
30
+ * goes out of scope, the underlying libxml structure is freed. Libxml
31
+ * itself then frees all child node (recursively).
32
+ *
33
+ * For all other nodes (the vast majority), the bindings create temporary
34
+ * Ruby objects that get freed once they go out of scope. Thus there can be
35
+ * more than one ruby object pointing to the same xml node. To mostly hide
36
+ * this from programmers on the ruby side, the #eql? and #== methods are
37
+ * overriden to check if two ruby objects wrap the same xmlnode. If they do,
38
+ * then the methods return true. During the mark phase, each of these temporary
39
+ * objects marks its owning document, thereby keeping the Ruby document object
40
+ * alive and thus the xmldoc tree.
41
+ *
42
+ * In the sweep phase of the garbage collector, or when a program ends,
43
+ * there is no order to how Ruby objects are freed. In fact, the ruby document
44
+ * object is almost always freed before any ruby objects that wrap child nodes.
45
+ * However, this is ok because those ruby objects do not have a free function
46
+ * and are no longer in scope (since if they were the document would not be freed).
47
+ */
48
+
49
+ static void rxml_node_free(xmlNodePtr xnode)
50
+ {
51
+ /* The ruby object wrapping the xml object no longer exists and this
52
+ is a standalone node without a document or parent so ruby is
53
+ responsible for freeing the underlying node.*/
54
+ if (xnode->doc == NULL && xnode->parent == NULL)
55
+ {
56
+ // Remove the back linkage from libxml to Ruby
57
+ xnode->_private = NULL;
58
+ xmlFreeNode(xnode);
59
+ }
60
+ }
61
+
62
+ void rxml_node_manage(xmlNodePtr xnode, VALUE node)
63
+ {
64
+ RDATA(node)->dfree = (RUBY_DATA_FUNC)rxml_node_free;
65
+ xnode->_private = (void*)node;
66
+ }
67
+
68
+ void rxml_node_unmanage(xmlNodePtr xnode, VALUE node)
69
+ {
70
+ RDATA(node)->dfree = NULL;
71
+ xnode->_private = NULL;
72
+ }
73
+
74
+ xmlNodePtr rxml_node_root(xmlNodePtr xnode)
75
+ {
76
+ xmlNodePtr current = xnode;
77
+
78
+ while (current->parent)
79
+ {
80
+ current = current->parent;
81
+ }
82
+
83
+ return current;
84
+ }
85
+
86
+ void rxml_node_mark(xmlNodePtr xnode)
87
+ {
88
+ if (xnode->doc)
89
+ {
90
+ if (xnode->doc->_private)
91
+ {
92
+ VALUE doc = (VALUE)xnode->doc->_private;
93
+ rb_gc_mark(doc);
94
+ }
95
+ }
96
+ else if (xnode->parent)
97
+ {
98
+ xmlNodePtr root = rxml_node_root(xnode);
99
+ if (root->_private)
100
+ {
101
+ VALUE node = (VALUE)root->_private;
102
+ rb_gc_mark(node);
103
+ }
104
+ }
105
+ }
106
+
107
+ VALUE rxml_node_wrap(xmlNodePtr xnode)
108
+ {
109
+ VALUE result = Qnil;
110
+
111
+ // Is this node already wrapped?
112
+ if (xnode->_private)
113
+ {
114
+ result = (VALUE)xnode->_private;
115
+ }
116
+ else
117
+ {
118
+ result = Data_Wrap_Struct(cXMLNode, rxml_node_mark, NULL, xnode);
119
+ }
120
+
121
+ if (!xnode->doc && !xnode->parent)
122
+ {
123
+ rxml_node_manage(xnode, result);
124
+ }
125
+ return result;
126
+ }
127
+
128
+ static VALUE rxml_node_alloc(VALUE klass)
129
+ {
130
+ // This node was created from Ruby so we are responsible for freeing it not libxml
131
+ return Data_Wrap_Struct(klass, rxml_node_mark, rxml_node_free, NULL);
132
+ }
133
+
134
+ static xmlNodePtr rxml_get_xnode(VALUE node)
135
+ {
136
+ xmlNodePtr result;
137
+ Data_Get_Struct(node, xmlNode, result);
138
+
139
+ if (!result)
140
+ rb_raise(rb_eRuntimeError, "This node has already been freed.");
141
+
142
+ return result;
143
+ }
144
+
145
+ /*
146
+ * call-seq:
147
+ * XML::Node.new_cdata(content = nil) -> XML::Node
148
+ *
149
+ * Create a new #CDATA node, optionally setting
150
+ * the node's content.
151
+ */
152
+ static VALUE rxml_node_new_cdata(int argc, VALUE *argv, VALUE klass)
153
+ {
154
+ VALUE content = Qnil;
155
+ xmlNodePtr xnode;
156
+
157
+ rb_scan_args(argc, argv, "01", &content);
158
+
159
+ if (NIL_P(content))
160
+ {
161
+ xnode = xmlNewCDataBlock(NULL, NULL, 0);
162
+ }
163
+ else
164
+ {
165
+ content = rb_obj_as_string(content);
166
+ xnode = xmlNewCDataBlock(NULL, (xmlChar*) StringValuePtr(content), (int)RSTRING_LEN(content));
167
+ }
168
+
169
+ if (xnode == NULL)
170
+ rxml_raise(xmlGetLastError());
171
+
172
+ return rxml_node_wrap(xnode);
173
+ }
174
+
175
+ /*
176
+ * call-seq:
177
+ * XML::Node.new_comment(content = nil) -> XML::Node
178
+ *
179
+ * Create a new comment node, optionally setting
180
+ * the node's content.
181
+ *
182
+ */
183
+ static VALUE rxml_node_new_comment(int argc, VALUE *argv, VALUE klass)
184
+ {
185
+ VALUE content = Qnil;
186
+ xmlNodePtr xnode;
187
+
188
+ rb_scan_args(argc, argv, "01", &content);
189
+
190
+ if (NIL_P(content))
191
+ {
192
+ xnode = xmlNewComment(NULL);
193
+ }
194
+ else
195
+ {
196
+ content = rb_obj_as_string(content);
197
+ xnode = xmlNewComment((xmlChar*) StringValueCStr(content));
198
+ }
199
+
200
+ if (xnode == NULL)
201
+ rxml_raise(xmlGetLastError());
202
+
203
+ return rxml_node_wrap(xnode);
204
+ }
205
+
206
+ /*
207
+ * call-seq:
208
+ * XML::Node.new_pi(name, content = nil) -> XML::Node
209
+ *
210
+ * Create a new pi node, optionally setting
211
+ * the node's content.
212
+ *
213
+ */
214
+ static VALUE rxml_node_new_pi(int argc, VALUE *argv, VALUE klass)
215
+ {
216
+ VALUE name = Qnil;
217
+ VALUE content = Qnil;
218
+ xmlNodePtr xnode;
219
+
220
+ rb_scan_args(argc, argv, "11", &name, &content);
221
+
222
+ if (NIL_P(name))
223
+ {
224
+ rb_raise(rb_eRuntimeError, "You must provide me with a name for a PI.");
225
+ }
226
+ name = rb_obj_as_string(name);
227
+ if (NIL_P(content))
228
+ {
229
+ xnode = xmlNewPI((xmlChar*) StringValuePtr(name), NULL);
230
+ }
231
+ else
232
+ {
233
+ content = rb_obj_as_string(content);
234
+ xnode = xmlNewPI((xmlChar*) StringValuePtr(name), (xmlChar*) StringValueCStr(content));
235
+ }
236
+
237
+ if (xnode == NULL)
238
+ rxml_raise(xmlGetLastError());
239
+
240
+ return rxml_node_wrap(xnode);
241
+ }
242
+
243
+ /*
244
+ * call-seq:
245
+ * XML::Node.new_text(content) -> XML::Node
246
+ *
247
+ * Create a new text node.
248
+ *
249
+ */
250
+ static VALUE rxml_node_new_text(VALUE klass, VALUE content)
251
+ {
252
+ xmlNodePtr xnode;
253
+ Check_Type(content, T_STRING);
254
+ content = rb_obj_as_string(content);
255
+
256
+ xnode = xmlNewText((xmlChar*) StringValueCStr(content));
257
+
258
+ if (xnode == NULL)
259
+ rxml_raise(xmlGetLastError());
260
+
261
+ return rxml_node_wrap(xnode);
262
+ }
263
+
264
+ static VALUE rxml_node_content_set(VALUE self, VALUE content);
265
+
266
+ /*
267
+ * call-seq:
268
+ * XML::Node.initialize(name, content = nil, namespace = nil) -> XML::Node
269
+ *
270
+ * Creates a new element with the specified name, content and
271
+ * namespace. The content and namespace may be nil.
272
+ */
273
+ static VALUE rxml_node_initialize(int argc, VALUE *argv, VALUE self)
274
+ {
275
+ VALUE name;
276
+ VALUE content;
277
+ VALUE ns;
278
+ xmlNodePtr xnode = NULL;
279
+ xmlNsPtr xns = NULL;
280
+
281
+ rb_scan_args(argc, argv, "12", &name, &content, &ns);
282
+
283
+ name = rb_obj_as_string(name);
284
+
285
+ if (!NIL_P(ns))
286
+ Data_Get_Struct(ns, xmlNs, xns);
287
+
288
+ xnode = xmlNewNode(xns, (xmlChar*) StringValuePtr(name));
289
+
290
+ if (xnode == NULL)
291
+ rxml_raise(xmlGetLastError());
292
+
293
+ // Link the ruby wrapper to the underlying libxml node
294
+ RDATA(self)->data = xnode;
295
+
296
+ // Ruby is in charge of managing this node's memory
297
+ rxml_node_manage(xnode, self);
298
+
299
+ if (!NIL_P(content))
300
+ rxml_node_content_set(self, content);
301
+
302
+ return self;
303
+ }
304
+
305
+ static VALUE rxml_node_modify_dom(VALUE self, VALUE target,
306
+ xmlNodePtr (*xmlFunc)(xmlNodePtr, xmlNodePtr))
307
+ {
308
+ xmlNodePtr xnode, xtarget, xresult;
309
+
310
+ if (rb_obj_is_kind_of(target, cXMLNode) == Qfalse)
311
+ rb_raise(rb_eTypeError, "Must pass an XML::Node object");
312
+
313
+ xnode = rxml_get_xnode(self);
314
+ xtarget = rxml_get_xnode(target);
315
+
316
+ if (xtarget->doc != NULL && xtarget->doc != xnode->doc)
317
+ rb_raise(eXMLError, "Nodes belong to different documents. You must first import the node by calling LibXML::XML::Document.import");
318
+
319
+ xmlUnlinkNode(xtarget);
320
+
321
+ // Target is about to have a parent, so stop having ruby manage it.
322
+ rxml_node_unmanage(xtarget, target);
323
+
324
+ // This target node could be freed here and be replaced by a different node
325
+ xresult = xmlFunc(xnode, xtarget);
326
+
327
+ if (!xresult)
328
+ rxml_raise(xmlGetLastError());
329
+
330
+ /* Assume the target was freed, we need to fix up the ruby object to point to the
331
+ newly returned node. */
332
+ RDATA(target)->data = xresult;
333
+
334
+ return target;
335
+ }
336
+
337
+ /*
338
+ * call-seq:
339
+ * node.base_uri -> "uri"
340
+ *
341
+ * Obtain this node's base URI.
342
+ */
343
+ static VALUE rxml_node_base_uri_get(VALUE self)
344
+ {
345
+ xmlNodePtr xnode;
346
+ xmlChar* base_uri;
347
+ VALUE result = Qnil;
348
+
349
+ xnode = rxml_get_xnode(self);
350
+
351
+ if (xnode->doc == NULL)
352
+ return (result);
353
+
354
+ base_uri = xmlNodeGetBase(xnode->doc, xnode);
355
+ if (base_uri)
356
+ {
357
+ result = rxml_new_cstr( base_uri, NULL);
358
+ xmlFree(base_uri);
359
+ }
360
+
361
+ return (result);
362
+ }
363
+
364
+ // TODO node_base_set should support setting back to nil
365
+
366
+ /*
367
+ * call-seq:
368
+ * node.base_uri = "uri"
369
+ *
370
+ * Set this node's base URI.
371
+ */
372
+ static VALUE rxml_node_base_uri_set(VALUE self, VALUE uri)
373
+ {
374
+ xmlNodePtr xnode;
375
+
376
+ Check_Type(uri, T_STRING);
377
+ xnode = rxml_get_xnode(self);
378
+ if (xnode->doc == NULL)
379
+ return (Qnil);
380
+
381
+ xmlNodeSetBase(xnode, (xmlChar*) StringValuePtr(uri));
382
+ return (Qtrue);
383
+ }
384
+
385
+ /*
386
+ * call-seq:
387
+ * node.content -> "string"
388
+ *
389
+ * Obtain this node's content as a string.
390
+ */
391
+ static VALUE rxml_node_content_get(VALUE self)
392
+ {
393
+ xmlNodePtr xnode;
394
+ xmlChar *content;
395
+ VALUE result = Qnil;
396
+
397
+ xnode = rxml_get_xnode(self);
398
+ content = xmlNodeGetContent(xnode);
399
+ if (content)
400
+ {
401
+ result = rxml_new_cstr(content, NULL);
402
+ xmlFree(content);
403
+ }
404
+
405
+ return result;
406
+ }
407
+
408
+ /*
409
+ * call-seq:
410
+ * node.content = "string"
411
+ *
412
+ * Set this node's content to the specified string.
413
+ */
414
+ static VALUE rxml_node_content_set(VALUE self, VALUE content)
415
+ {
416
+ xmlNodePtr xnode;
417
+ xmlChar* encoded_content;
418
+
419
+ Check_Type(content, T_STRING);
420
+ xnode = rxml_get_xnode(self);
421
+ encoded_content = xmlEncodeSpecialChars(xnode->doc, (xmlChar*) StringValuePtr(content));
422
+ xmlNodeSetContent(xnode, encoded_content);
423
+ xmlFree(encoded_content);
424
+ return (Qtrue);
425
+ }
426
+
427
+ /*
428
+ * call-seq:
429
+ * node.debug -> true|false
430
+ *
431
+ * Print libxml debugging information to stdout.
432
+ * Requires that libxml was compiled with debugging enabled.
433
+ */
434
+ static VALUE rxml_node_debug(VALUE self)
435
+ {
436
+ #ifdef LIBXML_DEBUG_ENABLED
437
+ xmlNodePtr xnode;
438
+ xnode = rxml_get_xnode(self);
439
+ xmlDebugDumpNode(NULL, xnode, 2);
440
+ return Qtrue;
441
+ #else
442
+ rb_warn("libxml was compiled without debugging support.");
443
+ return Qfalse;
444
+ #endif
445
+ }
446
+
447
+ /*
448
+ * call-seq:
449
+ * node.first -> XML::Node
450
+ *
451
+ * Returns this node's first child node if any.
452
+ */
453
+ static VALUE rxml_node_first_get(VALUE self)
454
+ {
455
+ xmlNodePtr xnode;
456
+
457
+ xnode = rxml_get_xnode(self);
458
+
459
+ if (xnode->children)
460
+ return (rxml_node_wrap(xnode->children));
461
+ else
462
+ return (Qnil);
463
+ }
464
+
465
+
466
+ /*
467
+ * call-seq:
468
+ * curr_node << "Some text"
469
+ * curr_node << node
470
+ *
471
+ * Add the specified text or XML::Node as a new child node to the
472
+ * current node.
473
+ *
474
+ * If the specified argument is a string, it should be a raw string
475
+ * that contains unescaped XML special characters. Entity references
476
+ * are not supported.
477
+ *
478
+ * The method will return the current node.
479
+ */
480
+ static VALUE rxml_node_content_add(VALUE self, VALUE obj)
481
+ {
482
+ xmlNodePtr xnode;
483
+ VALUE str;
484
+
485
+ xnode = rxml_get_xnode(self);
486
+
487
+ /* XXX This should only be legal for a CDATA type node, I think,
488
+ * resulting in a merge of content, as if a string were passed
489
+ * danj 070827
490
+ */
491
+ if (rb_obj_is_kind_of(obj, cXMLNode))
492
+ {
493
+ rxml_node_modify_dom(self, obj, xmlAddChild);
494
+ }
495
+ else
496
+ {
497
+ str = rb_obj_as_string(obj);
498
+ if (NIL_P(str) || TYPE(str) != T_STRING)
499
+ rb_raise(rb_eTypeError, "invalid argument: must be string or XML::Node");
500
+
501
+ xmlNodeAddContent(xnode, (xmlChar*) StringValuePtr(str));
502
+ }
503
+ return self;
504
+ }
505
+
506
+ /*
507
+ * call-seq:
508
+ * node.doc -> document
509
+ *
510
+ * Obtain the XML::Document this node belongs to.
511
+ */
512
+ static VALUE rxml_node_doc(VALUE self)
513
+ {
514
+ xmlDocPtr xdoc = NULL;
515
+ xmlNodePtr xnode = rxml_get_xnode(self);
516
+
517
+ switch (xnode->type)
518
+ {
519
+ case XML_DOCUMENT_NODE:
520
+ #ifdef LIBXML_DOCB_ENABLED
521
+ case XML_DOCB_DOCUMENT_NODE:
522
+ #endif
523
+ case XML_HTML_DOCUMENT_NODE:
524
+ case XML_NAMESPACE_DECL:
525
+ break;
526
+ case XML_ATTRIBUTE_NODE:
527
+ xdoc = (xmlDocPtr)((xmlAttrPtr) xnode->doc);
528
+ break;
529
+ default:
530
+ xdoc = xnode->doc;
531
+ }
532
+
533
+ if (xdoc == NULL)
534
+ return (Qnil);
535
+
536
+ return (VALUE)xdoc->_private;
537
+ }
538
+
539
+ /*
540
+ * call-seq:
541
+ * node.to_s -> "string"
542
+ * node.to_s(:indent => true, :encoding => 'UTF-8', :level => 0) -> "string"
543
+ *
544
+ * Converts a node, and all of its children, to a string representation.
545
+ * To include only the node's children, use the the XML::Node#inner_xml
546
+ * method.
547
+ *
548
+ * You may provide an optional hash table to control how the string is
549
+ * generated. Valid options are:
550
+ *
551
+ * :indent - Specifies if the string should be indented. The default value
552
+ * is true. Note that indentation is only added if both :indent is
553
+ * true and XML.indent_tree_output is true. If :indent is set to false,
554
+ * then both indentation and line feeds are removed from the result.
555
+ *
556
+ * :level - Specifies the indentation level. The amount of indentation
557
+ * is equal to the (level * number_spaces) + number_spaces, where libxml
558
+ * defaults the number of spaces to 2. Thus a level of 0 results in
559
+ * 2 spaces, level 1 results in 4 spaces, level 2 results in 6 spaces, etc.
560
+ *
561
+ * :encoding - Specifies the output encoding of the string. It
562
+ * defaults to XML::Encoding::UTF8. To change it, use one of the
563
+ * XML::Encoding encoding constants. */
564
+
565
+ static VALUE rxml_node_to_s(int argc, VALUE *argv, VALUE self)
566
+ {
567
+ VALUE result = Qnil;
568
+ VALUE options = Qnil;
569
+ xmlNodePtr xnode;
570
+ xmlCharEncodingHandlerPtr encodingHandler;
571
+ xmlOutputBufferPtr output;
572
+
573
+ int level = 0;
574
+ int indent = 1;
575
+ const xmlChar *xencoding = (const xmlChar*)"UTF-8";
576
+
577
+ rb_scan_args(argc, argv, "01", &options);
578
+
579
+ if (!NIL_P(options))
580
+ {
581
+ VALUE rencoding, rindent, rlevel;
582
+ Check_Type(options, T_HASH);
583
+ rencoding = rb_hash_aref(options, ID2SYM(rb_intern("encoding")));
584
+ rindent = rb_hash_aref(options, ID2SYM(rb_intern("indent")));
585
+ rlevel = rb_hash_aref(options, ID2SYM(rb_intern("level")));
586
+
587
+ if (rindent == Qfalse)
588
+ indent = 0;
589
+
590
+ if (rlevel != Qnil)
591
+ level = NUM2INT(rlevel);
592
+
593
+ if (rencoding != Qnil)
594
+ {
595
+ xencoding = (const xmlChar*)xmlGetCharEncodingName((xmlCharEncoding)NUM2INT(rencoding));
596
+ if (!xencoding)
597
+ rb_raise(rb_eArgError, "Unknown encoding value: %d", NUM2INT(rencoding));
598
+ }
599
+ }
600
+
601
+ encodingHandler = xmlFindCharEncodingHandler((const char*)xencoding);
602
+ output = xmlAllocOutputBuffer(encodingHandler);
603
+
604
+ xnode = rxml_get_xnode(self);
605
+
606
+ xmlNodeDumpOutput(output, xnode->doc, xnode, level, indent, (const char*)xencoding);
607
+ xmlOutputBufferFlush(output);
608
+
609
+ #ifdef LIBXML2_NEW_BUFFER
610
+ if (output->conv)
611
+ result = rxml_new_cstr(xmlBufContent(output->conv), xencoding);
612
+ else
613
+ result = rxml_new_cstr(xmlBufContent(output->buffer), xencoding);
614
+ #else
615
+ if (output->conv)
616
+ result = rxml_new_cstr(xmlBufferContent(output->conv), xencoding);
617
+ else
618
+ result = rxml_new_cstr(xmlBufferContent(output->buffer), xencoding);
619
+ #endif
620
+
621
+ xmlOutputBufferClose(output);
622
+
623
+ return result;
624
+ }
625
+
626
+
627
+ /*
628
+ * call-seq:
629
+ * node.each -> XML::Node
630
+ *
631
+ * Iterates over this node's children, including text
632
+ * nodes, element nodes, etc. If you wish to iterate
633
+ * only over child elements, use XML::Node#each_element.
634
+ *
635
+ * doc = XML::Document.new('model/books.xml')
636
+ * doc.root.each {|node| puts node}
637
+ */
638
+ static VALUE rxml_node_each(VALUE self)
639
+ {
640
+ xmlNodePtr xnode;
641
+ xmlNodePtr xcurrent;
642
+ xnode = rxml_get_xnode(self);
643
+
644
+ xcurrent = xnode->children;
645
+
646
+ while (xcurrent)
647
+ {
648
+ /* The user could remove this node, so first stache
649
+ away the next node. */
650
+ xmlNodePtr xnext = xcurrent->next;
651
+
652
+ rb_yield(rxml_node_wrap(xcurrent));
653
+ xcurrent = xnext;
654
+ }
655
+ return Qnil;
656
+ }
657
+
658
+ /*
659
+ * call-seq:
660
+ * node.empty? -> (true|false)
661
+ *
662
+ * Determine whether this node is an empty or whitespace only text-node.
663
+ */
664
+ static VALUE rxml_node_empty_q(VALUE self)
665
+ {
666
+ xmlNodePtr xnode;
667
+ xnode = rxml_get_xnode(self);
668
+ if (xnode == NULL)
669
+ return (Qnil);
670
+
671
+ return ((xmlIsBlankNode(xnode) == 1) ? Qtrue : Qfalse);
672
+ }
673
+
674
+
675
+ /*
676
+ * call-seq:
677
+ * node.eql?(other_node) => (true|false)
678
+ *
679
+ * Test equality between the two nodes. Two nodes are equal
680
+ * if they are the same node.*/
681
+ static VALUE rxml_node_eql_q(VALUE self, VALUE other)
682
+ {
683
+ if (self == other)
684
+ {
685
+ return Qtrue;
686
+ }
687
+ else if (NIL_P(other))
688
+ {
689
+ return Qfalse;
690
+ }
691
+ else
692
+ {
693
+ xmlNodePtr xnode = rxml_get_xnode(self);
694
+ xmlNodePtr xnode_other = rxml_get_xnode(other);
695
+ return xnode == xnode_other ? Qtrue : Qfalse;
696
+ }
697
+ }
698
+
699
+ /*
700
+ * call-seq:
701
+ * node.lang -> "string"
702
+ *
703
+ * Obtain the language set for this node, if any.
704
+ * This is set in XML via the xml:lang attribute.
705
+ */
706
+ static VALUE rxml_node_lang_get(VALUE self)
707
+ {
708
+ xmlNodePtr xnode;
709
+ xmlChar *lang;
710
+ VALUE result = Qnil;
711
+
712
+ xnode = rxml_get_xnode(self);
713
+ lang = xmlNodeGetLang(xnode);
714
+
715
+ if (lang)
716
+ {
717
+ result = rxml_new_cstr( lang, NULL);
718
+ xmlFree(lang);
719
+ }
720
+
721
+ return (result);
722
+ }
723
+
724
+ // TODO node_lang_set should support setting back to nil
725
+
726
+ /*
727
+ * call-seq:
728
+ * node.lang = "string"
729
+ *
730
+ * Set the language for this node. This affects the value
731
+ * of the xml:lang attribute.
732
+ */
733
+ static VALUE rxml_node_lang_set(VALUE self, VALUE lang)
734
+ {
735
+ xmlNodePtr xnode;
736
+
737
+ Check_Type(lang, T_STRING);
738
+ xnode = rxml_get_xnode(self);
739
+ xmlNodeSetLang(xnode, (xmlChar*) StringValuePtr(lang));
740
+
741
+ return (Qtrue);
742
+ }
743
+
744
+ /*
745
+ * call-seq:
746
+ * node.last -> XML::Node
747
+ *
748
+ * Obtain the last child node of this node, if any.
749
+ */
750
+ static VALUE rxml_node_last_get(VALUE self)
751
+ {
752
+ xmlNodePtr xnode;
753
+
754
+ xnode = rxml_get_xnode(self);
755
+
756
+ if (xnode->last)
757
+ return (rxml_node_wrap(xnode->last));
758
+ else
759
+ return (Qnil);
760
+ }
761
+
762
+ /*
763
+ * call-seq:
764
+ * node.line_num -> num
765
+ *
766
+ * Obtain the line number (in the XML document) that this
767
+ * node was read from. If +default_line_numbers+ is set
768
+ * false (the default), this method returns zero.
769
+ */
770
+ static VALUE rxml_node_line_num(VALUE self)
771
+ {
772
+ xmlNodePtr xnode;
773
+ long line_num;
774
+ xnode = rxml_get_xnode(self);
775
+
776
+ line_num = xmlGetLineNo(xnode);
777
+ if (line_num == -1)
778
+ return (Qnil);
779
+ else
780
+ return (LONG2NUM((long) line_num));
781
+ }
782
+
783
+ /*
784
+ * call-seq:
785
+ * node.xlink? -> (true|false)
786
+ *
787
+ * Determine whether this node is an xlink node.
788
+ */
789
+ static VALUE rxml_node_xlink_q(VALUE self)
790
+ {
791
+ xmlNodePtr xnode;
792
+ xlinkType xlt;
793
+
794
+ xnode = rxml_get_xnode(self);
795
+ xlt = xlinkIsLink(xnode->doc, xnode);
796
+
797
+ if (xlt == XLINK_TYPE_NONE)
798
+ return (Qfalse);
799
+ else
800
+ return (Qtrue);
801
+ }
802
+
803
+ /*
804
+ * call-seq:
805
+ * node.xlink_type -> num
806
+ *
807
+ * Obtain the type identifier for this xlink, if applicable.
808
+ * If this is not an xlink node (see +xlink?+), will return
809
+ * nil.
810
+ */
811
+ static VALUE rxml_node_xlink_type(VALUE self)
812
+ {
813
+ xmlNodePtr xnode;
814
+ xlinkType xlt;
815
+
816
+ xnode = rxml_get_xnode(self);
817
+ xlt = xlinkIsLink(xnode->doc, xnode);
818
+
819
+ if (xlt == XLINK_TYPE_NONE)
820
+ return (Qnil);
821
+ else
822
+ return (INT2NUM(xlt));
823
+ }
824
+
825
+ /*
826
+ * call-seq:
827
+ * node.xlink_type_name -> "string"
828
+ *
829
+ * Obtain the type name for this xlink, if applicable.
830
+ * If this is not an xlink node (see +xlink?+), will return
831
+ * nil.
832
+ */
833
+ static VALUE rxml_node_xlink_type_name(VALUE self)
834
+ {
835
+ xmlNodePtr xnode;
836
+ xlinkType xlt;
837
+
838
+ xnode = rxml_get_xnode(self);
839
+ xlt = xlinkIsLink(xnode->doc, xnode);
840
+
841
+ switch (xlt)
842
+ {
843
+ case XLINK_TYPE_NONE:
844
+ return (Qnil);
845
+ case XLINK_TYPE_SIMPLE:
846
+ return (rxml_new_cstr((const xmlChar*)"simple", NULL));
847
+ case XLINK_TYPE_EXTENDED:
848
+ return (rxml_new_cstr((const xmlChar*)"extended", NULL));
849
+ case XLINK_TYPE_EXTENDED_SET:
850
+ return (rxml_new_cstr((const xmlChar*)"extended_set", NULL));
851
+ default:
852
+ rb_fatal("Unknowng xlink type, %d", xlt);
853
+ }
854
+ }
855
+
856
+ /*
857
+ * call-seq:
858
+ * node.name -> "string"
859
+ *
860
+ * Obtain this node's name.
861
+ */
862
+ static VALUE rxml_node_name_get(VALUE self)
863
+ {
864
+ xmlNodePtr xnode;
865
+ const xmlChar *name;
866
+
867
+ xnode = rxml_get_xnode(self);
868
+
869
+ switch (xnode->type)
870
+ {
871
+ case XML_DOCUMENT_NODE:
872
+ #ifdef LIBXML_DOCB_ENABLED
873
+ case XML_DOCB_DOCUMENT_NODE:
874
+ #endif
875
+ case XML_HTML_DOCUMENT_NODE:
876
+ {
877
+ xmlDocPtr doc = (xmlDocPtr) xnode;
878
+ name = doc->URL;
879
+ break;
880
+ }
881
+ case XML_ATTRIBUTE_NODE:
882
+ {
883
+ xmlAttrPtr attr = (xmlAttrPtr) xnode;
884
+ name = attr->name;
885
+ break;
886
+ }
887
+ case XML_NAMESPACE_DECL:
888
+ {
889
+ xmlNsPtr ns = (xmlNsPtr) xnode;
890
+ name = ns->prefix;
891
+ break;
892
+ }
893
+ default:
894
+ name = xnode->name;
895
+ break;
896
+ }
897
+
898
+ if (xnode->name == NULL)
899
+ return (Qnil);
900
+ else
901
+ return (rxml_new_cstr( name, NULL));
902
+ }
903
+
904
+ /*
905
+ * call-seq:
906
+ * node.name = "string"
907
+ *
908
+ * Set this node's name.
909
+ */
910
+ static VALUE rxml_node_name_set(VALUE self, VALUE name)
911
+ {
912
+ xmlNodePtr xnode;
913
+ const xmlChar *xname;
914
+
915
+ Check_Type(name, T_STRING);
916
+ xnode = rxml_get_xnode(self);
917
+ xname = (const xmlChar*)StringValuePtr(name);
918
+
919
+ /* Note: calling xmlNodeSetName() for a text node is ignored by libXML. */
920
+ xmlNodeSetName(xnode, xname);
921
+
922
+ return (Qtrue);
923
+ }
924
+
925
+ /*
926
+ * call-seq:
927
+ * node.next -> XML::Node
928
+ *
929
+ * Returns the next sibling node if one exists.
930
+ */
931
+ static VALUE rxml_node_next_get(VALUE self)
932
+ {
933
+ xmlNodePtr xnode;
934
+
935
+ xnode = rxml_get_xnode(self);
936
+
937
+ if (xnode->next)
938
+ return (rxml_node_wrap(xnode->next));
939
+ else
940
+ return (Qnil);
941
+ }
942
+
943
+ /*
944
+ * call-seq:
945
+ * curr_node.next = node
946
+ *
947
+ * Adds the specified node as the next sibling of the current node.
948
+ * If the node already exists in the document, it is first removed
949
+ * from its existing context. Any adjacent text nodes will be
950
+ * merged together, meaning the returned node may be different
951
+ * than the original node.
952
+ */
953
+ static VALUE rxml_node_next_set(VALUE self, VALUE next)
954
+ {
955
+ return rxml_node_modify_dom(self, next, xmlAddNextSibling);
956
+ }
957
+
958
+ /*
959
+ * call-seq:
960
+ * node.parent -> XML::Node
961
+ *
962
+ * Obtain this node's parent node, if any.
963
+ */
964
+ static VALUE rxml_node_parent_get(VALUE self)
965
+ {
966
+ xmlNodePtr xnode;
967
+
968
+ xnode = rxml_get_xnode(self);
969
+
970
+ if (xnode->parent)
971
+ return (rxml_node_wrap(xnode->parent));
972
+ else
973
+ return (Qnil);
974
+ }
975
+
976
+ /*
977
+ * call-seq:
978
+ * node.path -> path
979
+ *
980
+ * Obtain this node's path.
981
+ */
982
+ static VALUE rxml_node_path(VALUE self)
983
+ {
984
+ xmlNodePtr xnode;
985
+ xmlChar *path;
986
+ VALUE result = Qnil;
987
+
988
+ xnode = rxml_get_xnode(self);
989
+ path = xmlGetNodePath(xnode);
990
+
991
+ if (path)
992
+ {
993
+ result = rxml_new_cstr( path, NULL);
994
+ xmlFree(path);
995
+ }
996
+
997
+ return result;
998
+ }
999
+
1000
+ /*
1001
+ * call-seq:
1002
+ * node.prev -> XML::Node
1003
+ *
1004
+ * Obtain the previous sibling, if any.
1005
+ */
1006
+ static VALUE rxml_node_prev_get(VALUE self)
1007
+ {
1008
+ xmlNodePtr xnode;
1009
+ xmlNodePtr node;
1010
+ xnode = rxml_get_xnode(self);
1011
+
1012
+ switch (xnode->type)
1013
+ {
1014
+ case XML_DOCUMENT_NODE:
1015
+ #ifdef LIBXML_DOCB_ENABLED
1016
+ case XML_DOCB_DOCUMENT_NODE:
1017
+ #endif
1018
+ case XML_HTML_DOCUMENT_NODE:
1019
+ case XML_NAMESPACE_DECL:
1020
+ node = NULL;
1021
+ break;
1022
+ case XML_ATTRIBUTE_NODE:
1023
+ {
1024
+ xmlAttrPtr attr = (xmlAttrPtr) xnode;
1025
+ node = (xmlNodePtr) attr->prev;
1026
+ }
1027
+ break;
1028
+ default:
1029
+ node = xnode->prev;
1030
+ break;
1031
+ }
1032
+
1033
+ if (node == NULL)
1034
+ return (Qnil);
1035
+ else
1036
+ return (rxml_node_wrap(node));
1037
+ }
1038
+
1039
+ /*
1040
+ * call-seq:
1041
+ * curr_node.prev = node
1042
+ *
1043
+ * Adds the specified node as the previous sibling of the current node.
1044
+ * If the node already exists in the document, it is first removed
1045
+ * from its existing context. Any adjacent text nodes will be
1046
+ * merged together, meaning the returned node may be different
1047
+ * than the original node.
1048
+ */
1049
+ static VALUE rxml_node_prev_set(VALUE self, VALUE prev)
1050
+ {
1051
+ return rxml_node_modify_dom(self, prev, xmlAddPrevSibling);
1052
+ }
1053
+
1054
+ /*
1055
+ * call-seq:
1056
+ * node.attributes -> attributes
1057
+ *
1058
+ * Returns the XML::Attributes for this node.
1059
+ */
1060
+ static VALUE rxml_node_attributes_get(VALUE self)
1061
+ {
1062
+ xmlNodePtr xnode;
1063
+
1064
+ xnode = rxml_get_xnode(self);
1065
+ return rxml_attributes_new(xnode);
1066
+ }
1067
+
1068
+ /*
1069
+ * call-seq:
1070
+ * node.property("name") -> "string"
1071
+ * node["name"] -> "string"
1072
+ *
1073
+ * Obtain the named property.
1074
+ */
1075
+ static VALUE rxml_node_attribute_get(VALUE self, VALUE name)
1076
+ {
1077
+ VALUE attributes = rxml_node_attributes_get(self);
1078
+ return rxml_attributes_attribute_get(attributes, name);
1079
+ }
1080
+
1081
+ /*
1082
+ * call-seq:
1083
+ * node["name"] = "string"
1084
+ *
1085
+ * Set the named property.
1086
+ */
1087
+ static VALUE rxml_node_property_set(VALUE self, VALUE name, VALUE value)
1088
+ {
1089
+ VALUE attributes = rxml_node_attributes_get(self);
1090
+ return rxml_attributes_attribute_set(attributes, name, value);
1091
+ }
1092
+
1093
+ /*
1094
+ * call-seq:
1095
+ * node.remove! -> node
1096
+ *
1097
+ * Removes this node and its children from the document tree by setting its document,
1098
+ * parent and siblings to nil. You can add the returned node back into a document.
1099
+ * Otherwise, the node will be freed once any references to it go out of scope.
1100
+ */
1101
+
1102
+ static VALUE rxml_node_remove_ex(VALUE self)
1103
+ {
1104
+ xmlNodePtr xnode = rxml_get_xnode(self);
1105
+
1106
+ // Now unlink the node from its parent
1107
+ xmlUnlinkNode(xnode);
1108
+
1109
+ // Ruby now manages this node
1110
+ rxml_node_manage(xnode, self);
1111
+
1112
+ // Now return the removed node so the user can do something with it
1113
+ return self;
1114
+ }
1115
+
1116
+ /*
1117
+ * call-seq:
1118
+ * curr_node.sibling = node
1119
+ *
1120
+ * Adds the specified node as the end of the current node's list
1121
+ * of siblings. If the node already exists in the document, it
1122
+ * is first removed from its existing context. Any adjacent text
1123
+ * nodes will be merged together, meaning the returned node may
1124
+ * be different than the original node.
1125
+ */
1126
+ static VALUE rxml_node_sibling_set(VALUE self, VALUE sibling)
1127
+ {
1128
+ return rxml_node_modify_dom(self, sibling, xmlAddSibling);
1129
+ }
1130
+
1131
+ /*
1132
+ * call-seq:
1133
+ * text_node.output_escaping? -> (true|false)
1134
+ * element_node.output_escaping? -> (true|false|nil)
1135
+ * attribute_node.output_escaping? -> (true|false|nil)
1136
+ * other_node.output_escaping? -> (nil)
1137
+ *
1138
+ * Determine whether this node escapes it's output or not.
1139
+ *
1140
+ * Text nodes return only +true+ or +false+. Element and attribute nodes
1141
+ * examine their immediate text node children to determine the value.
1142
+ * Any other type of node always returns +nil+.
1143
+ *
1144
+ * If an element or attribute node has at least one immediate child text node
1145
+ * and all the immediate text node children have the same +output_escaping?+
1146
+ * value, that value is returned. Otherwise, +nil+ is returned.
1147
+ */
1148
+ static VALUE rxml_node_output_escaping_q(VALUE self)
1149
+ {
1150
+ xmlNodePtr xnode;
1151
+ xnode = rxml_get_xnode(self);
1152
+
1153
+ switch (xnode->type) {
1154
+ case XML_TEXT_NODE:
1155
+ return xnode->name==xmlStringTextNoenc ? Qfalse : Qtrue;
1156
+ case XML_ELEMENT_NODE:
1157
+ case XML_ATTRIBUTE_NODE:
1158
+ {
1159
+ xmlNodePtr tmp = xnode->children;
1160
+ const xmlChar *match = NULL;
1161
+
1162
+ /* Find the first text node and use it as the reference. */
1163
+ while (tmp && tmp->type != XML_TEXT_NODE)
1164
+ tmp = tmp->next;
1165
+ if (! tmp)
1166
+ return Qnil;
1167
+ match = tmp->name;
1168
+
1169
+ /* Walk the remaining text nodes until we run out or one doesn't match. */
1170
+ while (tmp && (tmp->type != XML_TEXT_NODE || match == tmp->name))
1171
+ tmp = tmp->next;
1172
+
1173
+ /* We're left with either the mismatched node or the aggregate result. */
1174
+ return tmp ? Qnil : (match==xmlStringTextNoenc ? Qfalse : Qtrue);
1175
+ }
1176
+ break;
1177
+ default:
1178
+ return Qnil;
1179
+ }
1180
+ }
1181
+
1182
+ /*
1183
+ * call-seq:
1184
+ * text_node.output_escaping = true|false
1185
+ * element_node.output_escaping = true|false
1186
+ * attribute_node.output_escaping = true|false
1187
+ *
1188
+ * Controls whether this text node or the immediate text node children of an
1189
+ * element or attribute node escapes their output. Any other type of node
1190
+ * will simply ignore this operation.
1191
+ *
1192
+ * Text nodes which are added to an element or attribute node will be affected
1193
+ * by any previous setting of this property.
1194
+ */
1195
+ static VALUE rxml_node_output_escaping_set(VALUE self, VALUE value)
1196
+ {
1197
+ xmlNodePtr xnode;
1198
+ xnode = rxml_get_xnode(self);
1199
+
1200
+ switch (xnode->type) {
1201
+ case XML_TEXT_NODE:
1202
+ xnode->name = (value != Qfalse && value != Qnil) ? xmlStringText : xmlStringTextNoenc;
1203
+ break;
1204
+ case XML_ELEMENT_NODE:
1205
+ case XML_ATTRIBUTE_NODE:
1206
+ {
1207
+ const xmlChar *name = (value != Qfalse && value != Qnil) ? xmlStringText : xmlStringTextNoenc;
1208
+ xmlNodePtr tmp;
1209
+ for (tmp = xnode->children; tmp; tmp = tmp->next)
1210
+ if (tmp->type == XML_TEXT_NODE)
1211
+ tmp->name = name;
1212
+ }
1213
+ break;
1214
+ default:
1215
+ return Qnil;
1216
+ }
1217
+
1218
+ return (value!=Qfalse && value!=Qnil) ? Qtrue : Qfalse;
1219
+ }
1220
+
1221
+ /*
1222
+ * call-seq:
1223
+ * node.space_preserve -> (true|false)
1224
+ *
1225
+ * Determine whether this node preserves whitespace.
1226
+ */
1227
+ static VALUE rxml_node_space_preserve_get(VALUE self)
1228
+ {
1229
+ xmlNodePtr xnode;
1230
+
1231
+ xnode = rxml_get_xnode(self);
1232
+ return (INT2NUM(xmlNodeGetSpacePreserve(xnode)));
1233
+ }
1234
+
1235
+ /*
1236
+ * call-seq:
1237
+ * node.space_preserve = true|false
1238
+ *
1239
+ * Control whether this node preserves whitespace.
1240
+ */
1241
+ static VALUE rxml_node_space_preserve_set(VALUE self, VALUE value)
1242
+ {
1243
+ xmlNodePtr xnode;
1244
+ xnode = rxml_get_xnode(self);
1245
+
1246
+ if (value == Qfalse)
1247
+ xmlNodeSetSpacePreserve(xnode, 0);
1248
+ else
1249
+ xmlNodeSetSpacePreserve(xnode, 1);
1250
+
1251
+ return (Qnil);
1252
+ }
1253
+
1254
+ /*
1255
+ * call-seq:
1256
+ * node.type -> num
1257
+ *
1258
+ * Obtain this node's type identifier.
1259
+ */
1260
+ static VALUE rxml_node_type(VALUE self)
1261
+ {
1262
+ xmlNodePtr xnode;
1263
+ xnode = rxml_get_xnode(self);
1264
+ return (INT2NUM(xnode->type));
1265
+ }
1266
+
1267
+ /*
1268
+ * call-seq:
1269
+ * node.copy -> XML::Node
1270
+ *
1271
+ * Creates a copy of this node. To create a
1272
+ * shallow copy set the deep parameter to false.
1273
+ * To create a deep copy set the deep parameter
1274
+ * to true.
1275
+ *
1276
+ */
1277
+ static VALUE rxml_node_copy(VALUE self, VALUE deep)
1278
+ {
1279
+ xmlNodePtr xnode;
1280
+ xmlNodePtr xcopy;
1281
+ int recursive = (deep == Qnil || deep == Qfalse) ? 0 : 1;
1282
+ xnode = rxml_get_xnode(self);
1283
+
1284
+ xcopy = xmlCopyNode(xnode, recursive);
1285
+
1286
+ if (xcopy)
1287
+ return rxml_node_wrap(xcopy);
1288
+ else
1289
+ return Qnil;
1290
+ }
1291
+
1292
+ void rxml_init_node(void)
1293
+ {
1294
+ cXMLNode = rb_define_class_under(mXML, "Node", rb_cObject);
1295
+
1296
+ rb_define_const(cXMLNode, "SPACE_DEFAULT", INT2NUM(0));
1297
+ rb_define_const(cXMLNode, "SPACE_PRESERVE", INT2NUM(1));
1298
+ rb_define_const(cXMLNode, "SPACE_NOT_INHERIT", INT2NUM(-1));
1299
+ rb_define_const(cXMLNode, "XLINK_ACTUATE_AUTO", INT2NUM(1));
1300
+ rb_define_const(cXMLNode, "XLINK_ACTUATE_NONE", INT2NUM(0));
1301
+ rb_define_const(cXMLNode, "XLINK_ACTUATE_ONREQUEST", INT2NUM(2));
1302
+ rb_define_const(cXMLNode, "XLINK_SHOW_EMBED", INT2NUM(2));
1303
+ rb_define_const(cXMLNode, "XLINK_SHOW_NEW", INT2NUM(1));
1304
+ rb_define_const(cXMLNode, "XLINK_SHOW_NONE", INT2NUM(0));
1305
+ rb_define_const(cXMLNode, "XLINK_SHOW_REPLACE", INT2NUM(3));
1306
+ rb_define_const(cXMLNode, "XLINK_TYPE_EXTENDED", INT2NUM(2));
1307
+ rb_define_const(cXMLNode, "XLINK_TYPE_EXTENDED_SET", INT2NUM(3));
1308
+ rb_define_const(cXMLNode, "XLINK_TYPE_NONE", INT2NUM(0));
1309
+ rb_define_const(cXMLNode, "XLINK_TYPE_SIMPLE", INT2NUM(1));
1310
+
1311
+ rb_define_const(cXMLNode, "ELEMENT_NODE", INT2FIX(XML_ELEMENT_NODE));
1312
+ rb_define_const(cXMLNode, "ATTRIBUTE_NODE", INT2FIX(XML_ATTRIBUTE_NODE));
1313
+ rb_define_const(cXMLNode, "TEXT_NODE", INT2FIX(XML_TEXT_NODE));
1314
+ rb_define_const(cXMLNode, "CDATA_SECTION_NODE", INT2FIX(XML_CDATA_SECTION_NODE));
1315
+ rb_define_const(cXMLNode, "ENTITY_REF_NODE", INT2FIX(XML_ENTITY_REF_NODE));
1316
+ rb_define_const(cXMLNode, "ENTITY_NODE", INT2FIX(XML_ENTITY_NODE));
1317
+ rb_define_const(cXMLNode, "PI_NODE", INT2FIX(XML_PI_NODE));
1318
+ rb_define_const(cXMLNode, "COMMENT_NODE", INT2FIX(XML_COMMENT_NODE));
1319
+ rb_define_const(cXMLNode, "DOCUMENT_NODE", INT2FIX(XML_DOCUMENT_NODE));
1320
+ rb_define_const(cXMLNode, "DOCUMENT_TYPE_NODE", INT2FIX(XML_DOCUMENT_TYPE_NODE));
1321
+ rb_define_const(cXMLNode, "DOCUMENT_FRAG_NODE", INT2FIX(XML_DOCUMENT_FRAG_NODE));
1322
+ rb_define_const(cXMLNode, "NOTATION_NODE", INT2FIX(XML_NOTATION_NODE));
1323
+ rb_define_const(cXMLNode, "HTML_DOCUMENT_NODE", INT2FIX(XML_HTML_DOCUMENT_NODE));
1324
+ rb_define_const(cXMLNode, "DTD_NODE", INT2FIX(XML_DTD_NODE));
1325
+ rb_define_const(cXMLNode, "ELEMENT_DECL", INT2FIX(XML_ELEMENT_DECL));
1326
+ rb_define_const(cXMLNode, "ATTRIBUTE_DECL", INT2FIX(XML_ATTRIBUTE_DECL));
1327
+ rb_define_const(cXMLNode, "ENTITY_DECL", INT2FIX(XML_ENTITY_DECL));
1328
+ rb_define_const(cXMLNode, "NAMESPACE_DECL", INT2FIX(XML_NAMESPACE_DECL));
1329
+ rb_define_const(cXMLNode, "XINCLUDE_START", INT2FIX(XML_XINCLUDE_START));
1330
+ rb_define_const(cXMLNode, "XINCLUDE_END", INT2FIX(XML_XINCLUDE_END));
1331
+
1332
+ #ifdef LIBXML_DOCB_ENABLED
1333
+ rb_define_const(cXMLNode, "DOCB_DOCUMENT_NODE", INT2FIX(XML_DOCB_DOCUMENT_NODE));
1334
+ #else
1335
+ rb_define_const(cXMLNode, "DOCB_DOCUMENT_NODE", Qnil);
1336
+ #endif
1337
+
1338
+ rb_define_singleton_method(cXMLNode, "new_cdata", rxml_node_new_cdata, -1);
1339
+ rb_define_singleton_method(cXMLNode, "new_comment", rxml_node_new_comment, -1);
1340
+ rb_define_singleton_method(cXMLNode, "new_pi", rxml_node_new_pi, -1);
1341
+ rb_define_singleton_method(cXMLNode, "new_text", rxml_node_new_text, 1);
1342
+
1343
+ /* Initialization */
1344
+ rb_define_alloc_func(cXMLNode, rxml_node_alloc);
1345
+ rb_define_method(cXMLNode, "initialize", rxml_node_initialize, -1);
1346
+
1347
+ /* Traversal */
1348
+ rb_include_module(cXMLNode, rb_mEnumerable);
1349
+ rb_define_method(cXMLNode, "[]", rxml_node_attribute_get, 1);
1350
+ rb_define_method(cXMLNode, "each", rxml_node_each, 0);
1351
+ rb_define_method(cXMLNode, "first", rxml_node_first_get, 0);
1352
+ rb_define_method(cXMLNode, "last", rxml_node_last_get, 0);
1353
+ rb_define_method(cXMLNode, "next", rxml_node_next_get, 0);
1354
+ rb_define_method(cXMLNode, "parent", rxml_node_parent_get, 0);
1355
+ rb_define_method(cXMLNode, "prev", rxml_node_prev_get, 0);
1356
+
1357
+ /* Modification */
1358
+ rb_define_method(cXMLNode, "[]=", rxml_node_property_set, 2);
1359
+ rb_define_method(cXMLNode, "<<", rxml_node_content_add, 1);
1360
+ rb_define_method(cXMLNode, "sibling=", rxml_node_sibling_set, 1);
1361
+ rb_define_method(cXMLNode, "next=", rxml_node_next_set, 1);
1362
+ rb_define_method(cXMLNode, "prev=", rxml_node_prev_set, 1);
1363
+
1364
+ /* Rest of the node api */
1365
+ rb_define_method(cXMLNode, "attributes", rxml_node_attributes_get, 0);
1366
+ rb_define_method(cXMLNode, "base_uri", rxml_node_base_uri_get, 0);
1367
+ rb_define_method(cXMLNode, "base_uri=", rxml_node_base_uri_set, 1);
1368
+ rb_define_method(cXMLNode, "blank?", rxml_node_empty_q, 0);
1369
+ rb_define_method(cXMLNode, "copy", rxml_node_copy, 1);
1370
+ rb_define_method(cXMLNode, "content", rxml_node_content_get, 0);
1371
+ rb_define_method(cXMLNode, "content=", rxml_node_content_set, 1);
1372
+ rb_define_method(cXMLNode, "debug", rxml_node_debug, 0);
1373
+ rb_define_method(cXMLNode, "doc", rxml_node_doc, 0);
1374
+ rb_define_method(cXMLNode, "empty?", rxml_node_empty_q, 0);
1375
+ rb_define_method(cXMLNode, "eql?", rxml_node_eql_q, 1);
1376
+ rb_define_method(cXMLNode, "lang", rxml_node_lang_get, 0);
1377
+ rb_define_method(cXMLNode, "lang=", rxml_node_lang_set, 1);
1378
+ rb_define_method(cXMLNode, "line_num", rxml_node_line_num, 0);
1379
+ rb_define_method(cXMLNode, "name", rxml_node_name_get, 0);
1380
+ rb_define_method(cXMLNode, "name=", rxml_node_name_set, 1);
1381
+ rb_define_method(cXMLNode, "node_type", rxml_node_type, 0);
1382
+ rb_define_method(cXMLNode, "output_escaping?", rxml_node_output_escaping_q, 0);
1383
+ rb_define_method(cXMLNode, "output_escaping=", rxml_node_output_escaping_set, 1);
1384
+ rb_define_method(cXMLNode, "path", rxml_node_path, 0);
1385
+ rb_define_method(cXMLNode, "remove!", rxml_node_remove_ex, 0);
1386
+ rb_define_method(cXMLNode, "space_preserve", rxml_node_space_preserve_get, 0);
1387
+ rb_define_method(cXMLNode, "space_preserve=", rxml_node_space_preserve_set, 1);
1388
+ rb_define_method(cXMLNode, "to_s", rxml_node_to_s, -1);
1389
+ rb_define_method(cXMLNode, "xlink?", rxml_node_xlink_q, 0);
1390
+ rb_define_method(cXMLNode, "xlink_type", rxml_node_xlink_type, 0);
1391
+ rb_define_method(cXMLNode, "xlink_type_name", rxml_node_xlink_type_name, 0);
1392
+
1393
+ rb_define_alias(cXMLNode, "==", "eql?");
1394
+ }