Class: Nokogiri::XML::DTD

Inherits:
NokogiriXmlNode
  • Object
show all
Defined in:
lib/nokogiri/xml/dtd.rb,
ext/nokogiri/xml_dtd.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(name) ⇒ Object

Create a new node with name



475
476
477
478
479
480
481
482
483
484
485
# File 'ext/nokogiri/xml_node.c', line 475

static VALUE new(VALUE klass, VALUE name)
{
  xmlNodePtr node = xmlNewNode(NULL, (xmlChar *)StringValuePtr(name));
  VALUE rb_node = Nokogiri_wrap_xml_node(node) ;

  if(rb_block_given_p()) rb_yield(rb_node);

  Nokogiri_xml_node_owned_set(node);

  return rb_node;
}

.new_from_str(string) ⇒ Object

Create a new node by parsing string



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'ext/nokogiri/xml_node.c', line 494

static VALUE new_from_str(VALUE klass, VALUE xml)
{
    /*
     *  I couldn't find a more efficient way to do this. So we create a new
     *  document and copy (recursively) the root node.
     */
    VALUE rb_doc ;
    xmlDocPtr doc ;
    xmlNodePtr node ;

    rb_doc = rb_funcall(cNokogiriXmlDocument, rb_intern("read_memory"), 4,
                        xml, Qnil, Qnil, INT2NUM(0));
    Data_Get_Struct(rb_doc, xmlDoc, doc);
    node = xmlCopyNode(xmlDocGetRootElement(doc), 1); /* 1 => recursive */
    return Nokogiri_wrap_xml_node(node);
}

Instance Method Details

#[]=(property, value) ⇒ Object

Set the property to value



183
184
185
186
187
188
189
190
191
# File 'ext/nokogiri/xml_node.c', line 183

static VALUE set(VALUE self, VALUE property, VALUE value)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  xmlSetProp(node, (xmlChar *)StringValuePtr(property),
      (xmlChar *)StringValuePtr(value));

  return value;
}

#add_next_sibling(node) ⇒ Object

Insert node after this node (as a sibling).



415
416
417
418
419
420
421
422
423
424
425
426
# File 'ext/nokogiri/xml_node.c', line 415

static VALUE add_next_sibling(VALUE self, VALUE rb_node)
{
  xmlNodePtr node, new_sibling;
  Data_Get_Struct(self, xmlNode, node);
  Data_Get_Struct(rb_node, xmlNode, new_sibling);
  xmlAddNextSibling(node, new_sibling);

  rb_funcall(rb_node, rb_intern("decorate!"), 0);
  Nokogiri_xml_node_owned_set(new_sibling);

  return rb_node;
}

#add_previous_sibling(node) ⇒ Object

Insert node before this node (as a sibling).



434
435
436
437
438
439
440
441
442
443
444
445
# File 'ext/nokogiri/xml_node.c', line 434

static VALUE add_previous_sibling(VALUE self, VALUE rb_node)
{
  xmlNodePtr node, new_sibling;
  Data_Get_Struct(self, xmlNode, node);
  Data_Get_Struct(rb_node, xmlNode, new_sibling);
  xmlAddPrevSibling(node, new_sibling);

  rb_funcall(rb_node, rb_intern("decorate!"), 0);
  Nokogiri_xml_node_owned_set(new_sibling);

  return rb_node;
}

#attributesObject

returns a hash containing the node’s attributes.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'ext/nokogiri/xml_dtd.c', line 53

static VALUE attributes(VALUE self)
{
  xmlDtdPtr dtd;
  Data_Get_Struct(self, xmlDtd, dtd);

  if(!dtd->attributes) return Qnil;

  VALUE hash = rb_hash_new();

  xmlHashScan((xmlHashTablePtr)dtd->attributes, element_copier, (void *)hash);

  return hash;
}

#blank?Boolean

Is this node blank?



83
84
85
86
87
88
89
90
# File 'ext/nokogiri/xml_node.c', line 83

static VALUE blank_eh(VALUE self)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  if(1 == xmlIsBlankNode(node))
    return Qtrue;
  return Qfalse;
}

#childObject

Returns the child node



151
152
153
154
155
156
157
158
159
160
# File 'ext/nokogiri/xml_node.c', line 151

static VALUE child(VALUE self)
{
  xmlNodePtr node, child;
  Data_Get_Struct(self, xmlNode, node);

  child = node->children;
  if(!child) return Qnil;

  return Nokogiri_wrap_xml_node(child);
}

#contentObject

Returns the content for this Node



300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'ext/nokogiri/xml_node.c', line 300

static VALUE get_content(VALUE self)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);

  xmlChar * content = xmlNodeGetContent(node);
  if(content) {
    VALUE rval = rb_str_new2((char *)content);
    xmlFree(content);
    return rval;
  }
  return Qnil;
}

#documentObject

Returns the Nokogiri::XML::Document associated with this Node



400
401
402
403
404
405
406
407
# File 'ext/nokogiri/xml_node.c', line 400

static VALUE document(VALUE self)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);

  if(!node->doc) return Qnil;
  return (VALUE)node->doc->_private;
}

#dupObject

Copy this node



51
52
53
54
55
56
57
58
59
60
# File 'ext/nokogiri/xml_node.c', line 51

static VALUE duplicate_node(VALUE self)
{
  xmlNodePtr node, dup;
  Data_Get_Struct(self, xmlNode, node);

  dup = xmlCopyNode(node, 1);
  if(dup == NULL) return Qnil;

  return Nokogiri_wrap_xml_node(dup);
}

#elementsObject

Get a hash of the elements for this DTD.



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'ext/nokogiri/xml_dtd.c', line 93

static VALUE elements(VALUE self)
{
  xmlDtdPtr dtd;
  Data_Get_Struct(self, xmlDtd, dtd);

  if(!dtd->elements) return Qnil;

  VALUE hash = rb_hash_new();

  xmlHashScan((xmlHashTablePtr)dtd->elements, element_copier, (void *)hash);

  return hash;
}

#encode_special_chars(string) ⇒ Object

Encode any special characters in string



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'ext/nokogiri/xml_node.c', line 9

static VALUE encode_special_chars(VALUE self, VALUE string)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  xmlChar * encoded = xmlEncodeSpecialChars(
      node->doc,
      (const xmlChar *)StringValuePtr(string)
  );

  VALUE encoded_str = rb_str_new2((const char *)encoded);
  free(encoded);

  return encoded_str;
}

#entitiesObject

Get a hash of the elements for this DTD.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'ext/nokogiri/xml_dtd.c', line 33

static VALUE entities(VALUE self)
{
  xmlDtdPtr dtd;
  Data_Get_Struct(self, xmlDtd, dtd);

  if(!dtd->entities) return Qnil;

  VALUE hash = rb_hash_new();

  xmlHashScan((xmlHashTablePtr)dtd->entities, element_copier, (void *)hash);

  return hash;
}

#internal_subsetObject

Get the internal subset



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'ext/nokogiri/xml_node.c', line 30

static VALUE internal_subset(VALUE self)
{
  xmlNodePtr node;
  xmlDocPtr doc;
  Data_Get_Struct(self, xmlNode, node);

  if(!node->doc) return Qnil;

  doc = node->doc;

  if(!doc->intSubset) return Qnil;

  return Nokogiri_wrap_xml_node((xmlNodePtr)doc->intSubset);
}

#key?(attribute) ⇒ Boolean

Returns true if attribute is set



168
169
170
171
172
173
174
175
# File 'ext/nokogiri/xml_node.c', line 168

static VALUE key_eh(VALUE self, VALUE attribute)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  if(xmlHasProp(node, (xmlChar *)StringValuePtr(attribute)))
    return Qtrue;
  return Qfalse;
}

#nameObject

Returns the name for this Node



368
369
370
371
372
373
# File 'ext/nokogiri/xml_node.c', line 368

static VALUE get_name(VALUE self)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  return rb_str_new2((const char *)node->name);
}

#name=(new_name) ⇒ Object

Set the name for this Node



354
355
356
357
358
359
360
# File 'ext/nokogiri/xml_node.c', line 354

static VALUE set_name(VALUE self, VALUE new_name)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  xmlNodeSetName(node, (xmlChar*)StringValuePtr(new_name));
  return new_name;
}

#namespacesObject

returns a hash containing the node’s namespaces.



253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'ext/nokogiri/xml_node.c', line 253

static VALUE namespaces(VALUE self)
{
    /* this code in the mode of xmlHasProp() */
    xmlNodePtr node ;
    VALUE attr ;

    attr = rb_hash_new() ;
    Data_Get_Struct(self, xmlNode, node);

    Nokogiri_xml_node_namespaces(node, attr);

    return attr ;
}

#next_siblingObject

Returns the next sibling node



98
99
100
101
102
103
104
105
106
107
# File 'ext/nokogiri/xml_node.c', line 98

static VALUE next_sibling(VALUE self)
{
  xmlNodePtr node, sibling;
  Data_Get_Struct(self, xmlNode, node);

  sibling = node->next;
  if(!sibling) return Qnil;

  return Nokogiri_wrap_xml_node(sibling) ;
}

#notationsObject

Get a hash of the notations for this DTD.



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'ext/nokogiri/xml_dtd.c', line 73

static VALUE notations(VALUE self)
{
  xmlDtdPtr dtd;
  Data_Get_Struct(self, xmlDtd, dtd);

  if(!dtd->notations) return Qnil;

  VALUE hash = rb_hash_new();

  xmlHashScan((xmlHashTablePtr)dtd->notations, notation_copier, (void *)hash);

  return hash;
}

#parentObject

Get the parent Node for this Node



337
338
339
340
341
342
343
344
345
346
# File 'ext/nokogiri/xml_node.c', line 337

static VALUE get_parent(VALUE self)
{
  xmlNodePtr node, parent;
  Data_Get_Struct(self, xmlNode, node);

  parent = node->parent;
  if(!parent) return Qnil;

  return Nokogiri_wrap_xml_node(parent) ;
}

#parent=(parent_node) ⇒ Object

Set the parent Node for this Node



320
321
322
323
324
325
326
327
328
329
# File 'ext/nokogiri/xml_node.c', line 320

static VALUE set_parent(VALUE self, VALUE parent_node)
{
  xmlNodePtr node, parent;
  Data_Get_Struct(self, xmlNode, node);
  Data_Get_Struct(parent_node, xmlNode, parent);

  xmlAddChild(parent, node);
  Nokogiri_xml_node_owned_set(node);
  return parent_node;
}

#pathObject

Returns the path associated with this Node



381
382
383
384
385
386
387
388
389
390
391
392
# File 'ext/nokogiri/xml_node.c', line 381

static VALUE path(VALUE self)
{
  xmlNodePtr node;
  xmlChar *path ;
  VALUE rval ;
  Data_Get_Struct(self, xmlNode, node);
  
  path = xmlGetNodePath(node);
  rval = rb_str_new2((char *)path);
  xmlFree(path);
  return rval ;
}

#previous_siblingObject

Returns the previous sibling node



115
116
117
118
119
120
121
122
123
124
# File 'ext/nokogiri/xml_node.c', line 115

static VALUE previous_sibling(VALUE self)
{
  xmlNodePtr node, sibling;
  Data_Get_Struct(self, xmlNode, node);

  sibling = node->prev;
  if(!sibling) return Qnil;

  return Nokogiri_wrap_xml_node(sibling);
}

#remove_attribute(property) ⇒ Object

remove the property property



199
200
201
202
203
204
205
206
207
# File 'ext/nokogiri/xml_node.c', line 199

static VALUE remove_prop(VALUE self, VALUE property)
{
  xmlNodePtr node;
  xmlAttrPtr attr ;
  Data_Get_Struct(self, xmlNode, node);
  attr = xmlHasProp(node, (xmlChar *)StringValuePtr(property));
  if (attr) { xmlRemoveProp(attr); }
  return Qnil;
}

#replace(new_node) ⇒ Object

replace node with the new node in the document.



132
133
134
135
136
137
138
139
140
141
142
# File 'ext/nokogiri/xml_node.c', line 132

static VALUE replace(VALUE self, VALUE _new_node)
{
  xmlNodePtr node, new_node;
  Data_Get_Struct(self, xmlNode, node);
  Data_Get_Struct(_new_node, xmlNode, new_node);

  xmlReplaceNode(node, new_node);
  Nokogiri_xml_node_owned_set(node);
  Nokogiri_xml_node_owned_set(new_node);
  return self ;
}

#to_xmlObject

Returns this node as XML



453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'ext/nokogiri/xml_node.c', line 453

static VALUE to_xml(VALUE self)
{
  xmlBufferPtr buf ;
  xmlNodePtr node ;
  VALUE xml ;

  Data_Get_Struct(self, xmlNode, node);

  buf = xmlBufferCreate() ;
  xmlNodeDump(buf, node->doc, node, 2, 1);
  xml = rb_str_new2((char*)buf->content);
  xmlBufferFree(buf);
  return xml ;
}

#typeObject

Get the type for this node



273
274
275
276
277
278
# File 'ext/nokogiri/xml_node.c', line 273

static VALUE type(VALUE self)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  return INT2NUM((int)node->type);
}

Unlink this node from its current context.



68
69
70
71
72
73
74
75
# File 'ext/nokogiri/xml_node.c', line 68

static VALUE unlink_node(VALUE self)
{
  xmlNodePtr node;
  Data_Get_Struct(self, xmlNode, node);
  xmlUnlinkNode(node);
  Nokogiri_xml_node_owned_set(node);
  return self;
}