Class: Riddl::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/riddl/wrapper.rb,
lib/ruby/riddl/wrapper.rb,
lib/ruby/riddl/wrapper/declaration.rb,
lib/ruby/riddl/wrapper/description.rb,
lib/ruby/riddl/wrapper/layerchecker.rb,
lib/ruby/riddl/wrapper/messageparser.rb,
lib/ruby/riddl/wrapper/resourcechecker.rb,
lib/ruby/riddl/wrapper/declaration/tile.rb,
lib/ruby/riddl/wrapper/declaration/facade.rb,
lib/ruby/riddl/wrapper/description/access.rb,
lib/ruby/riddl/wrapper/description/resource.rb,
lib/ruby/riddl/wrapper/declaration/interface.rb,
lib/ruby/riddl/wrapper/description/message_and_transformation.rb

Defined Under Namespace

Classes: Declaration, Description, IOMessages, LayerChecker, MessageParser, ResourceChecker, WrapperUtils

Constant Summary collapse

VERSION_MAJOR =

{{{

1
VERSION_MINOR =
0
VERSION =
"#{VERSION_MAJOR}.#{VERSION_MINOR}"
DESCRIPTION =
"http://riddl.org/ns/description/#{VERSION}"
DECLARATION =
"http://riddl.org/ns/declaration/#{VERSION}"
XINCLUDE =
"http://www.w3.org/2001/XInclude"
DESCRIPTION_FILE =
"#{File.dirname(__FILE__)}/ns/description/#{VERSION_MAJOR}.#{VERSION_MINOR}/description.rng"
DECLARATION_FILE =
"#{File.dirname(__FILE__)}/ns/declaration/#{VERSION_MAJOR}.#{VERSION_MINOR}/declaration.rng"
RIDDL_DESCRIPTION_SHOW =
"#{File.dirname(__FILE__)}/ns/common-patterns/riddl-description/show.xml"
RIDDL_DESCRIPTION_RESOURCE_SHOW =
"#{File.dirname(__FILE__)}/ns/common-patterns/riddl-description/resource-show.xml"
COMMON =
"datatypeLibrary=\"http://www.w3.org/2001/XMLSchema-datatypes\" xmlns=\"#{DESCRIPTION}\" xmlns:xi=\"http://www.w3.org/2001/XInclude\""
CHECK =
"<element name=\"check\" datatypeLibrary=\"http://www.w3.org/2001/XMLSchema-datatypes\" xmlns=\"http://relaxng.org/ns/structure/1.0\"><data/></element>"

Instance Method Summary collapse

Constructor Details

#initialize(name, get_description = false) ⇒ Wrapper

}}}



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
# File 'lib/ruby/riddl/wrapper.rb', line 64

def initialize(name,get_description=false)
  #{{{
  @doc = nil

  fpath = nil
  if name.is_a?(XML::Smart::Dom)
    @doc = name
  else  
    begin
      fh = name.respond_to?(:read) ? name : open(name)
      @doc = XML::Smart.string(fh.read)
      fh.close
      fpath = File.dirname(fh.path) if fh.is_a?(File) || fh.is_a?(Tempfile)
    rescue
      begin
        @doc = XML::Smart.string(name)
      rescue
        raise SpecificationError, "#{name.inspect} is no RIDDL description or declaration (neither a file, url or string)."
      end
    end  
  end  

  @doc.register_namespace 'x', XINCLUDE
  @doc.find('//x:include/@href').each do |i|
    if i.value =~ /^http:\/\/(www\.)?riddl\.org(\/ns\/common-patterns\/.*)/
      t = File.expand_path(File.dirname(__FILE__)) + $2
      i.value = t if File.exists?(t)
    end
  end
  fpath.nil? ? @doc.xinclude! : @doc.xinclude!(fpath)

  qname = @doc.root.qname
  @is_description = qname.href == DESCRIPTION && qname.name ==  'description'
  @is_declaration = qname.href == DECLARATION && qname.name ==  'declaration'

  @doc.register_namespace 'des', DESCRIPTION
  @doc.register_namespace 'dec', DECLARATION
  if @is_description && get_description
    rds = XML::Smart::open_unprotected(RIDDL_DESCRIPTION_SHOW)
    rrds = XML::Smart::open_unprotected(RIDDL_DESCRIPTION_RESOURCE_SHOW)
    @doc.root.prepend rds.find('/xmlns:description/xmlns:message')
    @doc.root.prepend rrds.find('/xmlns:description/xmlns:message')
    @doc.find("/des:description/des:resource").each do |r|
      r.prepend rds.find('/xmlns:description/xmlns:resource/*')
    end  
    @doc.find("/des:description//des:resource").each do |r|
      r.prepend rrds.find('/xmlns:description/xmlns:resource/*')
    end  
  end
  if @is_declaration  && get_description
    @doc.root.prepend("dec:interface",:name=>"riddldescription").add XML::Smart::open_unprotected(RIDDL_DESCRIPTION_SHOW).root
    @doc.root.prepend("dec:interface",:name=>"riddlresourcedescription").add XML::Smart::open_unprotected(RIDDL_DESCRIPTION_RESOURCE_SHOW).root
    @doc.root.find("dec:facade").first.append('dec:tile').append("layer",:name => "riddldescription")
    @doc.root.find("dec:facade").first.append('dec:tile').append("layer",:name => "riddlresourcedescription",:everywhere => 'true')
  end  

  @declaration = @description = nil
  #}}}
end

Instance Method Details

#check_message(params, headers, message) ⇒ Object



209
210
211
212
213
214
215
216
217
218
# File 'lib/ruby/riddl/wrapper.rb', line 209

def check_message(params,headers,message)
  description
  declaration
  #{{{
  return true if message.class == Riddl::Wrapper::Description::Star
  return true if message.nil? && params == []
  mp = MessageParser.new(params,headers)
  mp.check(message,true)
  #}}}
end

#declarationObject



124
125
126
127
128
129
130
131
# File 'lib/ruby/riddl/wrapper.rb', line 124

def declaration
  #{{{
  if @declaration.nil? && @is_declaration
    @declaration = Riddl::Wrapper::Declaration.new(@doc)
  end
  @declaration
  #}}}
end

#declaration?Boolean

Returns:

  • (Boolean)


303
# File 'lib/ruby/riddl/wrapper.rb', line 303

def declaration?; @is_declaration; end

#descriptionObject



133
134
135
136
137
138
139
140
# File 'lib/ruby/riddl/wrapper.rb', line 133

def description
  #{{{
  if @description.nil? && @is_description
    @description = Riddl::Wrapper::Description.new(@doc)
  end
  @description
  #}}}
end

#description?Boolean

Returns:

  • (Boolean)


304
# File 'lib/ruby/riddl/wrapper.rb', line 304

def description?; @is_description; end

#io_messages(path, operation, params, headers) ⇒ Object



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
# File 'lib/ruby/riddl/wrapper.rb', line 156

def io_messages(path,operation,params,headers)
  #{{{
  description
  declaration
 
  req = @description.get_resource(path).access_methods if @is_description
  req = @declaration.get_resource(path).composition    if @is_declaration

  mp = MessageParser.new(params,headers)
  if req.has_key?(operation)
    if @is_description
      r = req[operation][0]
      r.select{|o|o.class==Riddl::Wrapper::Description::RequestInOut}.each do |o|
        return IOMessages.new(o.in, o.out) if mp.check(o.in)
      end
      r.select{|o|o.class==Riddl::Wrapper::Description::RequestTransformation}.each do |o|
        # TODO guess structure from input, create new output structure
        return IOMessages.new(Riddl::Wrapper::Description::Star.new, Riddl::Wrapper::Description::Star.new)
      end
      r.select{|o|o.class==Riddl::Wrapper::Description::RequestStarOut}.each do |o|
        return IOMessages.new(Riddl::Wrapper::Description::Star.new, o.out)
      end
      r.select{|o|o.class==Riddl::Wrapper::Description::RequestPass}.each do |o|
        return IOMessages.new(Riddl::Wrapper::Description::Star.new, Riddl::Wrapper::Description::Star.new)
      end
      r.select{|o|o.class==Riddl::Wrapper::Description::WebSocket}.each do |o|
        return IOMessages.new(nil,nil,nil,nil)
      end
    end  
    if @is_declaration
      r = req[operation]
      r.select{|o|o.result.class==Riddl::Wrapper::Description::RequestInOut}.each do |o|
        return IOMessages.new(o.result.in, o.result.out, o.route, o.result.interface) if mp.check(o.result.in)
      end
      r.select{|o|o.result.class==Riddl::Wrapper::Description::RequestTransformation}.each do |o|
        # TODO guess structure from input, create new output structure
        return IOMessages.new(Riddl::Wrapper::Description::Star.new, Riddl::Wrapper::Description::Star.new, o.route, o.result.interface)
      end
      r.select{|o|o.result.class==Riddl::Wrapper::Description::RequestStarOut}.each do |o|
        return IOMessages.new(Riddl::Wrapper::Description::Star.new, o.result.out, o.route, o.result.interface)
      end
      r.select{|o|o.result.class==Riddl::Wrapper::Description::RequestPass}.each do |o|
        return IOMessages.new(Riddl::Wrapper::Description::Star.new, Riddl::Wrapper::Description::Star.new, o.route, o.result.interface)
      end
      r.select{|o|o.result.class==Riddl::Wrapper::Description::WebSocket}.each do |o|
        return IOMessages.new(nil,nil,nil,o.result.interface)
      end
    end  
  end  
  return nil
  #}}}
end

#load_necessary_handlers!Object



238
239
240
241
242
243
244
245
246
# File 'lib/ruby/riddl/wrapper.rb', line 238

def load_necessary_handlers!
  #{{{
  @doc.find("//des:parameter/@handler").map{|h|h.to_s}.uniq.each do |h|
    if File.exists?(File.dirname(__FILE__) + '/handlers/' + File.basename(h) + ".rb")
      require File.expand_path(File.dirname(__FILE__) + '/handlers/' + File.basename(h))
    end
  end
  #}}}
end

#load_necessary_roles!Object

}}}



247
248
249
250
251
252
253
254
255
256
# File 'lib/ruby/riddl/wrapper.rb', line 247

def load_necessary_roles!
  #{{{
  @doc.find("//des:resource/@role").map{|h|h.to_s}.uniq.each do |h|
    h = Protocols::HTTP::Generator::escape(h)
    if File.exists?(File.dirname(__FILE__) + '/roles/' + h + '.rb')
      require File.expand_path(File.dirname(__FILE__) + '/roles/' + h)
    end
  end
  #}}}
end

#pathsObject



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/ruby/riddl/wrapper.rb', line 258

def paths
  #{{{
  description
  declaration
  tmp = []
  tmp = @description.paths if @is_description 
  tmp = @declaration.paths if @is_declaration
  tmp.map do |t|
    [t[0],Regexp.new("^" + t[0].gsub(/\{\}/,"[^/]+") + (t[1] ? '\/?' : '\/?$'))]
  end
  #}}}
end

#resource_description(path) ⇒ Object

}}}



150
151
152
153
154
# File 'lib/ruby/riddl/wrapper.rb', line 150

def resource_description(path)
  req = @description.get_resource(path).description_xml(@doc.namespaces) if @is_description
  req = @declaration.get_resource(path).description_xml(@doc.namespaces) if @is_declaration
  req.to_s
end

#role(path) ⇒ Object

{{{



142
143
144
145
146
147
148
# File 'lib/ruby/riddl/wrapper.rb', line 142

def role(path) #{{{
  description
  declaration
  return @description.get_resource(path).role if @is_description
  return @declaration.get_resource(path).role if @is_declaration
  nil
end

#validate!Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/ruby/riddl/wrapper.rb', line 220

def validate!
  #{{{
  if @is_description
    xval = @doc.validate_against(XML::Smart.open_unprotected(DESCRIPTION_FILE))
    xchk = ResourceChecker.new(@doc).check
    puts xchk.join("\n") unless xchk.empty?
    return xval && xchk.empty?
  end
  if @is_declaration
    xval = @doc.validate_against(XML::Smart.open_unprotected(DECLARATION_FILE))
    xchk = LayerChecker.new(@doc).check
    puts xchk.join("\n") unless xchk.empty?
    return xval && xchk.empty?
  end
  nil
  #}}}
end