Class: Ant::ProjectConverter
- Inherits:
-
Object
- Object
- Ant::ProjectConverter
- Includes:
- REXML::SAX2Listener
- Defined in:
- lib/rake/ant/project_converter.rb
Constant Summary collapse
- ID =
/\A[a-zA-Z_][a-zA-Z0-9_]*\z/
Class Method Summary collapse
Instance Method Summary collapse
- #characters(text) ⇒ Object
- #emit(code, attributes = nil) ⇒ Object
- #emit_do(tag) ⇒ Object
- #emit_end(tag) ⇒ Object
- #end_element(uri, localname, qname) ⇒ Object
-
#initialize ⇒ ProjectConverter
constructor
A new instance of ProjectConverter.
- #print_with_indent(str) ⇒ Object
- #safe_method_name(s, attributes) ⇒ Object
- #start_element(uri, localname, qname, attributes) ⇒ Object
- #symbol_or_string(s) ⇒ Object
- #value_inspect(s) ⇒ Object
Constructor Details
#initialize ⇒ ProjectConverter
Returns a new instance of ProjectConverter.
9 10 11 12 13 |
# File 'lib/rake/ant/project_converter.rb', line 9 def initialize @output = $stdout @depth = 0 @seen = ['<DOC>', 0] end |
Class Method Details
.convert(file = "build.xml") ⇒ Object
93 94 95 96 97 98 |
# 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 |
Instance Method Details
#characters(text) ⇒ Object
34 35 36 |
# 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) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# 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) ⇒ Object
38 39 40 41 |
# 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) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# 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) ⇒ Object
29 30 31 32 |
# 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) ⇒ Object
65 66 67 68 |
# File 'lib/rake/ant/project_converter.rb', line 65 def print_with_indent(str) @output.print " " * @depth @output.print str end |
#safe_method_name(s, attributes) ⇒ Object
84 85 86 87 88 89 90 91 |
# 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) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# 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) ⇒ Object
72 73 74 75 76 77 78 |
# 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) ⇒ Object
80 81 82 |
# File 'lib/rake/ant/project_converter.rb', line 80 def value_inspect(s) s.inspect.gsub("\\n", "\n") end |