Class: DSLize::Formatter::XSD

Inherits:
Base show all
Defined in:
lib/dslize/formatter/xsd_formatter.rb

Instance Attribute Summary

Attributes inherited from Base

#objects, #superclasses

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from DSLize::Formatter::Base

Instance Method Details

#generate!(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dslize/formatter/xsd_formatter.rb', line 8

def generate!(*args)
  schema = XPain::Builder.new do |xsd|
    xsd.schema do
      objects.each do |name, options|
        parent = superclasses[name]
        
        xsd.element(name, :type => (options[:type] or name))
        
        xsd.complexType({ :name => name, :abstract => options[:abstract], :mixed => false }.select { |k,v| !v.nil? }) do
          extends_base_if_needed(xsd, parent) do
            
            # relations
            xsd.sequence do
              { :has_one => nil, :has_many => "unbounded"}.each do |relation, max_occurs|
                (options[relation] or []).each do |k, v|
                  named_object_if_needed(xsd, k, v) do
                    clazz = v[:type] or k
                    subclasses = get_subclasses(clazz)
                    if subclasses.empty?
                      xsd.element(nil, { :ref => clazz, :maxOccurs => max_occurs }.select { |k,v| !v.nil? })
                    else
                      xsd.choice({ :maxOccurs => max_occurs }.select { |k,v| !v.nil? }) do
                        xsd.annotation do
                          xsd.appinfo do
                            xsd.property :name => clazz, :xmlns => "http://java.sun.com/xml/ns/jaxb"
                          end
                        end
                        subclasses.each do |sub_type|
                          xsd.element(nil, :ref => sub_type)
                        end
                        if !objects[clazz][:abstract]
                          xsd.element(nil, :ref => clazz)
                        end
                      end
                    end
                  end
                end
              end
            end
          
            # attributes
            (options[:attributes] or []).each do |k, v|
              xsd.attribute({ :name => k, :default => v[:default], :type => xsd_type(v[:type]) }.select { |k,v| !v.nil? })
            end
            
          end
        end
        
      end
    end
  end
  
  File.open(args[0], "w") do |f|
    f << schema.to_xml.gsub(' name=""', ''). # xpain is buggy and always write a name on <element>
      gsub(' xmlns:xsd="http://www.w3.org/2001/XMLSchema"', ' xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.0" elementFormDefault="qualified" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"')
  end
end