rxsd 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +56 -0
- data/Rakefile +46 -0
- data/bin/gen_ruby_definitions.rb +37 -0
- data/bin/rxsd-test.rb +3 -10
- data/lib/rxsd.rb +1 -1
- data/lib/rxsd/builder.rb +11 -8
- data/lib/rxsd/builders/ruby_class.rb +2 -2
- data/lib/rxsd/builders/ruby_definition.rb +1 -1
- data/lib/rxsd/builders/ruby_object.rb +1 -1
- data/lib/rxsd/builtin_types.rb +32 -9
- data/lib/rxsd/common.rb +2 -1
- data/lib/rxsd/exceptions.rb +1 -1
- data/lib/rxsd/libxml_adapter.rb +61 -24
- data/lib/rxsd/loader.rb +1 -1
- data/lib/rxsd/parser.rb +8 -8
- data/lib/rxsd/resolver.rb +2 -2
- data/lib/rxsd/translator.rb +12 -12
- data/lib/rxsd/xml.rb +31 -18
- data/lib/rxsd/xsd/attribute.rb +1 -1
- data/lib/rxsd/xsd/attribute_group.rb +1 -1
- data/lib/rxsd/xsd/choice.rb +1 -1
- data/lib/rxsd/xsd/complex_content.rb +1 -1
- data/lib/rxsd/xsd/complex_type.rb +3 -2
- data/lib/rxsd/xsd/element.rb +5 -5
- data/lib/rxsd/xsd/extension.rb +1 -1
- data/lib/rxsd/xsd/group.rb +1 -1
- data/lib/rxsd/xsd/list.rb +1 -1
- data/lib/rxsd/xsd/restriction.rb +1 -1
- data/lib/rxsd/xsd/schema.rb +3 -2
- data/lib/rxsd/xsd/sequence.rb +1 -1
- data/lib/rxsd/xsd/simple_content.rb +1 -1
- data/lib/rxsd/xsd/simple_type.rb +1 -1
- data/spec/builder_spec.rb +224 -0
- data/spec/loader_spec.rb +22 -0
- data/spec/parser_spec.rb +475 -0
- data/spec/resolver_spec.rb +609 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/translator_spec.rb +105 -0
- data/spec/types_spec.rb +47 -0
- data/spec/xml_spec.rb +189 -0
- metadata +45 -55
- data/README +0 -19
data/lib/rxsd/loader.rb
CHANGED
data/lib/rxsd/parser.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# xml / xsd parsers
|
2
2
|
#
|
3
|
-
# Copyright (C)
|
3
|
+
# Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
|
4
4
|
# See COPYING for the License of this software
|
5
5
|
|
6
6
|
module RXSD
|
7
7
|
|
8
|
-
#
|
8
|
+
# Provides class methods to parse xsd and xml data
|
9
9
|
class Parser
|
10
10
|
private
|
11
11
|
def initialize
|
@@ -13,10 +13,10 @@ class Parser
|
|
13
13
|
|
14
14
|
public
|
15
15
|
|
16
|
-
#
|
16
|
+
# Parse xsd specified by uri or in raw data form into RXSD::XSD::Schema instance
|
17
17
|
# args should be a hash w/ optional keys:
|
18
|
-
#
|
19
|
-
#
|
18
|
+
# * :uri location which to load resource from
|
19
|
+
# * :raw raw data which to parse
|
20
20
|
def self.parse_xsd(args)
|
21
21
|
data = Loader.load(args[:uri]) unless args[:uri].nil?
|
22
22
|
data = args[:raw] unless args[:raw].nil?
|
@@ -33,7 +33,7 @@ class Parser
|
|
33
33
|
return schema
|
34
34
|
end
|
35
35
|
|
36
|
-
#
|
36
|
+
# Parse xml specified by uri or in raw data form into RXSD::XSD::SchemaInstance instance
|
37
37
|
def self.parse_xml(args)
|
38
38
|
data = Loader.load(args[:uri]) unless args[:uri].nil?
|
39
39
|
data = args[:raw] unless args[:raw].nil?
|
@@ -46,12 +46,12 @@ class Parser
|
|
46
46
|
return schema_instance
|
47
47
|
end
|
48
48
|
|
49
|
-
#
|
49
|
+
# Return true is specified class is builtin, else false
|
50
50
|
def self.is_builtin?(builtin_class)
|
51
51
|
[Array, String, Boolean, Char, Time, XSDFloat, XSDInteger].include? builtin_class
|
52
52
|
end
|
53
53
|
|
54
|
-
#
|
54
|
+
# Return ruby class corresponding to builtin type
|
55
55
|
def self.parse_builtin_type(builtin_type_name)
|
56
56
|
res = nil
|
57
57
|
|
data/lib/rxsd/resolver.rb
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
#
|
3
3
|
# resolves hanging node relationships and provides overall node access
|
4
4
|
#
|
5
|
-
# Copyright (C)
|
5
|
+
# Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
|
6
6
|
# See COPYING for the License of this software
|
7
7
|
|
8
8
|
module RXSD
|
9
9
|
|
10
|
-
#
|
10
|
+
# Resolves xsd relationships, used internally
|
11
11
|
class Resolver
|
12
12
|
|
13
13
|
# return hash of xsd types -> array of type instances for all nodes
|
data/lib/rxsd/translator.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
#
|
3
3
|
# transaltes xsd <-> ruby classes & xml <-> instances
|
4
4
|
#
|
5
|
-
# Copyright (C)
|
5
|
+
# Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
|
6
6
|
# See COPYING for the License of this software
|
7
7
|
|
8
8
|
# include whatever output builders you want here,
|
@@ -55,7 +55,7 @@ class Schema
|
|
55
55
|
results = []
|
56
56
|
cbs.each { |cb|
|
57
57
|
# probably a better way to do this at some point than invoking the copy constructors
|
58
|
-
|
58
|
+
|
59
59
|
# FIXME we create class builders in the translator on the fly, this may cause
|
60
60
|
# problems for later operations that need to access class builder attributes which
|
61
61
|
# have been created
|
@@ -83,29 +83,29 @@ class SchemaInstance
|
|
83
83
|
# array of object builders represented by current instance
|
84
84
|
attr_accessor :object_builders
|
85
85
|
|
86
|
-
#
|
86
|
+
# Return array of ObjectBuilders parsed out of a RXSD::XML::Node.
|
87
87
|
# Optionally specify parent ObjectBuilder to use
|
88
88
|
def self.builders_from_xml(node, parent = nil)
|
89
89
|
node_builder = ObjectBuilder.new(:tag_name => node.name, :attributes => node.attrs, :parent => parent)
|
90
90
|
parent.children.push node_builder unless parent.nil? || parent.children.include?(node_builder)
|
91
91
|
builders = [ node_builder ]
|
92
|
-
node.
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
92
|
+
if node.text?
|
93
|
+
node_builder.content = node.content
|
94
|
+
else
|
95
|
+
node.children.each { |c|
|
96
|
+
builders += SchemaInstance.builders_from_xml(c, node_builder )
|
97
|
+
}
|
98
|
+
end
|
99
99
|
return builders
|
100
100
|
end
|
101
101
|
|
102
|
-
#
|
102
|
+
# Create new schema instance w/ specified args
|
103
103
|
def initialize(args = {})
|
104
104
|
@object_builders = args[:builders] if args.has_key? :builders
|
105
105
|
end
|
106
106
|
|
107
107
|
# translates SchemaInstance's objects into instances of the specified output type.
|
108
|
-
# :output_type may be one of
|
108
|
+
# * :output_type may be one of
|
109
109
|
# * :ruby_objects
|
110
110
|
# Must specify :schema argument containing RXSD::XSD::Schema to use in object creation
|
111
111
|
def to(output_type, args = {})
|
data/lib/rxsd/xml.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# xml parsing subsystem
|
2
2
|
#
|
3
|
-
# Copyright (C)
|
3
|
+
# Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
|
4
4
|
# See COPYING for the License of this software
|
5
5
|
|
6
6
|
require 'rubygems'
|
@@ -11,31 +11,31 @@ require 'libxml_adapter'
|
|
11
11
|
module RXSD
|
12
12
|
module XML
|
13
13
|
|
14
|
-
# RXSD XML node interface subclasses must conform to and
|
14
|
+
# RXSD XML node interface that subclasses must conform to and implement methods
|
15
15
|
class Node
|
16
16
|
|
17
|
-
#
|
17
|
+
# Should return name of node, eg <foo> => "foo"
|
18
18
|
virtual :name
|
19
19
|
|
20
|
-
#
|
20
|
+
# Return hash of attribute name / values
|
21
21
|
virtual :attrs
|
22
22
|
|
23
|
-
#
|
23
|
+
# Should return bool if node has a parent
|
24
24
|
virtual :parent?
|
25
25
|
|
26
|
-
#
|
26
|
+
# Should return this nodes's parent, if any
|
27
27
|
virtual :parent
|
28
28
|
|
29
|
-
#
|
29
|
+
# Should return children nodes
|
30
30
|
virtual :children
|
31
31
|
|
32
|
-
#
|
32
|
+
# Should return bool if node only contains text
|
33
33
|
virtual :text?
|
34
34
|
|
35
|
-
#
|
35
|
+
# Should return string contents of text node
|
36
36
|
virtual :content
|
37
37
|
|
38
|
-
#
|
38
|
+
# Should return list of namespaces corresponding to node
|
39
39
|
virtual :namespaces
|
40
40
|
|
41
41
|
# should also define class method 'xml_root' that returns a node instance corresponding
|
@@ -43,7 +43,7 @@ class Node
|
|
43
43
|
|
44
44
|
############################################################################
|
45
45
|
|
46
|
-
#
|
46
|
+
# Node factory, returns root node corresponding to specified backend and xml data
|
47
47
|
def self.factory(args = {})
|
48
48
|
backend = args[:backend]
|
49
49
|
xml = args[:xml]
|
@@ -53,40 +53,53 @@ class Node
|
|
53
53
|
return nil
|
54
54
|
end
|
55
55
|
|
56
|
-
#
|
56
|
+
# Returns root node
|
57
57
|
def root
|
58
58
|
parent? ? parent.root : self
|
59
59
|
end
|
60
60
|
|
61
|
-
#
|
61
|
+
# Provides accessor interface to related obj, in our case related xsd obj
|
62
62
|
attr_accessor :related
|
63
63
|
|
64
|
-
#
|
64
|
+
# Instantiate all children of provided class type
|
65
65
|
def children_objs(klass)
|
66
66
|
elements = []
|
67
67
|
children.find_all { |c| c.name == klass.tag_name }.each { |c|
|
68
|
-
elements
|
68
|
+
elements << klass.from_xml(c)
|
69
|
+
}
|
69
70
|
return elements
|
70
71
|
end
|
71
72
|
|
72
|
-
#
|
73
|
+
# Instantiate first child of provided class type
|
73
74
|
def child_obj(klass)
|
74
75
|
return children_objs(klass)[0]
|
75
76
|
end
|
76
77
|
|
77
|
-
#
|
78
|
+
# Return 'value' attribute of all children w/ specified tag
|
78
79
|
def child_values(tag_name)
|
79
80
|
values = []
|
80
81
|
children.find_all { |c| c.name == tag_name }.each { |c| values.push(c.attrs['value']) }
|
81
82
|
return values
|
82
83
|
end
|
83
84
|
|
84
|
-
#
|
85
|
+
# Return 'value' attribute of first childw/ specified tag
|
85
86
|
def child_value(tag_name)
|
86
87
|
return child_values(tag_name)[0]
|
87
88
|
end
|
88
89
|
|
89
90
|
end
|
90
91
|
|
92
|
+
# RXSD XML namespace interface that subclasses must conform to and implement methods
|
93
|
+
class Namespace
|
94
|
+
# Should return prefix of namespace
|
95
|
+
virtual :prefix
|
96
|
+
|
97
|
+
# Should return href of namespace
|
98
|
+
virtual :href
|
99
|
+
|
100
|
+
# Should return namespace in string format
|
101
|
+
virtual :to_s
|
102
|
+
end
|
103
|
+
|
91
104
|
end # module XML
|
92
105
|
end # module RXSD
|
data/lib/rxsd/xsd/attribute.rb
CHANGED
data/lib/rxsd/xsd/choice.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# The XSD ComplexType definition
|
2
2
|
#
|
3
|
-
# Copyright (C)
|
3
|
+
# Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
|
4
4
|
# See COPYING for the License of this software
|
5
5
|
|
6
6
|
module RXSD
|
@@ -33,7 +33,8 @@ class ComplexType
|
|
33
33
|
|
34
34
|
# returns array of all children
|
35
35
|
def children
|
36
|
-
(@attributes + @attribute_groups).push(@simple_content).
|
36
|
+
(@attributes + @attribute_groups).push(@simple_content).
|
37
|
+
push(@complex_content).push(@choice).push(@group).push(@sequence)
|
37
38
|
end
|
38
39
|
|
39
40
|
# node passed in should be a xml node representing the complex type
|
data/lib/rxsd/xsd/element.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# The XSD Element definition
|
2
2
|
#
|
3
|
-
# Copyright (C)
|
3
|
+
# Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
|
4
4
|
# See COPYING for the License of this software
|
5
5
|
|
6
6
|
module RXSD
|
@@ -13,7 +13,7 @@ class Element
|
|
13
13
|
# element attribute values
|
14
14
|
attr_accessor :id, :name, :type,
|
15
15
|
:nillable, :abstract, :ref,
|
16
|
-
:
|
16
|
+
:substitutionGroup, :form, :maxOccurs,
|
17
17
|
:minOccurs, :default, :fixed
|
18
18
|
|
19
19
|
# simple type in element
|
@@ -64,7 +64,7 @@ class Element
|
|
64
64
|
ref = ref.split(':')[1] if !(ref.nil? || ref.index(":").nil?)
|
65
65
|
element.ref = ref
|
66
66
|
|
67
|
-
element.
|
67
|
+
element.substitutionGroup = node.attrs["substitutionGroup"]
|
68
68
|
element.form = node.attrs.has_key?("form") ?
|
69
69
|
node.attrs["form"] :
|
70
70
|
node.root.attrs["elementFormDefault"]
|
@@ -108,8 +108,8 @@ class Element
|
|
108
108
|
@ref = node_objs[Element].find { |no| no.name == @ref }
|
109
109
|
end
|
110
110
|
|
111
|
-
unless @
|
112
|
-
@substitutionGroup = node_objs[Element].find { |no| no.name == @
|
111
|
+
unless @substitutionGroup.nil?
|
112
|
+
@substitutionGroup = node_objs[Element].find { |no| no.name == @substitutionGroup }
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
data/lib/rxsd/xsd/extension.rb
CHANGED
data/lib/rxsd/xsd/group.rb
CHANGED
data/lib/rxsd/xsd/list.rb
CHANGED
data/lib/rxsd/xsd/restriction.rb
CHANGED
data/lib/rxsd/xsd/schema.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# The XSD Schema definition
|
2
2
|
#
|
3
|
-
# Copyright (C)
|
3
|
+
# Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
|
4
4
|
# See COPYING for the License of this software
|
5
5
|
|
6
6
|
module RXSD
|
@@ -40,7 +40,8 @@ class Schema
|
|
40
40
|
|
41
41
|
# returns array of all schema children
|
42
42
|
def children
|
43
|
-
([@elements] + [@simple_types] + [@complex_types] + [@attributes] +
|
43
|
+
([@elements] + [@simple_types] + [@complex_types] + [@attributes] +
|
44
|
+
[@attribute_groups] + [@groups]).flatten
|
44
45
|
end
|
45
46
|
|
46
47
|
# node passed in should be a xml root node representing the schema
|
data/lib/rxsd/xsd/sequence.rb
CHANGED
data/lib/rxsd/xsd/simple_type.rb
CHANGED
@@ -0,0 +1,224 @@
|
|
1
|
+
# tests the builder module
|
2
|
+
#
|
3
|
+
# Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
|
4
|
+
# See COPYING for the License of this software
|
5
|
+
|
6
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
7
|
+
|
8
|
+
describe "Builder" do
|
9
|
+
|
10
|
+
# test to_class_builder method on all XSD classes
|
11
|
+
|
12
|
+
it "should return schema class builders" do
|
13
|
+
schema = Schema.new
|
14
|
+
elem1 = MockXSDEntity.new
|
15
|
+
elem2 = MockXSDEntity.new
|
16
|
+
|
17
|
+
schema.elements = [elem1, elem2]
|
18
|
+
class_builders = schema.to_class_builders
|
19
|
+
|
20
|
+
class_builders.size.should == 2
|
21
|
+
class_builders[0].class.should == ClassBuilder
|
22
|
+
class_builders[0].instance_variable_get("@xsd_obj").should == elem1
|
23
|
+
class_builders[1].class.should == ClassBuilder
|
24
|
+
class_builders[1].instance_variable_get("@xsd_obj").should == elem2
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return element class builders" do
|
28
|
+
elem1 = MockXSDEntity.new
|
29
|
+
st1 = MockXSDEntity.new
|
30
|
+
ct1 = MockXSDEntity.new
|
31
|
+
|
32
|
+
element = Element.new
|
33
|
+
element.name = "foo_element"
|
34
|
+
element.ref = elem1
|
35
|
+
cb = element.to_class_builder
|
36
|
+
cb.class.should == ClassBuilder
|
37
|
+
cb.instance_variable_get("@xsd_obj").should == elem1
|
38
|
+
cb.klass_name.should == "FooElement"
|
39
|
+
|
40
|
+
# FIXME the next two 'type' test cases need to be fixed / expanded
|
41
|
+
element = Element.new
|
42
|
+
element.name = "bar_element"
|
43
|
+
element.type = st1
|
44
|
+
cb = element.to_class_builder
|
45
|
+
cb.class.should == ClassBuilder
|
46
|
+
#cb.instance_variable_get("@xsd_obj").should == st1 TODO since clone is invoked, @xsd_obj test field never gets copied, fix this
|
47
|
+
cb.klass_name.should == "BarElement"
|
48
|
+
|
49
|
+
element = Element.new
|
50
|
+
element.type = ct1
|
51
|
+
cb = element.to_class_builder
|
52
|
+
cb.class.should == ClassBuilder
|
53
|
+
#cb.instance_variable_get("@xsd_obj").should == ct1
|
54
|
+
|
55
|
+
element = Element.new
|
56
|
+
element.simple_type = st1
|
57
|
+
cb = element.to_class_builder
|
58
|
+
cb.class.should == ClassBuilder
|
59
|
+
cb.instance_variable_get("@xsd_obj").should == st1
|
60
|
+
|
61
|
+
element = Element.new
|
62
|
+
element.complex_type = ct1
|
63
|
+
cb = element.to_class_builder
|
64
|
+
cb.class.should == ClassBuilder
|
65
|
+
cb.instance_variable_get("@xsd_obj").should == ct1
|
66
|
+
end
|
67
|
+
|
68
|
+
# FIXME test other XSD classes' to_class_builder methods
|
69
|
+
|
70
|
+
##########
|
71
|
+
|
72
|
+
it "should correctly return associated builders" do
|
73
|
+
gp = ClassBuilder.new
|
74
|
+
p = ClassBuilder.new :base_builder => gp
|
75
|
+
c = ClassBuilder.new :base_builder => p
|
76
|
+
as = ClassBuilder.new
|
77
|
+
c.associated_builder = as
|
78
|
+
at1 = ClassBuilder.new
|
79
|
+
at2 = ClassBuilder.new
|
80
|
+
c.attribute_builders.push at1
|
81
|
+
c.attribute_builders.push at2
|
82
|
+
|
83
|
+
ab = c.associated
|
84
|
+
ab.size.should == 5
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should build class" do
|
88
|
+
cb1 = RubyClassBuilder.new :klass => String, :klass_name => "Widget"
|
89
|
+
cb1.build.should == String
|
90
|
+
|
91
|
+
cb2 = RubyClassBuilder.new :klass_name => "Foobar"
|
92
|
+
c2 = cb2.build
|
93
|
+
c2.should == Foobar
|
94
|
+
c2.superclass.should == Object
|
95
|
+
|
96
|
+
acb = RubyClassBuilder.new :klass => Array, :klass_name => "ArrSocket", :associated_builder => cb1
|
97
|
+
ac = acb.build
|
98
|
+
ac.should == Array
|
99
|
+
|
100
|
+
tcb = RubyClassBuilder.new :klass_name => "CamelCased"
|
101
|
+
|
102
|
+
cb3 = RubyClassBuilder.new :klass_name => "Foomoney", :base_builder => cb2
|
103
|
+
cb3.attribute_builders.push cb1
|
104
|
+
cb3.attribute_builders.push tcb
|
105
|
+
cb3.attribute_builders.push acb
|
106
|
+
c3 = cb3.build
|
107
|
+
c3.should == Foomoney
|
108
|
+
c3.superclass.should == Foobar
|
109
|
+
c3i = c3.new
|
110
|
+
c3i.method(:widget).should_not be_nil
|
111
|
+
c3i.method(:widget).arity.should == 0
|
112
|
+
c3i.method(:widget=).should_not be_nil
|
113
|
+
c3i.method(:widget=).arity.should == 1
|
114
|
+
c3i.method(:camel_cased).should_not be_nil
|
115
|
+
c3i.method(:camel_cased).arity.should == 0
|
116
|
+
c3i.method(:camel_cased=).should_not be_nil
|
117
|
+
c3i.method(:camel_cased=).arity.should == 1
|
118
|
+
c3i.method(:arr_socket).should_not be_nil
|
119
|
+
c3i.method(:arr_socket).arity.should == 0
|
120
|
+
c3i.method(:arr_socket=).should_not be_nil
|
121
|
+
c3i.method(:arr_socket=).arity.should == 1
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should build definition" do
|
125
|
+
cb1 = RubyDefinitionBuilder.new :klass => String, :klass_name => "Widget"
|
126
|
+
cb1.build.should == "class String\nend"
|
127
|
+
|
128
|
+
cb2 = RubyDefinitionBuilder.new :klass_name => "Foobar"
|
129
|
+
d2 = cb2.build
|
130
|
+
d2.should == "class Foobar < Object\nend"
|
131
|
+
|
132
|
+
acb = RubyDefinitionBuilder.new :klass => Array, :klass_name => "ArrSocket", :associated_builder => cb1
|
133
|
+
ad = acb.build
|
134
|
+
ad.should == "class Array\nend"
|
135
|
+
|
136
|
+
tcb = RubyDefinitionBuilder.new :klass_name => "CamelCased"
|
137
|
+
|
138
|
+
cb3 = RubyDefinitionBuilder.new :klass_name => "Foomoney", :base_builder => cb2
|
139
|
+
cb3.attribute_builders.push cb1
|
140
|
+
cb3.attribute_builders.push tcb
|
141
|
+
cb3.attribute_builders.push acb
|
142
|
+
d3 = cb3.build
|
143
|
+
d3.should == "class Foomoney < Foobar\n" +
|
144
|
+
"attr_accessor :widget\n" +
|
145
|
+
"attr_accessor :camel_cased\n" +
|
146
|
+
"attr_accessor :arr_socket\n" +
|
147
|
+
"end"
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should build object" do
|
151
|
+
schema_data = "<schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>" +
|
152
|
+
"<xs:element name='Godzilla'>" +
|
153
|
+
"<xs:complexType>" +
|
154
|
+
"<xs:simpleContent>" +
|
155
|
+
"<xs:extension base='xs:string'>" +
|
156
|
+
"<xs:attribute name='first_attr' type='xs:string' />" +
|
157
|
+
"<xs:attribute name='SecondAttr' type='xs:integer' />" +
|
158
|
+
"</xs:extension>" +
|
159
|
+
"</xs:simpleContent>"+
|
160
|
+
"</xs:complexType>"+
|
161
|
+
"</xs:element>" +
|
162
|
+
"</schema>"
|
163
|
+
|
164
|
+
schema = Parser.parse_xsd :raw => schema_data
|
165
|
+
rbclasses = schema.to :ruby_classes
|
166
|
+
|
167
|
+
rob = RubyObjectBuilder.new :tag_name => "Godzilla", :content => "some stuff", :attributes => { "first_attr" => "first_val", "SecondAttr" => "420" }
|
168
|
+
obj = rob.build schema
|
169
|
+
|
170
|
+
obj.class.should == Godzilla
|
171
|
+
obj.should == "some stuff" # since obj derives from string
|
172
|
+
obj.first_attr.should == "first_val"
|
173
|
+
obj.second_attr.should == 420
|
174
|
+
|
175
|
+
|
176
|
+
schema_data = "<schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>" +
|
177
|
+
'<xs:element name="employee" type="fullpersoninfo"/>' +
|
178
|
+
'<xs:complexType name="personinfo">'+
|
179
|
+
'<xs:attribute name="ssn" type="xs:string" />' +
|
180
|
+
'<xs:sequence>'+
|
181
|
+
'<xs:element name="firstname" type="xs:string"/>'+
|
182
|
+
'<xs:element name="lastname" type="xs:string"/>'+
|
183
|
+
'</xs:sequence>'+
|
184
|
+
'</xs:complexType>'+
|
185
|
+
'<xs:complexType name="fullpersoninfo">'+
|
186
|
+
'<xs:complexContent>'+
|
187
|
+
'<xs:extension base="personinfo">'+
|
188
|
+
'<xs:attribute name="residency" type="xs:string" />' +
|
189
|
+
'<xs:sequence>'+
|
190
|
+
'<xs:element name="address" type="xs:string"/>'+
|
191
|
+
'<xs:element name="country" type="xs:string"/>'+
|
192
|
+
'</xs:sequence>'+
|
193
|
+
'</xs:extension>'+
|
194
|
+
'</xs:complexContent>'+
|
195
|
+
'</xs:complexType> '+
|
196
|
+
"</schema>"
|
197
|
+
|
198
|
+
schema = Parser.parse_xsd :raw => schema_data
|
199
|
+
rbclasses = schema.to :ruby_classes
|
200
|
+
|
201
|
+
rob = RubyObjectBuilder.new :tag_name => "employee", :attributes => { "ssn" => "111-22-3333", "residency" => "citizen" },
|
202
|
+
:children => [ ObjectBuilder.new(:tag_name => "firstname", :content => "mo" ),
|
203
|
+
ObjectBuilder.new(:tag_name => "lastname", :content => "morsi"),
|
204
|
+
ObjectBuilder.new(:tag_name => "address", :content => "wouldn't you like to know :-p"),
|
205
|
+
ObjectBuilder.new(:tag_name => "country", :content => "USA") ]
|
206
|
+
|
207
|
+
obj = rob.build schema
|
208
|
+
|
209
|
+
obj.class.should == Employee
|
210
|
+
obj.ssn.should == "111-22-3333"
|
211
|
+
obj.residency.should == "citizen"
|
212
|
+
obj.firstname.should == "mo"
|
213
|
+
obj.lastname.should == "morsi"
|
214
|
+
obj.country.should == "USA"
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
class MockXSDEntity
|
219
|
+
def to_class_builder
|
220
|
+
cb = ClassBuilder.new
|
221
|
+
cb.instance_variable_set("@xsd_obj", self)
|
222
|
+
return cb
|
223
|
+
end
|
224
|
+
end
|