vtd-xml 0.0.2-java → 0.0.3-java

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.
data/README.md CHANGED
@@ -29,16 +29,31 @@ With the following example XML:
29
29
  <author name="Charles Dickens" />
30
30
  <language>English</language>
31
31
  <publisher>Chapman &amp; Hall</publisher>
32
+ <ratings>
33
+ <rating userID="1">5</rating>
34
+ <rating userID="4">2</rating>
35
+ <rating userID="2">3</rating>
36
+ </ratings>
32
37
  </book>
33
38
  <book title="The Lord of the Rings" sold="150000000" firstPublished="1954">
34
39
  <author name="J. R. R. Tolkien" />
35
40
  <language>English</language>
36
41
  <publisher>George Allen &amp; Unwin</publisher>
42
+ <ratings>
43
+ <rating userID="1">5</rating>
44
+ <rating userID="4">4</rating>
45
+ <rating userID="2">5</rating>
46
+ </ratings>
37
47
  </book>
38
48
  <book title="The Little Prince" sold="140000000" firstPublished="1943">
39
49
  <author name="Antoine de Saint-Exupéry" />
40
50
  <language>French</language>
41
51
  <publisher>Gallimard</publisher>
52
+ <ratings>
53
+ <rating userID="4">4</rating>
54
+ <rating userID="2">2</rating>
55
+ <rating userID="1">3</rating>
56
+ </ratings>
42
57
  </book>
43
58
  </books>
44
59
  ```
@@ -86,11 +101,13 @@ node.attributes # => returns every attribute
86
101
  ### Node traversal
87
102
 
88
103
  ``` ruby
89
- node.with_first_child('language') do |child|
90
- child.text # => "English"
91
- end
104
+ child = node.children('language').first
105
+ child.text # => "English"
92
106
 
93
- node['title'] # => "A Tale of Two Cities"
107
+ node.children('rating').map do |child|
108
+ child.text.to_i
109
+ end
110
+ # => [5,2,3]
94
111
  ```
95
112
 
96
113
  **See the examples directory for more.**
data/Rakefile CHANGED
@@ -1,7 +1,5 @@
1
1
  require 'bundler/gem_tasks'
2
2
  $: << 'lib'
3
3
 
4
- Rake.add_rakelib 'tasks'
5
-
6
4
  desc 'Run RSpec code examples'
7
- task :default => :spec
5
+ task :default => ['fixtures:generate', :spec]
@@ -8,7 +8,3 @@ xml = VTD::Xml.open('spec/fixtures/books.xml')
8
8
  # Make use of the entire attribute list
9
9
  node = xml.find('//book').max_by { |node| node.attributes['sold'].to_i }
10
10
  puts "Max sold was #{node.attributes['sold'].inspect}"
11
-
12
- # Make use of the attribute cache
13
- node = xml.find('//book').max_by { |node| node['sold'].to_i }
14
- puts "Max sold was #{node['sold'].inspect}"
@@ -41,20 +41,35 @@ module VTD
41
41
  f.puts \
42
42
  '<?xml version="1.0" encoding="utf-8"?>',
43
43
  '<books>',
44
- ' <book title="A Tale of Two Cities" sold="200000000" firstPublished="1859">',
44
+ ' <book title="A Tale of Two Cities" sold="200000000" firstPublished="1859" rank="1">',
45
45
  ' <author name="Charles Dickens" />',
46
46
  ' <language>English</language>',
47
47
  ' <publisher>Chapman &amp; Hall</publisher>',
48
+ ' <ratings>',
49
+ ' <rating userID="1">5</rating>',
50
+ ' <rating userID="4">2</rating>',
51
+ ' <rating userID="2">3</rating>',
52
+ ' </ratings>',
48
53
  ' </book>',
49
- ' <book title="The Lord of the Rings" sold="150000000" firstPublished="1954">',
54
+ ' <book title="The Lord of the Rings" sold="150000000" firstPublished="1954" rank="2">',
50
55
  ' <author name="J. R. R. Tolkien" />',
51
56
  ' <language>English</language>',
52
57
  ' <publisher>George Allen &amp; Unwin</publisher>',
58
+ ' <ratings>',
59
+ ' <rating userID="1">5</rating>',
60
+ ' <rating userID="4">4</rating>',
61
+ ' <rating userID="2">5</rating>',
62
+ ' </ratings>',
53
63
  ' </book>',
54
- ' <book title="The Little Prince" sold="140000000" firstPublished="1943">',
64
+ ' <book title="The Little Prince" sold="140000000" firstPublished="1943" rank="3">',
55
65
  ' <author name="Antoine de Saint-Exupéry" />',
56
66
  ' <language>French</language>',
57
67
  ' <publisher>Gallimard</publisher>',
68
+ ' <ratings>',
69
+ ' <rating userID="4">4</rating>',
70
+ ' <rating userID="2">2</rating>',
71
+ ' <rating userID="1">3</rating>',
72
+ ' </ratings>',
58
73
  ' </book>',
59
74
  '</books>'
60
75
  end
@@ -3,11 +3,6 @@ require 'vtd/xml/node/attributes'
3
3
  module VTD
4
4
  module Xml
5
5
  class Node
6
- TYPES = {
7
- first_child: com.ximpleware.VTDNav::FIRST_CHILD,
8
- parent: com.ximpleware.VTDNav::PARENT
9
- }
10
-
11
6
  include Attributes
12
7
 
13
8
  attr_reader :name, :text
@@ -16,7 +11,6 @@ module VTD
16
11
  @nav = nav
17
12
  @auto_pilot = auto_pilot
18
13
  @current = current
19
- super
20
14
  end
21
15
 
22
16
  def name
@@ -27,10 +21,14 @@ module VTD
27
21
  @text ||= string_from_index @nav.get_text
28
22
  end
29
23
 
30
- def with_first_child(name = nil)
31
- if move_to(:first_child, name)
32
- yield dup
33
- move_to(:parent)
24
+ def children(options = {})
25
+ Enumerator.new do |yielder|
26
+ @nav.push
27
+ next_element unless child_selected?(options)
28
+
29
+ yielder << dup while next_element
30
+
31
+ @nav.pop
34
32
  end
35
33
  end
36
34
 
@@ -40,20 +38,18 @@ module VTD
40
38
 
41
39
  private
42
40
 
43
- def string_from_index(index)
44
- @nav.to_normalized_string(index) if index != -1
41
+ def child_selected?(options)
42
+ name = options.fetch(:only, '*')
43
+ @auto_pilot.select_element(name)
44
+ name != '*'
45
45
  end
46
46
 
47
- def move_to(type, name = nil)
48
- if name
49
- @nav.to_element(find_type(type), name)
50
- else
51
- @nav.to_element(find_type(type))
52
- end
47
+ def next_element
48
+ @auto_pilot.iterate
53
49
  end
54
50
 
55
- def find_type(type)
56
- TYPES[type] or raise ArgumentError, "Unknown node type: #{type.inspect}"
51
+ def string_from_index(index)
52
+ @nav.to_normalized_string(index) if index != -1
57
53
  end
58
54
  end
59
55
  end
@@ -4,22 +4,15 @@ module VTD
4
4
  module Attributes
5
5
  AttributeMissing = Class.new(StandardError)
6
6
 
7
- def initialize(*)
8
- @attribute_cache = {}
9
- @attributes = nil
10
- end
11
-
12
7
  def attributes
13
- return @attributes if @attributes
14
-
15
- @attributes = {}
8
+ attributes = {}
16
9
 
17
10
  @auto_pilot.select_attr('*')
18
11
  while (i = @auto_pilot.iterate_attr) != -1
19
- @attributes[string_from_index i] = string_from_index i + 1
12
+ attributes[string_from_index i] = string_from_index i + 1
20
13
  end
21
14
 
22
- @attribute_cache = @attributes
15
+ attributes
23
16
  end
24
17
 
25
18
  def [](key)
@@ -49,7 +42,7 @@ module VTD
49
42
  private
50
43
 
51
44
  def find_attribute(key)
52
- @attribute_cache[key] ||= string_from_index @nav.get_attr_val(key)
45
+ string_from_index @nav.get_attr_val(key)
53
46
  end
54
47
  end
55
48
  end
@@ -1,5 +1,5 @@
1
1
  module VTD
2
2
  module Xml
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
5
5
  end
@@ -3,7 +3,9 @@ namespace :fixtures do
3
3
  task :generate do
4
4
  require 'vtd/xml/generator'
5
5
 
6
- root = File.expand_path '../../spec/fixtures', __FILE__
6
+ root = File.expand_path('../../spec/fixtures', __FILE__)
7
+
8
+ puts 'Generating XML fixtures...'
7
9
  VTD::Xml::Generator.new(root).generate
8
10
  end
9
11
  end
File without changes
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe VTD::Xml::Node do
4
+ let(:parser) { VTD::Xml::Parser.new 'spec/fixtures/books.xml' }
5
+ let(:book) { parser.find('//book[1]').first }
6
+
7
+ describe 'node attributes' do
8
+ let(:title) { 'A Tale of Two Cities' }
9
+ let(:sold) { '200000000' }
10
+ let(:published) { '1859' }
11
+
12
+ it 'can be accessed with []' do
13
+ expect(book['title']).to eq(title)
14
+ end
15
+
16
+ it 'can be accessed with fetch' do
17
+ expect(book.fetch('title')).to eq(title)
18
+ end
19
+
20
+ it 'uses the second argument when fetch fails' do
21
+ expect(book.fetch('not-present', 'Arg')).to eq('Arg')
22
+ end
23
+
24
+ it 'uses a block when fetch fails' do
25
+ expect(book.fetch('not-present') { 'Block' }).to eq('Block')
26
+ end
27
+
28
+ it 'can slice attributes' do
29
+ expect(book.slice('title', 'sold')).to eq(
30
+ 'title' => title, 'sold' => sold)
31
+ end
32
+
33
+ it 'can return all attributes' do
34
+ expect(book.attributes).to eq(
35
+ 'title' => title, 'sold' => sold, 'firstPublished' => published, 'rank' => '1')
36
+ end
37
+ end
38
+
39
+ describe '#children' do
40
+ it 'filters children by name' do
41
+ expect(book.children(only: 'rating').to_a.size).to eq(3)
42
+ end
43
+
44
+ it 'iterates through all child elements when no element name is given' do
45
+ expect(book.children.to_a.size).to eq(7)
46
+ end
47
+
48
+ it 'is enumerable' do
49
+ expect(book.children).to respond_to(:map)
50
+ end
51
+
52
+ it 'return an empty array when no matches are found' do
53
+ expect(book.children(only: 'soundtrack').to_a).to eq([])
54
+ end
55
+
56
+ it 'doesnt affect existing node traversal' do
57
+ ranks = parser.find('//book').map do |book|
58
+ book.children.to_a
59
+ book['rank']
60
+ end
61
+
62
+ expect(ranks).to eq(%w(1 2 3))
63
+ end
64
+ end
65
+ end
@@ -14,68 +14,4 @@ describe VTD::Xml::Parser do
14
14
  expect(node.text).to be_nil
15
15
  end
16
16
  end
17
-
18
- describe 'node attributes' do
19
- let(:title) { 'A Tale of Two Cities' }
20
- let(:sold) { '200000000' }
21
- let(:published) { '1859' }
22
-
23
- it 'can be accessed with []' do
24
- expect(book['title']).to eq(title)
25
- end
26
-
27
- it 'can be accessed with fetch' do
28
- expect(book.fetch('title')).to eq(title)
29
- end
30
-
31
- it 'uses the second argument when fetch fails' do
32
- expect(book.fetch('not-present', 'Arg')).to eq('Arg')
33
- end
34
-
35
- it 'uses a block when fetch fails' do
36
- expect(book.fetch('not-present') { 'Block' }).to eq('Block')
37
- end
38
-
39
- it 'can slice attributes' do
40
- expect(book.slice('title', 'sold')).to eq(
41
- 'title' => title, 'sold' => sold)
42
- end
43
-
44
- it 'can return all attributes' do
45
- expect(book.attributes).to eq(
46
- 'title' => title, 'sold' => sold, 'firstPublished' => published)
47
- end
48
- end
49
-
50
- describe 'node elements' do
51
- it 'can access node child element' do
52
- book.with_first_child do |child|
53
- expect(child['name']).to eq('Charles Dickens')
54
- end
55
- end
56
-
57
- it 'can access node child elements' do
58
- book.with_first_child('publisher') do |child|
59
- expect(child.text).to eq('Chapman & Hall')
60
- end
61
- end
62
-
63
- it 'returns nil when no element is found' do
64
- expect { |b|
65
- book.with_first_child('soundtrack', &b)
66
- }.to_not yield_control
67
- end
68
-
69
- it 'returns the cursor back to the initial position' do
70
- book.with_first_child('publisher') {}
71
-
72
- expect { |b|
73
- book.with_first_child(&b)
74
- }.to yield_with_args
75
-
76
- book.with_first_child do |child|
77
- expect(child['name']).to eq('Charles Dickens')
78
- end
79
- end
80
- end
81
17
  end
@@ -21,4 +21,5 @@ Gem::Specification.new do |gem|
21
21
  gem.require_paths = ['lib']
22
22
 
23
23
  gem.add_development_dependency 'rake'
24
+ gem.add_development_dependency 'rspec'
24
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vtd-xml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: java
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-02-19 00:00:00.000000000 Z
13
+ date: 2013-03-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -30,6 +30,24 @@ dependencies:
30
30
  none: false
31
31
  prerelease: false
32
32
  type: :development
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: !binary |-
40
+ MA==
41
+ none: false
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: !binary |-
47
+ MA==
48
+ none: false
49
+ prerelease: false
50
+ type: :development
33
51
  description: VTD-XML for JRuby
34
52
  email:
35
53
  - james@logi.cl
@@ -57,11 +75,12 @@ files:
57
75
  - lib/vtd/xml/node/attributes.rb
58
76
  - lib/vtd/xml/parser.rb
59
77
  - lib/vtd/xml/version.rb
78
+ - rakelib/fixtures.rake
79
+ - rakelib/rspec.rake
60
80
  - spec/spec_helper.rb
81
+ - spec/vtd/xml/node_spec.rb
61
82
  - spec/vtd/xml/parser_spec.rb
62
83
  - spec/vtd/xml_spec.rb
63
- - tasks/fixtures.rake
64
- - tasks/rspec.rake
65
84
  - vtd-xml.gemspec
66
85
  homepage: https://github.com/jcf/vtd-xml
67
86
  licenses:
@@ -92,5 +111,6 @@ specification_version: 3
92
111
  summary: A thin wrapper around the VTD-XML Java library
93
112
  test_files:
94
113
  - spec/spec_helper.rb
114
+ - spec/vtd/xml/node_spec.rb
95
115
  - spec/vtd/xml/parser_spec.rb
96
116
  - spec/vtd/xml_spec.rb