xml_node_stream 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,68 +1,39 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
- # -*- encoding: utf-8 -*-
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "xml_node_stream"
3
+ spec.version = File.read(File.expand_path("../VERSION", __FILE__)).strip
4
+ spec.authors = ["Brian Durand"]
5
+ spec.email = ["bbdurand@gmail.com"]
5
6
 
6
- Gem::Specification.new do |s|
7
- s.name = %q{xml_node_stream}
8
- s.version = "1.0.0"
7
+ spec.summary = "Memory-efficient XML parser that reduces memory allocation when parsing large XML documents while maintaining a simple, easy-to-use interface."
9
8
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Brian Durand"]
12
- s.date = %q{2010-02-07}
13
- s.email = %q{brian@embellishedvisions.com}
14
- s.extra_rdoc_files = [
15
- "README.rdoc"
16
- ]
17
- s.files = [
18
- "MIT_LICENSE",
19
- "README.rdoc",
20
- "Rakefile",
21
- "VERSION",
22
- "init.rb",
23
- "lib/xml_node_stream.rb",
24
- "lib/xml_node_stream/node.rb",
25
- "lib/xml_node_stream/parser.rb",
26
- "lib/xml_node_stream/parser/base.rb",
27
- "lib/xml_node_stream/parser/libxml_parser.rb",
28
- "lib/xml_node_stream/parser/nokogiri_parser.rb",
29
- "lib/xml_node_stream/parser/rexml_parser.rb",
30
- "lib/xml_node_stream/selector.rb",
31
- "spec/node_spec.rb",
32
- "spec/parser_spec.rb",
33
- "spec/selector_spec.rb",
34
- "spec/spec_helper.rb",
35
- "spec/test.xml",
36
- "spec/xml_node_stream_spec.rb",
37
- "xml_node_stream.gemspec"
38
- ]
39
- s.homepage = %q{http://github.com/bdurand/xml_node_stream}
40
- s.rdoc_options = ["--charset=UTF-8"]
41
- s.require_paths = ["lib"]
42
- s.rubygems_version = %q{1.3.5}
43
- s.summary = %q{Simple XML parser wrapper that provides the benefits of stream parsing with the ease of using document nodes.}
44
- s.test_files = [
45
- "spec/node_spec.rb",
46
- "spec/parser_spec.rb",
47
- "spec/selector_spec.rb",
48
- "spec/spec_helper.rb",
49
- "spec/xml_node_stream_spec.rb"
50
- ]
9
+ spec.homepage = "https://github.com/bdurand/xml_node_stream"
10
+ spec.license = "MIT"
51
11
 
52
- if s.respond_to? :specification_version then
53
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
54
- s.specification_version = 3
12
+ spec.metadata = {
13
+ "homepage_uri" => spec.homepage,
14
+ "source_code_uri" => spec.homepage,
15
+ "changelog_uri" => "#{spec.homepage}/blob/main/CHANGELOG.md"
16
+ }
55
17
 
56
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
57
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
58
- s.add_development_dependency(%q<jeweler>, [">= 0"])
59
- else
60
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
61
- s.add_dependency(%q<jeweler>, [">= 0"])
62
- end
63
- else
64
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
65
- s.add_dependency(%q<jeweler>, [">= 0"])
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ ignore_files = %w[
21
+ .
22
+ AGENTS.md
23
+ Appraisals
24
+ Gemfile
25
+ Gemfile.lock
26
+ Rakefile
27
+ bin/
28
+ gemfiles/
29
+ spec/
30
+ benchmark/
31
+ ]
32
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| ignore_files.any? { |path| f.start_with?(path) } }
66
34
  end
67
- end
68
35
 
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_development_dependency "bundler"
39
+ end
metadata CHANGED
@@ -1,52 +1,40 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: xml_node_stream
3
- version: !ruby/object:Gem::Version
4
- version: 1.0.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
5
  platform: ruby
6
- authors:
6
+ authors:
7
7
  - Brian Durand
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
-
12
- date: 2010-02-07 00:00:00 -06:00
13
- default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: rspec
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: bundler
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
21
16
  - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.2.9
24
- version:
25
- - !ruby/object:Gem::Dependency
26
- name: jeweler
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
27
19
  type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
30
- requirements:
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
31
23
  - - ">="
32
- - !ruby/object:Gem::Version
33
- version: "0"
34
- version:
35
- description:
36
- email: brian@embellishedvisions.com
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ email:
27
+ - bbdurand@gmail.com
37
28
  executables: []
38
-
39
29
  extensions: []
40
-
41
- extra_rdoc_files:
42
- - README.rdoc
43
- files:
44
- - MIT_LICENSE
45
- - README.rdoc
46
- - Rakefile
30
+ extra_rdoc_files: []
31
+ files:
32
+ - CHANGELOG.md
33
+ - MIT-LICENSE
34
+ - README.md
47
35
  - VERSION
48
- - init.rb
49
36
  - lib/xml_node_stream.rb
37
+ - lib/xml_node_stream/http_stream.rb
50
38
  - lib/xml_node_stream/node.rb
51
39
  - lib/xml_node_stream/parser.rb
52
40
  - lib/xml_node_stream/parser/base.rb
@@ -54,44 +42,30 @@ files:
54
42
  - lib/xml_node_stream/parser/nokogiri_parser.rb
55
43
  - lib/xml_node_stream/parser/rexml_parser.rb
56
44
  - lib/xml_node_stream/selector.rb
57
- - spec/node_spec.rb
58
- - spec/parser_spec.rb
59
- - spec/selector_spec.rb
60
- - spec/spec_helper.rb
61
- - spec/test.xml
62
- - spec/xml_node_stream_spec.rb
63
45
  - xml_node_stream.gemspec
64
- has_rdoc: true
65
- homepage: http://github.com/bdurand/xml_node_stream
66
- licenses: []
67
-
68
- post_install_message:
69
- rdoc_options:
70
- - --charset=UTF-8
71
- require_paths:
46
+ homepage: https://github.com/bdurand/xml_node_stream
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/bdurand/xml_node_stream
51
+ source_code_uri: https://github.com/bdurand/xml_node_stream
52
+ changelog_uri: https://github.com/bdurand/xml_node_stream/blob/main/CHANGELOG.md
53
+ rdoc_options: []
54
+ require_paths:
72
55
  - lib
73
- required_ruby_version: !ruby/object:Gem::Requirement
74
- requirements:
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
75
58
  - - ">="
76
- - !ruby/object:Gem::Version
77
- version: "0"
78
- version:
79
- required_rubygems_version: !ruby/object:Gem::Requirement
80
- requirements:
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
81
63
  - - ">="
82
- - !ruby/object:Gem::Version
83
- version: "0"
84
- version:
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
85
66
  requirements: []
86
-
87
- rubyforge_project:
88
- rubygems_version: 1.3.5
89
- signing_key:
90
- specification_version: 3
91
- summary: Simple XML parser wrapper that provides the benefits of stream parsing with the ease of using document nodes.
92
- test_files:
93
- - spec/node_spec.rb
94
- - spec/parser_spec.rb
95
- - spec/selector_spec.rb
96
- - spec/spec_helper.rb
97
- - spec/xml_node_stream_spec.rb
67
+ rubygems_version: 4.0.3
68
+ specification_version: 4
69
+ summary: Memory-efficient XML parser that reduces memory allocation when parsing large
70
+ XML documents while maintaining a simple, easy-to-use interface.
71
+ test_files: []
data/README.rdoc DELETED
@@ -1,61 +0,0 @@
1
- = XML Node Stream
2
-
3
- This gem provides a very easy to use XML parser the provides the benefits of both stream parsing (i.e. SAX) and document parsing (i.e. DOM). In addition, it provides a unified parsing language for each of the major Ruby XML parsers (REXML, Nokogiri, and LibXML) so that your code doesn't have to be bound to a particular XML library.
4
-
5
- == Stream Parsing
6
-
7
- The primary purpose of this gem is to facilitate parsing large XML files (i.e. several megabytes in size). Often, reading these files into a document structure is not feasible because the whole document must be read into memory. Stream/SAX parsing solves this issue by reading in the file incrementally and providing callbacks for various events. This method can be quite painful to deal with for any sort of complex document structure.
8
-
9
- This gem attempts to solve both of these issues by combining the best features of both. Parsing is performed by a stream parser which construct document style nodes and calls back to the application code with these nodes. When your application is done with a node, it can release it to free up memory and keep your heap from bloating.
10
-
11
- In order to keep the interface simple and universal, only XML elements and text nodes are supported. XML processing instructions and comments will be ignored.
12
-
13
- == Examples
14
-
15
- Suppose we have file with every book in the world in it:
16
-
17
- <books>
18
- <book isbn="123456">
19
- <title>Moby Dick</title>
20
- <author>Herman Melville</author>
21
- <categories>
22
- <category>Fiction</category>
23
- <category>Adventure</category>
24
- </categories>
25
- </book>
26
- <book isbn="98765643">
27
- <title>The Decline and Fall of the Roman Empire</title>
28
- <author>Edward Gibbon</author>
29
- <category>
30
- <category>History</category>
31
- <category>Ancient</category>
32
- </categories>
33
- </book>
34
- ...
35
- </books>
36
-
37
- And we want to get them into our Books data model:
38
-
39
- XmlNodeStream.parse('/tmp/books.xml') do |node|
40
- if node.path == '/books/book'
41
- book = Book.new
42
- book.isbn = node['isbn']
43
- book.title = node.find('title').value
44
- book.author = node.find('author/text()')
45
- book.categories = node.select('categories/category/text()')
46
- book.save
47
- node.release!
48
- end
49
- end
50
-
51
- == Releasing Nodes
52
-
53
- In the above example, what prevents memory bloat when parsing a large document is the call to node.release!. This call will remove the node from the node tree. The general practice is to look for the higher level nodes you are interested in and then release them immediately. If there are nodes you don't care about at all, those can be released immediately as well.
54
-
55
- A sample 77Mb XML document parsed into Nokogiri consumes over 800Mb of memory. Parsing the same document with XmlNodeStream and releasing top level nodes as they're processed uses less than 1Mb.
56
-
57
- == XPath
58
-
59
- You can use a subset of the XPath language to navigate nodes. The only parts of XPath implemented are the paths themselves and the text() function. The text() function is useful for getting the value of node directly from the find or select methods without having to do a nil check on the nodes. For instance, in the above example we can get the name of an author with node.find('author/text()') instead of node.find('author').value if node.find('author').
60
-
61
- The rest of the XPath language is not implemented since it is a programming language and there is really no need for it since we already have Ruby at our disposal which is far more powerful than XPath. See the Selector class for details.
data/Rakefile DELETED
@@ -1,42 +0,0 @@
1
- require 'rubygems'
2
- require 'rake'
3
- require 'rake/rdoctask'
4
-
5
- desc 'Default: run unit tests.'
6
- task :default => :test
7
-
8
- begin
9
- require 'spec/rake/spectask'
10
- desc 'Test xml_node_stream.'
11
- Spec::Rake::SpecTask.new(:test) do |t|
12
- t.spec_files = 'spec/**/*_spec.rb'
13
- end
14
- rescue LoadError
15
- tast :test do
16
- STDERR.puts "You must have rspec >= 1.2.9 to run the tests"
17
- end
18
- end
19
-
20
- desc 'Generate documentation for xml_node_stream.'
21
- Rake::RDocTask.new(:rdoc) do |rdoc|
22
- rdoc.rdoc_dir = 'rdoc'
23
- rdoc.options << '--title' << 'XML Node Stream' << '--line-numbers' << '--inline-source' << '--main' << 'README.rdoc'
24
- rdoc.rdoc_files.include('README.rdoc')
25
- rdoc.rdoc_files.include('lib/**/*.rb')
26
- end
27
-
28
- begin
29
- require 'jeweler'
30
- Jeweler::Tasks.new do |gem|
31
- gem.name = "xml_node_stream"
32
- gem.summary = %Q{Simple XML parser wrapper that provides the benefits of stream parsing with the ease of using document nodes.}
33
- gem.email = "brian@embellishedvisions.com"
34
- gem.homepage = "http://github.com/bdurand/xml_node_stream"
35
- gem.authors = ["Brian Durand"]
36
- gem.add_development_dependency('rspec', '>= 1.2.9')
37
- gem.add_development_dependency('jeweler')
38
- end
39
-
40
- Jeweler::GemcutterTasks.new
41
- rescue LoadError
42
- end
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require "#{File.dirname(__FILE__)}/lib/xml_node_stream"
data/spec/node_spec.rb DELETED
@@ -1,140 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
-
3
- describe XmlNodeStream::Node do
4
-
5
- it "should have a name" do
6
- node = XmlNodeStream::Node.new("tag")
7
- node.name.should == "tag"
8
- end
9
-
10
- it "should have attributes" do
11
- node = XmlNodeStream::Node.new("tag")
12
- node.attributes.should == {}
13
- node["attr1"].should == nil
14
- node = XmlNodeStream::Node.new("tag", nil, "attr1" => "val1", "attr2" => "val2")
15
- node.attributes.should == {"attr1" => "val1", "attr2" => "val2"}
16
- node["attr1"].should == "val1"
17
- end
18
-
19
- it "should have a value" do
20
- node = XmlNodeStream::Node.new("tag")
21
- node.value.should == nil
22
- node = XmlNodeStream::Node.new("tag", nil, nil, "value")
23
- node.value.should == "value"
24
- end
25
-
26
- it "should have a parent and children" do
27
- parent = XmlNodeStream::Node.new("tag")
28
- parent.parent.should == nil
29
- parent.children.should == []
30
- child_1 = XmlNodeStream::Node.new("child", parent)
31
- child_2 = XmlNodeStream::Node.new("child")
32
- parent.add_child(child_2)
33
- parent.children.should == [child_1, child_2]
34
- child_1.parent.should == parent
35
- child_2.parent.should == parent
36
- end
37
-
38
- it "should be able to remove children" do
39
- parent = XmlNodeStream::Node.new("tag")
40
- child_1 = XmlNodeStream::Node.new("child", parent)
41
- child_2 = XmlNodeStream::Node.new("child", parent)
42
- parent.children.should == [child_1, child_2]
43
- parent.remove_child(child_1)
44
- parent.children.should == [child_2]
45
- child_1.parent.should == nil
46
- end
47
-
48
- it "should release itself from its parent" do
49
- parent = XmlNodeStream::Node.new("tag")
50
- child_1 = XmlNodeStream::Node.new("child", parent)
51
- child_2 = XmlNodeStream::Node.new("child", parent)
52
- parent.children.should == [child_1, child_2]
53
- child_1.release!
54
- parent.children.should == [child_2]
55
- child_1.parent.should == nil
56
- end
57
-
58
- it "should have ancestors" do
59
- parent = XmlNodeStream::Node.new("tag")
60
- child = XmlNodeStream::Node.new("child", parent)
61
- grandchild = XmlNodeStream::Node.new("grandchild", child)
62
- parent.ancestors.should == []
63
- child.ancestors.should == [parent]
64
- grandchild.ancestors.should == [child, parent]
65
- end
66
-
67
- it "should have descendants" do
68
- parent = XmlNodeStream::Node.new("tag")
69
- child_1 = XmlNodeStream::Node.new("child", parent)
70
- child_2 = XmlNodeStream::Node.new("child", parent)
71
- grandchild_1 = XmlNodeStream::Node.new("grandchild", child_1)
72
- grandchild_2 = XmlNodeStream::Node.new("grandchild", child_1)
73
- parent.descendants.should == [child_1, child_2, grandchild_1, grandchild_2]
74
- child_1.descendants.should == [grandchild_1, grandchild_2]
75
- grandchild_1.descendants.should == []
76
- end
77
-
78
- it "should have a root node" do
79
- parent = XmlNodeStream::Node.new("tag")
80
- child = XmlNodeStream::Node.new("child", parent)
81
- grandchild = XmlNodeStream::Node.new("grandchild", child)
82
- parent.root.should == parent
83
- child.root.should == parent
84
- grandchild.root.should == parent
85
- end
86
-
87
- it "should have a path" do
88
- parent = XmlNodeStream::Node.new("tag")
89
- child = XmlNodeStream::Node.new("child", parent)
90
- grandchild = XmlNodeStream::Node.new("grandchild", child)
91
- parent.path.should == "/tag"
92
- child.path.should == "/tag/child"
93
- grandchild.path.should == "/tag/child/grandchild"
94
- end
95
-
96
- it "should be able to select related nodes using a selector" do
97
- parent = XmlNodeStream::Node.new("tag")
98
- child_1 = XmlNodeStream::Node.new("child", parent)
99
- child_2 = XmlNodeStream::Node.new("child", parent)
100
- grandchild_1 = XmlNodeStream::Node.new("grandchild", child_1, nil, "val1")
101
- grandchild_2 = XmlNodeStream::Node.new("grandchild", child_1, nil, "val2")
102
- parent.select("nothing").should == []
103
- parent.select("child").should == [child_1, child_2]
104
- parent.select("child/grandchild").should == [grandchild_1, grandchild_2]
105
- parent.select("child/grandchild/text()").should == ["val1", "val2"]
106
- grandchild_1.select("../..").should == [parent]
107
- end
108
-
109
- it "should be able to find the first related node using a selector" do
110
- parent = XmlNodeStream::Node.new("tag")
111
- child_1 = XmlNodeStream::Node.new("child", parent)
112
- child_2 = XmlNodeStream::Node.new("child", parent)
113
- grandchild_1 = XmlNodeStream::Node.new("grandchild", child_1, nil, "val1")
114
- grandchild_2 = XmlNodeStream::Node.new("grandchild", child_1, nil, "val2")
115
- parent.find("nothing").should == nil
116
- parent.find("child").should == child_1
117
- parent.find("child/grandchild").should == grandchild_1
118
- parent.find("child/grandchild/text()").should == "val1"
119
- grandchild_1.find("../..").should == parent
120
- end
121
-
122
- it "should append text which strips whitespace from the start and end of the value" do
123
- node = XmlNodeStream::Node.new("tag")
124
- node.append(" ")
125
- node.append(" \t\r\nhello ")
126
- node.append(" there\n")
127
- node.finish!
128
- node.value.should == "hello there"
129
- end
130
-
131
- it "should append cdata which preserves all whitespace" do
132
- node = XmlNodeStream::Node.new("tag")
133
- node.append_cdata(" ")
134
- node.append(" \t\r\nhello ")
135
- node.append_cdata(" there\n")
136
- node.finish!
137
- node.value.should == " \t\r\nhello there\n"
138
- end
139
-
140
- end
data/spec/parser_spec.rb DELETED
@@ -1,148 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
-
3
- describe XmlNodeStream::Parser do
4
-
5
- before :each do
6
- @text_xml_path = File.expand_path(File.join(File.dirname(__FILE__), 'test.xml'))
7
- end
8
-
9
- it "should parse a document in a string" do
10
- validate_text_xml(XmlNodeStream::Parser.parse(File.read(@text_xml_path)))
11
- end
12
-
13
- it "should parse a document in a file path string" do
14
- validate_text_xml(XmlNodeStream::Parser.parse(@text_xml_path))
15
- end
16
-
17
- it "should parse a document in a file path" do
18
- validate_text_xml(XmlNodeStream::Parser.parse(Pathname.new(@text_xml_path)))
19
- end
20
-
21
- it "should parse a document in a url string" do
22
- uri = URI.parse("http://test.host/test.xml")
23
- URI.should_receive(:parse).with("http://test.host/test.xml").and_return(uri)
24
- File.open(@text_xml_path) do |stream|
25
- uri.should_receive(:open).and_return(stream)
26
- validate_text_xml(XmlNodeStream::Parser.parse("http://test.host/test.xml"))
27
- end
28
- end
29
-
30
- it "should parse a document in a URI" do
31
- uri = URI.parse("http://test.host/test.xml")
32
- stream = mock(:stream)
33
- File.open(@text_xml_path) do |stream|
34
- uri.should_receive(:open).and_return(stream)
35
- validate_text_xml(XmlNodeStream::Parser.parse(uri))
36
- end
37
- end
38
-
39
- it "should parse a document in a stream" do
40
- io = StringIO.new(File.read(@text_xml_path))
41
- io.should_not_receive(:close)
42
- validate_text_xml(XmlNodeStream::Parser.parse(io))
43
- end
44
-
45
- it "should call a block with each element in a document" do
46
- nodes = []
47
- XmlNodeStream::Parser.parse(@text_xml_path) do |node|
48
- nodes << node.path
49
- end
50
- nodes.should == %w(
51
- /library/authors/author/name
52
- /library/authors/author
53
- /library/authors/author/name
54
- /library/authors/author
55
- /library/authors/author/name
56
- /library/authors/author
57
- /library/authors
58
- /library/collection/section/book/title
59
- /library/collection/section/book/author
60
- /library/collection/section/book/abstract
61
- /library/collection/section/book/volumes
62
- /library/collection/section/book
63
- /library/collection/section
64
- /library/collection/section/book/title
65
- /library/collection/section/book/author
66
- /library/collection/section/book/abstract
67
- /library/collection/section/book
68
- /library/collection/section/book/title
69
- /library/collection/section/book/author
70
- /library/collection/section/book/abstract
71
- /library/collection/section/book
72
- /library/collection/section/book/title
73
- /library/collection/section/book/alternate_title
74
- /library/collection/section/book/author
75
- /library/collection/section/book/abstract
76
- /library/collection/section/book
77
- /library/collection/section
78
- /library/collection
79
- /library
80
- )
81
- end
82
-
83
- XmlNodeStream::Parser::SUPPORTED_PARSERS.each do |parser_name|
84
- context "with #{parser_name}" do
85
- before :all do
86
- @save_parser_name = XmlNodeStream::Parser.parser_name
87
- XmlNodeStream::Parser.parser_name = parser_name
88
- end
89
-
90
- after :all do
91
- XmlNodeStream::Parser.parser_name = @save_parser_name
92
- end
93
-
94
- it "should parse a document" do
95
- begin
96
- validate_text_xml(XmlNodeStream::Parser.parse(@text_xml_path))
97
- rescue NotImplementedError
98
- pending("#{parser_name} is not installed for testing")
99
- end
100
- end
101
- end
102
- end
103
-
104
- def validate_text_xml (root)
105
- validate(root, :name => "library", :children => ["authors", "collection"])
106
-
107
- validate(root.children[0], :name => "authors", :children => ["author"] * 3)
108
- validate(root.children[0].children[0], :name => "author", :attributes => {"id" => "1"}, :children => ["name"])
109
- validate(root.children[0].children[0].children[0], :name => "name", :value => "Edward Gibbon")
110
- validate(root.children[0].children[1], :name => "author", :attributes => {"id" => "2"}, :children => ["name"])
111
- validate(root.children[0].children[1].children[0], :name => "name", :value => "Herman Melville")
112
- validate(root.children[0].children[2], :name => "author", :attributes => {"id" => "3"}, :children => ["name"])
113
- validate(root.children[0].children[2].children[0], :name => "name", :value => "Jack London")
114
-
115
- validate(root.children[1], :name => "collection", :children => ["section"] * 2)
116
- history = root.children[1].children[0]
117
- fiction = root.children[1].children[1]
118
-
119
- validate(history, :name => "section", :attributes => {"id" => "100", "name" => "History"}, :children => ["book"])
120
- validate(history.children[0], :name => "book", :attributes => {"id" => "1"}, :children => ["title", "author", "abstract", "volumes"])
121
- validate(history.children[0].children[0], :name => "title", :value => "The Decline & Fall of the Roman Empire")
122
- validate(history.children[0].children[1], :name => "author", :value => nil, :attributes => {"id" => "1"})
123
- validate(history.children[0].children[2], :name => "abstract", :value => "History of the fall of Rome.")
124
- validate(history.children[0].children[3], :name => "volumes", :value => "6")
125
-
126
- validate(fiction, :name => "section", :attributes => {"id" => "200", "name" => "Fiction"}, :children => ["book"] * 3)
127
- validate(fiction.children[0], :name => "book", :attributes => {"id" => "2"}, :children => ["title", "author", "abstract"])
128
- validate(fiction.children[0].children[0], :name => "title", :value => "Call of the Wild")
129
- validate(fiction.children[0].children[1], :name => "author", :value => nil, :attributes => {"id" => "3"})
130
- validate(fiction.children[0].children[2], :name => "abstract", :value => "\n A dog goes to Alaska.\n ")
131
- validate(fiction.children[1], :name => "book", :attributes => {"id" => "3"}, :children => ["title", "author", "abstract"])
132
- validate(fiction.children[1].children[0], :name => "title", :value => "White Fang")
133
- validate(fiction.children[1].children[1], :name => "author", :value => nil, :attributes => {"id" => "3"})
134
- validate(fiction.children[1].children[2], :name => "abstract", :value => "Dogs, wolves, etc.")
135
- validate(fiction.children[2], :name => "book", :attributes => {"id" => "4"}, :children => ["title", "alternate_title", "author", "abstract"])
136
- validate(fiction.children[2].children[0], :name => "title", :value => "Moby Dick")
137
- validate(fiction.children[2].children[1], :name => "alternate_title", :value => "The Whale")
138
- validate(fiction.children[2].children[2], :name => "author", :value => nil, :attributes => {"id" => "2"})
139
- validate(fiction.children[2].children[3], :name => "abstract", :value => "A mad captain seeks a mysterious white whale.")
140
- end
141
-
142
- def validate (node, options)
143
- node.name.should == options[:name]
144
- node.attributes.should == (options[:attributes] || {})
145
- node.value.should == (options.include?(:value) ? options[:value] : "")
146
- node.children.collect{|c| c.name}.should == (options[:children] || [])
147
- end
148
- end