libxml-io 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 505e7a54d194ddc69c072cd2e594eb5d98d8e444
4
+ data.tar.gz: c23e3c7055e1ed400393fc599d7ba87dabb3a360
5
+ SHA512:
6
+ metadata.gz: caed745b6199ab0f92d8f9d0b552c0e9133fccd917c88f3cf90fb3a1be808c7292e3b6d71336204d87b98a1d24124619b26dcd81ee798b95b1b5037a60ef4248
7
+ data.tar.gz: 0b1100315eaa9730194ac32394eebad4ab8549c3288e41b4dbdce67885082f9bb81efb2318ab0b5c7b7a2bdb7da9adbee69efb870cb9e1512866b7f789efa899
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in libxml-io.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ libxml-io (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.11.3)
10
+ rake (10.5.0)
11
+ rake-compiler (1.0.4)
12
+ rake
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.16)
19
+ libxml-io!
20
+ minitest (~> 5.11)
21
+ rake (~> 10.0)
22
+ rake-compiler (~> 1.0)
23
+
24
+ BUNDLED WITH
25
+ 1.16.3
data/LICENSE.md ADDED
@@ -0,0 +1,24 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2018:
4
+
5
+ * Anton Kozhemyachenko
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ 'Software'), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # LibXML::IO
2
+
3
+ The libxml-io gem provides Ruby language bindings for Libxml2 [xmlRegisterInputCallbacks](http://xmlsoft.org/html/libxml-xmlIO.html#xmlRegisterInputCallbacks).
4
+ Can be used with any gem using Libxml2 XML toolkit, however this gem was primary designed to use with [Nokogiri](https://github.com/sparklemotion/nokogiri) which doesn't support this feature.
5
+ Inspired by part of [Libxml-Ruby](https://github.com/xml4r/libxml-ruby).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'libxml-io'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install libxml-io
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'libxml/io'
27
+
28
+ class InputCbc
29
+ def initialize
30
+ @xsd = ['test_common.xsd']
31
+ end
32
+
33
+ def match filename
34
+ @xsd.include? filename
35
+ end
36
+
37
+ def query filename
38
+ case filename
39
+ when 'test_common.xsd'
40
+ GET_FILE
41
+ end
42
+ end
43
+ end
44
+
45
+ LibXML::IO::InputCallbacks.register
46
+ LibXML::IO::InputCallbacks.add_handler InputCbc.new
47
+
48
+ INTERACT_WITH_LIBXML
49
+
50
+ LibXML::IO::InputCallbacks.clear_handlers
51
+ LibXML::IO::InputCallbacks.pop
52
+ ```
53
+
54
+ ## License
55
+
56
+ MIT. See [`LICENSE.md`](LICENSE.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task default: :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.test_files = FileList['test/test*.rb']
9
+ t.verbose = true
10
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mkmf'
4
+
5
+ def crash(str)
6
+ printf(" extconf failure: %s\n", str)
7
+ exit 1
8
+ end
9
+
10
+ xc = with_config('xml2-config')
11
+ if xc
12
+ cflags = `#{xc} --cflags`.chomp
13
+ if $CHILD_STATUS
14
+ libs = `#{xc} --libs`.chomp
15
+ if $CHILD_STATUS
16
+ $CFLAGS += ' ' + cflags
17
+ $libs = libs + ' ' + $libs
18
+ end
19
+ end
20
+ else
21
+ dir_config('xml2')
22
+ end
23
+
24
+ unless find_header('libxml/xmlversion.h',
25
+ '/opt/include/libxml2',
26
+ '/opt/local/include/libxml2',
27
+ '/usr/local/include/libxml2',
28
+ '/usr/include/libxml2') &&
29
+ find_library('xml2', 'xmlParseDoc',
30
+ '/opt/lib',
31
+ '/opt/local/lib',
32
+ '/usr/local/lib',
33
+ '/usr/lib')
34
+ crash(<<EOL)
35
+ need libxml2.
36
+
37
+ Install the library or try one of the following options to extconf.rb:
38
+
39
+ --with-xml2-config=/path/to/xml2-config
40
+ --with-xml2-dir=/path/to/libxml2
41
+ --with-xml2-lib=/path/to/libxml2/lib
42
+ --with-xml2-include=/path/to/libxml2/include
43
+ EOL
44
+ end
45
+
46
+ $INCFLAGS << ' -I/usr/local/include'
47
+ $CFLAGS << ' ' << $INCFLAGS
48
+
49
+ create_header
50
+ create_makefile('libxml_io')
@@ -0,0 +1,189 @@
1
+ #include "ruby.h"
2
+
3
+ #include <libxml/parser.h>
4
+ #include <libxml/xmlreader.h>
5
+
6
+ static VALUE LibXMLIO = Qnil;
7
+
8
+ typedef struct libxml_io_doc_context {
9
+ char *buffer;
10
+ char *bpos;
11
+ int remaining;
12
+ } libxml_io_doc_context;
13
+
14
+ typedef struct libxml_io_handler {
15
+ VALUE class;
16
+ struct libxml_io_handler *next_handler;
17
+ } libxml_io_handler;
18
+
19
+ static libxml_io_handler *first_handler = 0;
20
+
21
+ static int libxml_io_registered = 0;
22
+
23
+ static int libxml_io_match(char const *filename)
24
+ {
25
+ libxml_io_handler *handler;
26
+ VALUE res;
27
+
28
+ handler = first_handler;
29
+ while (handler) {
30
+ res = rb_funcall(handler->class, rb_intern("match"), 1, rb_str_new2(filename));
31
+ if (!(RUBY_Qfalse == res || RUBY_Qnil == res || RUBY_Qundef == res)) {
32
+ return 1;
33
+ }
34
+ handler = handler->next_handler;
35
+ }
36
+ return 0;
37
+ }
38
+
39
+ static void* libxml_io_open(char const *filename)
40
+ {
41
+ libxml_io_handler *handler;
42
+ VALUE res;
43
+
44
+ handler = first_handler;
45
+ while (handler) {
46
+ res = rb_funcall(handler->class, rb_intern("query"), 1, rb_str_new2(filename));
47
+ if (!(RUBY_Qfalse == res || RUBY_Qnil == res || RUBY_Qundef == res || !strlen(StringValuePtr(res)))) {
48
+ libxml_io_doc_context *libxml_io_doc;
49
+ libxml_io_doc = (libxml_io_doc_context*)malloc(sizeof(libxml_io_doc_context));
50
+ libxml_io_doc->buffer = strdup(StringValuePtr(res));
51
+ libxml_io_doc->bpos = libxml_io_doc->buffer;
52
+ libxml_io_doc->remaining = (int)strlen(libxml_io_doc->buffer);
53
+ return libxml_io_doc;
54
+ }
55
+ handler = handler->next_handler;
56
+ }
57
+
58
+ return 0;
59
+ }
60
+
61
+ static int libxml_io_read(void *context, char *buffer, int len)
62
+ {
63
+ int ret_len;
64
+ libxml_io_doc_context *libxml_io_doc = (libxml_io_doc_context*)context;
65
+
66
+ if (len >= libxml_io_doc->remaining) {
67
+ ret_len = libxml_io_doc->remaining;
68
+ } else {
69
+ ret_len = len;
70
+ }
71
+ strncpy(buffer, libxml_io_doc->bpos, ret_len);
72
+ libxml_io_doc->bpos += ret_len;
73
+ libxml_io_doc->remaining -= ret_len;
74
+
75
+ return ret_len;
76
+ }
77
+
78
+ static int libxml_io_close(void *context)
79
+ {
80
+ ruby_xfree(((libxml_io_doc_context*)context)->buffer);
81
+ ruby_xfree(context);
82
+ return 1;
83
+ }
84
+
85
+ static VALUE libxml_io_register_input_callbacks()
86
+ {
87
+ if (libxml_io_registered) {
88
+ return Qfalse;
89
+ }
90
+ xmlRegisterInputCallbacks(libxml_io_match, libxml_io_open, libxml_io_read, libxml_io_close);
91
+ libxml_io_registered = 1;
92
+ return Qtrue;
93
+ }
94
+
95
+ static VALUE libxml_io_pop_input_callbacks()
96
+ {
97
+ if (!libxml_io_registered) {
98
+ return Qfalse;
99
+ }
100
+ xmlPopInputCallbacks();
101
+ libxml_io_registered = 0;
102
+ return Qtrue;
103
+ }
104
+
105
+ static VALUE libxml_io_add_handler(VALUE self, VALUE class)
106
+ {
107
+ libxml_io_handler *handler;
108
+
109
+ handler = (libxml_io_handler*)malloc(sizeof(libxml_io_handler));
110
+ handler->next_handler = 0;
111
+ handler->class = class;
112
+
113
+ if (!first_handler) {
114
+ first_handler = handler;
115
+ } else {
116
+ libxml_io_handler *pos;
117
+ pos = first_handler;
118
+ while (pos->next_handler) {
119
+ pos = pos->next_handler;
120
+ }
121
+ pos->next_handler = handler;
122
+ }
123
+
124
+ return Qtrue;
125
+ }
126
+
127
+ static VALUE libxml_io_remove_handler(VALUE self, VALUE class)
128
+ {
129
+ libxml_io_handler *save_handler, *handler;
130
+
131
+ if (!first_handler) {
132
+ return Qfalse;
133
+ }
134
+
135
+ if (class == first_handler->class) {
136
+ save_handler = first_handler->next_handler;
137
+ ruby_xfree(first_handler);
138
+ first_handler = save_handler;
139
+ return Qtrue;
140
+ }
141
+
142
+ handler = first_handler;
143
+ while (handler->next_handler) {
144
+ if (class == handler->next_handler->class) {
145
+ save_handler = handler->next_handler->next_handler;
146
+ ruby_xfree(handler->next_handler);
147
+ handler->next_handler = save_handler;
148
+ return Qtrue;
149
+ }
150
+ handler = handler->next_handler;
151
+ }
152
+ return Qfalse;
153
+ }
154
+
155
+ static VALUE libxml_io_clear_handlers(VALUE self)
156
+ {
157
+ libxml_io_handler *save_handler, *handler;
158
+
159
+ handler = first_handler;
160
+ while (handler) {
161
+ save_handler = handler;
162
+ handler = handler->next_handler;
163
+ ruby_xfree(save_handler);
164
+ }
165
+ first_handler = 0;
166
+ return Qtrue;
167
+ }
168
+
169
+ static void libxml_io_init_input_callbacks(void)
170
+ {
171
+ VALUE cInputCallbacks = rb_define_class_under(LibXMLIO, "InputCallbacks", rb_cObject);
172
+
173
+ rb_define_singleton_method(cInputCallbacks, "register",
174
+ libxml_io_register_input_callbacks, 0);
175
+ rb_define_singleton_method(cInputCallbacks, "pop",
176
+ libxml_io_pop_input_callbacks, 0);
177
+ rb_define_singleton_method(cInputCallbacks, "add_handler",
178
+ libxml_io_add_handler, 1);
179
+ rb_define_singleton_method(cInputCallbacks, "remove_handler",
180
+ libxml_io_remove_handler, 1);
181
+ rb_define_singleton_method(cInputCallbacks, "clear_handlers",
182
+ libxml_io_clear_handlers, 0);
183
+ }
184
+
185
+ void Init_libxml_io()
186
+ {
187
+ LibXMLIO = rb_define_module_under(rb_define_module("LibXML"), "IO");
188
+ libxml_io_init_input_callbacks();
189
+ }
data/lib/libxml/io.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'libxml/io/version'
2
+
3
+ begin
4
+ RUBY_VERSION =~ /(\d+.\d+)/
5
+ require "#{Regexp.last_match(1)}/libxml_io"
6
+ rescue LoadError
7
+ require 'libxml_io'
8
+ end
9
+
10
+ module LibXML
11
+ module IO
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module LibXML
2
+ module IO
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
data/libxml-io.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'libxml/io/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'libxml-io'
7
+ spec.version = LibXML::IO::VERSION
8
+ spec.authors = ['Anton Kozhemyachenko']
9
+ spec.email = ['gmerzu@gmail.com']
10
+
11
+ spec.homepage = 'https://github.com/gmerzu/libxml-io_gem'
12
+ spec.summary = 'LibXML xmlRegisterInputCallbacks extenstion for Ruby.'
13
+ spec.description = <<-EOS
14
+ Allow to register an I/O callback for handling parser input (xmlRegisterInputCallbacks).
15
+ Primary designed to use with Nokogiri which doesn't support this feature.
16
+ EOS
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'bin'
24
+ spec.extensions = ['ext/libxml-io/extconf.rb']
25
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.required_ruby_version = '>= 1.8.6'
29
+
30
+ spec.add_development_dependency 'bundler', '~> 1.16'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rake-compiler', '~> 1.0'
33
+ spec.add_development_dependency 'minitest', '~> 5.11'
34
+
35
+ spec.license = 'MIT'
36
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: libxml-io
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anton Kozhemyachenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.11'
69
+ description: |2
70
+ Allow to register an I/O callback for handling parser input (xmlRegisterInputCallbacks).
71
+ Primary designed to use with Nokogiri which doesn't support this feature.
72
+ email:
73
+ - gmerzu@gmail.com
74
+ executables:
75
+ - setup
76
+ extensions:
77
+ - ext/libxml-io/extconf.rb
78
+ extra_rdoc_files: []
79
+ files:
80
+ - ".gitignore"
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.md
84
+ - README.md
85
+ - Rakefile
86
+ - bin/setup
87
+ - ext/libxml-io/extconf.rb
88
+ - ext/libxml-io/libxml-io.c
89
+ - lib/libxml/io.rb
90
+ - lib/libxml/io/version.rb
91
+ - libxml-io.gemspec
92
+ homepage: https://github.com/gmerzu/libxml-io_gem
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 1.8.6
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.11
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: LibXML xmlRegisterInputCallbacks extenstion for Ruby.
116
+ test_files: []