class Rake::Ant::ProjectConverter

Constants

ID

Public Class Methods

convert(file = "build.xml") click to toggle source
# File lib/rake/ant/project_converter.rb, line 93
def self.convert(file = "build.xml")
  source = File.exists?(file) ? File.new(file) : $stdin
  parser = REXML::Parsers::SAX2Parser.new source
  parser.listen self.new
  parser.parse
end
new() click to toggle source
# File lib/rake/ant/project_converter.rb, line 9
def initialize
  @output = $stdout
  @depth = 0
  @seen = ['<DOC>', 0]
end

Public Instance Methods

characters(text) click to toggle source
# File lib/rake/ant/project_converter.rb, line 34
def characters(text)
  @output.print value_inspect(text.strip) if text.strip.length > 0
end
emit(code, attributes = nil) click to toggle source
# File lib/rake/ant/project_converter.rb, line 53
def emit(code, attributes = nil)
  print_with_indent safe_method_name(code, attributes)
  if attributes
    first = true
    attributes.each do |k,v|
      @output.print "," unless first
      @output.print " #{symbol_or_string(k)} => #{value_inspect(v)}"
      first = false
    end
  end
end
emit_do(tag) click to toggle source
# File lib/rake/ant/project_converter.rb, line 38
def emit_do(tag)
  @output.puts " do" if @seen.last[1] != @depth
  @seen << [tag, @depth]
end
emit_end(tag) click to toggle source
# File lib/rake/ant/project_converter.rb, line 43
def emit_end(tag)
  level = @seen.last
  if level[0] == tag && level[1] == @depth
    @output.puts
  else
    print_with_indent "end\n"
    @seen << ["/#{tag}", @depth]
  end
end
end_element(uri, localname, qname) click to toggle source
# File lib/rake/ant/project_converter.rb, line 29
def end_element(uri, localname, qname)
  @depth -= 1
  emit_end(localname)
end
print_with_indent(str) click to toggle source
safe_method_name(s, attributes) click to toggle source
# File lib/rake/ant/project_converter.rb, line 84
def safe_method_name(s, attributes)
  s = Ant.safe_method_name(s)
  if s =~ ID
    s
  else
    "_element #{s.inspect}#{attributes.nil? || attributes.empty? ? '' : ','}"
  end
end
start_element(uri, localname, qname, attributes) click to toggle source
# File lib/rake/ant/project_converter.rb, line 15
def start_element(uri, localname, qname, attributes)
  emit_do(localname)
  case localname
  when "project"
    @output.puts "require 'ant'"
    emit "ant", attributes
  when "description"
    print_with_indent "project.description = "
  else
    emit localname, attributes
  end
  @depth += 1
end
symbol_or_string(s) click to toggle source
# File lib/rake/ant/project_converter.rb, line 72
def symbol_or_string(s)
  if s =~ ID
    ":#{s}"
  else
    s.inspect
  end
end
value_inspect(s) click to toggle source
# File lib/rake/ant/project_converter.rb, line 80
def value_inspect(s)
  s.inspect.gsub("\\n", "\n")
end