rxsd 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,138 @@
1
+ # The XSD Extension definition
2
+ #
3
+ # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
+ # See COPYING for the License of this software
5
+
6
+ module RXSD
7
+ module XSD
8
+
9
+ # XSD Extension defintion
10
+ # http://www.w3schools.com/Schema/el_extension.asp
11
+ class Extension
12
+
13
+ # extension attributes
14
+ attr_accessor :id, :base
15
+
16
+ # extension group children
17
+ attr_accessor :group, :choice, :sequence, :attributes, :attribute_groups
18
+
19
+ # extension parent
20
+ attr_accessor :parent
21
+
22
+ # xml tag name
23
+ def self.tag_name
24
+ "extension"
25
+ end
26
+
27
+ # return xsd node info
28
+ def info
29
+ "extension id: #{@id} base: #{@base.nil? ? "" : (@base.class == String || Parser.is_builtin?(@base)) ? @base : @base.name }"
30
+ end
31
+
32
+ # returns array of all children
33
+ def children
34
+ c = []
35
+ c.push @group unless @group.nil?
36
+ c.push @choice unless @choice.nil?
37
+ c.push @sequence unless @sequence.nil?
38
+ c += @attributes unless @attributes.nil?
39
+ c += @attribute_groups unless @attribute_groups.nil?
40
+ return c
41
+ end
42
+
43
+ # node passed in should be a xml node representing the extension
44
+ def self.from_xml(node)
45
+ extension = Extension.new
46
+ extension.parent = node.parent.related
47
+ node.related = extension
48
+
49
+ # TODO extension attributes: | anyAttributes
50
+ extension.id = node.attrs["id"]
51
+ extension.base = node.attrs["base"]
52
+
53
+ # TODO extension children: | anyAttribute
54
+ extension.group = node.child_obj Group
55
+ extension.choice = node.child_obj Choice
56
+ extension.sequence = node.child_obj Sequence
57
+ extension.attributes = node.children_objs Attribute
58
+ extension.attribute_groups = node.children_objs AttributeGroup
59
+
60
+ return extension
61
+ end
62
+
63
+ # resolve hanging references given complete xsd node object array
64
+ def resolve(node_objs)
65
+ unless @base.nil?
66
+ builtin = Parser.parse_builtin_type @base
67
+ simple = node_objs[SimpleType].find { |no| no.name == @base }
68
+ complex = node_objs[ComplexType].find { |no| no.name == @base }
69
+ if !builtin.nil?
70
+ @base = builtin
71
+ elsif !simple.nil?
72
+ @base = simple
73
+ elsif !complex.nil?
74
+ @base = complex
75
+ end
76
+ end
77
+ end
78
+
79
+ # convert extension to class builder
80
+ def to_class_builder(cb = nil)
81
+ unless defined? @class_builder
82
+ @class_builder = cb.nil? ? ClassBuilder.new : cb
83
+
84
+ # convert extension to builder
85
+ if Parser.is_builtin? @base
86
+ @class_builder.base = @base
87
+ elsif !@base.nil?
88
+ @class_builder.base_builder = @base.to_class_builder
89
+ end
90
+
91
+ unless @group.nil?
92
+ @group.to_class_builders.each { |gcb|
93
+ @class_builder.attribute_builders.push gcb
94
+ }
95
+ end
96
+
97
+ unless @choice.nil?
98
+ @choice.to_class_builders.each { |ccb|
99
+ @class_builder.attribute_builders.push ccb
100
+ }
101
+ end
102
+
103
+ unless @sequence.nil?
104
+ @sequence.to_class_builders.each { |scb|
105
+ @class_builder.attribute_builders.push scb
106
+ }
107
+ end
108
+
109
+ @attributes.each { |att|
110
+ @class_builder.attribute_builders.push att.to_class_builder
111
+ }
112
+
113
+ @attribute_groups.each { |atg|
114
+ atg.to_class_builders.each { |atcb|
115
+ @class_builder.attribute_builders.push atcb
116
+ }
117
+ }
118
+ end
119
+
120
+ return @class_builder
121
+ end
122
+
123
+ # return all child attributes assocaited w/ extension
124
+ def child_attributes
125
+ atts = []
126
+ atts += @base.child_attributes unless @base.nil? || ![SimpleType, ComplexType].include?(@base.class)
127
+ atts += @choice.child_attributes unless @choice.nil?
128
+ atts += @sequence.child_attributes unless @sequence.nil?
129
+ atts += @group.child_attributes unless @group.nil?
130
+ @attribute_groups.each { |atg| atts += atg.child_attributes } unless @attribute_groups.nil?
131
+ @attributes.each { |att| atts += att.child_attributes } unless @attributes.nil?
132
+ return atts
133
+ end
134
+
135
+ end
136
+
137
+ end # module XSD
138
+ end # module RXSD
@@ -0,0 +1,100 @@
1
+ # The XSD Group definition
2
+ #
3
+ # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
+ # See COPYING for the License of this software
5
+
6
+ module RXSD
7
+ module XSD
8
+
9
+ # XSD Group defintion
10
+ # http://www.w3schools.com/Schema/el_group.asp
11
+ class Group
12
+
13
+ # group attributes
14
+ attr_accessor :id, :name, :ref, :maxOccurs, :minOccurs
15
+
16
+ # group children
17
+ attr_accessor :choice, :sequence
18
+
19
+ # group parent
20
+ attr_accessor :parent
21
+
22
+ # xml tag name
23
+ def self.tag_name
24
+ "group"
25
+ end
26
+
27
+ # return xsd node info
28
+ def info
29
+ "group id: #{@id} name: #{@name} ref: #{ref.nil? ? "" : ref.class == String ? ref : ref.name} "
30
+ end
31
+
32
+ # returns array of all children
33
+ def children
34
+ c = []
35
+ c.push @choice unless @choice.nil?
36
+ c.push @sequence unless @sequence.nil?
37
+ return c
38
+ end
39
+
40
+ # node passed in should be a xml node representing the group
41
+ def self.from_xml(node)
42
+ group = Group.new
43
+ group.parent = node.parent.related
44
+ node.related = group
45
+
46
+ # TODO group attributes: | anyAttributes
47
+ group.id = node.attrs["id"]
48
+ group.name = node.attrs["name"]
49
+ group.ref = node.attrs["ref"]
50
+
51
+ group.maxOccurs = node.attrs.has_key?("maxOccurs") ?
52
+ (node.attrs["maxOccurs"] == "unbounded" ? "unbounded" : node.attrs["maxOccurs"].to_i) : 1
53
+ group.minOccurs = node.attrs.has_key?("minOccurs") ?
54
+ (node.attrs["minOccurs"] == "unbounded" ? "unbounded" : node.attrs["minOccurs"].to_i) : 1
55
+
56
+
57
+ # TODO group children: | element(?)
58
+ group.choice = node.child_obj Choice
59
+ group.sequence = node.child_obj Sequence
60
+
61
+ return group
62
+ end
63
+
64
+ # resolve hanging references given complete xsd node object array
65
+ def resolve(node_objs)
66
+ unless @ref.nil?
67
+ @ref = node_objs[Group].find { |no| no.name == @ref }
68
+ end
69
+ end
70
+
71
+ # convert group to array of class builders
72
+ def to_class_builders
73
+ unless defined? @class_builder
74
+ # just dispatch to ref or child
75
+ @class_builder = []
76
+
77
+ if !@ref.nil?
78
+ @class_builder = @ref.to_class_builders
79
+ elsif !@choice.nil?
80
+ @class_builder = @choice.to_class_builders
81
+ elsif !@sequence.nil?
82
+ @class_builder = @sequence.to_class_builders
83
+ end
84
+ end
85
+
86
+ return @class_builder
87
+ end
88
+
89
+ # return all child attributes assocaited w/ group
90
+ def child_attributes
91
+ atts = []
92
+ atts += @sequence.child_attributes unless @sequence.nil?
93
+ atts += @choice.child_attributes unless @choice.nil?
94
+ return atts
95
+ end
96
+
97
+ end
98
+
99
+ end # module XSD
100
+ end # module RXSD
@@ -0,0 +1,101 @@
1
+ # The XSD List definition
2
+ #
3
+ # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
+ # See COPYING for the License of this software
5
+
6
+ module RXSD
7
+ module XSD
8
+
9
+ # XSD List defintion
10
+ # http://www.w3schools.com/Schema/el_list.asp
11
+ class List
12
+
13
+ # list attributes
14
+ attr_accessor :id, :itemType
15
+
16
+ # list children
17
+ attr_accessor :simple_type
18
+
19
+ # list parent
20
+ attr_accessor :parent
21
+
22
+ # xml tag name
23
+ def self.tag_name
24
+ "list"
25
+ end
26
+
27
+ # return xsd node info
28
+ def info
29
+ "list id: #{@id}"
30
+ end
31
+
32
+ # returns array of all children
33
+ def children
34
+ c = []
35
+ c.push @simple_type unless @simple_type.nil?
36
+ return c
37
+ end
38
+
39
+ # node passed in should be a xml node representing the list
40
+ def self.from_xml(node)
41
+ list = List.new
42
+ list.parent = node.parent.related
43
+ node.related = list
44
+
45
+ # TODO list attributes: | anyAttributes
46
+ list.id = node.attrs["id"]
47
+
48
+
49
+ if node.children.find { |c| c.name == SimpleType.tag_name }.nil?
50
+ list.itemType = node.attrs["itemType"]
51
+ else
52
+ list.simple_type = node.child_obj SimpleType
53
+ end
54
+
55
+ return list
56
+ end
57
+
58
+ # resolve hanging references given complete xsd node object array
59
+ def resolve(node_objs)
60
+ unless @itemType.nil?
61
+ builtin = Parser.parse_builtin_type @itemType
62
+ @itemType = !builtin.nil? ? builtin : node_objs[SimpleType].find { |no| no.name == @itemType }
63
+ end
64
+ end
65
+
66
+ # convert list to class builder
67
+ def to_class_builder
68
+ unless defined? @class_builder
69
+ # convert list to builder producing array of classes specified by item type or simple type
70
+ @class_builder = ClassBuilder.new :klass => Array
71
+
72
+ if !@itemType.nil?
73
+ if @itemType.class == SimpleType
74
+ @class_builder.associated_builder = @itemType.to_class_builder
75
+ else
76
+ @class_builder.associated_builder = ClassBuilder.new :klass => @itemType
77
+ end
78
+
79
+ elsif !@simple_type.nil?
80
+ @class_builder.associated_builder = @simple_type.to_class_builder
81
+
82
+ end
83
+ end
84
+
85
+ return @class_builder
86
+ end
87
+
88
+
89
+ # return all child_attributes associated w/ simple type
90
+ def child_attributes
91
+ if !@itemType.nil? && @itemType.class == SimpleType
92
+ return @itemType.child_attributes
93
+ elsif !@simple_type.nil?
94
+ return @simple_type.child_attributes
95
+ end
96
+ end
97
+
98
+ end
99
+
100
+ end # module XSD
101
+ end # module RXSD
@@ -0,0 +1,186 @@
1
+ # The XSD Restriction definition
2
+ #
3
+ # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
+ # See COPYING for the License of this software
5
+
6
+ module RXSD
7
+ module XSD
8
+
9
+ # XSD Restriction defintion
10
+ # http://www.w3schools.com/Schema/el_restriction.asp
11
+ class Restriction
12
+
13
+ # restriction attributes
14
+ attr_accessor :id, :base
15
+
16
+ # restriction group children
17
+ attr_accessor :group, :choice, :sequence, :attributes, :attribute_groups, :simple_type
18
+
19
+ # restrictions
20
+ attr_accessor :min_exclusive, :min_inclusive, :max_exclusive, :max_inclusive,
21
+ :total_digits, :fraction_digits, :length, :min_length, :max_length,
22
+ :enumerations, :whitespace, :pattern
23
+
24
+ # restriction parent
25
+ attr_accessor :parent
26
+
27
+ # xml tag name
28
+ def self.tag_name
29
+ "restriction"
30
+ end
31
+
32
+ # return xsd node info
33
+ def info
34
+ "extension id: #{@id} base: #{@base.nil? ? "" : (@base.class == String || Parser.is_builtin?(@base)) ? @base : @base.name }"
35
+ end
36
+
37
+ # returns array of all children
38
+ def children
39
+ c = []
40
+ c.push @group unless @group.nil?
41
+ c.push @choice unless @choice.nil?
42
+ c.push @sequence unless @sequence.nil?
43
+ c += @attributes unless @attributes.nil?
44
+ c += @attribute_groups unless @attribute_groups.nil?
45
+ c.push @simple_type unless @simple_type.nil?
46
+ return c
47
+ end
48
+
49
+ # node passed in should be a xml node representing the restriction
50
+ def self.from_xml(node)
51
+ restriction = Restriction.new
52
+ restriction.parent = node.parent.related
53
+ node.related = restriction
54
+
55
+ # TODO restriction attributes: | anyAttributes
56
+ restriction.id = node.attrs["id"]
57
+ restriction.base = node.attrs["base"]
58
+
59
+ if node.parent.name == ComplexContent.tag_name
60
+ # TODO restriction children: | anyAttribute
61
+ restriction.group = node.child_obj Group
62
+ restriction.choice = node.child_obj Choice
63
+ restriction.sequence = node.child_obj Sequence
64
+ restriction.attributes = node.children_objs Attribute
65
+ restriction.attribute_groups = node.children_objs AttributeGroup
66
+
67
+ elsif node.parent.name == SimpleContent.tag_name
68
+ # TODO restriction children: | anyAttribute
69
+ restriction.attributes = node.children_objs Attribute
70
+ restriction.attribute_groups = node.children_objs AttributeGroup
71
+ restriction.simple_type = node.child_obj SimpleType
72
+ parse_restrictions(restriction, node)
73
+
74
+ else # SimpleType
75
+ restriction.attributes = []
76
+ restriction.attribute_groups = []
77
+ restriction.simple_type = node.child_obj SimpleType
78
+ parse_restrictions(restriction, node)
79
+
80
+ end
81
+
82
+ return restriction
83
+ end
84
+
85
+ # resolve hanging references given complete xsd node object array
86
+ def resolve(node_objs)
87
+ unless @base.nil?
88
+ builtin = Parser.parse_builtin_type @base
89
+ simple = node_objs[SimpleType].find { |no| no.name == @base }
90
+ complex = node_objs[ComplexType].find { |no| no.name == @base }
91
+ if !builtin.nil?
92
+ @base = builtin
93
+ elsif !simple.nil?
94
+ @base = simple
95
+ elsif !complex.nil?
96
+ @base = complex
97
+ end
98
+ end
99
+ end
100
+
101
+ # convert restriction to class builder
102
+ def to_class_builder(cb = nil)
103
+ unless defined? @class_builder
104
+ @class_builder = cb.nil? ? ClassBuilder.new : cb
105
+
106
+ # convert restriction to builder
107
+ if Parser.is_builtin? @base
108
+ @class_builder.base = @base
109
+ elsif !@base.nil?
110
+ @class_builder.base_builder = @base.to_class_builder
111
+ end
112
+
113
+ unless @group.nil?
114
+ @group.to_class_builders.each { |gcb|
115
+ @class_builder.attribute_builders.push gcb
116
+ }
117
+ end
118
+
119
+ unless @choice.nil?
120
+ @choice.to_class_builders.each { |ccb|
121
+ @class_builder.attribute_builders.push ccb
122
+ }
123
+ end
124
+
125
+ unless @sequence.nil?
126
+ @sequence.to_class_builders.each { |scb|
127
+ @class_builder.attribute_builders.push scb
128
+ }
129
+ end
130
+
131
+ @attributes.each { |att|
132
+ @class_builder.attribute_builders.push att.to_class_builder
133
+ }
134
+
135
+ @attribute_groups.each { |atg|
136
+ atg.to_class_builders.each { |atcb|
137
+ @class_builder.attribute_builders.push atcb
138
+ }
139
+ }
140
+
141
+ unless @simple_type.nil?
142
+ @class_builder.attribute_builders.push @simple_type.to_class_builder
143
+ end
144
+
145
+ # FIXME add facets
146
+ end
147
+
148
+ return @class_builder
149
+ end
150
+
151
+ # return all child attributes assocaited w/ restriction
152
+ def child_attributes
153
+ atts = []
154
+ atts += @base.child_attributes unless @base.nil? || ![SimpleType, ComplexType].include?(@base.class)
155
+ atts += @sequence.child_attributes unless @sequence.nil?
156
+ atts += @choice.child_attributes unless @choice.nil?
157
+ atts += @group.child_attributes unless @group.nil?
158
+ atts += @simple_type.child_attributes unless @simple_type.nil?
159
+ @attribute_groups.each { |atg| atts += atg.child_attributes } unless @attribute_groups.nil?
160
+ @attributes.each { |att| atts += att.child_attributes } unless @attributes.nil?
161
+ return atts
162
+ end
163
+
164
+
165
+ private
166
+
167
+ # internal helper method
168
+ def self.parse_restrictions(restriction, node)
169
+ restriction.min_exclusive = node.child_value("minExclusive").to_i
170
+ restriction.min_inclusive = node.child_value("minInclusive").to_i
171
+ restriction.max_exclusive = node.child_value("maxExclusive").to_i
172
+ restriction.max_inclusive = node.child_value("maxInclusive").to_i
173
+ restriction.total_digits = node.child_value("totalDigits").to_i
174
+ restriction.fraction_digits = node.child_value("fractionDigits").to_i
175
+ restriction.length = node.child_value("length").to_i
176
+ restriction.min_length = node.child_value("minLength").to_i
177
+ restriction.max_length = node.child_value("maxLength").to_i
178
+ restriction.enumerations = node.child_values "enumeration"
179
+ restriction.whitespace = node.child_value "whitespace"
180
+ restriction.pattern = node.child_value "pattern"
181
+ end
182
+
183
+ end
184
+
185
+ end # module XSD
186
+ end # module RXSD