julien51-sax-machine 0.0.18 → 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ module SAXMachine
3
3
 
4
4
  class CollectionConfig
5
5
  attr_reader :name
6
+ attr_reader :default_xmlns
6
7
 
7
8
  def initialize(name, options)
8
9
  @name = name.to_s
@@ -13,9 +14,17 @@ module SAXMachine
13
14
  when String then [options[:xmlns]]
14
15
  else nil
15
16
  end
17
+ @default_xmlns = options[:default_xmlns]
18
+ if @default_xmlns && @xmlns && !@xmlns.include?('')
19
+ @xmlns << ''
20
+ end
16
21
  end
17
22
 
18
23
  def handler(nsstack)
24
+ if nsstack.nil? || nsstack[''] == ''
25
+ nsstack = NSStack.new(nsstack, nsstack)
26
+ nsstack[''] = @default_xmlns
27
+ end
19
28
  SAXHandler.new(@class.new, nsstack)
20
29
  end
21
30
 
data/lib/sax-machine.rb CHANGED
@@ -7,5 +7,5 @@ require "sax-machine/sax_handler"
7
7
  require "sax-machine/sax_config"
8
8
 
9
9
  module SAXMachine
10
- VERSION = "0.0.18"
11
- end
10
+ VERSION = "0.0.19"
11
+ end
@@ -331,6 +331,41 @@ describe "SAXMachine" do
331
331
  document = @klass.parse("<root xmlns:a='urn:test'><a>foo</a><a>foo</a><a:a>bar</a:a></root>")
332
332
  document.a.should == 'bar'
333
333
  end
334
+
335
+ it "should parse multiple namespaces" do
336
+ klass = Class.new do
337
+ include SAXMachine
338
+ element :a, :xmlns => 'urn:test'
339
+ element :b, :xmlns => 'urn:test2'
340
+ end
341
+ document = klass.parse("<root xmlns='urn:test' xmlns:b='urn:test2'><b:b>bar</b:b><a>foo</a></root>")
342
+ document.a.should == 'foo'
343
+ document.b.should == 'bar'
344
+ end
345
+
346
+ context "when passing a default namespace" do
347
+ before :each do
348
+ @xmlns = 'urn:test'
349
+ class Inner
350
+ include SAXMachine
351
+ element :a, :xmlns => @xmlns
352
+ end
353
+ @outer = Class.new do
354
+ include SAXMachine
355
+ elements :root, :default_xmlns => @xmlns, :class => Inner
356
+ end
357
+ end
358
+
359
+ it "should replace the empty namespace with a default" do
360
+ document = @outer.parse("<root><a>Hello</a></root>")
361
+ document.root[0].a.should == 'Hello'
362
+ end
363
+
364
+ it "should not replace another namespace" do
365
+ document = @outer.parse("<root xmlns='urn:test2'><a>Hello</a></root>")
366
+ document.root[0].a.should == 'Hello'
367
+ end
368
+ end
334
369
  end
335
370
 
336
371
  end
@@ -454,4 +489,91 @@ describe "SAXMachine" do
454
489
  f.entries[0].orig_link.should == 'http://www.pauldix.net/2008/09/marshal-data-to.html'
455
490
  end
456
491
  end
492
+
493
+ describe "another full example" do
494
+
495
+ RSS_XMLNS = 'http://purl.org/rss/1.0/'
496
+ ATOM_XMLNS = 'http://www.w3.org/2005/Atom'
497
+ class Entry
498
+ include SAXMachine
499
+ element :title, :xmlns => RSS_XMLNS
500
+ element :title, :xmlns => ATOM_XMLNS
501
+ element :link, :xmlns => RSS_XMLNS
502
+ element :link, :xmlns => ATOM_XMLNS, :value => 'href'
503
+ end
504
+ class Channel
505
+ include SAXMachine
506
+ element :title, :xmlns => RSS_XMLNS
507
+ element :title, :xmlns => ATOM_XMLNS
508
+ element :link, :xmlns => RSS_XMLNS
509
+ element :link, :xmlns => ATOM_XMLNS, :value => 'href'
510
+ elements :entry, :as => :entries, :class => Entry
511
+ elements :item, :as => :entries, :class => Entry
512
+ end
513
+ class Root
514
+ include SAXMachine
515
+ elements :rss, :as => :channels, :default_xmlns => RSS_XMLNS, :class => Channel
516
+ elements :feed, :as => :channels, :default_xmlns => ATOM_XMLNS, :class => Channel
517
+ end
518
+
519
+ context "when parsing a complex example" do
520
+ before :all do
521
+ @document = Root.parse(<<-eoxml).channels[0]
522
+ <?xml version="1.0" encoding="UTF-8"?>
523
+ <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"
524
+ xmlns:content="http://purl.org/rss/1.0/modules/content/"
525
+ xmlns:wfw="http://wellformedweb.org/CommentAPI/"
526
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
527
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
528
+ xmlns:cc="http://web.resource.org/cc/">
529
+ <channel>
530
+ <title>Delicious/tag/pubsubhubbub</title>
531
+ <atom:link rel="self" type="application/rss+xml" href="http://feeds.delicious.com/v2/rss/tag/pubsubhubbub?count=15"/>
532
+ <link>http://delicious.com/tag/pubsubhubbub</link>
533
+ <description>recent bookmarks tagged pubsubhubbub</description>
534
+ </channel>
535
+ </rss>
536
+ eoxml
537
+ end
538
+ it "should parse the title" do
539
+ @document.title.should == 'Delicious/tag/pubsubhubbub'
540
+ end
541
+ it "should parse the link" do
542
+ @document.link.should == 'http://feeds.delicious.com/v2/rss/tag/pubsubhubbub?count=15'
543
+ end
544
+ end
545
+ context "when parsing a Twitter example" do
546
+ before :all do
547
+ @document = Root.parse(<<-eoxml).channels[0]
548
+ <?xml version="1.0" encoding="UTF-8"?>
549
+ <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
550
+ <channel>
551
+ <atom:link type="application/rss+xml" rel="self" href="http://twitter.com/statuses/user_timeline/5381582.rss"/>
552
+ <title>Twitter / julien51</title>
553
+ <link>http://twitter.com/julien51</link>
554
+ <description>Twitter updates from julien / julien51.</description>
555
+ <language>en-us</language>
556
+ <ttl>40</ttl>
557
+ <item>
558
+ <title>julien51: @github : I get an error when trying to build one of my gems (julien51-sax-machine), it seems related to another gem's gemspec.</title>
559
+ <description>julien51: @github : I get an error when trying to build one of my gems (julien51-sax-machine), it seems related to another gem's gemspec.</description>
560
+ <pubDate>Thu, 30 Jul 2009 01:00:30 +0000</pubDate>
561
+ <guid>http://twitter.com/julien51/statuses/2920716033</guid>
562
+ <link>http://twitter.com/julien51/statuses/2920716033</link>
563
+ </item>
564
+ </channel>
565
+ </rss>
566
+ eoxml
567
+ end
568
+ it "should parse the title" do
569
+ @document.title.should == 'Twitter / julien51'
570
+ end
571
+ it "should parse the link" do
572
+ @document.link.should == 'http://twitter.com/statuses/user_timeline/5381582.rss'
573
+ end
574
+ it "should find an entry" do
575
+ @document.entries.length.should == 1
576
+ end
577
+ end
578
+ end
457
579
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: julien51-sax-machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.18
4
+ version: 0.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix