xmlcodec 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +460 -0
- data/README +81 -0
- data/Rakefile +84 -0
- data/lib/XMLElement.rb +489 -0
- data/lib/XMLStreamObjectParser.rb +136 -0
- data/lib/XMLStreamParser.rb +116 -0
- data/lib/XMLSubElements.rb +103 -0
- data/lib/XMLUtils.rb +58 -0
- data/lib/xmlcodec.rb +4 -0
- data/test/TestPartialExport.rb +145 -0
- data/test/TestXMLElement.rb +177 -0
- data/test/TestXMLStreamObjectParser.rb +160 -0
- data/test/TestXMLStreamParser.rb +47 -0
- data/test/TestXMLUtils.rb +17 -0
- data/test/simple_objects.rb +47 -0
- metadata +59 -0
@@ -0,0 +1,160 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "XMLStreamObjectParser"
|
3
|
+
require 'simple_objects.rb'
|
4
|
+
|
5
|
+
include XMLCodec
|
6
|
+
|
7
|
+
class MyStreamListener
|
8
|
+
attr_reader :abc, :subels, :mult, :subel
|
9
|
+
def el_abc(el)
|
10
|
+
@abc = el.get_object
|
11
|
+
end
|
12
|
+
|
13
|
+
def el_subels(el)
|
14
|
+
@subels = el.get_object
|
15
|
+
end
|
16
|
+
|
17
|
+
def el_mult(el)
|
18
|
+
@mult = el.get_object
|
19
|
+
end
|
20
|
+
|
21
|
+
def el_subel(el)
|
22
|
+
@subel = el.get_object
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class MyConsumingStreamListener
|
27
|
+
attr_reader :abc
|
28
|
+
|
29
|
+
def el_abc(el)
|
30
|
+
el.consume
|
31
|
+
@abc = el.get_object
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class TestXMLStreamObjectParser < Test::Unit::TestCase
|
36
|
+
def initialize(*args)
|
37
|
+
super(*args)
|
38
|
+
|
39
|
+
@test_file = "test_ead_stream_object_parser.xml"
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_simple
|
43
|
+
value = 'somevalue'
|
44
|
+
file = '<abc>'+value+'</abc>'
|
45
|
+
|
46
|
+
listener = MyStreamListener.new
|
47
|
+
parser = XMLStreamObjectParser.new(listener)
|
48
|
+
parser.parse(file)
|
49
|
+
el = listener.abc
|
50
|
+
assert_equal el.value, value
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_attr
|
54
|
+
value = 'somevalue'
|
55
|
+
attrvalue = 'the attr value'
|
56
|
+
file = '<subel someattr="'+attrvalue+'"><abc>'+value+'</abc></subel>'
|
57
|
+
|
58
|
+
listener = MyStreamListener.new
|
59
|
+
parser = XMLStreamObjectParser.new(listener)
|
60
|
+
parser.parse(file)
|
61
|
+
|
62
|
+
el = listener.abc
|
63
|
+
assert_equal el.value, value
|
64
|
+
|
65
|
+
subel = parser.top_element
|
66
|
+
assert_equal el, subel.abc
|
67
|
+
assert_equal attrvalue, subel.someattr
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_multiple_names
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_mult
|
75
|
+
value = 'somevalue'
|
76
|
+
file = '<mult><abc>'+value+'</abc></mult>'
|
77
|
+
|
78
|
+
listener = MyStreamListener.new
|
79
|
+
parser = XMLStreamObjectParser.new(listener)
|
80
|
+
parser.parse(file)
|
81
|
+
|
82
|
+
el = listener.abc
|
83
|
+
assert_equal el.value, value
|
84
|
+
assert_equal el, parser.top_element.abc[0]
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_subelements
|
88
|
+
value = 'somevalue'
|
89
|
+
file = '<subels><abc>'+value+'</abc></subels>'
|
90
|
+
|
91
|
+
listener = MyStreamListener.new
|
92
|
+
parser = XMLStreamObjectParser.new(listener)
|
93
|
+
parser.parse(file)
|
94
|
+
|
95
|
+
el = listener.abc
|
96
|
+
assert_equal el.value, value
|
97
|
+
assert_equal el, parser.top_element.subelements[0]
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_consume_mult
|
101
|
+
value = 'somevalue'
|
102
|
+
file = '<subel><abc>'+value+'</abc></subel>'
|
103
|
+
|
104
|
+
listener = MyConsumingStreamListener.new
|
105
|
+
parser = XMLStreamObjectParser.new(listener)
|
106
|
+
parser.parse(file)
|
107
|
+
|
108
|
+
el = listener.abc
|
109
|
+
assert_equal el.value, value
|
110
|
+
assert_nil parser.top_element.abc
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_multnames_subelements
|
114
|
+
value1 = 'somevalue1'
|
115
|
+
value2 = 'somevalue2'
|
116
|
+
file = '<subels><abc2>'+value1+'</abc2><abc3>'+value2+'</abc3></subels>'
|
117
|
+
|
118
|
+
listener = MyStreamListener.new
|
119
|
+
parser = XMLStreamObjectParser.new(listener)
|
120
|
+
parser.parse(file)
|
121
|
+
|
122
|
+
el = listener.subels
|
123
|
+
assert_equal 2, el.subelements.size
|
124
|
+
[value1, value2].each_with_index do |value, index|
|
125
|
+
assert_kind_of SimpleElementMultName, el.subelements[index]
|
126
|
+
assert_equal value, el.subelements[index].value
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_multnames_subel_mult
|
131
|
+
value1 = 'somevalue1'
|
132
|
+
value2 = 'somevalue2'
|
133
|
+
file = '<mult><abc2>'+value1+'</abc2><abc3>'+value2+'</abc3></mult>'
|
134
|
+
|
135
|
+
listener = MyStreamListener.new
|
136
|
+
parser = XMLStreamObjectParser.new(listener)
|
137
|
+
parser.parse(file)
|
138
|
+
|
139
|
+
el = listener.mult
|
140
|
+
assert_equal 2, el.abc2.size
|
141
|
+
[value1, value2].each_with_index do |value, index|
|
142
|
+
assert_kind_of SimpleElementMultName, el.abc2[index]
|
143
|
+
assert_equal value, el.abc2[index].value
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_multnames_subel
|
148
|
+
value = 'somevalue'
|
149
|
+
file = '<subel><abc3>'+value+'</abc3></subel>'
|
150
|
+
|
151
|
+
listener = MyStreamListener.new
|
152
|
+
parser = XMLStreamObjectParser.new(listener)
|
153
|
+
parser.parse(file)
|
154
|
+
|
155
|
+
el = listener.subel
|
156
|
+
assert_not_nil el.abc2
|
157
|
+
assert_kind_of SimpleElementMultName, el.abc2
|
158
|
+
assert_equal value, el.abc2.value
|
159
|
+
end
|
160
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..")
|
2
|
+
$-w = true
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'XMLStreamParser'
|
6
|
+
|
7
|
+
class MyStreamListener
|
8
|
+
attr_reader :def_id, :def_parent_id
|
9
|
+
attr_reader :abc_id, :abc_parent_id
|
10
|
+
def element(el)
|
11
|
+
case el.name
|
12
|
+
when "def"
|
13
|
+
@def_id, = el.element_id
|
14
|
+
@def_parent_id = el.parent_id
|
15
|
+
el.consume
|
16
|
+
when "abc"
|
17
|
+
@abc_id, = el.element_id
|
18
|
+
@abc_parent_id = el.parent_id
|
19
|
+
el.consume
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class TestXMLStreamParser < Test::Unit::TestCase
|
25
|
+
def initialize(*args)
|
26
|
+
super(*args)
|
27
|
+
@doc = "<abc attr1='1' attr2='2'>abc<xpto attr1='a'></xpto><def><x></x></def></abc>"
|
28
|
+
@doc_no_def = "<abc attr1='1' attr2='2'>abc<xpto attr1='a'></xpto></abc>"
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_total_parse
|
32
|
+
parser = XMLUtils::XMLStreamParser.new
|
33
|
+
parser.parse(@doc)
|
34
|
+
assert_equal(@doc, parser.content)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_consumed_parse
|
38
|
+
listener = MyStreamListener.new
|
39
|
+
parser = XMLUtils::XMLStreamParser.new(listener)
|
40
|
+
parser.parse(@doc)
|
41
|
+
assert_equal(@doc_no_def, parser.content)
|
42
|
+
assert_equal(listener.abc_id, listener.def_parent_id)
|
43
|
+
assert_equal(0, listener.abc_parent_id)
|
44
|
+
assert_equal(1, listener.abc_id)
|
45
|
+
assert(listener.def_id > listener.def_parent_id)
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..")
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'XMLUtils'
|
5
|
+
|
6
|
+
class TestXMLUtils < Test::Unit::TestCase
|
7
|
+
def test_create_open_tag
|
8
|
+
tag = XMLUtils::create_open_tag("name", {"arg1" => "val1", "arg2" => "val2"})
|
9
|
+
assert_equal("<name arg1='val1' arg2='val2'>", tag)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_escape_xml
|
13
|
+
assert_equal '< abc', XMLUtils::escape_xml('< abc')
|
14
|
+
assert_equal '> abc', XMLUtils::escape_xml('> abc')
|
15
|
+
assert_equal '& abc', XMLUtils::escape_xml('& abc')
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'XMLElement'
|
2
|
+
include XMLCodec
|
3
|
+
|
4
|
+
class SimpleElement < XMLElement
|
5
|
+
elwithvalue
|
6
|
+
elname 'abc'
|
7
|
+
end
|
8
|
+
|
9
|
+
class SimpleElement2 < XMLElement
|
10
|
+
elwithvalue
|
11
|
+
elname 'abc1'
|
12
|
+
end
|
13
|
+
|
14
|
+
class SimpleElementMultName < XMLElement
|
15
|
+
elwithvalue
|
16
|
+
elnames 'abc2', 'abc3'
|
17
|
+
|
18
|
+
attr_writer :set_elname
|
19
|
+
|
20
|
+
alias_method :oldelname, :elname
|
21
|
+
def elname
|
22
|
+
if instance_variables.index("@set_elname")
|
23
|
+
return @set_elname
|
24
|
+
else
|
25
|
+
return 'abc2'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class TestElement < XMLElement
|
31
|
+
elname 'subel'
|
32
|
+
xmlsubel :abc
|
33
|
+
xmlsubel :abc2
|
34
|
+
xmlsubel :subel
|
35
|
+
xmlattr :someattr
|
36
|
+
end
|
37
|
+
|
38
|
+
class SubelElement < XMLElement
|
39
|
+
elname 'subels'
|
40
|
+
xmlsubelements
|
41
|
+
end
|
42
|
+
|
43
|
+
class SubelMultElement < XMLElement
|
44
|
+
elname 'mult'
|
45
|
+
xmlsubel_mult :abc
|
46
|
+
xmlsubel_mult :abc2
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: xmlcodec
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2006-05-12 00:00:00 +01:00
|
8
|
+
summary: Generic Importer/Exporter of XML formats
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: pedrocr@gmail.com
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description: A library that eases the creation of XML importers and exporters for ruby.
|
15
|
+
autorequire: rake
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
authors:
|
29
|
+
- "Pedro C\xC3\xB4rte-Real"
|
30
|
+
files:
|
31
|
+
- test/TestXMLStreamParser.rb
|
32
|
+
- test/TestXMLElement.rb
|
33
|
+
- test/TestPartialExport.rb
|
34
|
+
- test/TestXMLUtils.rb
|
35
|
+
- test/TestXMLStreamObjectParser.rb
|
36
|
+
- test/simple_objects.rb
|
37
|
+
- lib/XMLStreamParser.rb
|
38
|
+
- lib/XMLElement.rb
|
39
|
+
- lib/XMLStreamObjectParser.rb
|
40
|
+
- lib/XMLSubElements.rb
|
41
|
+
- lib/XMLUtils.rb
|
42
|
+
- lib/xmlcodec.rb
|
43
|
+
- README
|
44
|
+
- LICENSE
|
45
|
+
- Rakefile
|
46
|
+
test_files: []
|
47
|
+
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
extra_rdoc_files: []
|
51
|
+
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
requirements:
|
57
|
+
- none
|
58
|
+
dependencies: []
|
59
|
+
|