teius 0.5 → 0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ Gem::manage_gems
3
+ require 'rake'
4
+ require 'rake/gempackagetask'
5
+ require 'rake/testtask'
6
+
7
+ task :default => ['test']
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = %q{teius}
11
+ # Note: use symbols, not constants for node type
12
+ s.version = "0.12"
13
+ s.date = %q{2006-07-27}
14
+ s.summary = %q{Light-weight Ruby API to LibXML.}
15
+ s.email = %q{joshmh@gmail.com}
16
+ s.homepage = %q{http://teius.rubyforge.org}
17
+ s.rubyforge_project = %q{teius}
18
+ s.description = %q{Teius is a very lightweight Ruby API around the LibXML C library. The idea is to use a syntax reminiscent of Ruby On Rails' find method to quickly process information from an XML document.}
19
+ s.require_paths = [ 'lib' ]
20
+ s.autorequire = %q{teius}
21
+ s.has_rdoc = false
22
+ s.authors = ["Joshua Harvey"]
23
+ s.files = FileList[ '{test,xml,doc}/**/*', 'ext/*.rb', 'ext/*.c', 'lib/*.rb',
24
+ 'Rakefile' ].to_a
25
+ s.test_file = 'test/teius_test.rb'
26
+ s.platform = Gem::Platform::RUBY
27
+ s.extensions = [ 'ext/extconf.rb' ]
28
+ # s.rdoc_options = ["--main", "README"]
29
+ # s.extra_rdoc_files = ["README"]
30
+ end
31
+
32
+ win_spec = spec.clone
33
+ win_spec.platform = %q{mswin32}
34
+ win_spec.files = FileList["{ext,lib,test,xml,doc,dll}/**/*"].to_a
35
+ win_spec.extensions = nil
36
+ win_spec.require_paths << 'dll'
37
+
38
+ Rake::GemPackageTask.new(spec) do |pkg|
39
+ pkg.need_zip = false
40
+ pkg.need_tar = true
41
+ end
42
+
43
+ Rake::GemPackageTask.new(win_spec) do |pkg|
44
+ pkg.need_zip = true
45
+ pkg.need_tar = false
46
+ end
47
+
48
+ Rake::TestTask.new do |t|
49
+ t.libs << "test"
50
+ t.test_files = FileList['test/*.rb']
51
+ t.libs = ['../lib','..']
52
+ t.ruby_opts = ['-xtest']
53
+ t.verbose = true
54
+ end
@@ -1,7 +1,51 @@
1
1
  require 'mkmf'
2
2
 
3
- dir_config('libxml', '/usr/include/libxml', '/usr/lib');
4
- dir_config('libxslt', '/usr/include', '/usr/lib');
3
+ def crash(str)
4
+ printf(" extconf failure: %s\n", str)
5
+ exit 1
6
+ end
7
+
8
+ dir_config('xml2');
9
+ unless (have_library('xml2', 'xmlParseDoc') or
10
+ find_library('xml2', '/opt/lib', '/usr/local/lib', '/usr/lib')) and
11
+ (have_header('libxml/xmlversion.h') or
12
+ find_header('libxml/xmlversion.h',
13
+ '/opt/include/libxml2',
14
+ '/usr/local/include/libxml2',
15
+ '/usr/include/libxml',
16
+ '/usr/include/libxml2'))
17
+ crash(<<EOL)
18
+ need libxml2.
19
+
20
+ Install the library or try one of the following options to extconf.rb:
21
+
22
+ --with-xml2-dir=/path/to/libxml2
23
+ --with-xml2-lib=/path/to/libxml2/lib
24
+ --with-xml2-include=/path/to/libxml2/include
25
+ EOL
26
+ end
27
+
28
+ dir_config('libxslt');
29
+ unless (have_library('xslt', 'xsltApplyStylesheet') or
30
+ find_library('xslt', '/opt/lib', '/usr/local/lib', '/usr/lib')) and
31
+ (have_header('libxslt/xslt.h') or
32
+ find_header('libxslt/xslt.h',
33
+ '/opt/include/libxslt',
34
+ '/usr/local/include/libxslt',
35
+ '/usr/include/libxslt'))
36
+ crash(<<EOL)
37
+ need libxslt.
38
+
39
+ Install the library or try one of the following options to extconf.rb:
40
+
41
+ --with-xslt-dir=/path/to/libxslt
42
+ --with-xslt-lib=/path/to/libxslt/lib
43
+ --with-xslt-include=/path/to/libxslt/include
44
+ EOL
45
+ end
46
+
5
47
  have_library('xml2', 'xmlReadFile')
6
48
  have_library('xslt', 'xsltParseStylesheetFile')
7
- create_makefile("teius")
49
+
50
+ $CFLAGS << ' ' << $INCFLAGS
51
+ create_makefile('teius')
@@ -35,7 +35,7 @@ static VALUE node_value(VALUE self) {
35
35
  Data_Get_Struct(self, xmlNode, node);
36
36
 
37
37
  xmlChar *val = xmlNodeGetContent(node);
38
- VALUE rVal = rb_str_new2(val);
38
+ VALUE rVal = rb_str_new2((char *)val);
39
39
  xmlFree(val);
40
40
 
41
41
  return rVal;
@@ -59,9 +59,9 @@ static VALUE node_attributes(VALUE self) {
59
59
  xmlAttrPtr attrs = node->properties;
60
60
  VALUE rHash = rb_hash_new();
61
61
  while (attrs != NULL) {
62
- VALUE rName = rb_str_new2(attrs->name);
62
+ VALUE rName = rb_str_new2((char *)attrs->name);
63
63
  xmlChar *val = xmlNodeGetContent(attrs->children);
64
- VALUE rVal = rb_str_new2(val);
64
+ VALUE rVal = rb_str_new2((char *)val);
65
65
  rb_hash_aset(rHash, rName, rVal);
66
66
  xmlFree(val);
67
67
  attrs = attrs->next;
@@ -90,7 +90,7 @@ static VALUE node_find(int argc, VALUE *argv, VALUE self) {
90
90
  context->node = node;
91
91
 
92
92
  char *xpath = StringValuePtr(rXpath);
93
- result = xmlXPathEvalExpression(xpath, context);
93
+ result = xmlXPathEvalExpression((xmlChar *)xpath, context);
94
94
  if (result == NULL) {
95
95
  xmlErrorPtr err = xmlGetLastError();
96
96
  xmlXPathFreeContext(context);
@@ -147,13 +147,42 @@ static VALUE node_find(int argc, VALUE *argv, VALUE self) {
147
147
  static VALUE node_name(VALUE self) {
148
148
  xmlNodePtr node = NULL;
149
149
  Data_Get_Struct(self, xmlNode, node);
150
- return rb_str_new2(node->name);
150
+ return rb_str_new2((char *)node->name);
151
151
  }
152
152
 
153
153
  static VALUE node_xpath(VALUE self) {
154
154
  xmlNodePtr node = NULL;
155
155
  Data_Get_Struct(self, xmlNode, node);
156
- return rb_str_new2(xmlGetNodePath(node));
156
+ return rb_str_new2((char *)xmlGetNodePath(node));
157
+ }
158
+
159
+ static VALUE node_type(VALUE self) {
160
+ xmlNodePtr node = NULL;
161
+ Data_Get_Struct(self, xmlNode, node);
162
+
163
+ switch (node->type) {
164
+ case 1: return ID2SYM(rb_intern("element"));
165
+ case 2: return ID2SYM(rb_intern("attribute"));
166
+ case 3: return ID2SYM(rb_intern("text"));
167
+ case 4: return ID2SYM(rb_intern("cdata_section"));
168
+ case 5: return ID2SYM(rb_intern("entity_ref"));
169
+ case 6: return ID2SYM(rb_intern("entity"));
170
+ case 7: return ID2SYM(rb_intern("pi"));
171
+ case 8: return ID2SYM(rb_intern("comment"));
172
+ case 9: return ID2SYM(rb_intern("document"));
173
+ case 10: return ID2SYM(rb_intern("document_type"));
174
+ case 11: return ID2SYM(rb_intern("document_fragment"));
175
+ case 12: return ID2SYM(rb_intern("notation"));
176
+ case 13: return ID2SYM(rb_intern("html_document"));
177
+ case 14: return ID2SYM(rb_intern("dtd"));
178
+ case 15: return ID2SYM(rb_intern("element_declaration"));
179
+ case 16: return ID2SYM(rb_intern("attribute_declaration"));
180
+ case 17: return ID2SYM(rb_intern("entity_declaration"));
181
+ case 18: return ID2SYM(rb_intern("namespace_declaration"));
182
+ case 19: return ID2SYM(rb_intern("xinclude_start"));
183
+ case 20: return ID2SYM(rb_intern("xinclude_end"));
184
+ case 21: return ID2SYM(rb_intern("docb_document"));
185
+ }
157
186
  }
158
187
 
159
188
  static VALUE node_pointer(VALUE self) {
@@ -180,12 +209,27 @@ static VALUE node_content(VALUE self) {
180
209
  buf = xmlBufferCreate();
181
210
  size = xmlNodeDump(buf, node->doc, node, 0, 1);
182
211
  mem = xmlBufferContent(buf);
183
- VALUE rStr = rb_str_new2(mem);
212
+ VALUE rStr = rb_str_new2((char *)mem);
184
213
  xmlBufferFree(buf);
185
214
 
186
215
  return rStr;
187
216
  }
188
217
 
218
+ static VALUE node_children(VALUE self) {
219
+ xmlNodePtr node = NULL;
220
+ xmlNodePtr cur_child = NULL;
221
+
222
+ Data_Get_Struct(self, xmlNode, node);
223
+
224
+ VALUE rArr = rb_ary_new();
225
+ for (cur_child = node->children; cur_child != NULL; cur_child = cur_child->next) {
226
+ VALUE rNode = Data_Wrap_Struct(cNode, 0, 0, cur_child);
227
+ rb_ary_push(rArr, rNode);
228
+ }
229
+
230
+ return rArr;
231
+ }
232
+
189
233
  static VALUE document_parse_file(VALUE self, VALUE rFilename) {
190
234
  xmlDocPtr doc = xmlReadFile(StringValuePtr(rFilename), NULL, 0);
191
235
  if (doc == NULL) {
@@ -195,20 +239,20 @@ static VALUE document_parse_file(VALUE self, VALUE rFilename) {
195
239
  }
196
240
 
197
241
  /* Load new document */
198
- VALUE rDoc = Data_Wrap_Struct(cDocument, 0, doc_free, doc);
242
+ VALUE rDoc = Data_Wrap_Struct(self, 0, doc_free, doc);
199
243
 
200
244
  return rDoc;
201
245
  }
202
246
 
203
247
  static VALUE document_parse_string(VALUE self, VALUE rDocString) {
204
- xmlDocPtr doc = xmlReadDoc(StringValuePtr(rDocString), NULL, NULL, 0);
248
+ xmlDocPtr doc = xmlReadDoc((xmlChar *)StringValuePtr(rDocString), NULL, NULL, 0);
205
249
  if (doc == NULL) {
206
250
  xmlErrorPtr err = xmlGetLastError();
207
251
  rb_raise(cParseError, "could not parse string: %s", err->message);
208
252
  }
209
253
 
210
254
  /* Load new document */
211
- VALUE rDoc = Data_Wrap_Struct(cDocument, 0, doc_free, doc);
255
+ VALUE rDoc = Data_Wrap_Struct(self, 0, doc_free, doc);
212
256
 
213
257
  return rDoc;
214
258
  }
@@ -220,7 +264,7 @@ static VALUE document_to_s(VALUE self) {
220
264
 
221
265
  Data_Get_Struct(self, xmlDoc, doc);
222
266
  xmlDocDumpMemory(doc, &mem, &size);
223
- VALUE rStr = rb_str_new2(mem);
267
+ VALUE rStr = rb_str_new2((char *)mem);
224
268
  xmlFree(mem);
225
269
 
226
270
  return rStr;
@@ -244,7 +288,7 @@ static void stylesheet_free(void *p) {
244
288
  }
245
289
 
246
290
  static VALUE stylesheet_parse_file(VALUE self, VALUE rFilename) {
247
- xsltStylesheetPtr xsl = xsltParseStylesheetFile(StringValuePtr(rFilename));
291
+ xsltStylesheetPtr xsl = xsltParseStylesheetFile((xmlChar *)StringValuePtr(rFilename));
248
292
  if (xsl == NULL) {
249
293
  xmlErrorPtr err = xmlGetLastError();
250
294
  rb_raise(cParseError, "could not parse file %s: %s",
@@ -252,11 +296,23 @@ static VALUE stylesheet_parse_file(VALUE self, VALUE rFilename) {
252
296
  }
253
297
 
254
298
  /* Load new document */
255
- VALUE rStylesheet = Data_Wrap_Struct(cStylesheet, 0, stylesheet_free, xsl);
299
+ VALUE rStylesheet = Data_Wrap_Struct(self, 0, stylesheet_free, xsl);
256
300
 
257
301
  return rStylesheet;
258
302
  }
259
303
 
304
+ static VALUE stylesheet_output_method(VALUE self) {
305
+ xsltStylesheetPtr stylesheet = NULL;
306
+
307
+ Data_Get_Struct(self, xsltStylesheet, stylesheet);
308
+ if (stylesheet->method == NULL) {
309
+ return Qnil;
310
+ } else {
311
+ VALUE rStr = rb_str_new2((char *)stylesheet->method);
312
+ return rStr;
313
+ }
314
+ }
315
+
260
316
  static VALUE stylesheet_apply(VALUE self, VALUE rDocument) {
261
317
  xsltStylesheetPtr stylesheet = NULL;
262
318
  xmlDocPtr doc = NULL, res = NULL;
@@ -304,7 +360,7 @@ static VALUE stylesheet_save_to_string(VALUE self, VALUE rResult) {
304
360
  rb_raise(rb_eStandardError, "could not save stylesheet result to string: %s",
305
361
  err->message);
306
362
  }
307
- VALUE rStr = rb_str_new2(mem);
363
+ VALUE rStr = rb_str_new2((char *)mem);
308
364
  xmlFree(mem);
309
365
 
310
366
  return rStr;
@@ -341,9 +397,11 @@ void Init_teius() {
341
397
  rb_define_method(cNode, "find", node_find, -1);
342
398
  rb_define_method(cNode, "name", node_name, 0);
343
399
  rb_define_method(cNode, "xpath", node_xpath, 0);
400
+ rb_define_method(cNode, "type", node_type, 0);
344
401
  rb_define_method(cNode, "pointer", node_pointer, 0);
345
402
  rb_define_method(cNode, "line", node_line, 0);
346
403
  rb_define_method(cNode, "content", node_content, 0);
404
+ rb_define_method(cNode, "children", node_children, 0);
347
405
 
348
406
  /* Document */
349
407
  cDocument = rb_define_class_under(mTeius, "Document", cNode);
@@ -353,8 +411,9 @@ void Init_teius() {
353
411
  rb_define_method(cDocument, "root", document_root, 0);
354
412
 
355
413
  /* XSL Stylesheet */
356
- cStylesheet = rb_define_class_under(mTeius, "Stylesheet", cNode);
414
+ cStylesheet = rb_define_class_under(mTeius, "Stylesheet", rb_cObject);
357
415
  rb_define_singleton_method(cStylesheet, "parse_file", stylesheet_parse_file, 1);
416
+ rb_define_method(cStylesheet, "output_method", stylesheet_output_method, 0);
358
417
  rb_define_method(cStylesheet, "apply", stylesheet_apply, 1);
359
418
  rb_define_method(cStylesheet, "save_to_string", stylesheet_save_to_string, 1);
360
419
  rb_define_method(cStylesheet, "save_to_file", stylesheet_save_to_file, 2);
@@ -1,6 +1,9 @@
1
- require File.join(File.dirname(__FILE__), '..', 'ext', 'teius.so')
1
+ $:.unshift File.dirname(__FILE__)
2
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'ext')
3
+ require 'teius.so'
2
4
 
3
5
  module Teius
6
+
4
7
  class Document
5
8
 
6
9
  def self.content(xml)
@@ -43,11 +46,7 @@ module Teius
43
46
  def siblings
44
47
  self.find :all, 'preceding-sibling::*|following-sibling::*'
45
48
  end
46
-
47
- def children
48
- self.find :all, 'child::*'
49
- end
50
-
49
+
51
50
  def to_s
52
51
  "#{name}:#{line}"
53
52
  end
@@ -6,6 +6,9 @@ require 'tempfile'
6
6
  class TeiusTest < Test::Unit::TestCase
7
7
  include Teius
8
8
 
9
+ class SubTeius < Document
10
+ end
11
+
9
12
  def setup
10
13
  @xml_dir = File.join(File.dirname(__FILE__), '..', 'xml')
11
14
  @doc = Document.parse_file File.join(@xml_dir,
@@ -26,7 +29,13 @@ class TeiusTest < Test::Unit::TestCase
26
29
  assert_equal 'xt.3608787-update-post-event', el.value
27
30
  assert_equal 'doc-id', el.name
28
31
  end
29
-
32
+
33
+ def test_inheritance
34
+ doc = SubTeius.parse_file File.join(@xml_dir,
35
+ 'xt.3608787-update-post-event.xml')
36
+ assert_kind_of SubTeius, doc
37
+ end
38
+
30
39
  def test_relative_paths
31
40
  meta = @doc.find :first, '/sports-content/sports-metadata'
32
41
  assert_not_nil meta
@@ -135,6 +144,12 @@ class TeiusTest < Test::Unit::TestCase
135
144
  sibs = e.siblings
136
145
  assert_equal 9, sibs.size
137
146
  end
147
+
148
+ def test_children
149
+ e = @doc.find :first, '//sports-content-codes'
150
+ children = e.children
151
+ assert_equal 10, children.size
152
+ end
138
153
 
139
154
  def test_xslt
140
155
  current_dir = File.dirname(__FILE__)
@@ -142,10 +157,17 @@ class TeiusTest < Test::Unit::TestCase
142
157
  xsl_path = File.join(current_dir, 'test.xsl')
143
158
  doc = Document.parse_file test_doc_path
144
159
  xsl = Stylesheet.parse_file xsl_path
160
+ assert_equal 'xml', xsl.output_method
145
161
  assert_no_match %r{menu}, doc.to_s
146
162
  assert_match %r{menu}, xsl.transform(doc)
147
163
  end
148
164
 
165
+ def test_bogus_xslt
166
+ assert_raise(ParseError) {
167
+ xsl = Stylesheet.parse_file 'bogus'
168
+ }
169
+ end
170
+
149
171
  def test_xslt_write_to_file
150
172
  current_dir = File.dirname(__FILE__)
151
173
  test_doc_path = File.join(current_dir, 'input.xml')
@@ -193,4 +215,17 @@ class TeiusTest < Test::Unit::TestCase
193
215
  assert_equal "<library>\n <book/>\n</library>", content
194
216
  end
195
217
 
218
+ def test_node_type
219
+ node = @doc.find :first, '//team-metadata'
220
+ assert_equal :element, node.type
221
+ assert_equal :document, @doc.type
222
+ end
223
+
224
+ def test_text_node
225
+ title = @doc.find :first, '/sports-content/sports-metadata/sports-title'
226
+ text = title.children.first
227
+ assert_equal :text, text.type
228
+ assert_equal "Score Update: Chicago vs. Green Bay (Final)", text.value
229
+ end
230
+
196
231
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
2
+ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: teius
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.5"
7
- date: 2006-06-28 00:00:00 +03:00
6
+ version: "0.12"
7
+ date: 2006-07-27 00:00:00 +03:00
8
8
  summary: Light-weight Ruby API to LibXML.
9
9
  require_paths:
10
10
  - lib
@@ -26,6 +26,7 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
26
26
  platform: ruby
27
27
  signing_key:
28
28
  cert_chain:
29
+ post_install_message:
29
30
  authors:
30
31
  - Joshua Harvey
31
32
  files:
@@ -38,6 +39,7 @@ files:
38
39
  - ext/extconf.rb
39
40
  - ext/teius.c
40
41
  - lib/teius.rb
42
+ - Rakefile
41
43
  test_files:
42
44
  - test/teius_test.rb
43
45
  rdoc_options: []