multisax 0.0.0.3 → 0.0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt CHANGED
@@ -1,6 +1,4 @@
1
- Copyright (c) 2013, cielavenir
2
-
3
- All rights reserved.
1
+ - Copyright (c) 2013, T. Yamada
4
2
 
5
3
  Redistribution and use in source and binary forms, with or without
6
4
  modification, are permitted provided that the following conditions are met:
@@ -22,9 +20,9 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
20
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23
21
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
22
 
25
- A part of Nokogiri source code is cited to convert Nokogiri::XML::SAX::Document into module.
23
+ - A part of Nokogiri source code is cited to convert Nokogiri::XML::SAX::Document into module.
26
24
 
27
- Copyright © 2008 - 2012: Aaron Patterson, Mike Dalessio, Charles Nutter, Sergio Arbeo, Patrick Mahoney, Yoko Harada
25
+ - Copyright © 2008 - 2012: Aaron Patterson, Mike Dalessio, Charles Nutter, Sergio Arbeo, Patrick Mahoney, Yoko Harada
28
26
 
29
27
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
30
28
 
@@ -34,7 +32,7 @@ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
34
32
 
35
33
  Note: MultiSAX shouldn't load any copyleft libraries.
36
34
 
37
- Ruby (REXML): 2-clause BSD License (or Artistic License)
38
- Nokogiri: MIT License
39
- libxml-ruby: MIT License
40
- Ox: 3-clause BSD License
35
+ - Ruby (REXML): 2-clause BSD License (or Artistic License)
36
+ - Nokogiri: MIT License
37
+ - libxml-ruby: MIT License
38
+ - Ox: 3-clause BSD License
@@ -2,33 +2,32 @@
2
2
 
3
3
  * Please check spec/multisax.spec as an example.
4
4
  * Complex usage:
5
- ```ruby
6
- listener=MultiSAX::Sax.parse(xml,Class.new{
7
- include MultiSAX::Callbacks
8
- def initialize
9
- @content=Hash.new{|h,k|h[k]=[]}
10
- @current_tag=[]
11
- end
12
- attr_reader :content
5
+ require 'multisax'
6
+ listener=MultiSAX::Sax.parse(xml,Class.new{
7
+ include MultiSAX::Callbacks
8
+ def initialize
9
+ @content=Hash.new{|h,k|h[k]=[]}
10
+ @current_tag=[]
11
+ end
12
+ attr_reader :content
13
13
 
14
- def sax_tag_start(tag,attrs)
15
- @current_tag.push(tag)
16
- end
17
- def sax_tag_end(tag)
18
- if (t=@current_tag.pop)!=tag then raise "xml is malformed /#{t}" end
19
- end
20
- def sax_cdata(text)
21
- @content[@current_tag.last] << text
22
- end
23
- def sax_text(text)
24
- text.strip!
25
- @content[@current_tag.last] << text if text.size>0
26
- end
27
- def sax_comment(text)
28
- end
29
- }.new)
30
- listener.content.each{...}
31
- ```
14
+ def sax_tag_start(tag,attrs)
15
+ @current_tag.push(tag)
16
+ end
17
+ def sax_tag_end(tag)
18
+ if (t=@current_tag.pop)!=tag then raise "xml is malformed /#{t}" end
19
+ end
20
+ def sax_cdata(text)
21
+ @content[@current_tag.last] << text
22
+ end
23
+ def sax_text(text)
24
+ text.strip!
25
+ @content[@current_tag.last] << text if text.size>0
26
+ end
27
+ def sax_comment(text)
28
+ end
29
+ }.new)
30
+ listener.content.each{...}
32
31
 
33
32
  * Optional XML libraries:
34
33
  * gem install ox
@@ -47,5 +46,5 @@ listener.content.each{...}
47
46
 
48
47
  == Copyright
49
48
 
50
- Copyright (c) 2013 cielavenir. See LICENSE.txt for
49
+ Copyright (c) 2013 T. Yamada. See LICENSE.txt for
51
50
  further details.
data/Rakefile CHANGED
@@ -44,6 +44,8 @@ Rake::RDocTask.new do |rdoc|
44
44
 
45
45
  rdoc.rdoc_dir = 'rdoc'
46
46
  rdoc.title = "multisax #{version}"
47
+ rdoc.main = 'README.rdoc'
47
48
  rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('LICENSE*')
48
50
  rdoc.rdoc_files.include('lib/**/*.rb')
49
51
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0.3
1
+ 0.0.0.4
data/lib/multisax.rb CHANGED
@@ -1,14 +1,23 @@
1
- #MultiSAX: Ruby Gem to handle multiple SAX libraries
2
- #Copyright (c) 2013, T. Yamada All rights reserved under 2-clause BSDL.
3
- #Check LICENSE terms.
4
- #Note: MIT License is also applicable if that compresses LICENSE file.
1
+ # MultiSAX: Ruby Gem to handle multiple SAX libraries
2
+ #
3
+ # Copyright (c) 2013, T. Yamada All rights reserved under 2-clause BSDL.
4
+ #
5
+ # Check LICENSE terms.
6
+ #
7
+ # Note: MIT License is also applicable if that compresses LICENSE file.
5
8
 
6
9
  module MultiSAX
7
10
  #VERSION=''
8
11
 
12
+ # The class to handle XML libraries.
9
13
  class Sax
10
14
  @@parser=nil
11
15
  @@saxmodule=nil
16
+ # Library loader.
17
+ # Arguments are list (or Array) of libraries.
18
+ # Currently the following are supported (order by speed):
19
+ # :ox, :libxml, :nokogiri, :rexmlstream, :rexmlsax2
20
+ # If multiple selected, MultiSAX will try the libraries one by one and use the first usable one.
12
21
  def self.open(*list)
13
22
  return @@parser if @@parser
14
23
  list=[:ox,:libxml,:nokogiri,:rexmlstream,:rexmlsax2] if list.size==0
@@ -61,10 +70,19 @@ module MultiSAX
61
70
  }
62
71
  return @@parser
63
72
  end
73
+ # Reset MultiSAX state so that you can re-open() another library.
64
74
  def self.reset() @@parser=nil;@@saxmodule=nil end
75
+ # Returns which module is actually chosen.
65
76
  def self.parser() @@parser end
66
77
 
78
+ #--
67
79
  #def initialize(listener)
80
+ #++
81
+ # The main parsing method.
82
+ # Listener can be Class.new{include MultiSAX::Callbacks}.new. Returns the listener after SAX is applied.
83
+ # If you have not called open(), this will call it using default value (all libraries).
84
+ # SAX's listeners are usually modified destructively.
85
+ # So instances shouldn't be provided.
68
86
  def self.parse(body,listener)
69
87
  #self.class.open if !@@parser
70
88
  self.open if !@@parser
@@ -76,33 +94,40 @@ module MultiSAX
76
94
  when :ox
77
95
  @listener.instance_eval{
78
96
  extend @@saxmodule
79
- @saxwrapper_attr=[:xmldecl]
97
+ @saxwrapper_attr=[]
80
98
  def start_element(tag)
99
+ # I hope provided Listener's sax_tag_start will NOT be used elsewhere.
100
+ alias :attrs_done :attrs_done_normal
81
101
  @saxwrapper_tag=tag
82
102
  @saxwrapper_attr=[]
83
103
  end
104
+ # These "instance methods" are actually injected to listener class using instance_eval.
105
+ # i.e. not APIs. You cannot call these methods from outside.
84
106
  def attr(name,str)
85
107
  @saxwrapper_attr<<[name,str]
86
108
  end
109
+ #--
87
110
  #alias :attr_value :attr
88
- def attrs_done
89
- if @saxwrapper_attr.first==:xmldecl
90
- @saxwrapper_attr.shift
91
- version=@saxwrapper_attr.assoc(:version)
92
- version&&=version[1]
93
- encoding=@saxwrapper_attr.assoc(:encoding)
94
- encoding&&=encoding[1]
95
- standalone=@saxwrapper_attr.assoc(:standalone)
96
- standalone&&=standalone[1]
97
- sax_xmldecl(version,encoding,standalone)
98
- else
99
- sax_tag_start(@saxwrapper_tag.to_s,@saxwrapper_attr)
100
- end
111
+ #++
112
+ def attrs_done_xmldecl
113
+ version=@saxwrapper_attr.assoc(:version)
114
+ version&&=version[1]
115
+ encoding=@saxwrapper_attr.assoc(:encoding)
116
+ encoding&&=encoding[1]
117
+ standalone=@saxwrapper_attr.assoc(:standalone)
118
+ standalone&&=standalone[1]
119
+ sax_xmldecl(version,encoding,standalone)
101
120
  end
121
+ def attrs_done_normal
122
+ sax_tag_start(@saxwrapper_tag.to_s,@saxwrapper_attr)
123
+ end
124
+ alias :attrs_done :attrs_done_xmldecl
102
125
  def end_element(tag) sax_tag_end(tag.to_s) end
103
126
  alias :cdata :sax_cdata
104
127
  alias :text :sax_text
128
+ #--
105
129
  #alias :value :sax_text
130
+ #++
106
131
  alias :comment :sax_comment
107
132
  }
108
133
  when :libxml
@@ -161,8 +186,13 @@ module MultiSAX
161
186
  @listener
162
187
  end
163
188
  end
189
+
190
+ ##
191
+ # MultiSAX callbacks.
192
+ # MultiSAX::Sax listener should include this module.
164
193
  module Callbacks
165
- # https://github.com/sparklemotion/nokogiri/blob/master/lib/nokogiri/xml/sax/document.rb
194
+ # Cited from Nokogiri to convert Nokogiri::XML::SAX::Document into module.
195
+ # https://github.com/sparklemotion/nokogiri/blob/master/lib/nokogiri/xml/sax/document.rb
166
196
  def sax_start_element_namespace name, attrs = [], prefix = nil, uri = nil, ns = []
167
197
  # Deal with SAX v1 interface
168
198
  name = [prefix, name].compact.join(':')
data/multisax.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "multisax"
8
- s.version = "0.0.0.3"
8
+ s.version = "0.0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["cielavenir"]
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.email = "cielartisan@gmail.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
- "README.md"
17
+ "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
22
22
  "Gemfile",
23
23
  "Gemfile.lock",
24
24
  "LICENSE.txt",
25
- "README.md",
25
+ "README.rdoc",
26
26
  "Rakefile",
27
27
  "VERSION",
28
28
  "lib/multisax.rb",
@@ -15,6 +15,8 @@ class MultiSAXTester
15
15
  text.strip!
16
16
  @result<<text if text.size>0
17
17
  end
18
+ #def sax_xmldecl(version,encoding,standalone)
19
+ #end
18
20
  attr_reader :result
19
21
  end
20
22
  input_xml=<<"EOM"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multisax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0.3
4
+ version: 0.0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -81,14 +81,14 @@ executables: []
81
81
  extensions: []
82
82
  extra_rdoc_files:
83
83
  - LICENSE.txt
84
- - README.md
84
+ - README.rdoc
85
85
  files:
86
86
  - .document
87
87
  - .rspec
88
88
  - Gemfile
89
89
  - Gemfile.lock
90
90
  - LICENSE.txt
91
- - README.md
91
+ - README.rdoc
92
92
  - Rakefile
93
93
  - VERSION
94
94
  - lib/multisax.rb
@@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
110
  version: '0'
111
111
  segments:
112
112
  - 0
113
- hash: -2850175293617683642
113
+ hash: 4112567294364414967
114
114
  required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  none: false
116
116
  requirements: