xml-mapping 0.9.1 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +10 -53
- data/README.md +57 -0
- data/Rakefile +75 -85
- data/TODO.txt +12 -2
- data/examples/README +4 -4
- data/examples/cleanup.rb +11 -0
- data/examples/company_usage.intout +7 -7
- data/examples/documents_folders_usage.intout +2 -2
- data/examples/order_signature_enhanced_usage.intout +3 -3
- data/examples/order_usage.intout +26 -26
- data/examples/person.intout +3 -3
- data/examples/person_mm.intout +2 -2
- data/examples/publication.intout +2 -2
- data/examples/reader.intout +1 -1
- data/examples/stringarray_usage.intout +1 -1
- data/examples/time_augm.intout +6 -6
- data/examples/xpath_create_new.intout +13 -13
- data/examples/xpath_ensure_created.intout +2 -2
- data/examples/xpath_usage.intout +1 -1
- data/lib/xml/mapping.rb +0 -2
- data/lib/xml/mapping/base.rb +24 -9
- data/lib/xml/mapping/version.rb +1 -1
- data/test/company.rb +43 -0
- data/test/documents_folders.rb +7 -0
- data/test/multiple_mappings_test.rb +3 -1
- data/test/xml_mapping_adv_test.rb +20 -20
- data/test/xml_mapping_test.rb +31 -2
- data/{README → user_manual.md} +916 -154
- data/user_manual_xxpath.md +677 -0
- metadata +100 -112
- data/ChangeLog +0 -189
- data/README_XPATH +0 -202
- data/examples/company_usage.intin.rb +0 -19
- data/examples/documents_folders_usage.intin.rb +0 -18
- data/examples/order_signature_enhanced_usage.intin.rb +0 -12
- data/examples/order_usage.intin.rb +0 -120
- data/examples/person.intin.rb +0 -44
- data/examples/person_mm.intin.rb +0 -119
- data/examples/publication.intin.rb +0 -44
- data/examples/reader.intin.rb +0 -33
- data/examples/stringarray_usage.intin.rb +0 -11
- data/examples/time_augm.intin.rb +0 -19
- data/examples/time_augm_loading.intin.rb +0 -44
- data/examples/time_node.intin.rb +0 -79
- data/examples/time_node_w_marshallers.intin.rb +0 -48
- data/examples/xpath_create_new.intin.rb +0 -85
- data/examples/xpath_docvsroot.intin.rb +0 -30
- data/examples/xpath_ensure_created.intin.rb +0 -62
- data/examples/xpath_pathological.intin.rb +0 -42
- data/examples/xpath_usage.intin.rb +0 -51
- data/install.rb +0 -41
- data/test/xxpath_benchmark.result1.txt +0 -17
@@ -1,44 +0,0 @@
|
|
1
|
-
#:invisible:
|
2
|
-
$:.unshift "../lib"
|
3
|
-
require 'xml/mapping'
|
4
|
-
require 'xml/xxpath_methods'
|
5
|
-
|
6
|
-
class Time
|
7
|
-
include XML::Mapping
|
8
|
-
|
9
|
-
numeric_node :year, "year"
|
10
|
-
numeric_node :month, "month"
|
11
|
-
numeric_node :day, "mday"
|
12
|
-
numeric_node :hour, "hours"
|
13
|
-
numeric_node :min, "minutes"
|
14
|
-
numeric_node :sec, "seconds"
|
15
|
-
end
|
16
|
-
|
17
|
-
#<=
|
18
|
-
#:invisible_retval:
|
19
|
-
#:visible:
|
20
|
-
|
21
|
-
def Time.load_from_xml(xml, options={:mapping=>:_default})
|
22
|
-
year,month,day,hour,min,sec =
|
23
|
-
[xml.first_xpath("year").text.to_i,
|
24
|
-
xml.first_xpath("month").text.to_i,
|
25
|
-
xml.first_xpath("mday").text.to_i,
|
26
|
-
xml.first_xpath("hours").text.to_i,
|
27
|
-
xml.first_xpath("minutes").text.to_i,
|
28
|
-
xml.first_xpath("seconds").text.to_i]
|
29
|
-
Time.local(year,month,day,hour,min,sec)
|
30
|
-
end
|
31
|
-
#<=
|
32
|
-
#:invisible:
|
33
|
-
require 'test/unit/assertions'
|
34
|
-
include Test::Unit::Assertions
|
35
|
-
|
36
|
-
t = Time.now
|
37
|
-
t2 = Time.load_from_xml(t.save_to_xml)
|
38
|
-
|
39
|
-
assert_equal t.year, t2.year
|
40
|
-
assert_equal t.month, t2.month
|
41
|
-
assert_equal t.day, t2.day
|
42
|
-
assert_equal t.hour, t2.hour
|
43
|
-
assert_equal t.min, t2.min
|
44
|
-
assert_equal t.sec, t2.sec
|
data/examples/time_node.intin.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
#:invisible:
|
2
|
-
$:.unshift "../lib"
|
3
|
-
#<=
|
4
|
-
#:visible:
|
5
|
-
#:invisible_retval:
|
6
|
-
STDERR.puts "time_node top:\n#{$:.inspect}"
|
7
|
-
|
8
|
-
require 'xml/mapping/base'
|
9
|
-
|
10
|
-
STDERR.puts "time_node after require:\n#{$:.inspect}"
|
11
|
-
|
12
|
-
class TimeNode < XML::Mapping::SingleAttributeNode
|
13
|
-
def initialize(*args)
|
14
|
-
STDERR.puts "TimeNode#initialize top:\n#{$:.inspect}"
|
15
|
-
path,*args = super(*args)
|
16
|
-
@y_path = XML::XXPath.new(path+"/year")
|
17
|
-
@m_path = XML::XXPath.new(path+"/month")
|
18
|
-
@d_path = XML::XXPath.new(path+"/day")
|
19
|
-
args
|
20
|
-
end
|
21
|
-
|
22
|
-
def extract_attr_value(xml)
|
23
|
-
y,m,d = default_when_xpath_err{ [@y_path.first(xml).text.to_i,
|
24
|
-
@m_path.first(xml).text.to_i,
|
25
|
-
@d_path.first(xml).text.to_i]
|
26
|
-
}
|
27
|
-
Time.local(y,m,d)
|
28
|
-
end
|
29
|
-
|
30
|
-
def set_attr_value(xml, value)
|
31
|
-
@y_path.first(xml,:ensure_created=>true).text = value.year
|
32
|
-
@m_path.first(xml,:ensure_created=>true).text = value.month
|
33
|
-
@d_path.first(xml,:ensure_created=>true).text = value.day
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
XML::Mapping.add_node_class TimeNode
|
38
|
-
|
39
|
-
#<=
|
40
|
-
#:invisible:
|
41
|
-
require 'xml/mapping'
|
42
|
-
|
43
|
-
class Signature
|
44
|
-
include XML::Mapping
|
45
|
-
|
46
|
-
text_node :name, "Name"
|
47
|
-
text_node :position, "Position", :default_value=>"Some Employee"
|
48
|
-
time_node :signed_on, "signed-on", :default_value=>Time.now
|
49
|
-
end
|
50
|
-
|
51
|
-
|
52
|
-
require 'test/unit/assertions'
|
53
|
-
include Test::Unit::Assertions
|
54
|
-
|
55
|
-
xml = REXML::Document.new('
|
56
|
-
<Signature>
|
57
|
-
<Name>John Doe</Name>
|
58
|
-
<Position>product manager</Position>
|
59
|
-
<signed-on>
|
60
|
-
<day>13</day>
|
61
|
-
<month>2</month>
|
62
|
-
<year>2005</year>
|
63
|
-
</signed-on>
|
64
|
-
</Signature>
|
65
|
-
').root
|
66
|
-
|
67
|
-
|
68
|
-
s = Signature.load_from_xml xml
|
69
|
-
assert_equal Time.local(2005,2,13), s.signed_on
|
70
|
-
|
71
|
-
s.signed_on = Time.local(2006,6,15)
|
72
|
-
xml2 = s.save_to_xml
|
73
|
-
|
74
|
-
require 'xml/xxpath_methods'
|
75
|
-
assert_equal "15", xml2.first_xpath("signed-on/day").text
|
76
|
-
assert_equal "6", xml2.first_xpath("signed-on/month").text
|
77
|
-
assert_equal "2006", xml2.first_xpath("signed-on/year").text
|
78
|
-
|
79
|
-
#<=
|
@@ -1,48 +0,0 @@
|
|
1
|
-
#:invisible:
|
2
|
-
#:invisible_retval:
|
3
|
-
$:.unshift "../lib" #<=
|
4
|
-
#:visible:
|
5
|
-
require 'xml/mapping'
|
6
|
-
require 'xml/xxpath_methods'
|
7
|
-
|
8
|
-
class Signature
|
9
|
-
include XML::Mapping
|
10
|
-
|
11
|
-
text_node :name, "Name"
|
12
|
-
text_node :position, "Position", :default_value=>"Some Employee"
|
13
|
-
object_node :signed_on, "signed-on",
|
14
|
-
:unmarshaller=>proc{|xml|
|
15
|
-
y,m,d = [xml.first_xpath("year").text.to_i,
|
16
|
-
xml.first_xpath("month").text.to_i,
|
17
|
-
xml.first_xpath("day").text.to_i]
|
18
|
-
Time.local(y,m,d)
|
19
|
-
},
|
20
|
-
:marshaller=>proc{|xml,value|
|
21
|
-
e = xml.elements.add; e.name = "year"; e.text = value.year
|
22
|
-
e = xml.elements.add; e.name = "month"; e.text = value.month
|
23
|
-
e = xml.elements.add; e.name = "day"; e.text = value.day
|
24
|
-
|
25
|
-
# xml.first("year",:ensure_created=>true).text = value.year
|
26
|
-
# xml.first("month",:ensure_created=>true).text = value.month
|
27
|
-
# xml.first("day",:ensure_created=>true).text = value.day
|
28
|
-
}
|
29
|
-
end #<=
|
30
|
-
#:invisible:
|
31
|
-
require 'test/unit/assertions'
|
32
|
-
|
33
|
-
include Test::Unit::Assertions
|
34
|
-
|
35
|
-
t=Time.local(2005,12,1)
|
36
|
-
|
37
|
-
s=Signature.new
|
38
|
-
s.name = "Olaf Klischat"; s.position="chief"; s.signed_on=t
|
39
|
-
xml = s.save_to_xml
|
40
|
-
|
41
|
-
assert_equal "2005", xml.first_xpath("signed-on/year").text
|
42
|
-
assert_equal "12", xml.first_xpath("signed-on/month").text
|
43
|
-
assert_equal "1", xml.first_xpath("signed-on/day").text
|
44
|
-
|
45
|
-
s2 = Signature.load_from_xml xml
|
46
|
-
assert_equal "Olaf Klischat", s2.name
|
47
|
-
assert_equal "chief", s2.position
|
48
|
-
assert_equal t, s2.signed_on
|
@@ -1,85 +0,0 @@
|
|
1
|
-
#:invisible:
|
2
|
-
$:.unshift "../lib" #<=
|
3
|
-
#:visible:
|
4
|
-
require 'xml/xxpath'
|
5
|
-
|
6
|
-
d=REXML::Document.new <<EOS
|
7
|
-
<foo>
|
8
|
-
<bar>
|
9
|
-
<baz key="work">Java</baz>
|
10
|
-
<baz key="play">Ruby</baz>
|
11
|
-
</bar>
|
12
|
-
</foo>
|
13
|
-
EOS
|
14
|
-
|
15
|
-
|
16
|
-
rootelt=d.root
|
17
|
-
|
18
|
-
#:invisible_retval:
|
19
|
-
path1=XML::XXPath.new("/bar/baz[@key='work']")
|
20
|
-
|
21
|
-
#:visible_retval:
|
22
|
-
path1.create_new(rootelt)#<=
|
23
|
-
#:invisible_retval:
|
24
|
-
d.write($stdout,2)#<=
|
25
|
-
### a new element is created for *each* path element, regardless of
|
26
|
-
### what existed before. So a new "bar" element was added, with a new
|
27
|
-
### "baz" element inside it
|
28
|
-
|
29
|
-
### same call again...
|
30
|
-
#:visible_retval:
|
31
|
-
path1.create_new(rootelt)#<=
|
32
|
-
#:invisible_retval:
|
33
|
-
d.write($stdout,2)#<=
|
34
|
-
### same procedure -- new elements added for each path element
|
35
|
-
|
36
|
-
|
37
|
-
#:visible_retval:
|
38
|
-
## get reference to 1st "baz" element
|
39
|
-
firstbazelt=XML::XXPath.new("/bar/baz").first(rootelt)#<=
|
40
|
-
|
41
|
-
#:invisible_retval:
|
42
|
-
path2=XML::XXPath.new("@key2")
|
43
|
-
|
44
|
-
#:visible_retval:
|
45
|
-
path2.create_new(firstbazelt)#<=
|
46
|
-
#:invisible_retval:
|
47
|
-
d.write($stdout,2)#<=
|
48
|
-
### ok, new attribute node added
|
49
|
-
|
50
|
-
### same call again...
|
51
|
-
#:visible_retval:
|
52
|
-
#:handle_exceptions:
|
53
|
-
path2.create_new(firstbazelt)#<=
|
54
|
-
#:no_exceptions:
|
55
|
-
### can't create that path anew again -- an element can't have more
|
56
|
-
### than one attribute with the same name
|
57
|
-
|
58
|
-
#:invisible_retval:
|
59
|
-
### the document hasn't changed
|
60
|
-
d.write($stdout,2)#<=
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
### create_new the same path as in the ensure_created example
|
65
|
-
#:visible_retval:
|
66
|
-
baz6elt=XML::XXPath.new("/bar/baz[6]").create_new(rootelt)#<=
|
67
|
-
#:invisible_retval:
|
68
|
-
d.write($stdout,2)#<=
|
69
|
-
### ok, new "bar" element and 6th "baz" element inside it created
|
70
|
-
|
71
|
-
|
72
|
-
#:visible_retval:
|
73
|
-
#:handle_exceptions:
|
74
|
-
XML::XXPath.new("baz[6]").create_new(baz6elt.parent)#<=
|
75
|
-
#:no_exceptions:
|
76
|
-
#:invisible_retval:
|
77
|
-
### yep, baz[6] already existed and thus couldn't be created once
|
78
|
-
### again
|
79
|
-
|
80
|
-
### but of course...
|
81
|
-
#:visible_retval:
|
82
|
-
XML::XXPath.new("/bar/baz[6]").create_new(rootelt)#<=
|
83
|
-
#:invisible_retval:
|
84
|
-
d.write($stdout,2)#<=
|
85
|
-
### this works because *all* path elements are newly created
|
@@ -1,30 +0,0 @@
|
|
1
|
-
#:invisible:
|
2
|
-
$:.unshift "../lib" #<=
|
3
|
-
#:visible:
|
4
|
-
require 'xml/xxpath'
|
5
|
-
|
6
|
-
d=REXML::Document.new <<EOS
|
7
|
-
<foo>
|
8
|
-
<bar x="hello">
|
9
|
-
<first>
|
10
|
-
<second>pingpong</second>
|
11
|
-
</first>
|
12
|
-
</bar>
|
13
|
-
<bar x="goodbye"/>
|
14
|
-
</foo>
|
15
|
-
EOS
|
16
|
-
|
17
|
-
XML::XXPath.new("/foo/bar").all(d)#<=
|
18
|
-
|
19
|
-
XML::XXPath.new("/bar").all(d)#<=
|
20
|
-
|
21
|
-
XML::XXPath.new("/foo/bar").all(d.root)#<=
|
22
|
-
|
23
|
-
XML::XXPath.new("/bar").all(d.root)#<=
|
24
|
-
|
25
|
-
|
26
|
-
firstelt = XML::XXPath.new("/foo/bar/first").first(d)#<=
|
27
|
-
|
28
|
-
XML::XXPath.new("/first/second").all(firstelt)#<=
|
29
|
-
|
30
|
-
XML::XXPath.new("/second").all(firstelt)#<=
|
@@ -1,62 +0,0 @@
|
|
1
|
-
#:invisible:
|
2
|
-
$:.unshift "../lib" #<=
|
3
|
-
#:visible:
|
4
|
-
require 'xml/xxpath'
|
5
|
-
|
6
|
-
d=REXML::Document.new <<EOS
|
7
|
-
<foo>
|
8
|
-
<bar>
|
9
|
-
<baz key="work">Java</baz>
|
10
|
-
<baz key="play">Ruby</baz>
|
11
|
-
</bar>
|
12
|
-
</foo>
|
13
|
-
EOS
|
14
|
-
|
15
|
-
|
16
|
-
rootelt=d.root
|
17
|
-
|
18
|
-
#### ensuring that a specific path exists inside the document
|
19
|
-
|
20
|
-
#:visible_retval:
|
21
|
-
XML::XXPath.new("/bar/baz[@key='work']").first(rootelt,:ensure_created=>true)#<=
|
22
|
-
#:invisible_retval:
|
23
|
-
d.write($stdout,2)#<=
|
24
|
-
### no change (path existed before)
|
25
|
-
|
26
|
-
|
27
|
-
#:visible_retval:
|
28
|
-
XML::XXPath.new("/bar/baz[@key='42']").first(rootelt,:ensure_created=>true)#<=
|
29
|
-
#:invisible_retval:
|
30
|
-
d.write($stdout,2)#<=
|
31
|
-
### path was added
|
32
|
-
|
33
|
-
#:visible_retval:
|
34
|
-
XML::XXPath.new("/bar/baz[@key='42']").first(rootelt,:ensure_created=>true)#<=
|
35
|
-
#:invisible_retval:
|
36
|
-
d.write($stdout,2)#<=
|
37
|
-
### no change this time
|
38
|
-
|
39
|
-
#:visible_retval:
|
40
|
-
XML::XXPath.new("/bar/baz[@key2='hello']").first(rootelt,:ensure_created=>true)#<=
|
41
|
-
#:invisible_retval:
|
42
|
-
d.write($stdout,2)#<=
|
43
|
-
### this fit in the 1st "baz" element since
|
44
|
-
### there was no "key2" attribute there before.
|
45
|
-
|
46
|
-
#:visible_retval:
|
47
|
-
XML::XXPath.new("/bar/baz[2]").first(rootelt,:ensure_created=>true)#<=
|
48
|
-
#:invisible_retval:
|
49
|
-
d.write($stdout,2)#<=
|
50
|
-
### no change
|
51
|
-
|
52
|
-
#:visible_retval:
|
53
|
-
XML::XXPath.new("/bar/baz[6]/@haha").first(rootelt,:ensure_created=>true)#<=
|
54
|
-
#:invisible_retval:
|
55
|
-
d.write($stdout,2)#<=
|
56
|
-
### for there to be a 6th "baz" element, there must be 1st..5th "baz" elements
|
57
|
-
|
58
|
-
#:visible_retval:
|
59
|
-
XML::XXPath.new("/bar/baz[6]/@haha").first(rootelt,:ensure_created=>true)#<=
|
60
|
-
#:invisible_retval:
|
61
|
-
d.write($stdout,2)#<=
|
62
|
-
### no change this time
|
@@ -1,42 +0,0 @@
|
|
1
|
-
#:invisible:
|
2
|
-
$:.unshift "../lib" #<=
|
3
|
-
#:visible:
|
4
|
-
require 'xml/xxpath'
|
5
|
-
|
6
|
-
d=REXML::Document.new <<EOS
|
7
|
-
<foo>
|
8
|
-
<bar/>
|
9
|
-
<bar/>
|
10
|
-
</foo>
|
11
|
-
EOS
|
12
|
-
|
13
|
-
|
14
|
-
rootelt=d.root
|
15
|
-
|
16
|
-
|
17
|
-
XML::XXPath.new("*").all(rootelt)#<=
|
18
|
-
### ok
|
19
|
-
|
20
|
-
XML::XXPath.new("bar/*").first(rootelt, :allow_nil=>true)#<=
|
21
|
-
### ok, nothing there
|
22
|
-
|
23
|
-
### the same call with :ensure_created=>true
|
24
|
-
newelt = XML::XXPath.new("bar/*").first(rootelt, :ensure_created=>true)#<=
|
25
|
-
|
26
|
-
#:invisible_retval:
|
27
|
-
d.write($stdout,2)#<=
|
28
|
-
|
29
|
-
#:visible_retval:
|
30
|
-
### a new "unspecified" element was created
|
31
|
-
newelt.unspecified?#<=
|
32
|
-
|
33
|
-
### we must modify it to "specify" it
|
34
|
-
newelt.name="new-one"
|
35
|
-
newelt.text="hello!"
|
36
|
-
newelt.unspecified?#<=
|
37
|
-
|
38
|
-
#:invisible_retval:
|
39
|
-
d.write($stdout,2)#<=
|
40
|
-
|
41
|
-
### you could also set unspecified to false explicitly, as in:
|
42
|
-
newelt.unspecified=true
|
@@ -1,51 +0,0 @@
|
|
1
|
-
#:invisible:
|
2
|
-
$:.unshift "../lib" #<=
|
3
|
-
#:visible:
|
4
|
-
require 'xml/xxpath'
|
5
|
-
|
6
|
-
d=REXML::Document.new <<EOS
|
7
|
-
<foo>
|
8
|
-
<bar>
|
9
|
-
<baz key="work">Java</baz>
|
10
|
-
<baz key="play">Ruby</baz>
|
11
|
-
</bar>
|
12
|
-
<bar>
|
13
|
-
<baz key="ab">hello</baz>
|
14
|
-
<baz key="play">scrabble</baz>
|
15
|
-
<baz key="xy">goodbye</baz>
|
16
|
-
</bar>
|
17
|
-
<more>
|
18
|
-
<baz key="play">poker</baz>
|
19
|
-
</more>
|
20
|
-
</foo>
|
21
|
-
EOS
|
22
|
-
|
23
|
-
|
24
|
-
####read access
|
25
|
-
path=XML::XXPath.new("/foo/bar[2]/baz")
|
26
|
-
|
27
|
-
## path.all(document) gives all elements matching path in document
|
28
|
-
path.all(d)#<=
|
29
|
-
|
30
|
-
## loop over them
|
31
|
-
path.each(d){|elt| puts elt.text}#<=
|
32
|
-
|
33
|
-
## the first of those
|
34
|
-
path.first(d)#<=
|
35
|
-
|
36
|
-
## no match here (only three "baz" elements)
|
37
|
-
path2=XML::XXPath.new("/foo/bar[2]/baz[4]")
|
38
|
-
path2.all(d)#<=
|
39
|
-
|
40
|
-
#:handle_exceptions:
|
41
|
-
## "first" raises XML::XXPathError in such cases...
|
42
|
-
path2.first(d)#<=
|
43
|
-
#:no_exceptions:
|
44
|
-
|
45
|
-
##...unless we allow nil returns
|
46
|
-
path2.first(d,:allow_nil=>true)#<=
|
47
|
-
|
48
|
-
##attribute nodes can also be returned
|
49
|
-
keysPath=XML::XXPath.new("/foo/*/*/@key")
|
50
|
-
|
51
|
-
keysPath.all(d).map{|attr|attr.text}#<=
|