Class: RXSD::XSD::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/rxsd/xsd/schema.rb,
lib/rxsd/translator.rb

Overview

Schema defintion, top level XSD element www.w3schools.com/Schema/el_schema.asp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attribute_groupsObject

arrays of children in schema



21
22
23
# File 'lib/rxsd/xsd/schema.rb', line 21

def attribute_groups
  @attribute_groups
end

#attributeFormDefaultObject

schema attribute values



18
19
20
# File 'lib/rxsd/xsd/schema.rb', line 18

def attributeFormDefault
  @attributeFormDefault
end

#attributesObject

arrays of children in schema



21
22
23
# File 'lib/rxsd/xsd/schema.rb', line 21

def attributes
  @attributes
end

#complex_typesObject

arrays of children in schema



21
22
23
# File 'lib/rxsd/xsd/schema.rb', line 21

def complex_types
  @complex_types
end

#elementFormDefaultObject

schema attribute values



18
19
20
# File 'lib/rxsd/xsd/schema.rb', line 18

def elementFormDefault
  @elementFormDefault
end

#elementsObject

arrays of children in schema



21
22
23
# File 'lib/rxsd/xsd/schema.rb', line 21

def elements
  @elements
end

#groupsObject

arrays of children in schema



21
22
23
# File 'lib/rxsd/xsd/schema.rb', line 21

def groups
  @groups
end

#namespacesObject

hash of prefix => namespace values

" is the key of the default namespace (or use default_namespace)


15
16
17
# File 'lib/rxsd/xsd/schema.rb', line 15

def namespaces
  @namespaces
end

#parentObject

parent, always nil but here for conformity



24
25
26
# File 'lib/rxsd/xsd/schema.rb', line 24

def parent
  @parent
end

#simple_typesObject

arrays of children in schema



21
22
23
# File 'lib/rxsd/xsd/schema.rb', line 21

def simple_types
  @simple_types
end

#targetNamespaceObject

schema attribute values



18
19
20
# File 'lib/rxsd/xsd/schema.rb', line 18

def targetNamespace
  @targetNamespace
end

#versionObject

schema attribute values



18
19
20
# File 'lib/rxsd/xsd/schema.rb', line 18

def version
  @version
end

Class Method Details

.from_xml(node) ⇒ Object

node passed in should be a xml root node representing the schema



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rxsd/xsd/schema.rb', line 48

def self.from_xml(node)
   schema = Schema.new
   schema.parent = nil
   node.related= schema

   # set namespaces 
   schema.namespaces = {}
   node.namespaces.each{ |ns| schema.namespaces[ns.prefix] = ns.href }

   # parse version out of attrs
   schema.version = node.attrs["version"]

   # parse target namespace out of attrs
   schema.targetNamespace = node.attrs["targetNamespace"]

   # parse elementFormDefault out of attrs
   schema.elementFormDefault = node.attrs["elementFormDefault"]

   # parse attributeFormDefault out of attrs
   schema.attributeFormDefault = node.attrs["attributeFormDefault"]

   # TODO schema attrs: | blockDefault / finalDefault / anyAttr
   # TODO schema children: | import, notation, redefine / anyChild
   # FIXME handle "xs:include" (use Loader?)

   # parse elements
   schema.elements = node.children_objs Element

   # parse simple types
   schema.simple_types = node.children_objs SimpleType

   # parse complex types
   schema.complex_types = node.children_objs ComplexType

   # parse attributes
   schema.attributes = node.children_objs Attribute

   # parse attribute groups
   schema.attribute_groups = node.children_objs AttributeGroup

   # parse groups
   schema.groups = node.children_objs Group
   
   return schema
end

.tag_nameObject

xml tag name



32
33
34
# File 'lib/rxsd/xsd/schema.rb', line 32

def self.tag_name
  "schema"
end

Instance Method Details

#all_class_buildersObject

helper method, return all class builders in/under schema



43
44
45
46
47
# File 'lib/rxsd/translator.rb', line 43

def all_class_builders
     to_class_builders.collect { |cb| cb.associated.push cb }.flatten.uniq.compact # FIXME this only filters duplicates by obj id,
                                                                                   # its possible we have multiple objects refering
                                                                                   # to the same type, should filter these out here or sometime b4
end

#childrenObject

returns array of all schema children



42
43
44
45
# File 'lib/rxsd/xsd/schema.rb', line 42

def children
  ([@elements] + [@simple_types] + [@complex_types] + [@attributes] + 
   [@attribute_groups] + [@groups]).flatten
end

#default_namespaceObject

return default namespace



27
28
29
# File 'lib/rxsd/xsd/schema.rb', line 27

def default_namespace
   @namespaces[""]
end

#infoObject

return xsd node info



37
38
39
# File 'lib/rxsd/xsd/schema.rb', line 37

def info
  "schema"
end

#resolve(node_objs) ⇒ Object

resolve hanging references given complete xsd node object array



95
96
97
98
# File 'lib/rxsd/xsd/schema.rb', line 95

def resolve(node_objs)
   # right now does nothing
   #   (possible resolve include/import here)
end

#tagsObject

helper method, return hash of all tag names -> class builders under schema. An tag is an element or attribute name that can appear in a xml document conforming to the schema



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rxsd/translator.rb', line 23

def tags
     unless defined? @tags
       @tags = {}
       Resolver.
         node_objects(self)[Element].
         find_all { |no| no.class == Element }.
         each     { |elem|
           unless elem.name.nil?
             @tags[elem.name] = elem.to_class_builder
             eca = elem.child_attributes
             eca.each { |att|
               @tags[elem.name + ":" + att.name] = att.to_class_builder # prepend element to attribute name to prevent conflicts
             } unless eca.nil?
           end
         }
     end
     return @tags
end

#to(output_type) ⇒ Object

translates schema and all child entities to instances of specified output type. output_type may be one of

* :ruby_classes
* :ruby_definitions


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rxsd/translator.rb', line 53

def to(output_type)
   cbs = all_class_builders
   results = []
   cbs.each { |cb|
     # probably a better way to do this at some point than invoking the copy constructors

     # FIXME we create class builders in the translator on the fly, this may cause
     # problems for later operations that need to access class builder attributes which
     # have been created
     case(output_type)
      when :ruby_classes
         cl = RubyClassBuilder.new(:builder => cb).build
         results.push cl unless results.include? cl
         cb.klass = cl # small hack to get around the above fixme... for now
      when :ruby_definitions
         df = RubyDefinitionBuilder.new(:builder => cb).build
         results.push df unless results.include? df
     end
   }
   return results
end

#to_class_buildersObject

convert schema to array of class builders



101
102
103
104
105
106
107
108
109
110
# File 'lib/rxsd/xsd/schema.rb', line 101

def to_class_builders
   unless defined? @class_builder
     @class_builders = []
     @elements.each { |e|
       @class_builders.push e.to_class_builder
     }
   end

   return @class_builders
end