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
123
124
125
126
127
# 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  
    if name.is_a?(String) && name =~ /^http:\/\/(www\.)?riddl\.org(\/ns\/common-patterns\/.*)/
      name = File.expand_path(File.dirname(__FILE__)) + $2
    end
    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)
    rds.register_namespace('des','http://riddl.org/ns/description/1.0')
    rrds.register_namespace('des','http://riddl.org/ns/description/1.0')
    @doc.root.prepend rds.find('/des:description/des:message')
    @doc.root.prepend rrds.find('/des:description/des:message')
    @doc.find("/des:description/des:resource").each do |r|
      r.prepend rds.find('/des:description/des:resource/*')
    end  
    @doc.find("/des:description//des:resource").each do |r|
      r.prepend rrds.find('/des:description/des: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



214
215
216
217
218
219
220
221
222
223
# File 'lib/ruby/riddl/wrapper.rb', line 214

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



129
130
131
132
133
134
135
136
# File 'lib/ruby/riddl/wrapper.rb', line 129

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

#declaration?Boolean

Returns:

  • (Boolean)


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

def declaration?; @is_declaration; end

#descriptionObject



138
139
140
141
142
143
144
145
# File 'lib/ruby/riddl/wrapper.rb', line 138

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

#description?Boolean

Returns:

  • (Boolean)


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

def description?; @is_description; end

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



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

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



243
244
245
246
247
248
249
250
251
# File 'lib/ruby/riddl/wrapper.rb', line 243

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

}}}



252
253
254
255
256
257
258
259
260
261
# File 'lib/ruby/riddl/wrapper.rb', line 252

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

#pathsObject



263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/ruby/riddl/wrapper.rb', line 263

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

}}}



155
156
157
158
159
# File 'lib/ruby/riddl/wrapper.rb', line 155

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

{{{



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

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



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/ruby/riddl/wrapper.rb', line 225

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