xml-sax-machines 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 4
4
- :patch: 1
4
+ :patch: 2
5
5
  :build:
@@ -32,14 +32,20 @@ module XML
32
32
  @context = @document
33
33
  end
34
34
 
35
- def start_element(name, attributes = []) #:nodoc:
35
+ def start_element_namespace(name, attributes = [], prefix = nil, uri = nil, ns = [])
36
36
  super
37
- el = Nokogiri::XML::Element.new(name, @document)
38
- Hash[*attributes.flatten].each_pair{|k, v| el[k] = v}
37
+ el = Nokogiri::XML::Element.new(name, @document)
38
+ el.namespace = el.add_namespace_definition(prefix, uri) if uri
39
+ ns.each{|prefix, href| el.add_namespace_definition(prefix, href)}
39
40
  @context = @context.add_child(el)
41
+
42
+ # Node needs to be added to the document so namespaces are available.
43
+ attributes.each{|attribute| el[[attribute.prefix, attribute.localname].compact.join(':')] = attribute.value}
40
44
  end
41
45
 
42
- def end_element(name) #:nodoc:
46
+ #--
47
+ # TODO: Check namespace of context as well?
48
+ def end_element_namespace(name, prefix = nil, uri = nil)
43
49
  super
44
50
  raise "Unmatched closing element. Got '#{name}' but expected '#{@context.name}'" \
45
51
  unless name == @context.name
@@ -16,12 +16,9 @@ module XML
16
16
  characters
17
17
  comment
18
18
  end_document
19
- end_element
20
19
  end_element_namespace
21
20
  error
22
21
  start_document
23
- start_element
24
- start_element_namespace
25
22
  warning
26
23
  xmldecl
27
24
  }.each do |method|
@@ -31,6 +28,12 @@ module XML
31
28
  end
32
29
  end
33
30
 
31
+ def start_element_namespace name, attributes = [], prefix = nil, uri = nil, ns = []
32
+ atts = attributes.map{|attribute| [[attribute.prefix, attribute.localname].compact.join(':'), attribute.value]}
33
+ warn "start_element_namespace: [#{name.inspect}, #{atts.inspect}, #{prefix.inspect}, #{uri.inspect}, #{ns.inspect}]"
34
+ super
35
+ end
36
+
34
37
  end # Debug
35
38
  end # SAX
36
39
  end # XML
@@ -42,8 +42,8 @@ module XML
42
42
  @filter.end_document if @filter
43
43
  end
44
44
 
45
- def end_element(name) #:nodoc:
46
- @filter.end_element(name) if @filter
45
+ def end_element_namespace(name, prefix = nil, uri = nil) #:nodoc:
46
+ @filter.end_element_namespace(name, prefix, uri) if @filter
47
47
  end
48
48
 
49
49
  def error(string) #:nodoc:
@@ -54,8 +54,8 @@ module XML
54
54
  @filter.start_document if @filter
55
55
  end
56
56
 
57
- def start_element(name, attributes = []) #:nodoc:
58
- @filter.start_element(name, attributes = []) if @filter
57
+ def start_element_namespace(name, attributes = [], prefix = nil, uri = nil, ns = []) #:nodoc:
58
+ @filter.start_element_namespace(name, attributes, prefix, uri, ns) if @filter
59
59
  end
60
60
 
61
61
  def warning(string) #:nodoc:
@@ -54,7 +54,7 @@ module XML
54
54
  @buffer = 0
55
55
  end
56
56
 
57
- def start_element(name, attributes = []) #:nodoc:
57
+ def start_element_namespace(name, attributes = [], prefix = nil, uri = nil, ns = []) #:nodoc:
58
58
  super
59
59
  @find.each_pair do |xpath, block|
60
60
  if match = @document.at(xpath)
@@ -66,7 +66,7 @@ module XML
66
66
  end
67
67
  end
68
68
 
69
- def end_element(name) #:nodoc:
69
+ def end_element_namespace(name, prefix = nil, uri = nil) #:nodoc:
70
70
  path = @context.path
71
71
  if @buffer > 0 && block = @found.delete(path)
72
72
  @buffer -= 1
data/test/test_builder.rb CHANGED
@@ -40,6 +40,14 @@ describe 'Builder' do
40
40
  assert_equal 2, el.children.length
41
41
  end
42
42
 
43
+ it 'creates elements with namespaces' do
44
+ assert build('<r xmlns:bar="http://bar.local"><bar:foo/></r>').at('/r/bar:foo')
45
+ end
46
+
47
+ it 'create elements with namespaced attributes' do
48
+ assert build('<r xmlns:bar="http://bar.local"><foo bar:id="1"/></r>').at('/r/foo[@bar:id="1"]')
49
+ end
50
+
43
51
  protected
44
52
  def build(string)
45
53
  builder = XML::SAX::Builder.new
data/test/test_debug.rb CHANGED
@@ -9,16 +9,16 @@ describe 'XML::SAX::Debug event method warning' do
9
9
  assert_match regexp('end_document: []'), parse('<r/>')
10
10
  end
11
11
 
12
- it 'warns #start_element' do
13
- assert_match regexp('start_element: ["r", []]'), parse('<r/>')
12
+ it 'warns #start_element_namespace' do
13
+ assert_match regexp('start_element_namespace: ["r", [], nil, nil, []]'), parse('<r/>')
14
14
  end
15
15
 
16
16
  it 'warns #start_element with attributes' do
17
- assert_match regexp('start_element: ["r", [["id", "1"]]]'), parse('<r id="1"/>')
17
+ assert_match regexp('start_element_namespace: ["r", [["id", "1"]], nil, nil, []]'), parse('<r id="1"/>')
18
18
  end
19
19
 
20
20
  it 'warns #end_element' do
21
- assert_match regexp('end_element: ["r"]'), parse('<r/>')
21
+ assert_match regexp('end_element_namespace: ["r", nil, nil]'), parse('<r/>')
22
22
  end
23
23
 
24
24
  it 'warns #characters' do
@@ -1,4 +1,5 @@
1
1
  require_relative 'helper'
2
+ # TODO: These tests don't fail if the lambda doesn't run.
2
3
 
3
4
  describe 'XML::SAX::FragmentBuilder' do
4
5
  it 'calls callback for record' do
@@ -21,4 +22,17 @@ describe 'XML::SAX::FragmentBuilder' do
21
22
  parser << '<r><foo>text<el>el</el></foo></r>'
22
23
  parser.finish
23
24
  end
25
+
26
+ it 'handles namespaces like a boss' do
27
+ builder = XML::SAX::FragmentBuilder.new(
28
+ '//foo:title' => lambda{|el|
29
+ assert_equal 'foo', el.namespace.prefix
30
+ assert_equal 'http://foo.local', el.namespace.href
31
+ assert_equal 'title', el.name
32
+ }
33
+ )
34
+ parser = Nokogiri::XML::SAX::PushParser.new(builder)
35
+ parser << '<r xmlns:foo="http://foo.local"><title /><foo:title/></r>'
36
+ parser.finish
37
+ end
24
38
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{xml-sax-machines}
8
- s.version = "0.4.1"
8
+ s.version = "0.4.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Shane Hanna"]
12
- s.date = %q{2010-11-29}
12
+ s.date = %q{2011-02-04}
13
13
  s.description = %q{XML SAX Machines}
14
14
  s.email = %q{shane.hanna@gmail.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 1
9
- version: 0.4.1
8
+ - 2
9
+ version: 0.4.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Shane Hanna
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-29 00:00:00 +11:00
17
+ date: 2011-02-04 00:00:00 +11:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency