teius 0.1.0-mswin32 → 0.2-mswin32

Sign up to get free protection for your applications and to get access to all the features.
data/dll/libexslt.dll ADDED
Binary file
data/dll/libxml2.dll CHANGED
Binary file
data/dll/libxslt.dll ADDED
Binary file
data/ext/Makefile CHANGED
@@ -36,7 +36,7 @@ LIBRUBYARG_SHARED = -l$(RUBY_SO_NAME)
36
36
  LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static
37
37
 
38
38
  CFLAGS = -g -O2
39
- CPPFLAGS = -I. -I$(topdir) -I$(hdrdir) -I$(srcdir) -Id:/msys/local/include/libxml2
39
+ CPPFLAGS = -I. -I$(topdir) -I$(hdrdir) -I$(srcdir) -Id:/msys/local/include -Id:/msys/local/include/libxml2
40
40
  CXXFLAGS = $(CFLAGS)
41
41
  DLDFLAGS = -Wl,--enable-auto-import,--export-all
42
42
  LDSHARED = gcc -shared -s
@@ -72,7 +72,7 @@ extout =
72
72
  extout_prefix =
73
73
  target_prefix =
74
74
  LOCAL_LIBS =
75
- LIBS = $(LIBRUBYARG_SHARED) -lxml2 -lwsock32
75
+ LIBS = $(LIBRUBYARG_SHARED) -lxslt -lxml2 -lwsock32
76
76
  SRCS = teius.c
77
77
  OBJS = teius.o
78
78
  TARGET = teius
data/ext/extconf.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'mkmf'
2
2
 
3
- dir_config('libxml', '/usr/include/libxml2', '/usr/lib');
3
+ dir_config('libxml', '/usr/include/libxml', '/usr/lib');
4
+ dir_config('libxslt', '/usr/include', '/usr/lib');
4
5
  have_library('xml2', 'xmlReadFile')
6
+ have_library('xslt', 'xsltParseStylesheetFile')
5
7
  create_makefile("teius")
data/ext/mkmf.log ADDED
@@ -0,0 +1,26 @@
1
+ have_library: checking for xmlReadFile() in -lxml2... -------------------- yes
2
+
3
+ "gcc -o conftest -I. -Id:/msys/local/lib/ruby/1.8/i386-mingw32 -Id:/msys/local/include -Id:/msys/local/include/libxml2 -g -O2 conftest.c -L"d:/msys/local/lib" -L"d:/msys/local/lib" -lmsvcrt-ruby18-static -lxml2 -lwsock32 "
4
+ checked program was:
5
+ /* begin */
6
+
7
+ /*top*/
8
+ int main() { return 0; }
9
+ int t() { xmlReadFile(); return 0; }
10
+ /* end */
11
+
12
+ --------------------
13
+
14
+ have_library: checking for xsltParseStylesheetFile() in -lxslt... -------------------- yes
15
+
16
+ "gcc -o conftest -I. -Id:/msys/local/lib/ruby/1.8/i386-mingw32 -Id:/msys/local/include -Id:/msys/local/include/libxml2 -g -O2 conftest.c -L"d:/msys/local/lib" -L"d:/msys/local/lib" -lxml2 -lmsvcrt-ruby18-static -lxslt -lxml2 -lwsock32 "
17
+ checked program was:
18
+ /* begin */
19
+
20
+ /*top*/
21
+ int main() { return 0; }
22
+ int t() { xsltParseStylesheetFile(); return 0; }
23
+ /* end */
24
+
25
+ --------------------
26
+
data/ext/notes.txt ADDED
@@ -0,0 +1,2 @@
1
+ ruby extconf.rb --with-libxml-include=/usr/local/include/libxml2 --with-libxml-lib=/usr/local/lib --with-libxslt-include=
2
+ /usr/local/include --with-libxslt-lib=/usr/local/lib
data/ext/teius.c CHANGED
@@ -1,20 +1,31 @@
1
+ /* NOTES: possibly call xsltCleanupGlobals() and xmlCleanupParser() at some point */
2
+
1
3
  #include <stdio.h>
2
4
  #include "ruby.h"
3
5
  #include <libxml/parser.h>
4
6
  #include <libxml/tree.h>
5
7
  #include <libxml/xpath.h>
6
8
 
9
+ #include <libxslt/xslt.h>
10
+ #include <libxslt/xsltInternals.h>
11
+ #include <libxslt/transform.h>
12
+ #include <libxslt/xsltutils.h>
13
+
7
14
  static VALUE cParseError;
8
15
  static VALUE cXpathError;
9
16
  static VALUE cNodeNotFound;
10
17
  static VALUE cNodeNotUnique;
11
18
  static VALUE cNode;
12
19
  static VALUE cDocument;
20
+ static VALUE cStylesheet;
13
21
  static ID sRequired;
14
22
  static ID sFirst;
15
23
  static ID sAll;
16
24
  static ID sUnique;
17
25
 
26
+ /* Suppress error messages to stderr */
27
+ static void null_handler(void *ctx, const char *msg, ...) {}
28
+
18
29
  static void doc_free(void *p) {
19
30
  xmlFreeDoc(p);
20
31
  }
@@ -151,7 +162,7 @@ static VALUE node_pointer(VALUE self) {
151
162
  return INT2NUM((int)node);
152
163
  }
153
164
 
154
- static VALUE parse_file(VALUE self, VALUE rFilename) {
165
+ static VALUE document_parse_file(VALUE self, VALUE rFilename) {
155
166
  xmlDocPtr doc = xmlReadFile(StringValuePtr(rFilename), NULL, 0);
156
167
  if (doc == NULL) {
157
168
  xmlErrorPtr err = xmlGetLastError();
@@ -165,7 +176,7 @@ static VALUE parse_file(VALUE self, VALUE rFilename) {
165
176
  return rDoc;
166
177
  }
167
178
 
168
- static VALUE parse_string(VALUE self, VALUE rDocString) {
179
+ static VALUE document_parse_string(VALUE self, VALUE rDocString) {
169
180
  xmlDocPtr doc = xmlReadDoc(StringValuePtr(rDocString), NULL, NULL, 0);
170
181
  if (doc == NULL) {
171
182
  xmlErrorPtr err = xmlGetLastError();
@@ -179,7 +190,7 @@ static VALUE parse_string(VALUE self, VALUE rDocString) {
179
190
  }
180
191
 
181
192
  static VALUE document_to_s(VALUE self, VALUE rDocString) {
182
- xmlDocPtr doc = NULL;
193
+ xmlDocPtr doc = NULL;
183
194
  xmlChar *mem;
184
195
  int size;
185
196
 
@@ -191,9 +202,84 @@ static VALUE document_to_s(VALUE self, VALUE rDocString) {
191
202
  return rStr;
192
203
  }
193
204
 
205
+ /* LibXslt Stuff */
206
+ static void stylesheet_free(void *p) {
207
+ xsltFreeStylesheet(p);
208
+ }
209
+
210
+ static VALUE stylesheet_parse_file(VALUE self, VALUE rFilename) {
211
+ xsltStylesheetPtr xsl = xsltParseStylesheetFile(StringValuePtr(rFilename));
212
+ if (xsl == NULL) {
213
+ xmlErrorPtr err = xmlGetLastError();
214
+ rb_raise(cParseError, "could not parse file %s: %s",
215
+ StringValuePtr(rFilename), err->message);
216
+ }
217
+
218
+ /* Load new document */
219
+ VALUE rStylesheet = Data_Wrap_Struct(cStylesheet, 0, stylesheet_free, xsl);
220
+
221
+ return rStylesheet;
222
+ }
223
+
224
+ static VALUE stylesheet_apply(VALUE self, VALUE rDocument) {
225
+ xsltStylesheetPtr stylesheet = NULL;
226
+ xmlDocPtr doc = NULL, res = NULL;
227
+
228
+ Data_Get_Struct(self, xsltStylesheet, stylesheet);
229
+ Data_Get_Struct(rDocument, xmlDoc, doc);
230
+ res = xsltApplyStylesheet(stylesheet, doc, NULL);
231
+
232
+ VALUE rDoc = Data_Wrap_Struct(cDocument, 0, doc_free, res);
233
+
234
+ return rDoc;
235
+ }
236
+
237
+ static VALUE stylesheet_save_to_file(VALUE self, VALUE rResult, VALUE rFilename) {
238
+ xsltStylesheetPtr stylesheet = NULL;
239
+ xmlDocPtr result = NULL;
240
+ int st;
241
+
242
+ Data_Get_Struct(self, xsltStylesheet, stylesheet);
243
+ Data_Get_Struct(rResult, xmlDoc, result);
244
+
245
+ st = xsltSaveResultToFilename(StringValuePtr(rFilename), result, stylesheet, 0);
246
+ if (st == -1) {
247
+ xmlErrorPtr err = xmlGetLastError();
248
+ rb_raise(rb_eIOError, "could not save stylesheet result file %s: %s",
249
+ StringValuePtr(rFilename), err->message);
250
+ }
251
+
252
+ return Qnil;
253
+ }
254
+
255
+ static VALUE stylesheet_save_to_string(VALUE self, VALUE rResult) {
256
+ xsltStylesheetPtr stylesheet = NULL;
257
+ xmlDocPtr result = NULL;
258
+ int st;
259
+ xmlChar *mem;
260
+ int size;
261
+
262
+ Data_Get_Struct(self, xsltStylesheet, stylesheet);
263
+ Data_Get_Struct(rResult, xmlDoc, result);
264
+
265
+ st = xsltSaveResultToString(&mem, &size, result, stylesheet);
266
+ if (st == -1) {
267
+ xmlErrorPtr err = xmlGetLastError();
268
+ rb_raise(rb_eStandardError, "could not save stylesheet result to string: %s",
269
+ err->message);
270
+ }
271
+ VALUE rStr = rb_str_new2(mem);
272
+ xmlFree(mem);
273
+
274
+ return rStr;
275
+ }
276
+
194
277
  void Init_teius() {
195
278
  /* LIBXML_TEST_VERSION */
196
279
 
280
+ xmlSetGenericErrorFunc(NULL, null_handler);
281
+ xsltSetGenericErrorFunc(NULL, null_handler);
282
+
197
283
  /* Symbols */
198
284
  sRequired = rb_intern("required");
199
285
  sFirst = rb_intern("first");
@@ -223,7 +309,14 @@ void Init_teius() {
223
309
 
224
310
  /* Document */
225
311
  cDocument = rb_define_class_under(mTeius, "Document", cNode);
226
- rb_define_singleton_method(cDocument, "parse_file", parse_file, 1);
227
- rb_define_singleton_method(cDocument, "parse_string", parse_string, 1);
228
- rb_define_method(cNode, "to_s", document_to_s, 0);
312
+ rb_define_singleton_method(cDocument, "parse_file", document_parse_file, 1);
313
+ rb_define_singleton_method(cDocument, "parse_string", document_parse_string, 1);
314
+ rb_define_method(cDocument, "to_s", document_to_s, 0);
315
+
316
+ /* XSL Stylesheet */
317
+ cStylesheet = rb_define_class_under(mTeius, "Stylesheet", cNode);
318
+ rb_define_singleton_method(cStylesheet, "parse_file", stylesheet_parse_file, 1);
319
+ rb_define_method(cStylesheet, "apply", stylesheet_apply, 1);
320
+ rb_define_method(cStylesheet, "save_to_string", stylesheet_save_to_string, 1);
321
+ rb_define_method(cStylesheet, "save_to_file", stylesheet_save_to_file, 2);
229
322
  }
data/ext/teius.o CHANGED
Binary file
data/lib/teius.rb CHANGED
@@ -1,9 +1,10 @@
1
+ #require File.join(File.dirname(__FILE__), '..', 'ext', 'teius.so')
1
2
  require 'teius.so'
2
3
 
3
4
  module Teius
4
5
  class Node
5
6
  def fetch(xpath)
6
- node = find(:unique, xpath, :required => true)
7
+ node = find(:unique, xpath, :required => false)
7
8
  return node ? node.value : nil
8
9
  end
9
10
 
@@ -32,4 +33,20 @@ module Teius
32
33
  end
33
34
 
34
35
  end
36
+
37
+ class Stylesheet
38
+
39
+ # If no filename is provided, will return the result as a String
40
+ def transform(doc, filename = nil)
41
+ result = apply doc
42
+ if filename
43
+ out = save_to_file result, filename # returns nil
44
+ else
45
+ out = save_to_string result
46
+ end
47
+ out
48
+ end
49
+
50
+ end
51
+
35
52
  end
data/lib/teius.so CHANGED
Binary file
data/test/input.xml ADDED
@@ -0,0 +1,11 @@
1
+ <?xml version="1.0"?>
2
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4
+ <head>
5
+ <title>Test document</title>
6
+ </head>
7
+ <body>
8
+ <h1>Test document</h1>
9
+ <p>This paragraph indicates the rest of the document.</p>
10
+ </body>
11
+ </html>
data/test/teius_test.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
2
  require 'test/unit'
3
3
  require 'teius'
4
+ require 'tempfile'
4
5
 
5
6
  class TeiusTest < Test::Unit::TestCase
6
7
  include Teius
@@ -135,4 +136,40 @@ class TeiusTest < Test::Unit::TestCase
135
136
  assert_equal 9, sibs.size
136
137
  end
137
138
 
139
+ def test_xslt
140
+ current_dir = File.dirname(__FILE__)
141
+ test_doc_path = File.join(current_dir, 'input.xml')
142
+ xsl_path = File.join(current_dir, 'test.xsl')
143
+ doc = Document.parse_file test_doc_path
144
+ xsl = Stylesheet.parse_file xsl_path
145
+ assert_no_match %r{menu}, doc.to_s
146
+ assert_match %r{menu}, xsl.transform(doc)
147
+ end
148
+
149
+ def test_xslt_write_to_file
150
+ current_dir = File.dirname(__FILE__)
151
+ test_doc_path = File.join(current_dir, 'input.xml')
152
+ xsl_path = File.join(current_dir, 'test.xsl')
153
+ doc = Document.parse_file test_doc_path
154
+ xsl = Stylesheet.parse_file xsl_path
155
+ Tempfile.open('xslt_test_file') do |tf|
156
+ outpath = tf.path
157
+ xsl.transform doc, outpath
158
+ str = tf.read
159
+ assert_match %r{menu}, str
160
+ end
161
+ end
162
+
163
+ def test_xml_bad_file
164
+ assert_raise(Teius::ParseError) {
165
+ Document.parse_file File.join(File.dirname(__FILE__), 'bogus.xsl')
166
+ }
167
+ end
168
+
169
+ def test_xslt_bad_file
170
+ assert_raise(Teius::ParseError) {
171
+ Stylesheet.parse_file File.join(File.dirname(__FILE__), 'bogus.xsl')
172
+ }
173
+ end
174
+
138
175
  end
data/test/test.xsl ADDED
@@ -0,0 +1,27 @@
1
+ <?xml version="1.0"?>
2
+ <xsl:stylesheet version="1.0" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes" exclude-result-prefixes="xhtml xsl fn xs xdt">
3
+ <xsl:output method="xml" version="1.0" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" indent="yes"/>
4
+
5
+ <!-- the identity template -->
6
+ <xsl:template match="@*|node()">
7
+ <xsl:copy>
8
+ <xsl:apply-templates select="@*|node()"/>
9
+ </xsl:copy>
10
+ </xsl:template>
11
+
12
+ <!-- template for the head section. Only needed if we want to change, delete or add nodes. In our case we need it to add a link element pointing to an external CSS stylesheet. -->
13
+ <xsl:template match="xhtml:head">
14
+ <xsl:copy>
15
+ <link rel="StyleSheet" href="xhtml_test.css" type="text/css"/>
16
+ <xsl:apply-templates select="@*|node()"/>
17
+ </xsl:copy>
18
+ </xsl:template>
19
+
20
+ <!-- template for the body section. Only needed if we want to change, delete or add nodes. In our case we need it to add a div element containing a menu of navigation. -->
21
+ <xsl:template match="xhtml:body">
22
+ <xsl:copy>
23
+ <div class="menu"><p><a href="home">Homepage</a> &gt; <strong>Test document</strong></p></div>
24
+ <xsl:apply-templates select="@*|node()"/>
25
+ </xsl:copy>
26
+ </xsl:template>
27
+ </xsl:stylesheet>
metadata CHANGED
@@ -3,10 +3,11 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: teius
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2006-05-05 00:00:00 +02:00
6
+ version: "0.2"
7
+ date: 2006-06-01 00:00:00 +03:00
8
8
  summary: Light-weight Ruby API to LibXML.
9
9
  require_paths:
10
+ - lib
10
11
  - dll
11
12
  email: joshmh@gmail.com
12
13
  homepage: http://teius.rubyforge.org
@@ -30,16 +31,21 @@ authors:
30
31
  files:
31
32
  - ext/extconf.rb
32
33
  - ext/Makefile
34
+ - ext/mkmf.log
35
+ - ext/notes.txt
33
36
  - ext/teius.c
34
37
  - ext/teius.o
35
- - ext/teius.so
36
38
  - lib/teius.rb
37
39
  - lib/teius.so
40
+ - test/input.xml
38
41
  - test/teius_test.rb
42
+ - test/test.xsl
39
43
  - xml/xt.3608774-update-mid-event.xml
40
44
  - xml/xt.3608787-update-post-event.xml
41
45
  - doc/LICENCE.txt
46
+ - dll/libexslt.dll
42
47
  - dll/libxml2.dll
48
+ - dll/libxslt.dll
43
49
  test_files:
44
50
  - test/teius_test.rb
45
51
  rdoc_options: []
data/ext/teius.so DELETED
Binary file