astro-sax-machine 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,16 +8,25 @@ module SAXMachine
8
8
  @name = name.to_s
9
9
  @class = options[:class]
10
10
  @as = options[:as].to_s
11
+ @xmlns = case options[:xmlns]
12
+ when Array then options[:xmlns]
13
+ when String then [options[:xmlns]]
14
+ else nil
15
+ end
11
16
  end
12
17
 
13
- def handler
14
- SAXHandler.new(@class.new)
18
+ def handler(nsstack)
19
+ SAXHandler.new(@class.new, nsstack)
15
20
  end
16
21
 
17
22
  def accessor
18
23
  as
19
24
  end
20
25
 
26
+ def xmlns_match?(ns)
27
+ @xmlns.nil? || @xmlns.include?(ns)
28
+ end
29
+
21
30
  protected
22
31
 
23
32
  def as
@@ -30,4 +39,4 @@ module SAXMachine
30
39
  end
31
40
 
32
41
  end
33
- end
42
+ end
@@ -18,11 +18,19 @@ module SAXMachine
18
18
  @collection_elements << CollectionConfig.new(name, options)
19
19
  end
20
20
 
21
- def collection_config(name)
22
- @collection_elements.detect { |ce| ce.name.to_s == name.to_s }
21
+ def collection_config(name, nsstack)
22
+ prefix, name = name.split(':', 2)
23
+ prefix, name = nil, prefix unless name # No prefix
24
+ namespace = nsstack[prefix]
25
+
26
+ @collection_elements.detect { |ce|
27
+ ce.name.to_s == name.to_s &&
28
+ ce.xmlns_match?(namespace)
29
+ }
23
30
  end
24
31
 
25
32
  def element_configs_for_attribute(name, attrs)
33
+ name = name.split(':', 2).last
26
34
  @top_level_elements.select do |element_config|
27
35
  element_config.name == name &&
28
36
  element_config.has_value_and_attrs_match?(attrs)
@@ -5,9 +5,9 @@ module SAXMachine
5
5
  class SAXHandler < Nokogiri::XML::SAX::Document
6
6
  attr_reader :object
7
7
 
8
- def initialize(object)
8
+ def initialize(object, nsstack=nil)
9
9
  @object = object
10
- @nsstack = nil
10
+ @nsstack = nsstack || NSStack.new
11
11
  end
12
12
 
13
13
  def characters(string)
@@ -23,7 +23,7 @@ module SAXMachine
23
23
  end
24
24
 
25
25
  def start_element(name, attrs = [])
26
-
26
+
27
27
  @name = name
28
28
  @attrs = attrs.map { |a| SAXHandler.decode_xml(a) }
29
29
  @nsstack = NSStack.new(@nsstack, @attrs)
@@ -31,8 +31,8 @@ module SAXMachine
31
31
  if parsing_collection?
32
32
  @collection_handler.start_element(@name, @attrs)
33
33
 
34
- elsif @collection_config = sax_config.collection_config(@name)
35
- @collection_handler = @collection_config.handler
34
+ elsif @collection_config = sax_config.collection_config(@name, @nsstack)
35
+ @collection_handler = @collection_config.handler(@nsstack)
36
36
  @collection_handler.start_element(@name, @attrs)
37
37
 
38
38
  elsif (element_configs = sax_config.element_configs_for_attribute(@name, @attrs)).any?
@@ -46,6 +46,7 @@ module SAXMachine
46
46
 
47
47
  def end_element(name)
48
48
  if parsing_collection? && @collection_config.name == name
49
+ @collection_handler.end_element(name)
49
50
  @object.send(@collection_config.accessor) << @collection_handler.object
50
51
  reset_current_collection
51
52
 
@@ -326,6 +326,11 @@ describe "SAXMachine" do
326
326
  document = @klass.parse("<x:b>hello</x:b>")
327
327
  document.b.should == 'hello'
328
328
  end
329
+
330
+ it "should get the namespaced element even it's not first" do
331
+ document = @klass.parse("<root xmlns:a='urn:test'><a>foo</a><a>foo</a><a:a>bar</a:a></root>")
332
+ document.a.should == 'bar'
333
+ end
329
334
  end
330
335
 
331
336
  end
@@ -410,13 +415,16 @@ describe "SAXMachine" do
410
415
  end
411
416
 
412
417
  describe "full example" do
418
+ XMLNS_ATOM = "http://www.w3.org/2005/Atom"
419
+ XMLNS_FEEDBURNER = "http://rssnamespace.org/feedburner/ext/1.0"
420
+
413
421
  before :each do
414
422
  @xml = File.read('spec/sax-machine/atom.xml')
415
423
  class AtomEntry
416
424
  include SAXMachine
417
425
  element :title
418
426
  element :name, :as => :author
419
- element "feedburner:origLink", :as => :url
427
+ element :origLink, :as => :orig_link, :xmlns => XMLNS_FEEDBURNER
420
428
  element :summary
421
429
  element :content
422
430
  element :published
@@ -427,7 +435,7 @@ describe "SAXMachine" do
427
435
  element :title
428
436
  element :link, :value => :href, :as => :url, :with => {:type => "text/html"}
429
437
  element :link, :value => :href, :as => :feed_url, :with => {:type => "application/atom+xml"}
430
- elements :entry, :as => :entries, :class => AtomEntry
438
+ elements :entry, :as => :entries, :class => AtomEntry, :xmlns => XMLNS_ATOM
431
439
  end
432
440
  end # before
433
441
 
@@ -435,5 +443,15 @@ describe "SAXMachine" do
435
443
  f = Atom.parse(@xml)
436
444
  f.url.should == "http://www.pauldix.net/"
437
445
  end
446
+
447
+ it "should parse all entries" do
448
+ f = Atom.parse(@xml)
449
+ f.entries.length.should == 5
450
+ end
451
+
452
+ it "should parse the feedburner:origLink" do
453
+ f = Atom.parse(@xml)
454
+ f.entries[0].orig_link.should == 'http://www.pauldix.net/2008/09/marshal-data-to.html'
455
+ end
438
456
  end
439
457
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: astro-sax-machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix