rxsd 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,114 @@
1
+ # The XSD Schema 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
+ # Schema defintion, top level XSD element
10
+ # http://www.w3schools.com/Schema/el_schema.asp
11
+ class Schema
12
+
13
+ # hash of prefix => namespace values
14
+ # " is the key of the default namespace (or use default_namespace)
15
+ attr_accessor :namespaces
16
+
17
+ # schema attribute values
18
+ attr_accessor :version, :targetNamespace, :elementFormDefault, :attributeFormDefault
19
+
20
+ # arrays of children in schema
21
+ attr_accessor :elements, :simple_types, :complex_types, :attributes, :attribute_groups, :groups
22
+
23
+ # parent, always nil but here for conformity
24
+ attr_accessor :parent
25
+
26
+ # return default namespace
27
+ def default_namespace
28
+ @namespaces[""]
29
+ end
30
+
31
+ # xml tag name
32
+ def self.tag_name
33
+ "schema"
34
+ end
35
+
36
+ # return xsd node info
37
+ def info
38
+ "schema"
39
+ end
40
+
41
+ # returns array of all schema children
42
+ def children
43
+ ([@elements] + [@simple_types] + [@complex_types] + [@attributes] + [@attribute] + [@attribute_groups] + [@groups]).flatten
44
+ end
45
+
46
+ # node passed in should be a xml root node representing the schema
47
+ def self.from_xml(node)
48
+ schema = Schema.new
49
+ schema.parent = nil
50
+ node.related= schema
51
+
52
+ # set namespaces
53
+ schema.namespaces = {}
54
+ node.namespaces.each{ |ns| schema.namespaces[ns.prefix] = ns.href }
55
+
56
+ # parse version out of attrs
57
+ schema.version = node.attrs["version"]
58
+
59
+ # parse target namespace out of attrs
60
+ schema.targetNamespace = node.attrs["targetNamespace"]
61
+
62
+ # parse elementFormDefault out of attrs
63
+ schema.elementFormDefault = node.attrs["elementFormDefault"]
64
+
65
+ # parse attributeFormDefault out of attrs
66
+ schema.attributeFormDefault = node.attrs["attributeFormDefault"]
67
+
68
+ # TODO schema attrs: | blockDefault / finalDefault / anyAttr
69
+ # TODO schema children: | import, notation, redefine / anyChild
70
+ # FIXME handle "xs:include" (use Loader?)
71
+
72
+ # parse elements
73
+ schema.elements = node.children_objs Element
74
+
75
+ # parse simple types
76
+ schema.simple_types = node.children_objs SimpleType
77
+
78
+ # parse complex types
79
+ schema.complex_types = node.children_objs ComplexType
80
+
81
+ # parse attributes
82
+ schema.attributes = node.children_objs Attribute
83
+
84
+ # parse attribute groups
85
+ schema.attribute_groups = node.children_objs AttributeGroup
86
+
87
+ # parse groups
88
+ schema.groups = node.children_objs Group
89
+
90
+ return schema
91
+ end
92
+
93
+ # resolve hanging references given complete xsd node object array
94
+ def resolve(node_objs)
95
+ # right now does nothing
96
+ # (possible resolve include/import here)
97
+ end
98
+
99
+ # convert schema to array of class builders
100
+ def to_class_builders
101
+ unless defined? @class_builder
102
+ @class_builders = []
103
+ @elements.each { |e|
104
+ @class_builders.push e.to_class_builder
105
+ }
106
+ end
107
+
108
+ return @class_builders
109
+ end
110
+
111
+ end
112
+
113
+ end # module XSD
114
+ end # module RXSD
@@ -0,0 +1,108 @@
1
+ # The XSD Sequence 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 Sequence defintion
10
+ # http://www.w3schools.com/Schema/el_sequence.asp
11
+ class Sequence
12
+
13
+ # sequence attributes
14
+ attr_accessor :id, :maxOccurs, :minOccurs
15
+
16
+ # sequence children
17
+ attr_accessor :elements, :groups, :choices, :sequences
18
+
19
+ # sequence parent
20
+ attr_accessor :parent
21
+
22
+ # xml tag name
23
+ def self.tag_name
24
+ "sequence"
25
+ end
26
+
27
+ # return xsd node info
28
+ def info
29
+ "sequence id: #{@id}"
30
+ end
31
+
32
+ # returns array of all children
33
+ def children
34
+ @elements + @groups + @choices + @sequences
35
+ end
36
+
37
+ # node passed in should be a xml node representing the group
38
+ def self.from_xml(node)
39
+ sequence = Sequence.new
40
+ sequence.parent = node.parent.related
41
+ node.related = sequence
42
+
43
+ # TODO sequence attributes: | anyAttributes
44
+ sequence.id = node.attrs["id"]
45
+
46
+ sequence.maxOccurs = node.attrs.has_key?("maxOccurs") ?
47
+ (node.attrs["maxOccurs"] == "unbounded" ? "unbounded" : node.attrs["maxOccurs"].to_i) : 1
48
+ sequence.minOccurs = node.attrs.has_key?("minOccurs") ?
49
+ (node.attrs["minOccurs"] == "unbounded" ? "unbounded" : node.attrs["minOccurs"].to_i) : 1
50
+
51
+ # TODO sequence children: | any
52
+ sequence.elements = node.children_objs Element
53
+ sequence.groups = node.children_objs Group
54
+ sequence.choices = node.children_objs Choice
55
+ sequence.sequences = node.children_objs Sequence
56
+
57
+ return sequence
58
+ end
59
+
60
+ # resolve hanging references given complete xsd node object array
61
+ def resolve(node_objs)
62
+ end
63
+
64
+ # convert sequence to array of class builders
65
+ def to_class_builders
66
+ # FIXME enforce "all attributes must appear in set order"
67
+
68
+ unless defined? @class_builders
69
+ @class_builders = []
70
+ @elements.each { |e|
71
+ @class_builders.push e.to_class_builder
72
+ }
73
+ @groups.each { |g|
74
+ g.to_class_builders.each { |gcb|
75
+ @class_builders.push gcb
76
+ }
77
+ }
78
+ @choices.each { |c|
79
+ c.to_class_builders.each { |ccb|
80
+ @class_builders.push ccb
81
+ }
82
+ }
83
+ @sequences.each { |s|
84
+ s.to_class_builders.each { |scb|
85
+ @class_builders.push scb
86
+ }
87
+ }
88
+ end
89
+
90
+ return @class_builders
91
+ end
92
+
93
+ # return all child attributes assocaited w/ choice
94
+ def child_attributes
95
+ atts = []
96
+ @elements.each { |elem|
97
+ ca = elem.child_attributes
98
+ atts += ca unless ca.nil?
99
+ } unless @elements.nil?
100
+ @sequences.each { |seq| atts += seq.child_attributes } unless @sequences.nil?
101
+ @choices.each { |ch| atts += ch.child_attributes } unless @choices.nil?
102
+ @groups.each { |gr| atts += gr.child_attributes } unless @groups.nil?
103
+ return atts
104
+ end
105
+ end
106
+
107
+ end # module XSD
108
+ end # module RXSD
@@ -0,0 +1,86 @@
1
+ # The XSD SimpleContent 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 SimpleContent defintion
10
+ # http://www.w3schools.com/Schema/el_simpleContent.asp
11
+ class SimpleContent
12
+
13
+ # simple content attributes
14
+ attr_accessor :id
15
+
16
+ # simple content children
17
+ attr_accessor :restriction, :extension
18
+
19
+ # simple content parent
20
+ attr_accessor :parent
21
+
22
+ # xml tag name
23
+ def self.tag_name
24
+ "simpleContent"
25
+ end
26
+
27
+ # return xsd node info
28
+ def info
29
+ "simple_content id: #{@id}"
30
+ end
31
+
32
+ # returns array of all children
33
+ def children
34
+ c = []
35
+ c.push @restriction unless @restriction.nil?
36
+ c.push @extension unless @extension.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
+ simple_content = SimpleContent.new
43
+ simple_content.parent = node.parent.related
44
+ node.related = simple_content
45
+
46
+ # TODO group attributes: | anyAttributes
47
+ simple_content.id = node.attrs["id"]
48
+
49
+ simple_content.restriction = node.child_obj Restriction
50
+ simple_content.extension = node.child_obj Extension
51
+
52
+ return simple_content
53
+ end
54
+
55
+ # resolve hanging references given complete xsd node object array
56
+ def resolve(node_objs)
57
+ end
58
+
59
+ # convert simple content to class builder
60
+ def to_class_builder(cb = nil)
61
+ unless defined? @class_builder
62
+ # dispatch to child restriction/extension
63
+ @class_builder = cb
64
+
65
+ if !@restriction.nil?
66
+ @class_builder= @restriction.to_class_builder(@class_builder)
67
+ elsif !@extension.nil?
68
+ @class_builder= @extension.to_class_builder(@class_builder)
69
+ end
70
+ end
71
+
72
+ return @class_builder
73
+ end
74
+
75
+ # return all child attributes associated w/ simple content
76
+ def child_attributes
77
+ atts = []
78
+ atts += @restriction.child_attributes unless @restriction.nil?
79
+ atts += @extension.child_attributes unless @extension.nil?
80
+ return atts
81
+ end
82
+
83
+ end
84
+
85
+ end # module XSD
86
+ end # module RXSD
@@ -0,0 +1,101 @@
1
+ # The XSD SimpleType 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 SimpleType defintion
10
+ # http://www.w3schools.com/Schema/el_simpletype.asp
11
+ class SimpleType
12
+
13
+ # simple type attribute values
14
+ attr_accessor :id, :name
15
+
16
+ # children in schema (only one will be populated)
17
+ attr_accessor :list, :restriction
18
+
19
+ # simpleType parent
20
+ attr_accessor :parent
21
+
22
+ # xml tag name
23
+ def self.tag_name
24
+ "simpleType"
25
+ end
26
+
27
+ # return xsd node info
28
+ def info
29
+ "simple_type id: #{@id} name: #{@name}"
30
+ end
31
+
32
+ # returns array of all children
33
+ def children
34
+ c = []
35
+ c.push @list unless @list.nil?
36
+ c.push @restriction unless @restriction.nil?
37
+ return c
38
+ end
39
+
40
+ # node passed in should be a xml node representing the simple type
41
+ def self.from_xml(node)
42
+ simpleType = SimpleType.new
43
+ simpleType.parent = node.parent.related
44
+ node.related = simpleType
45
+
46
+ # TODO simpleType attributes: | anyAttr
47
+ simpleType.id = node.attrs["id"]
48
+ simpleType.name = node.attrs["name"]
49
+
50
+ # parse lists
51
+ simpleType.list = node.child_obj List
52
+
53
+ # parse restrictions
54
+ simpleType.restriction = node.child_obj Restriction
55
+
56
+ # TODO simpleType children: | unions
57
+ #simpleType.unions = node.child_obj Union
58
+
59
+ return simpleType
60
+ end
61
+
62
+ # resolve hanging references given complete xsd node object array
63
+ def resolve(node_objs)
64
+ end
65
+
66
+ # convert simple type to class builder
67
+ def to_class_builder
68
+ unless defined? @class_builder
69
+ @class_builder = nil
70
+
71
+ if !@list.nil?
72
+ # dispatch to child list
73
+ @class_builder = @list.to_class_builder
74
+
75
+ elsif !@restriction.nil?
76
+ # grab restriction class builder w/ base class and facets
77
+ @class_builder = @restriction.to_class_builder
78
+
79
+ #else
80
+ # @class_builder = ClassBuilder.new
81
+
82
+ end
83
+ end
84
+
85
+ @class_builder.klass_name = @name.camelize unless @name.nil?
86
+ return @class_builder
87
+ end
88
+
89
+ # return all child_attributes associated w/ simple type
90
+ def child_attributes
91
+ if !@list.nil?
92
+ return @list.child_attributes
93
+ elsif !@restriction.nil?
94
+ return @restriction.child_attributes
95
+ end
96
+ end
97
+
98
+ end
99
+
100
+ end # module XSD
101
+ end # module RXSD
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rxsd
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.2"
5
+ platform: ruby
6
+ authors:
7
+ - Mohammed Morsi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-09 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: libxml-ruby
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.4
34
+ version:
35
+ description: RXSD is a library to translate xsd schemas and xml implementations into ruby classes/objects
36
+ email: movitto@yahoo.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ files:
44
+ - LICENSE
45
+ - COPYING
46
+ - bin/rxsd-test.rb
47
+ - lib/rxsd.rb
48
+ - lib/rxsd/builder.rb
49
+ - lib/rxsd/builtin_types.rb
50
+ - lib/rxsd/common.rb
51
+ - lib/rxsd/exceptions.rb
52
+ - lib/rxsd/libxml_adapter.rb
53
+ - lib/rxsd/loader.rb
54
+ - lib/rxsd/parser.rb
55
+ - lib/rxsd/resolver.rb
56
+ - lib/rxsd/translator.rb
57
+ - lib/rxsd/xml.rb
58
+ - lib/rxsd/builders/ruby_class.rb
59
+ - lib/rxsd/builders/ruby_definition.rb
60
+ - lib/rxsd/builders/ruby_object.rb
61
+ - lib/rxsd/xsd/attribute_group.rb
62
+ - lib/rxsd/xsd/attribute.rb
63
+ - lib/rxsd/xsd/choice.rb
64
+ - lib/rxsd/xsd/complex_content.rb
65
+ - lib/rxsd/xsd/complex_type.rb
66
+ - lib/rxsd/xsd/element.rb
67
+ - lib/rxsd/xsd/extension.rb
68
+ - lib/rxsd/xsd/group.rb
69
+ - lib/rxsd/xsd/list.rb
70
+ - lib/rxsd/xsd/restriction.rb
71
+ - lib/rxsd/xsd/schema.rb
72
+ - lib/rxsd/xsd/sequence.rb
73
+ - lib/rxsd/xsd/simple_content.rb
74
+ - lib/rxsd/xsd/simple_type.rb
75
+ - README
76
+ has_rdoc: true
77
+ homepage: http://projects.morsi.org/RXSD
78
+ licenses: []
79
+
80
+ post_install_message:
81
+ rdoc_options:
82
+ - --inline-source
83
+ - --charset=UTF-8
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ version:
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.1
97
+ version:
98
+ requirements: []
99
+
100
+ rubyforge_project:
101
+ rubygems_version: 1.3.5
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: RXSD is a library to translate xsd schemas and xml implementations into ruby classes/objects
105
+ test_files: []
106
+