Class: Teius::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/teius.rb,
ext/teius.c

Direct Known Subclasses

Document

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



28
29
30
31
# File 'lib/teius.rb', line 28

def ==(other)
  return false unless other.kind_of? Node
  self.pointer == other.pointer
end

#[](key) ⇒ Object



24
25
26
# File 'lib/teius.rb', line 24

def [](key)
  attributes[key]
end

#attributesObject

Returns a ruby hash of node’s elements



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/teius.c', line 55

static VALUE node_attributes(VALUE self) {
  xmlNodePtr node = NULL;
  Data_Get_Struct(self, xmlNode, node);

  xmlAttrPtr attrs = node->properties;
  VALUE rHash = rb_hash_new();
  while (attrs != NULL) {
    VALUE rName = rb_str_new2((char *)attrs->name);
    xmlChar *val = xmlNodeGetContent(attrs->children);    
    VALUE rVal = rb_str_new2((char *)val);
    rb_hash_aset(rHash, rName, rVal);    
    xmlFree(val);
    attrs = attrs->next;
  }

  return rHash;
}

#childrenObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'ext/teius.c', line 218

static VALUE node_children(VALUE self) {
  xmlNodePtr node = NULL;
  xmlNodePtr cur_child = NULL;
  
  Data_Get_Struct(self, xmlNode, node);
  
  VALUE rArr = rb_ary_new();
  for (cur_child = node->children;	cur_child != NULL; cur_child = cur_child->next) {
    VALUE rNode = Data_Wrap_Struct(cNode, 0, 0, cur_child);
    rb_ary_push(rArr, rNode);
  }
  
  return rArr;
}

#contentObject

Returns the recursive content of the node in string format



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'ext/teius.c', line 202

static VALUE node_content(VALUE self) {
  xmlNodePtr node = NULL;
  const xmlChar *mem;
  xmlBufferPtr buf;
  int size;
  
  Data_Get_Struct(self, xmlNode, node);
  buf = xmlBufferCreate();
  size = xmlNodeDump(buf, node->doc, node, 0, 1);
  mem  = xmlBufferContent(buf);
  VALUE rStr = rb_str_new2((char *)mem);
  xmlBufferFree(buf);
  
  return rStr;
}

#documentObject



44
45
46
47
48
49
50
51
52
# File 'ext/teius.c', line 44

static VALUE node_document(VALUE self) {
  xmlNodePtr node = NULL;
  Data_Get_Struct(self, xmlNode, node);

  xmlDocPtr doc = node->doc;

  /* Don't GC this one -- it's just a reference to the original */
  return Data_Wrap_Struct(cDocument, 0, 0, doc);
}

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/teius.rb', line 33

def eql?(other)
  return false unless other.kind_of? Node
  self.pointer == other.pointer
end

#fetch(xpath) ⇒ Object



19
20
21
22
# File 'lib/teius.rb', line 19

def fetch(xpath)
  node = find(:unique, xpath, :required => false)
  return node ? node.value : nil
end

#find(*args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/teius.c', line 73

static VALUE node_find(int argc, VALUE *argv, VALUE self) {
  VALUE rType, rXpath, rOptions;
  rb_scan_args(argc, argv, "21", &rType, &rXpath, &rOptions);
  int required = 0;
  if (argc == 3) {
    VALUE rRequired = rb_hash_aref(rOptions, ID2SYM(sRequired));
    required = rRequired != Qnil && rRequired != Qfalse;
  }

  xmlNodePtr node = NULL;
  Data_Get_Struct(self, xmlNode, node);
  xmlDocPtr doc = node->doc;

  xmlXPathContextPtr context;
  xmlXPathObjectPtr result;

  context = xmlXPathNewContext(doc);
  context->node = node;

  char *xpath = StringValuePtr(rXpath);
  result  = xmlXPathEvalExpression((xmlChar *)xpath, context);
  if (result == NULL) {
    xmlErrorPtr  err = xmlGetLastError();
    xmlXPathFreeContext(context);
    rb_raise(cXpathError, "Couldn't evaluate xpath [%s]: %s",
      xpath, err->message);
  }

  xmlNodeSetPtr node_set = result->nodesetval;
  int size = node_set != NULL ? node_set->nodeNr : 0;

  ID type = SYM2ID(rType);
  if (size == 0 && required) {
    xmlXPathFreeObject(result);
    xmlXPathFreeContext(context);
    rb_raise(cNodeNotFound, "No such node in %s: %s", node->name, xpath);
  } else if (size > 1 && type == sUnique) {
    xmlXPathFreeObject(result);
    xmlXPathFreeContext(context);
    rb_raise(cNodeNotUnique, "node at path %s from %s is not unique", 
      xpath, node->name);
  }

  VALUE rResult;
  if (type == sFirst || type == sUnique) {      /* Just return first node */
    if (size == 0) {
      rResult = Qnil;
    } else {
      rResult = Data_Wrap_Struct(cNode, 0, 0, node_set->nodeTab[0]);      
    }
  } else if (type == sAll) { /* Return ruby array of all nodes */
    int i;
    VALUE arr = rb_ary_new2(size);
    for (i=0; i < size; i++) {
      xmlNodePtr cur_node = node_set->nodeTab[i];

      /* Create Node and store it in arr */
      VALUE rNode = Data_Wrap_Struct(cNode, 0, 0, cur_node);
      rb_ary_store(arr, i, rNode);
    }
    rResult = arr;    
  } else {
    xmlXPathFreeObject(result);
    xmlXPathFreeContext(context);
    rb_raise(rb_eArgError, "Unknown type: :%s (should be :first, :unique or :all)", 
      rb_id2name(type));
  }

  xmlXPathFreeObject(result);
  xmlXPathFreeContext(context);

  return rResult;
}

#hashObject



38
39
40
# File 'lib/teius.rb', line 38

def hash
  self.pointer
end

#lineObject

Returns the line number the node starts on



195
196
197
198
199
# File 'ext/teius.c', line 195

static VALUE node_line(VALUE self) {
  xmlNodePtr node = NULL;
  Data_Get_Struct(self, xmlNode, node);
  return INT2NUM(node->line);
}

#nameObject



147
148
149
150
151
# File 'ext/teius.c', line 147

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

#parentObject



42
43
44
# File 'lib/teius.rb', line 42

def parent
  self.find :first, '..'
end

#pointerObject



188
189
190
191
192
# File 'ext/teius.c', line 188

static VALUE node_pointer(VALUE self) {
  xmlNodePtr node = NULL;
  Data_Get_Struct(self, xmlNode, node);
  return INT2NUM((int)node);
}

#siblingsObject



46
47
48
# File 'lib/teius.rb', line 46

def siblings
  self.find :all, 'preceding-sibling::*|following-sibling::*'
end

#to_sObject



50
51
52
# File 'lib/teius.rb', line 50

def to_s
  "#{name}:#{line}"
end

#typeObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'ext/teius.c', line 159

static VALUE node_type(VALUE self) {
  xmlNodePtr node = NULL;
  Data_Get_Struct(self, xmlNode, node);
    
  switch (node->type) {
  case 1:  return ID2SYM(rb_intern("element"));  
  case 2:  return ID2SYM(rb_intern("attribute"));  
  case 3:  return ID2SYM(rb_intern("text"));  
  case 4:  return ID2SYM(rb_intern("cdata_section"));  
  case 5:  return ID2SYM(rb_intern("entity_ref"));  
  case 6:  return ID2SYM(rb_intern("entity"));  
  case 7:  return ID2SYM(rb_intern("pi"));  
  case 8:  return ID2SYM(rb_intern("comment"));  
  case 9:  return ID2SYM(rb_intern("document"));  
  case 10: return ID2SYM(rb_intern("document_type"));  
  case 11: return ID2SYM(rb_intern("document_fragment"));  
  case 12: return ID2SYM(rb_intern("notation"));  
  case 13: return ID2SYM(rb_intern("html_document"));  
  case 14: return ID2SYM(rb_intern("dtd"));  
  case 15: return ID2SYM(rb_intern("element_declaration"));  
  case 16: return ID2SYM(rb_intern("attribute_declaration"));  
  case 17: return ID2SYM(rb_intern("entity_declaration"));  
  case 18: return ID2SYM(rb_intern("namespace_declaration"));  
  case 19: return ID2SYM(rb_intern("xinclude_start"));  
  case 20: return ID2SYM(rb_intern("xinclude_end"));  
  case 21: return ID2SYM(rb_intern("docb_document"));  
  }
}

#valueObject



33
34
35
36
37
38
39
40
41
42
# File 'ext/teius.c', line 33

static VALUE node_value(VALUE self) {
  xmlNodePtr node = NULL;
  Data_Get_Struct(self, xmlNode, node);

  xmlChar *val = xmlNodeGetContent(node);  
  VALUE rVal = rb_str_new2((char *)val);
  xmlFree(val);

  return rVal;
}

#xpathObject



153
154
155
156
157
# File 'ext/teius.c', line 153

static VALUE node_xpath(VALUE self) {
  xmlNodePtr node = NULL;
  Data_Get_Struct(self, xmlNode, node);
  return rb_str_new2((char *)xmlGetNodePath(node));
}