Module: Erubis::JavaGenerator

Includes:
Generator
Included in:
Ejava, PI::Ejava
Defined in:
lib/erubis/engine/ejava.rb

Instance Attribute Summary

Attributes included from Generator

#escapefunc

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Generator

#escaped_expr

Class Method Details

.supported_propertiesObject

:nodoc:



17
18
19
20
21
22
23
# File 'lib/erubis/engine/ejava.rb', line 17

def self.supported_properties()   # :nodoc:
  return [
          [:indent,   '',       "indent spaces (ex. '  ')"],
          [:buf,      '_buf',   "output buffer name"],
          [:bufclass, 'StringBuffer', "output buffer class (ex. 'StringBuilder')"],
        ]
end

Instance Method Details

#add_expr_debug(src, code) ⇒ Object



69
70
71
72
73
# File 'lib/erubis/engine/ejava.rb', line 69

def add_expr_debug(src, code)
  code.strip!
  src << @indent if src.empty? || src[-1] == ?\n
  src << " System.err.println(\"*** debug: #{code}=\"+(#{code}));"
end

#add_expr_escaped(src, code) ⇒ Object



65
66
67
# File 'lib/erubis/engine/ejava.rb', line 65

def add_expr_escaped(src, code)
  add_expr_literal(src, escaped_expr(code))
end

#add_expr_literal(src, code) ⇒ Object



59
60
61
62
63
# File 'lib/erubis/engine/ejava.rb', line 59

def add_expr_literal(src, code)
  src << @indent if src.empty? || src[-1] == ?\n
  code.strip!
  src << " #{@buf}.append(#{code});"
end

#add_postamble(src) ⇒ Object



75
76
77
78
79
# File 'lib/erubis/engine/ejava.rb', line 75

def add_postamble(src)
  src << "\n" if src[-1] == ?;
  src << @indent << "return " << @buf << ".toString();\n"
  #src << @indent << "System.out.print(" << @buf << ".toString());\n"
end

#add_preamble(src) ⇒ Object



33
34
35
# File 'lib/erubis/engine/ejava.rb', line 33

def add_preamble(src)
  src << "#{@indent}#{@bufclass} #{@buf} = new #{@bufclass}();"
end

#add_stmt(src, code) ⇒ Object



55
56
57
# File 'lib/erubis/engine/ejava.rb', line 55

def add_stmt(src, code)
  src << code
end

#add_text(src, text) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/erubis/engine/ejava.rb', line 42

def add_text(src, text)
  return if text.empty?
  src << (src.empty? || src[-1] == ?\n ? @indent : ' ')
  src << @buf << ".append("
  i = 0
  text.each_line do |line|
    src << "\n" << @indent << '          + ' if i > 0
    i += 1
    src << '"' << escape_text(line) << '"'
  end
  src << ");" << (text[-1] == ?\n ? "\n" : "")
end

#escape_text(text) ⇒ Object



37
38
39
40
# File 'lib/erubis/engine/ejava.rb', line 37

def escape_text(text)
  @@table_ ||= { "\r"=>"\\r", "\n"=>"\\n", "\t"=>"\\t", '"'=>'\\"', "\\"=>"\\\\" }
  return text.gsub!(/[\r\n\t"\\]/) { |m| @@table_[m] } || text
end

#init_generator(properties = {}) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/erubis/engine/ejava.rb', line 25

def init_generator(properties={})
  super
  @escapefunc ||= 'escape'
  @indent = properties[:indent] || ''
  @buf = properties[:buf] || '_buf'
  @bufclass = properties[:bufclass] || 'StringBuffer'
end