rxsd 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/COPYING ADDED
@@ -0,0 +1,8 @@
1
+ RXSD is free software, you can redistribute it and/or modify
2
+ it under the terms of the GNU Lesser General Public License
3
+ as published by the Free Software Foundation, either version 3
4
+ of the License, or (at your option) any later version.
5
+
6
+ You should have received a copy of the the GNU Lesser
7
+ General Public License, along with RXSD. If not, see
8
+ <http://www.gnu.org/licenses/>
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README ADDED
@@ -0,0 +1,19 @@
1
+ RXSD - XSD / Ruby Translator
2
+ Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
3
+ See COPYING for the License of this software
4
+
5
+ RXSD is a library that translates XSD XML Schema Definitions into Ruby Classes
6
+ on the fly. It is able to read XSD resources and use them to define Ruby
7
+ classes in memory or string class definitions to be written to the filesystem
8
+
9
+ RXSD implements a full XSD parser that not only defines the various xsd schema
10
+ classes, parsing them out of a XSD file, but translates them into a
11
+ meta-class heirarchy, for use in subsequent transformations. The builder interface
12
+ can easily be extended to output any format one could want including classes
13
+ in other languages (Python, C++, Java, etc), other XML formats, etc.
14
+
15
+ RXSD also parses XML conforming to a XSD schema, and instantiates objects
16
+ corresponding to the XSD classes created. Furthermore, RXSD will work with
17
+ existing class definitions resulting in a quick way to map XSD to Ruby constructs,
18
+ letting you define the schema features that you need, and autogenerting handlers
19
+ to the others.
@@ -0,0 +1,41 @@
1
+ # Simple rxsd test utility
2
+ #
3
+ # Usage rxsd-test.rb uri-to-schema uri-to-xml-instance
4
+ #
5
+ # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
6
+ # See COPYING for the License of this software
7
+
8
+ require 'lib/rxsd'
9
+
10
+ if ARGV.size < 2
11
+ puts "missing required arguments"
12
+ exit
13
+ end
14
+
15
+ xsd_uri = ARGV[0]
16
+ xml_uri = ARGV[1]
17
+
18
+ schema = RXSD::Parser.parse_xsd :uri => xsd_uri
19
+ #def disp_child(obj)
20
+ # if obj.respond_to? 'children'
21
+ # puts "#{obj}"
22
+ # obj.children.each { |c|
23
+ # disp_child c
24
+ # }
25
+ # end
26
+ #end
27
+ #disp_child(schema)
28
+
29
+ puts "=======Classes======="
30
+ classes = schema.to :ruby_classes
31
+ puts classes.collect{ |cl| !cl.nil? ? (cl.to_s + " < " + cl.superclass.to_s) : ""}.sort.join("\n")
32
+
33
+ puts "=======Tags======="
34
+ puts schema.tags.collect { |n,cb| n + ": " + cb.to_s + ": " + (cb.nil? ? "ncb" : cb.klass_name.to_s + "-" + cb.klass.to_s) }.sort.join("\n")
35
+
36
+ puts "=======Objects======="
37
+ data = RXSD::Parser.parse_xml :uri => xml_uri
38
+ objs = data.to :ruby_objects, :schema => schema
39
+ objs.each { |obj|
40
+ puts "#{obj}"
41
+ }
@@ -0,0 +1,22 @@
1
+ # include all rxsd modules
2
+ #
3
+ # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
+ # See COPYING for the License of this software
5
+
6
+ lib = File.dirname(__FILE__)
7
+
8
+ $: << lib + '/rxsd/'
9
+
10
+ require 'rubygems'
11
+
12
+ require lib + '/rxsd/exceptions'
13
+ require lib + '/rxsd/common'
14
+ require lib + '/rxsd/builtin_types'
15
+ require lib + '/rxsd/xml'
16
+ require lib + '/rxsd/loader'
17
+ require lib + '/rxsd/resolver'
18
+ require lib + '/rxsd/parser'
19
+ require lib + '/rxsd/builder'
20
+ require lib + '/rxsd/translator'
21
+
22
+ Dir[lib + '/rxsd/xsd/*.rb'].each { |xsd_module| require xsd_module }
@@ -0,0 +1,159 @@
1
+ # RXSD builder
2
+ #
3
+ # responsible for providing interface to build any output format from XSD metadata
4
+ #
5
+ # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
6
+ # See COPYING for the License of this software
7
+
8
+ module RXSD
9
+
10
+ # base interface and helper methods to build classes in various formats from specified parameters
11
+ class ClassBuilder
12
+ # actual class built
13
+ attr_accessor :klass
14
+
15
+ # name of class to build
16
+ attr_accessor :klass_name
17
+
18
+ # class builder corresponding to associated type where approprate
19
+ attr_accessor :associated_builder
20
+
21
+ # class builder corresponding to base type where approprate
22
+ attr_accessor :base_builder
23
+
24
+ # array of class builders for all attributes
25
+ attr_accessor :attribute_builders
26
+
27
+ # name of the attribute which this class represents, for use in accessor construction
28
+ attr_accessor :attribute_name
29
+
30
+ # create a new class builder w/ specified args
31
+ def initialize(args = {})
32
+ if args.has_key? :builder
33
+ @klass = args[:builder].klass
34
+ @klass_name = args[:builder].klass_name
35
+ @associated_builder = args[:builder].associated_builder
36
+ @base_builder = args[:builder].base_builder
37
+ @attribute_builders = args[:builder].attribute_builders
38
+ @attribute_name = args[:builder].attribute_name
39
+
40
+ else
41
+ @klass = args[:klass]
42
+ @klass_name = args[:klass_name]
43
+
44
+ if args.has_key? :base_builder
45
+ @base_builder = args[:base_builder]
46
+ elsif args.has_key?(:base) && !args[:base].nil?
47
+ @base_builder = ClassBuilder.new :klass => args[:base]
48
+ end
49
+
50
+ @attribute_builders = []
51
+
52
+ end
53
+ end
54
+
55
+ # helper method
56
+ def base=(base)
57
+ @base_builder = ClassBuilder.new :klass => base
58
+ end
59
+
60
+ # perform a deep copy of builder, takes optional recursive guard
61
+ def clone(cloned = {})
62
+ return cloned[self] if cloned.has_key? self
63
+
64
+ cb = ClassBuilder.new
65
+ cloned[self] = cb
66
+ cb.klass = @klass
67
+ cb.klass_name = @klass_name
68
+ cb.attribute_name = @attribute_name
69
+ cb.associated_builder = @associated_builder.clone(cloned) unless @associated_builder.nil?
70
+ cb.base_builder = @base_builder.clone(cloned) unless @base_builder.nil?
71
+ @attribute_builders.each { |ab|
72
+ cb.attribute_builders.push ab.clone(cloned) unless ab.nil?
73
+ }
74
+ return cb
75
+ end
76
+
77
+ # helper method to get all associated class builders
78
+ def associated(builders = [])
79
+ unless @base_builder.nil? || builders.include?(@base_builder)
80
+ builders.push @base_builder
81
+ builders = @base_builder.associated(builders)
82
+ end
83
+
84
+ unless @associated_builder.nil? || builders.include?(@associated_builder)
85
+ builders.push @associated_builder
86
+ builders = @associated_builder.associated(builders)
87
+ end
88
+
89
+ @attribute_builders.each { |ab|
90
+ unless ab.nil? || builders.include?(ab)
91
+ builders.push ab
92
+ builders = ab.associated(builders)
93
+ end
94
+ }
95
+
96
+ return builders
97
+ end
98
+
99
+ # subclasses must implement build method to
100
+ # construct target output type
101
+ virtual :build
102
+
103
+ end # class ClassBuilder
104
+
105
+ # base interface and helper methods to build objects in various formats from specified parameters
106
+ class ObjectBuilder
107
+ # name of class instance to build
108
+ attr_accessor :tag_name
109
+
110
+ # content, for text based data types, will contain text to instantiate object w/
111
+ # TODO might want at some point to store a bool in ClassBuilder (only set to true in SimpleType) indicating
112
+ # that constructed class is a text based type, for verification purposes
113
+ attr_accessor :content
114
+
115
+ # actual object built
116
+ attr_accessor :obj
117
+
118
+ # hash of attribute names / values to assign to class instance attributes
119
+ attr_accessor :attributes
120
+
121
+ # array of children object builders to construct instances and assign to class instance attributes
122
+ attr_accessor :children
123
+
124
+ # parent object builder, optionally set
125
+ attr_accessor :parent
126
+
127
+ # create a new class builder w/ specified args
128
+ def initialize(args = {})
129
+ if args.has_key? :builder
130
+ @tag_name = args[:builder].tag_name
131
+ @content = args[:builder].content
132
+ @obj = args[:builder].obj
133
+ @attributes = args[:builder].attributes
134
+ @children = args[:builder].children
135
+ @parent = args[:builder].parent
136
+
137
+ else
138
+ @tag_name = args[:tag_name]
139
+ @content = args[:content]
140
+ @obj = args[:obj]
141
+ @attributes = args[:attributes]
142
+ @children = args[:children]
143
+ @parent = args[:parent]
144
+
145
+ end
146
+
147
+ @children = [] if @children.nil?
148
+ @attributes = [] if @attributes.nil?
149
+ end
150
+
151
+ # subclasses must implement build method to
152
+ # construct target output type, should take one parameter,
153
+ # the schema which to use in object construction
154
+ virtual :build
155
+
156
+ end
157
+
158
+
159
+ end # module RXSD
@@ -0,0 +1,79 @@
1
+ # RXSD Ruby Class builder
2
+ #
3
+ # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
+ # See COPYING for the License of this software
5
+
6
+ module RXSD
7
+
8
+ # Implements the RXSD::ClassBuilder interface to build Ruby Classes from xsd
9
+ class RubyClassBuilder < ClassBuilder
10
+
11
+
12
+ # implementation of RXSD::ClassBuilder::build
13
+ def build
14
+ # return if already built
15
+ return @klass unless @klass.nil?
16
+
17
+ # need the class name to build class
18
+ return nil if @klass_name.nil?
19
+
20
+ # return if we can find constant corresponding to class name
21
+ if Object.constants.include? @klass_name
22
+ @klass = @klass_name.constantize
23
+ return @klass
24
+ end
25
+
26
+ Logger.debug "building class #{@klass}/#{@klass_name} from xsd"
27
+
28
+ # determine object's superclass, creating it if need be
29
+ superclass = Object
30
+ unless @base_builder.nil?
31
+ if @base_builder.klass.nil?
32
+ @base_builder = RubyClassBuilder.new(:builder => @base_builder)
33
+ @base_builder.build
34
+ end
35
+ superclass = @base_builder.klass
36
+ end
37
+
38
+ # create class
39
+ Object.const_set(@klass_name, Class.new(superclass))
40
+ @klass = @klass_name.constantize
41
+
42
+ # FIXME should only do this if the klass corresponds to a simple type
43
+ @klass.class_method :from_s do |str|
44
+ new(:superclass_value => superclass.from_s(str))
45
+ end
46
+ @klass.send :define_method, :initialize do |*args|
47
+ args = args.first || Hash.new
48
+ if !args.nil? && args.has_key?(:superclass_value)
49
+ if Parser.is_builtin? superclass
50
+ super(args[:superclass_value])
51
+ else
52
+ super(:superclass_value => args[:superclass_value])
53
+ end
54
+ end
55
+ end
56
+
57
+ # define accessors for attributes
58
+ @attribute_builders.each { |atb|
59
+ unless atb.nil?
60
+ att_name = nil
61
+ if !atb.attribute_name.nil?
62
+ att_name = atb.attribute_name.underscore
63
+ elsif !atb.klass_name.nil?
64
+ att_name = atb.klass_name.underscore
65
+ elsif !atb.klass.nil?
66
+ att_name = atb.klass.to_s.underscore
67
+ end
68
+
69
+ @klass.send :attr_accessor, att_name.intern unless att_name.nil?
70
+ end
71
+ }
72
+
73
+ Logger.debug "class #{@klass} built, returning"
74
+ return @klass
75
+ end
76
+
77
+ end
78
+
79
+ end