teius 0.2-mswin32 → 0.5-mswin32

Sign up to get free protection for your applications and to get access to all the features.
data/ext/teius.c CHANGED
@@ -162,6 +162,30 @@ static VALUE node_pointer(VALUE self) {
162
162
  return INT2NUM((int)node);
163
163
  }
164
164
 
165
+ /* Returns the line number the node starts on */
166
+ static VALUE node_line(VALUE self) {
167
+ xmlNodePtr node = NULL;
168
+ Data_Get_Struct(self, xmlNode, node);
169
+ return INT2NUM(node->line);
170
+ }
171
+
172
+ /* Returns the recursive content of the node in string format */
173
+ static VALUE node_content(VALUE self) {
174
+ xmlNodePtr node = NULL;
175
+ const xmlChar *mem;
176
+ xmlBufferPtr buf;
177
+ int size;
178
+
179
+ Data_Get_Struct(self, xmlNode, node);
180
+ buf = xmlBufferCreate();
181
+ size = xmlNodeDump(buf, node->doc, node, 0, 1);
182
+ mem = xmlBufferContent(buf);
183
+ VALUE rStr = rb_str_new2(mem);
184
+ xmlBufferFree(buf);
185
+
186
+ return rStr;
187
+ }
188
+
165
189
  static VALUE document_parse_file(VALUE self, VALUE rFilename) {
166
190
  xmlDocPtr doc = xmlReadFile(StringValuePtr(rFilename), NULL, 0);
167
191
  if (doc == NULL) {
@@ -189,7 +213,7 @@ static VALUE document_parse_string(VALUE self, VALUE rDocString) {
189
213
  return rDoc;
190
214
  }
191
215
 
192
- static VALUE document_to_s(VALUE self, VALUE rDocString) {
216
+ static VALUE document_to_s(VALUE self) {
193
217
  xmlDocPtr doc = NULL;
194
218
  xmlChar *mem;
195
219
  int size;
@@ -202,6 +226,18 @@ static VALUE document_to_s(VALUE self, VALUE rDocString) {
202
226
  return rStr;
203
227
  }
204
228
 
229
+ static VALUE document_root(VALUE self) {
230
+ xmlDocPtr doc = NULL;
231
+ xmlNodePtr node = NULL;
232
+ VALUE rResult;
233
+
234
+ Data_Get_Struct(self, xmlDoc, doc);
235
+ node = xmlDocGetRootElement(doc);
236
+ rResult = Data_Wrap_Struct(cNode, 0, 0, node);
237
+
238
+ return rResult;
239
+ }
240
+
205
241
  /* LibXslt Stuff */
206
242
  static void stylesheet_free(void *p) {
207
243
  xsltFreeStylesheet(p);
@@ -306,12 +342,15 @@ void Init_teius() {
306
342
  rb_define_method(cNode, "name", node_name, 0);
307
343
  rb_define_method(cNode, "xpath", node_xpath, 0);
308
344
  rb_define_method(cNode, "pointer", node_pointer, 0);
345
+ rb_define_method(cNode, "line", node_line, 0);
346
+ rb_define_method(cNode, "content", node_content, 0);
309
347
 
310
348
  /* Document */
311
349
  cDocument = rb_define_class_under(mTeius, "Document", cNode);
312
350
  rb_define_singleton_method(cDocument, "parse_file", document_parse_file, 1);
313
351
  rb_define_singleton_method(cDocument, "parse_string", document_parse_string, 1);
314
352
  rb_define_method(cDocument, "to_s", document_to_s, 0);
353
+ rb_define_method(cDocument, "root", document_root, 0);
315
354
 
316
355
  /* XSL Stylesheet */
317
356
  cStylesheet = rb_define_class_under(mTeius, "Stylesheet", cNode);
data/ext/teius.o CHANGED
Binary file
data/ext/teius.so ADDED
Binary file
data/lib/teius.rb CHANGED
@@ -1,18 +1,34 @@
1
- #require File.join(File.dirname(__FILE__), '..', 'ext', 'teius.so')
2
- require 'teius.so'
1
+ require File.join(File.dirname(__FILE__), '..', 'ext', 'teius.so')
3
2
 
4
3
  module Teius
4
+ class Document
5
+
6
+ def self.content(xml)
7
+ parse_string(xml).content
8
+ end
9
+
10
+ def content
11
+ root.content
12
+ end
13
+ end
14
+
5
15
  class Node
6
16
  def fetch(xpath)
7
17
  node = find(:unique, xpath, :required => false)
8
18
  return node ? node.value : nil
9
19
  end
10
20
 
21
+ def [](key)
22
+ attributes[key]
23
+ end
24
+
11
25
  def ==(other)
26
+ return false unless other.kind_of? Node
12
27
  self.pointer == other.pointer
13
28
  end
14
29
 
15
30
  def eql?(other)
31
+ return false unless other.kind_of? Node
16
32
  self.pointer == other.pointer
17
33
  end
18
34
 
@@ -32,6 +48,10 @@ module Teius
32
48
  self.find :all, 'child::*'
33
49
  end
34
50
 
51
+ def to_s
52
+ "#{name}:#{line}"
53
+ end
54
+
35
55
  end
36
56
 
37
57
  class Stylesheet
data/test/teius_test.rb CHANGED
@@ -172,4 +172,25 @@ class TeiusTest < Test::Unit::TestCase
172
172
  }
173
173
  end
174
174
 
175
+ def test_root
176
+ root = @doc.root
177
+ assert_equal 'sports-content:2', root.to_s
178
+ end
179
+
180
+ def test_node_content
181
+ node = @doc.find :first, '//team-metadata'
182
+ expected = [ '<team-metadata team-key="l.nfl.com-t.24" alignment="away">',
183
+ ' <name first="Green Bay" last="Packers"/>', '</team-metadata>' ].join "\n"
184
+ assert_equal expected, node.content
185
+ end
186
+
187
+ def test_doc_content
188
+ assert_match /^<sports-content/, @doc.content
189
+ end
190
+
191
+ def test_doc_class_content
192
+ content = Document.content '<library><book/></library>'
193
+ assert_equal "<library>\n <book/>\n</library>", content
194
+ end
195
+
175
196
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: teius
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.2"
7
- date: 2006-06-01 00:00:00 +03:00
6
+ version: "0.5"
7
+ date: 2006-06-28 00:00:00 +03:00
8
8
  summary: Light-weight Ruby API to LibXML.
9
9
  require_paths:
10
10
  - lib
@@ -35,8 +35,8 @@ files:
35
35
  - ext/notes.txt
36
36
  - ext/teius.c
37
37
  - ext/teius.o
38
+ - ext/teius.so
38
39
  - lib/teius.rb
39
- - lib/teius.so
40
40
  - test/input.xml
41
41
  - test/teius_test.rb
42
42
  - test/test.xsl
data/lib/teius.so DELETED
Binary file