sax-machine-nokogiri-1.4.4-safe 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require=./spec/spec_helper.rb
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'nokogiri', '>= 1.4.4'
4
+ gem 'rspec', '>= 2.4.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ nokogiri (1.4.4)
6
+ rspec (2.4.0)
7
+ rspec-core (~> 2.4.0)
8
+ rspec-expectations (~> 2.4.0)
9
+ rspec-mocks (~> 2.4.0)
10
+ rspec-core (2.4.0)
11
+ rspec-expectations (2.4.0)
12
+ diff-lcs (~> 1.1.2)
13
+ rspec-mocks (2.4.0)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ nokogiri (>= 1.4.4)
20
+ rspec (>= 2.4.0)
data/README.textile ADDED
@@ -0,0 +1,87 @@
1
+ h1. SAX Machine
2
+
3
+ "http://github.com/pauldix/sax-machine/wikis":http://github.com/pauldix/sax-machine/wikis
4
+
5
+ "http://github.com/pauldix/sax-machine/tree/master":http://github.com/pauldix/sax-machine/tree/master
6
+
7
+ h2. Description
8
+
9
+ A declarative SAX parsing library backed by Nokogiri
10
+
11
+ h2. Usage
12
+
13
+ <pre>
14
+ require 'sax-machine'
15
+
16
+ # Class for parsing an atom entry out of a feedburner atom feed
17
+ class AtomEntry
18
+ include SAXMachine
19
+ element :title
20
+ # the :as argument makes this available through atom_entry.author instead of .name
21
+ element :name, :as => :author
22
+ element "feedburner:origLink", :as => :url
23
+ element :summary
24
+ element :content
25
+ element :published
26
+ end
27
+
28
+ # Class for parsing Atom feeds
29
+ class Atom
30
+ include SAXMachine
31
+ element :title
32
+ # the :with argument means that you only match a link tag that has an attribute of :type => "text/html"
33
+ # the :value argument means that instead of setting the value to the text between the tag,
34
+ # it sets it to the attribute value of :href
35
+ element :link, :value => :href, :as => :url, :with => {:type => "text/html"}
36
+ element :link, :value => :href, :as => :feed_url, :with => {:type => "application/atom+xml"}
37
+ elements :entry, :as => :entries, :class => AtomEntry
38
+ end
39
+
40
+ # you can then parse like this
41
+ feed = Atom.parse(xml_text)
42
+ # then you're ready to rock
43
+ feed.title # => whatever the title of the blog is
44
+ feed.url # => the main url of the blog
45
+ feed.feed_url # => goes to the feedburner feed
46
+
47
+ feed.entries.first.title # => title of the first entry
48
+ feed.entries.first.author # => the author of the first entry
49
+ feed.entries.first.url # => the permalink on the blog for this entry
50
+ # etc ...
51
+
52
+ # you can also use the elements method without specifying a class like so
53
+ class SomeServiceResponse
54
+ elements :message, :as => :messages
55
+ end
56
+
57
+ response = SomeServiceResponse.parse("<response><message>hi</message><message>world</message></response>")
58
+ response.messages.first # => "hi"
59
+ response.messages.last # => "world"
60
+ </pre>
61
+
62
+ h2. LICENSE
63
+
64
+ (The MIT License)
65
+
66
+ Copyright (c) 2009:
67
+
68
+ "Paul Dix":http://pauldix.net
69
+
70
+ Permission is hereby granted, free of charge, to any person obtaining
71
+ a copy of this software and associated documentation files (the
72
+ 'Software'), to deal in the Software without restriction, including
73
+ without limitation the rights to use, copy, modify, merge, publish,
74
+ distribute, sublicense, and/or sell copies of the Software, and to
75
+ permit persons to whom the Software is furnished to do so, subject to
76
+ the following conditions:
77
+
78
+ The above copyright notice and this permission notice shall be
79
+ included in all copies or substantial portions of the Software.
80
+
81
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
82
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
83
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
84
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
85
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
86
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
87
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'lib/sax-machine.rb'
2
+
3
+ task :test do
4
+ sh 'rspec spec'
5
+ end
6
+
7
+ task :install do
8
+ rm_rf "*.gem"
9
+ puts `gem build sax-machine.gemspec`
10
+ puts `sudo gem install sax-machine-#{SAXMachine::VERSION}.gem`
11
+ end
@@ -0,0 +1,11 @@
1
+ require "rubygems"
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
4
+
5
+ require "sax-machine/sax_document"
6
+ require "sax-machine/sax_handler"
7
+ require "sax-machine/sax_config"
8
+
9
+ module SAXMachine
10
+ VERSION = "0.0.15"
11
+ end
@@ -0,0 +1,45 @@
1
+ module SAXMachine
2
+ class SAXConfig
3
+
4
+ class CollectionConfig
5
+ attr_reader :name
6
+
7
+ def initialize(name, options)
8
+ @name = name.to_s
9
+ @class = options[:class]
10
+ @as = options[:as].to_s
11
+
12
+ if options.has_key?(:with)
13
+ # for faster comparisons later
14
+ @with = options[:with].to_a.flatten.collect {|o| o.to_s}
15
+ else
16
+ @with = nil
17
+ end
18
+ end
19
+
20
+ def accessor
21
+ as
22
+ end
23
+
24
+ def attrs_match?(attrs)
25
+ if @with
26
+ @with == (@with & attrs)
27
+ else
28
+ true
29
+ end
30
+ end
31
+
32
+ def data_class
33
+ @class || @name
34
+ end
35
+
36
+ protected
37
+
38
+ def as
39
+ @as
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,47 @@
1
+ require "sax-machine/sax_element_config"
2
+ require "sax-machine/sax_collection_config"
3
+
4
+ module SAXMachine
5
+ class SAXConfig
6
+ attr_accessor :top_level_elements, :collection_elements
7
+
8
+ def initialize
9
+ @top_level_elements = {}
10
+ @collection_elements = {}
11
+ end
12
+
13
+ def columns
14
+ @top_level_elements.map {|name, ecs| ecs }.flatten
15
+ end
16
+
17
+ def initialize_copy(sax_config)
18
+ @top_level_elements = sax_config.top_level_elements.clone
19
+ @collection_elements = sax_config.collection_elements.clone
20
+ end
21
+
22
+ def add_top_level_element(name, options)
23
+ @top_level_elements[name.to_s] = [] unless @top_level_elements[name.to_s]
24
+ @top_level_elements[name.to_s] << ElementConfig.new(name, options)
25
+ end
26
+
27
+ def add_collection_element(name, options)
28
+ @collection_elements[name.to_s] = [] unless @collection_elements[name.to_s]
29
+ @collection_elements[name.to_s] << CollectionConfig.new(name, options)
30
+ end
31
+
32
+ def collection_config(name, attrs)
33
+ ces = @collection_elements[name.to_s]
34
+ ces && ces.detect { |cc| cc.attrs_match?(attrs) }
35
+ end
36
+
37
+ def element_configs_for_attribute(name, attrs)
38
+ tes = @top_level_elements[name.to_s]
39
+ tes && tes.select { |ec| ec.has_value_and_attrs_match?(attrs) } || []
40
+ end
41
+
42
+ def element_config_for_tag(name, attrs)
43
+ tes = @top_level_elements[name.to_s]
44
+ tes && tes.detect { |ec| ec.attrs_match?(attrs) }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,82 @@
1
+ require "nokogiri"
2
+
3
+ module SAXMachine
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ def parse(xml_text)
10
+ sax_handler = SAXHandler.new(self)
11
+ parser = Nokogiri::XML::SAX::Parser.new(sax_handler)
12
+ parser.parse(xml_text)
13
+ self
14
+ end
15
+
16
+ module ClassMethods
17
+
18
+ def parse(xml_text)
19
+ new.parse(xml_text)
20
+ end
21
+
22
+ def element(name, options = {})
23
+ options[:as] ||= name
24
+ sax_config.add_top_level_element(name, options)
25
+
26
+ # we only want to insert the getter and setter if they haven't defined it from elsewhere.
27
+ # this is how we allow custom parsing behavior. So you could define the setter
28
+ # and have it parse the string into a date or whatever.
29
+ attr_reader options[:as] unless instance_methods.include?(options[:as].to_s)
30
+ attr_writer options[:as] unless instance_methods.include?("#{options[:as]}=")
31
+ end
32
+
33
+ def columns
34
+ sax_config.columns
35
+ end
36
+
37
+ def column(sym)
38
+ columns.select{|c| c.column == sym}[0]
39
+ end
40
+
41
+ def data_class(sym)
42
+ column(sym).data_class
43
+ end
44
+
45
+ def required?(sym)
46
+ column(sym).required?
47
+ end
48
+
49
+ def column_names
50
+ columns.map{|e| e.column}
51
+ end
52
+
53
+ def elements(name, options = {})
54
+ options[:as] ||= name
55
+ if options[:class]
56
+ sax_config.add_collection_element(name, options)
57
+ else
58
+ class_eval <<-SRC
59
+ def add_#{options[:as]}(value)
60
+ #{options[:as]} << value
61
+ end
62
+ SRC
63
+ sax_config.add_top_level_element(name, options.merge(:collection => true))
64
+ end
65
+
66
+ if !instance_methods.include?(options[:as].to_s)
67
+ class_eval <<-SRC
68
+ def #{options[:as]}
69
+ @#{options[:as]} ||= []
70
+ end
71
+ SRC
72
+ end
73
+
74
+ attr_writer options[:as] unless instance_methods.include?("#{options[:as]}=")
75
+ end
76
+
77
+ def sax_config
78
+ @sax_config ||= SAXConfig.new
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,65 @@
1
+ module SAXMachine
2
+ class SAXConfig
3
+
4
+ class ElementConfig
5
+ attr_reader :name, :setter, :data_class, :collection
6
+
7
+ def initialize(name, options)
8
+ @name = name.to_s
9
+
10
+ if options.has_key?(:with)
11
+ # for faster comparisons later
12
+ @with = options[:with].to_a.flatten.collect {|o| o.to_s}
13
+ else
14
+ @with = nil
15
+ end
16
+
17
+ if options.has_key?(:value)
18
+ @value = options[:value].to_s
19
+ else
20
+ @value = nil
21
+ end
22
+
23
+ @as = options[:as]
24
+ @collection = options[:collection]
25
+
26
+ if @collection
27
+ @setter = "add_#{options[:as]}"
28
+ else
29
+ @setter = "#{@as}="
30
+ end
31
+ @data_class = options[:class]
32
+ @required = options[:required]
33
+ end
34
+
35
+ def column
36
+ @as || @name.to_sym
37
+ end
38
+
39
+ def required?
40
+ @required
41
+ end
42
+
43
+ def value_from_attrs(attrs)
44
+ attrs.index(@value) ? attrs[attrs.index(@value) + 1] : nil
45
+ end
46
+
47
+ def attrs_match?(attrs)
48
+ if @with
49
+ @with == (@with & attrs)
50
+ else
51
+ true
52
+ end
53
+ end
54
+
55
+ def has_value_and_attrs_match?(attrs)
56
+ !@value.nil? && attrs_match?(attrs)
57
+ end
58
+
59
+ def collection?
60
+ @collection
61
+ end
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,67 @@
1
+ require "nokogiri"
2
+
3
+ module SAXMachine
4
+ class SAXHandler < Nokogiri::XML::SAX::Document
5
+ attr_reader :stack
6
+
7
+ def initialize(object)
8
+ @stack = [[object, nil, ""]]
9
+ @parsed_configs = {}
10
+ end
11
+
12
+ def characters(string)
13
+ object, config, value = stack.last
14
+ value << string
15
+ end
16
+
17
+ def cdata_block(string)
18
+ characters(string)
19
+ end
20
+
21
+ def start_element(name, attrs = [])
22
+ attrs.flatten!
23
+ object, config, value = stack.last
24
+ sax_config = object.class.respond_to?(:sax_config) ? object.class.sax_config : nil
25
+
26
+ if sax_config
27
+ if collection_config = sax_config.collection_config(name, attrs)
28
+ stack.push [object = collection_config.data_class.new, collection_config, ""]
29
+ object, sax_config, is_collection = object, object.class.sax_config, true
30
+ end
31
+ sax_config.element_configs_for_attribute(name, attrs).each do |ec|
32
+ unless parsed_config?(object, ec)
33
+ object.send(ec.setter, ec.value_from_attrs(attrs))
34
+ mark_as_parsed(object, ec)
35
+ end
36
+ end
37
+ if !collection_config && element_config = sax_config.element_config_for_tag(name, attrs)
38
+ stack.push [element_config.data_class ? element_config.data_class.new : object, element_config, ""]
39
+ end
40
+ end
41
+ end
42
+
43
+ def end_element(name)
44
+ (object, tag_config, _), (element, config, value) = stack[-2..-1]
45
+ return unless stack.size > 1 && config && config.name.to_s == name.to_s
46
+
47
+ unless parsed_config?(object, config)
48
+ if config.respond_to?(:accessor)
49
+ object.send(config.accessor) << element
50
+ else
51
+ value = config.data_class ? element : value
52
+ object.send(config.setter, value) unless value == ""
53
+ mark_as_parsed(object, config)
54
+ end
55
+ end
56
+ stack.pop
57
+ end
58
+
59
+ def mark_as_parsed(object, element_config)
60
+ @parsed_configs[[object.object_id, element_config.object_id]] = true unless element_config.collection?
61
+ end
62
+
63
+ def parsed_config?(object, element_config)
64
+ @parsed_configs[[object.object_id, element_config.object_id]]
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,527 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "SAXMachine" do
4
+ describe "element" do
5
+ describe "when parsing a single element" do
6
+ before :each do
7
+ @klass = Class.new do
8
+ include SAXMachine
9
+ element :title
10
+ end
11
+ end
12
+
13
+ it "should provide an accessor" do
14
+ document = @klass.new
15
+ document.title = "Title"
16
+ document.title.should == "Title"
17
+ end
18
+
19
+ it "should allow introspection of the elements" do
20
+ @klass.column_names.should =~ [:title]
21
+ end
22
+
23
+ it "should not overwrite the setter if there is already one present" do
24
+ @klass = Class.new do
25
+ def title=(val)
26
+ @title = "#{val} **"
27
+ end
28
+ include SAXMachine
29
+ element :title
30
+ end
31
+ document = @klass.new
32
+ document.title = "Title"
33
+ document.title.should == "Title **"
34
+ end
35
+ describe "the class attribute" do
36
+ before(:each) do
37
+ @klass = Class.new do
38
+ include SAXMachine
39
+ element :date, :class => DateTime
40
+ end
41
+ @document = @klass.new
42
+ @document.date = DateTime.now.to_s
43
+ end
44
+ it "should be available" do
45
+ @klass.data_class(:date).should == DateTime
46
+ end
47
+ end
48
+ describe "the required attribute" do
49
+ it "should be available" do
50
+ @klass = Class.new do
51
+ include SAXMachine
52
+ element :date, :required => true
53
+ end
54
+ @klass.required?(:date).should be_true
55
+ end
56
+ end
57
+
58
+ it "should not overwrite the accessor when the element is not present" do
59
+ document = @klass.new
60
+ document.title = "Title"
61
+ document.parse("<foo></foo>")
62
+ document.title.should == "Title"
63
+ end
64
+
65
+ it "should overwrite the value when the element is present" do
66
+ document = @klass.new
67
+ document.title = "Old title"
68
+ document.parse("<title>New title</title>")
69
+ document.title.should == "New title"
70
+ end
71
+
72
+ it "should save the element text into an accessor" do
73
+ document = @klass.parse("<title>My Title</title>")
74
+ document.title.should == "My Title"
75
+ end
76
+
77
+ it "should save cdata into an accessor" do
78
+ document = @klass.parse("<title><![CDATA[A Title]]></title>")
79
+ document.title.should == "A Title"
80
+ end
81
+
82
+ it "should save the element text into an accessor when there are multiple elements" do
83
+ document = @klass.parse("<xml><title>My Title</title><foo>bar</foo></xml>")
84
+ document.title.should == "My Title"
85
+ end
86
+
87
+ it "should save the first element text when there are multiple of the same element" do
88
+ document = @klass.parse("<xml><title>My Title</title><title>bar</title></xml>")
89
+ document.title.should == "My Title"
90
+
91
+ end
92
+ end
93
+
94
+ describe "when parsing multiple elements" do
95
+ before :each do
96
+ @klass = Class.new do
97
+ include SAXMachine
98
+ element :title
99
+ element :name
100
+ end
101
+ end
102
+
103
+ it "should save the element text for a second tag" do
104
+ document = @klass.parse("<xml><title>My Title</title><name>Paul</name></xml>")
105
+ document.name.should == "Paul"
106
+ document.title.should == "My Title"
107
+ end
108
+ end
109
+
110
+ describe "when using options for parsing elements" do
111
+ describe "using the 'as' option" do
112
+ before :each do
113
+ @klass = Class.new do
114
+ include SAXMachine
115
+ element :description, :as => :summary
116
+ end
117
+ end
118
+
119
+ it "should provide an accessor using the 'as' name" do
120
+ document = @klass.new
121
+ document.summary = "a small summary"
122
+ document.summary.should == "a small summary"
123
+ end
124
+
125
+ it "should save the element text into the 'as' accessor" do
126
+ document = @klass.parse("<description>here is a description</description>")
127
+ document.summary.should == "here is a description"
128
+ end
129
+ end
130
+
131
+ describe "using the :with option" do
132
+ describe "and the :value option" do
133
+ before :each do
134
+ @klass = Class.new do
135
+ include SAXMachine
136
+ element :link, :value => :href, :with => {:foo => "bar"}
137
+ end
138
+ end
139
+
140
+ it "should save the value of a matching element" do
141
+ document = @klass.parse("<link href='test' foo='bar'>asdf</link>")
142
+ document.link.should == "test"
143
+ end
144
+
145
+ it "should save the value of the first matching element" do
146
+ document = @klass.parse("<xml><link href='first' foo='bar' /><link href='second' foo='bar' /></xml>")
147
+ document.link.should == "first"
148
+ end
149
+
150
+ describe "and the :as option" do
151
+ before :each do
152
+ @klass = Class.new do
153
+ include SAXMachine
154
+ element :link, :value => :href, :as => :url, :with => {:foo => "bar"}
155
+ element :link, :value => :href, :as => :second_url, :with => {:asdf => "jkl"}
156
+ end
157
+ end
158
+
159
+ it "should save the value of the first matching element" do
160
+ document = @klass.parse("<xml><link href='first' foo='bar' /><link href='second' asdf='jkl' /><link href='second' foo='bar' /></xml>")
161
+ document.url.should == "first"
162
+ document.second_url.should == "second"
163
+ end
164
+
165
+ end
166
+ end
167
+
168
+ describe "with only one element" do
169
+ before :each do
170
+ @klass = Class.new do
171
+ include SAXMachine
172
+ element :link, :with => {:foo => "bar"}
173
+ end
174
+ end
175
+
176
+ it "should save the text of an element that has matching attributes" do
177
+ document = @klass.parse("<link foo=\"bar\">match</link>")
178
+ document.link.should == "match"
179
+ end
180
+
181
+ it "should not save the text of an element that doesn't have matching attributes" do
182
+ document = @klass.parse("<link>no match</link>")
183
+ document.link.should be_nil
184
+ end
185
+
186
+ it "should save the text of an element that has matching attributes when it is the second of that type" do
187
+ document = @klass.parse("<xml><link>no match</link><link foo=\"bar\">match</link></xml>")
188
+ document.link.should == "match"
189
+
190
+ end
191
+
192
+ it "should save the text of an element that has matching attributes plus a few more" do
193
+ document = @klass.parse("<xml><link>no match</link><link asdf='jkl' foo='bar'>match</link>")
194
+ document.link.should == "match"
195
+ end
196
+ end
197
+
198
+ describe "with multiple elements of same tag" do
199
+ before :each do
200
+ @klass = Class.new do
201
+ include SAXMachine
202
+ element :link, :as => :first, :with => {:foo => "bar"}
203
+ element :link, :as => :second, :with => {:asdf => "jkl"}
204
+ end
205
+ end
206
+
207
+ it "should match the first element" do
208
+ document = @klass.parse("<xml><link>no match</link><link foo=\"bar\">first match</link><link>no match</link></xml>")
209
+ document.first.should == "first match"
210
+ end
211
+
212
+ it "should match the second element" do
213
+ document = @klass.parse("<xml><link>no match</link><link foo='bar'>first match</link><link asdf='jkl'>second match</link><link>hi</link></xml>")
214
+ document.second.should == "second match"
215
+ end
216
+ end
217
+ end # using the 'with' option
218
+
219
+ describe "using the 'value' option" do
220
+ before :each do
221
+ @klass = Class.new do
222
+ include SAXMachine
223
+ element :link, :value => :foo
224
+ end
225
+ end
226
+
227
+ it "should save the attribute value" do
228
+ document = @klass.parse("<link foo='test'>hello</link>")
229
+ document.link.should == 'test'
230
+ end
231
+
232
+ it "should save the attribute value when there is no text enclosed by the tag" do
233
+ document = @klass.parse("<link foo='test'></link>")
234
+ document.link.should == 'test'
235
+ end
236
+
237
+ it "should save the attribute value when the tag close is in the open" do
238
+ document = @klass.parse("<link foo='test'/>")
239
+ document.link.should == 'test'
240
+ end
241
+
242
+ it "should save two different attribute values on a single tag" do
243
+ @klass = Class.new do
244
+ include SAXMachine
245
+ element :link, :value => :foo, :as => :first
246
+ element :link, :value => :bar, :as => :second
247
+ end
248
+ document = @klass.parse("<link foo='foo value' bar='bar value'></link>")
249
+ document.first.should == "foo value"
250
+ document.second.should == "bar value"
251
+ end
252
+
253
+ it "should not fail if one of the attribute hasn't been defined" do
254
+ @klass = Class.new do
255
+ include SAXMachine
256
+ element :link, :value => :foo, :as => :first
257
+ element :link, :value => :bar, :as => :second
258
+ end
259
+ document = @klass.parse("<link foo='foo value'></link>")
260
+ document.first.should == "foo value"
261
+ document.second.should be_nil
262
+ end
263
+ end
264
+
265
+ describe "when desiring both the content and attributes of an element" do
266
+ before :each do
267
+ @klass = Class.new do
268
+ include SAXMachine
269
+ element :link
270
+ element :link, :value => :foo, :as => :link_foo
271
+ element :link, :value => :bar, :as => :link_bar
272
+ end
273
+ end
274
+
275
+ it "should parse the element and attribute values" do
276
+ document = @klass.parse("<link foo='test1' bar='test2'>hello</link>")
277
+ document.link.should == 'hello'
278
+ document.link_foo.should == 'test1'
279
+ document.link_bar.should == 'test2'
280
+ end
281
+ end
282
+
283
+ end
284
+ end
285
+
286
+ describe "elements" do
287
+ describe "when parsing multiple elements" do
288
+ before :each do
289
+ @klass = Class.new do
290
+ include SAXMachine
291
+ elements :entry, :as => :entries
292
+ end
293
+ end
294
+
295
+ it "should provide a collection accessor" do
296
+ document = @klass.new
297
+ document.entries << :foo
298
+ document.entries.should == [:foo]
299
+ end
300
+
301
+ it "should parse a single element" do
302
+ document = @klass.parse("<entry>hello</entry>")
303
+ document.entries.should == ["hello"]
304
+ end
305
+
306
+ it "should parse multiple elements" do
307
+ document = @klass.parse("<xml><entry>hello</entry><entry>world</entry></xml>")
308
+ document.entries.should == ["hello", "world"]
309
+ end
310
+
311
+ it "should parse multiple elements when taking an attribute value" do
312
+ attribute_klass = Class.new do
313
+ include SAXMachine
314
+ elements :entry, :as => :entries, :value => :foo
315
+ end
316
+ doc = attribute_klass.parse("<xml><entry foo='asdf' /><entry foo='jkl' /></xml>")
317
+ doc.entries.should == ["asdf", "jkl"]
318
+ end
319
+ end
320
+
321
+ describe "when using the with and class options" do
322
+ before :each do
323
+ class Bar
324
+ include SAXMachine
325
+ element :title
326
+ end
327
+
328
+ class Foo
329
+ include SAXMachine
330
+ element :title
331
+ end
332
+
333
+ class Item
334
+ include SAXMachine
335
+
336
+ end
337
+ @klass = Class.new do
338
+ include SAXMachine
339
+ elements :item, :as => :items, :with => {:type => 'Bar'}, :class => Bar
340
+ elements :item, :as => :items, :with => {:type => 'Foo'}, :class => Foo
341
+ end
342
+ end
343
+
344
+ it "should cast into the correct class" do
345
+ document = @klass.parse("<items><item type=\"Bar\"><title>Bar title</title></item><item type=\"Foo\"><title>Foo title</title></item></items>")
346
+ document.items.size.should == 2
347
+ document.items.first.should be_a(Bar)
348
+ document.items.first.title.should == "Bar title"
349
+ document.items.last.should be_a(Foo)
350
+ document.items.last.title.should == "Foo title"
351
+ end
352
+ end
353
+
354
+ describe "when using the class option" do
355
+ before :each do
356
+ class Foo
357
+ include SAXMachine
358
+ element :title
359
+ end
360
+ @klass = Class.new do
361
+ include SAXMachine
362
+ elements :entry, :as => :entries, :class => Foo
363
+ end
364
+ end
365
+
366
+ it "should parse a single element with children" do
367
+ document = @klass.parse("<entry><title>a title</title></entry>")
368
+ document.entries.size.should == 1
369
+ document.entries.first.title.should == "a title"
370
+ end
371
+
372
+ it "should parse multiple elements with children" do
373
+ document = @klass.parse("<xml><entry><title>title 1</title></entry><entry><title>title 2</title></entry></xml>")
374
+ document.entries.size.should == 2
375
+ document.entries.first.title.should == "title 1"
376
+ document.entries.last.title.should == "title 2"
377
+ end
378
+
379
+ it "should not parse a top level element that is specified only in a child" do
380
+ document = @klass.parse("<xml><title>no parse</title><entry><title>correct title</title></entry></xml>")
381
+ document.entries.size.should == 1
382
+ document.entries.first.title.should == "correct title"
383
+ end
384
+
385
+ it "should parse out an attribute value from the tag that starts the collection" do
386
+ class Foo
387
+ element :entry, :value => :href, :as => :url
388
+ end
389
+ document = @klass.parse("<xml><entry href='http://pauldix.net'><title>paul</title></entry></xml>")
390
+ document.entries.size.should == 1
391
+ document.entries.first.title.should == "paul"
392
+ document.entries.first.url.should == "http://pauldix.net"
393
+ end
394
+ end
395
+
396
+ end
397
+
398
+ describe "full example" do
399
+ before :each do
400
+ @xml = File.read('spec/sax-machine/atom.xml')
401
+ class AtomEntry
402
+ include SAXMachine
403
+ element :title
404
+ element :name, :as => :author
405
+ element "feedburner:origLink", :as => :url
406
+ element :summary
407
+ element :content
408
+ element :published
409
+ end
410
+
411
+ class Atom
412
+ include SAXMachine
413
+ element :title
414
+ element :link, :value => :href, :as => :url, :with => {:type => "text/html"}
415
+ element :link, :value => :href, :as => :feed_url, :with => {:type => "application/atom+xml"}
416
+ elements :entry, :as => :entries, :class => AtomEntry
417
+ end
418
+ end # before
419
+
420
+ it "should parse the url" do
421
+ f = Atom.parse(@xml)
422
+ f.url.should == "http://www.pauldix.net/"
423
+ end
424
+ end
425
+
426
+ describe "parsing a tree" do
427
+ before do
428
+ @xml = %[
429
+ <categories>
430
+ <category id="1">
431
+ <title>First</title>
432
+ <categories>
433
+ <category id="2">
434
+ <title>Second</title>
435
+ </category>
436
+ </categories>
437
+ </category>
438
+ </categories>
439
+ ]
440
+ class CategoryCollection; end
441
+ class Category
442
+ include SAXMachine
443
+ attr_accessor :id
444
+ element :category, :value => :id, :as => :id
445
+ element :title
446
+ element :categories, :as => :collection, :class => CategoryCollection
447
+ end
448
+ class CategoryCollection
449
+ include SAXMachine
450
+ elements :category, :as => :categories, :class => Category
451
+ end
452
+ @collection = CategoryCollection.parse(@xml)
453
+ end
454
+
455
+ it "should parse the first category" do
456
+ @collection.categories.first.id.should == "1"
457
+ @collection.categories.first.title.should == "First"
458
+ end
459
+
460
+ it "should parse the nested category" do
461
+ @collection.categories.first.collection.categories.first.id.should == "2"
462
+ @collection.categories.first.collection.categories.first.title.should == "Second"
463
+ end
464
+ end
465
+
466
+ describe "parsing a tree without a collection class" do
467
+ before do
468
+ @xml = %[
469
+ <categories>
470
+ <category id="1">
471
+ <title>First</title>
472
+ <categories>
473
+ <category id="2">
474
+ <title>Second</title>
475
+ </category>
476
+ </categories>
477
+ </category>
478
+ </categories>
479
+ ]
480
+ class CategoryTree
481
+ include SAXMachine
482
+ attr_accessor :id
483
+ element :category, :value => :id, :as => :id
484
+ element :title
485
+ elements :category, :as => :categories, :class => CategoryTree
486
+ end
487
+ @collection = CategoryTree.parse(@xml)
488
+ end
489
+
490
+ it "should parse the first category" do
491
+ @collection.categories.first.id.should == "1"
492
+ @collection.categories.first.title.should == "First"
493
+ end
494
+
495
+ it "should parse the nested category" do
496
+ @collection.categories.first.categories.first.id.should == "2"
497
+ @collection.categories.first.categories.first.title.should == "Second"
498
+ end
499
+ end
500
+
501
+ describe "with element deeper inside the xml structure" do
502
+ before do
503
+ @xml = %[
504
+ <item id="1">
505
+ <texts>
506
+ <title>Hello</title>
507
+ </texts>
508
+ </item>
509
+ ]
510
+ @klass = Class.new do
511
+ include SAXMachine
512
+ attr_accessor :id
513
+ element :item, :value => "id", :as => :id
514
+ element :title
515
+ end
516
+ @item = @klass.parse(@xml)
517
+ end
518
+
519
+ it "should have an id" do
520
+ @item.id.should == "1"
521
+ end
522
+
523
+ it "should have a title" do
524
+ @item.title.should == "Hello"
525
+ end
526
+ end
527
+ end
@@ -0,0 +1,12 @@
1
+ require 'date'
2
+
3
+ # gem install redgreen for colored test output
4
+ begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end
5
+
6
+ path = File.expand_path(File.dirname(__FILE__) + "/../lib/")
7
+ $LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
8
+
9
+ require "lib/sax-machine"
10
+
11
+ # Spec::Runner.configure do |config|
12
+ # end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sax-machine-nokogiri-1.4.4-safe
3
+ version: !ruby/object:Gem::Version
4
+ hash: 1
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 15
10
+ version: 0.0.15
11
+ platform: ruby
12
+ authors:
13
+ - Paul Dix
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2009-01-13 00:00:00 +11:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: nokogiri
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">"
28
+ - !ruby/object:Gem::Version
29
+ hash: 31
30
+ segments:
31
+ - 0
32
+ - 0
33
+ - 0
34
+ version: 0.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description:
38
+ email: paul@pauldix.net
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - lib/sax-machine.rb
47
+ - lib/sax-machine/sax_config.rb
48
+ - lib/sax-machine/sax_collection_config.rb
49
+ - lib/sax-machine/sax_element_config.rb
50
+ - lib/sax-machine/sax_document.rb
51
+ - lib/sax-machine/sax_handler.rb
52
+ - README.textile
53
+ - Rakefile
54
+ - .rspec
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - spec/spec_helper.rb
58
+ - spec/sax-machine/sax_document_spec.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/pauldix/sax-machine
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options: []
65
+
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.7
90
+ signing_key:
91
+ specification_version: 2
92
+ summary: Declarative SAX Parsing with Nokogiri
93
+ test_files: []
94
+