summaryrefslogtreecommitdiff
path: root/cpp/rubygen/amqpgen.rb
blob: 1bd11848a626ecc6a297ffbe761c26ec03625adc (plain)
1
2
3
4
5
6
7
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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#
# Generic AMQP code generation library.
#

require 'delegate'
require 'rexml/document'
require 'pathname'
include REXML

# Handy String functions for converting names.
class String
  # Convert to CapitalizedForm.
  def caps() gsub( /(^|\W)(\w)/ ) { |m| $2.upcase } end

  # Convert to underbar_separated_form.
  def bars() tr('- .','_'); end

  # Convert to lowerCaseCapitalizedForm
  def lcaps() gsub( /\W(\w)/ ) { |m| $1.upcase } end
end

# Sort an array by name.
class Array
  def sort_by_name()
    sort() { |a,b| a.name <=> b.name }
  end
end

# Add collect to Elements
class Elements
  def collect(xpath, &block)
    result=[]
    each(xpath) { |el| result << yield(el) }
    result
  end
end

# An AmqpElement extends (delegates to) a REXML::Element
# 
# NB: AmqpElements cache various values, they assume that
# the XML model does not change after the AmqpElement has
# been created.
# 
class AmqpElement < DelegateClass(Element)
  def initialize(xml, amqp) super(xml); @amqp_parent=amqp; end

  attr_reader :amqp_parent

  # Return the name attribute, not the element name.
  def name() attributes["name"]; end

  def amqp_root()
    amqp_parent ? amqp_parent.amqp_root : self;
  end
end

# AMQP field element
class AmqpField < AmqpElement
  def initialize(xml, amqp) super; end;

  # Get AMQP type for a domain name.
  def domain_type(name)
    domain=elements["/amqp/domain[@name='#{name}']"]
    (domain and domain.attributes["type"] or name)
  end

  # Get the AMQP type of this field.
  def field_type()
    d=attributes["domain"]
    dt=domain_type d if d
    (dt or attributes["type"])
  end
end

# AMQP method element
class AmqpMethod < AmqpElement
  def initialize(xml, amqp) super; end

  def fields()
    @cache_fields ||= elements.collect("field") { |f| AmqpField.new(f,self); }
  end

  # Responses to this method (0-9)
  def responses()
    @cache_responses ||= elements.collect("response") { |el| new AmqpMethod(el,self) }
  end

  # Methods this method responds to (0-9)
  def responds_to()
    @cache_responds_to ||= elements.collect("../method/response[@name='#{attributes['name']}']") { |el|
      AmqpMethod.new(el.parent, amqp_parent)
    }
  end

  def request?() responds_to().empty?; end
  def response?() not request?; end
end

# AMQP class element.
class AmqpClass < AmqpElement
  def initialize(xml,amqp) super; end

  def methods()
    @cache_methods ||= elements.collect("method") { |el|
      AmqpMethod.new(el,self)
    }.sort_by_name
  end

  # chassis should be "client" or "server"
  def methods_on(chassis)
    elements.collect("method/chassis[@name='#{chassis}']/..") { |m|
      AmqpMethod.new(m,self)
    }.sort_by_name
  end
end

# AMQP root element.
class AmqpRoot < AmqpElement

  # Initialize with output directory and spec files from ARGV.
  def initialize(*specs)
    specs.size or raise "No XML spec files."
    specs.each { |f| File.exists?(f) or raise "Invalid XML file: #{f}"}
    super Document.new(File.new(specs.shift)).root, nil
    specs.each { |s|            # Merge in additional specs
      root=Document.new(File.new(s)).root
      merge(self,root)
    }
  end

  def version()
    attributes["major"]+"-"+attributes["minor"]
  end

  def classes()
    @cache_classes ||= elements.collect("class") { |c| AmqpClass.new(c,self) }.sort_by_name
  end
  
  # Return all methods on all classes.
  def methods() classes.collect { |c| c.methods }.flatten;  end
  
  # Return all methods on chassis for all classes.
  def methods_on(chassis)
    classes.collect { |c| c.methods_on(chassis) }.flatten
  end

  # Merge contents of elements.
  def merge(to,from)
    from.elements.each { |from_child|
      tag,name = from_child.name, from_child.attributes["name"]
      to_child=to.elements["./#{tag}[@name='#{name}']"]
      to_child ? merge(to_child, from_child) : to.add(from_child.clone) }
  end

  private :merge
  
end

# Base class for code generators.
# Supports setting a per-line prefix, useful for e.g. indenting code.
# 
class Generator
  # Takes directory for output or "-", meaning print file names that
  # would be generated.
  def initialize (outdir, amqp)
    @amqp=amqp
    @outdir=outdir
    @prefix=''                  # For indentation or comments.
    @indentstr='    '           # One indent level.
    raise "Invalid output directory: #{outdir}" unless @outdir=="-" or File.directory?(@outdir) 
  end

  # Create a new file, set @out. 
  def file(file)
    puts file                   
    if (@outdir != "-")         
      path=Pathname.new "#{@outdir}/#{file}"
      path.parent.mkpath
      path.open('w') { |@out| yield }
    end
  end

  # Append multi-line string to generated code, prefixing each line.
  def gen (str)
    str.each_line { |line|
      @out << @prefix unless @midline
      @out << line
      @midline = nil
    }
    # Note if we stopped mid-line
    @midline = /[^\n]\z/ === str
  end

  # Append str + '\n' to generated code.
  def genl(str="")
    gen str
    gen "\n"
  end

  # Generate code with added prefix.
  def prefix(add)
    save=@prefix
    @prefix+=add
    yield
    @prefix=save
  end
  
  # Generate indented code
  def indent(n=1,&block) prefix(@indentstr * n,&block); end

  attr_accessor :out
end