Object
A process definition parser.
Can parse XML, JSON, Ruby (and more) process definition representations.
# File lib/ruote/parser.rb, line 42 def initialize (context) @context = context end
Class method for parsing process definition (XML, Ruby, from file or from a string, ...) to syntax trees. Used by ruote-fluo for example.
# File lib/ruote/parser.rb, line 75 def self.parse (d) unless @parser require 'ostruct' require 'ruote/util/treechecker' @parser = Ruote::Parser.new( OpenStruct.new('treechecker' => Ruote::TreeChecker.new({}))) end @parser.parse(d) end
Returns true if the defintion is a remote URI
# File lib/ruote/parser.rb, line 154 def self.remote? (definition) u = URI.parse(definition) (u.scheme != nil) && ( ! ('A'..'Z').include?(u.scheme)) end
Turns the process definition tree (ruote syntax tree) to a JSON String.
# File lib/ruote/parser.rb, line 147 def self.to_json (tree) tree.to_json end
Turns the given process definition tree (ruote syntax tree) to a Ruby process definition (a String containing that ruby process definition).
Mainly used by ruote-fluo.
# File lib/ruote/parser.rb, line 128 def self.to_ruby (tree, level=0) expname = tree[0] expname = 'Ruote.process_definition' if level == 0 && expname == 'define' s = "#{' ' * level}#{expname}#{atts_to_ruby(tree[1])}" return "#{s}\n" if tree[2].empty? s << " do\n" tree[2].each { |child| s << to_ruby(child, level + 1) } s << "#{' ' * level}end\n" s end
Turns the given process definition tree (ruote syntax tree) to an XML String.
Mainly used by ruote-fluo.
# File lib/ruote/parser.rb, line 94 def self.to_xml (tree, options={}) require 'builder' # TODO : deal with "participant 'toto'" builder(options) do |xml| atts = tree[1].dup t = atts.find { |k, v| v == nil } if t atts.delete(t.first) key = tree[0] == 'if' ? 'test' : 'ref' atts[key] = t.first end atts = atts.inject({}) { |h, (k, v)| h[k.to_s.gsub(/\_/, '-')] = v; h } if tree[2].empty? xml.tag!(tree[0], atts) else xml.tag!(tree[0], atts) do tree[2].each { |child| to_xml(child, options) } end end end end
As used by to_ruby.
# File lib/ruote/parser.rb, line 198 def self.atts_to_ruby (atts) return '' if atts.empty? s = [] t = atts.find { |k, v| v == nil } s << t.first.inspect if t s = atts.inject(s) { |a, (k, v)| a << ":#{k} => #{v.inspect}" if t.nil? || k != t.first a }.join(', ') s.length > 0 ? " #{s}" : s end
A convenience method when building XML
# File lib/ruote/parser.rb, line 183 def self.builder (options={}, &block) if b = options[:builder] block.call(b) else b = Builder::XmlMarkup.new(:indent => (options[:indent] || 0)) options[:builder] = b b.instruct! unless options[:instruct] == false block.call(b) b.target! end end
Turns the input into a ruote syntax tree (raw process definition). This method is used by engine.launch(x) for example.
# File lib/ruote/parser.rb, line 50 def parse (definition) return definition if definition.is_a?(Array) and definition.size == 3 (return XmlParser.parse(definition)) rescue nil (return Rufus::Json.decode(definition)) rescue nil (return ruby_eval(definition)) rescue nil if definition.index("\n") == nil raise ArgumentError.new( "remote process definitions are not allowed" ) if Ruote::Parser.remote?(definition) && @context['remote_definition_allowed'] != true return parse(open(definition).read) end raise ArgumentError.new( "doesn't know how to parse definition (#{definition.class}) " + "or error in process definition") end
Evaluates the ruby string in the code, but at fist, thanks to the treechecker, makes sure it doesn't code malicious ruby code (at least tries very hard).
# File lib/ruote/parser.rb, line 167 def ruby_eval (s) @context.treechecker.check(s) eval(s) rescue Exception => e #puts '=' * 80 #p s #puts '-' * 80 #puts e #e.backtrace.each { |l| puts l } raise ArgumentError.new('probably not ruby') end
Generated with the Darkfish Rdoc Generator 2.