vtd-xml 0.0.1-java → 0.0.2-java
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -0
- data/lib/vtd/xml/generator.rb +6 -0
- data/lib/vtd/xml/node.rb +28 -0
- data/lib/vtd/xml/version.rb +1 -1
- data/spec/vtd/xml/parser_spec.rb +33 -1
- data/vtd-xml.gemspec +3 -3
- metadata +4 -2
data/README.md
CHANGED
@@ -27,12 +27,18 @@ With the following example XML:
|
|
27
27
|
<books>
|
28
28
|
<book title="A Tale of Two Cities" sold="200000000" firstPublished="1859">
|
29
29
|
<author name="Charles Dickens" />
|
30
|
+
<language>English</language>
|
31
|
+
<publisher>Chapman & Hall</publisher>
|
30
32
|
</book>
|
31
33
|
<book title="The Lord of the Rings" sold="150000000" firstPublished="1954">
|
32
34
|
<author name="J. R. R. Tolkien" />
|
35
|
+
<language>English</language>
|
36
|
+
<publisher>George Allen & Unwin</publisher>
|
33
37
|
</book>
|
34
38
|
<book title="The Little Prince" sold="140000000" firstPublished="1943">
|
35
39
|
<author name="Antoine de Saint-Exupéry" />
|
40
|
+
<language>French</language>
|
41
|
+
<publisher>Gallimard</publisher>
|
36
42
|
</book>
|
37
43
|
</books>
|
38
44
|
```
|
@@ -77,6 +83,16 @@ node.slice('title', 'missing')
|
|
77
83
|
node.attributes # => returns every attribute
|
78
84
|
```
|
79
85
|
|
86
|
+
### Node traversal
|
87
|
+
|
88
|
+
``` ruby
|
89
|
+
node.with_first_child('language') do |child|
|
90
|
+
child.text # => "English"
|
91
|
+
end
|
92
|
+
|
93
|
+
node['title'] # => "A Tale of Two Cities"
|
94
|
+
```
|
95
|
+
|
80
96
|
**See the examples directory for more.**
|
81
97
|
|
82
98
|
## Contributing
|
data/lib/vtd/xml/generator.rb
CHANGED
@@ -43,12 +43,18 @@ module VTD
|
|
43
43
|
'<books>',
|
44
44
|
' <book title="A Tale of Two Cities" sold="200000000" firstPublished="1859">',
|
45
45
|
' <author name="Charles Dickens" />',
|
46
|
+
' <language>English</language>',
|
47
|
+
' <publisher>Chapman & Hall</publisher>',
|
46
48
|
' </book>',
|
47
49
|
' <book title="The Lord of the Rings" sold="150000000" firstPublished="1954">',
|
48
50
|
' <author name="J. R. R. Tolkien" />',
|
51
|
+
' <language>English</language>',
|
52
|
+
' <publisher>George Allen & Unwin</publisher>',
|
49
53
|
' </book>',
|
50
54
|
' <book title="The Little Prince" sold="140000000" firstPublished="1943">',
|
51
55
|
' <author name="Antoine de Saint-Exupéry" />',
|
56
|
+
' <language>French</language>',
|
57
|
+
' <publisher>Gallimard</publisher>',
|
52
58
|
' </book>',
|
53
59
|
'</books>'
|
54
60
|
end
|
data/lib/vtd/xml/node.rb
CHANGED
@@ -3,6 +3,11 @@ 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
|
+
|
6
11
|
include Attributes
|
7
12
|
|
8
13
|
attr_reader :name, :text
|
@@ -22,11 +27,34 @@ module VTD
|
|
22
27
|
@text ||= string_from_index @nav.get_text
|
23
28
|
end
|
24
29
|
|
30
|
+
def with_first_child(name = nil)
|
31
|
+
if move_to(:first_child, name)
|
32
|
+
yield dup
|
33
|
+
move_to(:parent)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def inspect
|
38
|
+
%(#<#{self.class}:#{self.object_id} @current=#{@current} @nav=#{@nav}>)
|
39
|
+
end
|
40
|
+
|
25
41
|
private
|
26
42
|
|
27
43
|
def string_from_index(index)
|
28
44
|
@nav.to_normalized_string(index) if index != -1
|
29
45
|
end
|
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
|
53
|
+
end
|
54
|
+
|
55
|
+
def find_type(type)
|
56
|
+
TYPES[type] or raise ArgumentError, "Unknown node type: #{type.inspect}"
|
57
|
+
end
|
30
58
|
end
|
31
59
|
end
|
32
60
|
end
|
data/lib/vtd/xml/version.rb
CHANGED
data/spec/vtd/xml/parser_spec.rb
CHANGED
@@ -2,6 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe VTD::Xml::Parser do
|
4
4
|
let(:parser) { VTD::Xml::Parser.new 'spec/fixtures/books.xml' }
|
5
|
+
let(:book) { parser.find('//book[1]').first }
|
5
6
|
|
6
7
|
it 'will pull everything that matches your XPath' do
|
7
8
|
expect(parser.find('//book/author').to_a.length).to eq(3)
|
@@ -15,7 +16,6 @@ describe VTD::Xml::Parser do
|
|
15
16
|
end
|
16
17
|
|
17
18
|
describe 'node attributes' do
|
18
|
-
let(:book) { parser.find('//book[1]').first }
|
19
19
|
let(:title) { 'A Tale of Two Cities' }
|
20
20
|
let(:sold) { '200000000' }
|
21
21
|
let(:published) { '1859' }
|
@@ -46,4 +46,36 @@ describe VTD::Xml::Parser do
|
|
46
46
|
'title' => title, 'sold' => sold, 'firstPublished' => published)
|
47
47
|
end
|
48
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
|
49
81
|
end
|
data/vtd-xml.gemspec
CHANGED
@@ -5,10 +5,10 @@ require 'vtd/xml/version'
|
|
5
5
|
Gem::Specification.new do |gem|
|
6
6
|
gem.name = 'vtd-xml'
|
7
7
|
gem.version = VTD::Xml::VERSION
|
8
|
-
gem.platform
|
8
|
+
gem.platform = 'java'
|
9
9
|
|
10
|
-
gem.authors = ['James Conroy-Finn']
|
11
|
-
gem.email = ['james@logi.cl']
|
10
|
+
gem.authors = ['James Conroy-Finn', 'Jesper Kjeldgaard']
|
11
|
+
gem.email = ['james@logi.cl', 'thejspr@gmail.com']
|
12
12
|
|
13
13
|
gem.description = %q{VTD-XML for JRuby}
|
14
14
|
gem.summary = %q{A thin wrapper around the VTD-XML Java library}
|
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vtd-xml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: java
|
7
7
|
authors:
|
8
8
|
- James Conroy-Finn
|
9
|
+
- Jesper Kjeldgaard
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
13
|
+
date: 2013-02-19 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: rake
|
@@ -32,6 +33,7 @@ dependencies:
|
|
32
33
|
description: VTD-XML for JRuby
|
33
34
|
email:
|
34
35
|
- james@logi.cl
|
36
|
+
- thejspr@gmail.com
|
35
37
|
executables: []
|
36
38
|
extensions: []
|
37
39
|
extra_rdoc_files: []
|