Class: Ruboto::Util::XMLElement

Inherits:
Hash
  • Object
show all
Includes:
CodeFormatting, Verify
Defined in:
lib/ruboto/util/xml_element.rb

Overview

XMLElement:

Extends Hash to simulate a REXML::Element (but much faster) and provides
information in the necessary format to generate Java code.

Constant Summary

Constants included from Verify

Verify::ON_TRAVIS

Instance Method Summary collapse

Methods included from Verify

#project_api_level, #project_properties, #save_manifest, #save_ruboto_config, #save_test_manifest, #verify_activity, #verify_api, #verify_manifest, #verify_min_sdk, #verify_package, #verify_project_properties, #verify_ruboto_config, #verify_sdk_versions, #verify_strings, #verify_target_sdk, #verify_test_manifest

Methods included from CodeFormatting

#if_else, #method_call, #try_catch

Instance Method Details

#activity_super_guard(class_name, method_name) ⇒ Object



226
227
228
229
230
# File 'lib/ruboto/util/xml_element.rb', line 226

def activity_super_guard(class_name, method_name)
  if class_name == 'RubotoActivity' && method_name == 'onCreate'
    "if (preOnCreate(#{parameters.map { |i| i[0] }.join(', ')})) #{super_return};\n"
  end
end

#add_element(name, attributes) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/ruboto/util/xml_element.rb', line 29

def add_element(name, attributes)
  new_element = XMLElement.new
  new_element['name'] = name
  new_element['values'] = attributes

  self[name] = [] unless self[name]
  self[name] << new_element

  new_element
end

#all_methods(method_base = 'all', method_include = '', method_exclude = '', implements = '') ⇒ Object



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
# File 'lib/ruboto/util/xml_element.rb', line 64

def all_methods(method_base='all', method_include='', method_exclude='', implements='')
  # get all the methogs
  all_methods = get_elements('method').select { |m| m.attribute('static') != 'true' }

  # establish the base set of methods
  working_methods = case method_base.to_s
                    when 'all' then
                      all_methods
                    when 'none' then
                      []
                    when 'abstract' then
                      all_methods.select { |i| i.attribute('abstract') == 'true' }
                    when 'on' then
                      all_methods.select { |i| i.attribute('name').match(/^on[A-Z]/) }
                    end

  # make sure to include requested methods
  include_methods = method_include.split(',') if method_include.is_a?(String)
  all_methods.each { |i| working_methods << i if include_methods.include?(i.attribute('name')) }

  # make sure to exclude rejected methods
  exclude_methods = method_exclude.split(',') if method_exclude.is_a?(String)
  working_methods = working_methods.select { |i| not exclude_methods.include?(i.attribute('name')) }

  # remove methods marked final
  working_methods = working_methods.select { |i| (not i.attribute('final')) or i.attribute('final') == 'false' }

  # get additional methods from parent
  if name =='class' and attribute('extends')
    parent = root.find_class(attribute('extends'))
    parent_methods = parent.all_methods(method_base, method_include, method_exclude)
    working_signatures = working_methods.map(&:method_signature)
    working_methods += parent_methods.select { |i| not working_signatures.include?(i.method_signature) }
  end

  # get additional methods from interfaces
  if name =='class' and implements != ''
    implements.split(',').each do |i|
      interface = root.find_interface(i)
      abort("Unkown interface: #{i}") unless interface
      working_signatures = working_methods.map(&:method_signature)
      working_methods += interface.all_methods.select { |j| not working_signatures.include?(j.method_signature) }
    end
  end

  working_methods
end

#attribute(name) ⇒ Object



25
26
27
# File 'lib/ruboto/util/xml_element.rb', line 25

def attribute(name)
  self['values'][name]
end

#constant_stringObject



120
121
122
# File 'lib/ruboto/util/xml_element.rb', line 120

def constant_string
  'CB_' + attribute('name').gsub(/[A-Z]/) { |i| "_#{i}" }.upcase.gsub(/^ON_/, '')
end

#constructor_definition(class_name) ⇒ Object



254
255
256
# File 'lib/ruboto/util/xml_element.rb', line 254

def constructor_definition(class_name)
  method_call(nil, class_name, parameters, nil, [super_string]).indent.join("\n")
end

#default_returnObject



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ruboto/util/xml_element.rb', line 138

def default_return
  return nil unless attribute('return')
  case attribute('return')
  when 'boolean' then
    'return false;'
  when 'int' then
    'return 0;'
  when 'void' then
    nil
  else
    'return null;'
  end
end

#find_class(package_and_class) ⇒ Object



56
57
58
# File 'lib/ruboto/util/xml_element.rb', line 56

def find_class(package_and_class)
  find_class_or_interface(package_and_class, 'class')
end

#find_class_or_interface(klass, a_type) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruboto/util/xml_element.rb', line 44

def find_class_or_interface(klass, a_type)
  abort "ERROR: Can't parse package from #{klass}" unless klass.match(/([a-z.]+)\.([A-Z][A-Za-z.]+)/)

  package = self['package'].find { |i| i.attribute('name') == $1 }
  abort "ERROR: Can't find package #{$1}" unless package
  if a_type == 'either'
    package['class'].find { |i| i.attribute('name') == $2 } or package['interface'].find { |i| i.attribute('name') == $2 }
  else
    package[a_type].find { |i| i.attribute('name') == $2 }
  end
end

#find_interface(package_and_interface) ⇒ Object



60
61
62
# File 'lib/ruboto/util/xml_element.rb', line 60

def find_interface(package_and_interface)
  find_class_or_interface(package_and_interface, 'interface')
end

#get_elements(name) ⇒ Object



40
41
42
# File 'lib/ruboto/util/xml_element.rb', line 40

def get_elements(name)
  self[name] or []
end

#load_scriptObject



244
245
246
247
248
249
250
251
252
# File 'lib/ruboto/util/xml_element.rb', line 244

def load_script
  <<EOF
if (JRubyAdapter.isInitialized() && scriptInfo.isReadyToLoad()) {
  ScriptLoader.loadScript(this);
    } else {
  #{super_return}
    }
EOF
end

#method_definition(class_name) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/ruboto/util/xml_element.rb', line 189

def method_definition(class_name)
  entry_point_guard = (activity_super_guard(class_name, attribute('name')) ||
      service_super_guard(class_name, attribute('name')))
  method_call(
      (attribute('return') ? attribute('return') : 'void'),
      attribute('name'), parameters,
      get_elements('exception').map { |m| m.attribute('type') },
      ["if (ScriptLoader.isCalledFromJRuby()) #{super_return}",
          (entry_point_guard && (entry_point_guard + load_script)) ||
              if_else('!JRubyAdapter.isInitialized()',
                  [%Q{Log.i("Method called before JRuby runtime was initialized: #{class_name}##{attribute('name')}");},
                      super_return]),
          'String rubyClassName = scriptInfo.getRubyClassName();',
          "if (rubyClassName == null) #{super_return}",
          if_else(
              "(Boolean)JRubyAdapter.runScriptlet(rubyClassName + \".instance_methods(false).any?{|m| m.to_sym == :#{attribute('name')}}\")",
              ruby_call,
              if_else(
                  "(Boolean)JRubyAdapter.runScriptlet(rubyClassName + \".instance_methods(false).any?{|m| m.to_sym == :#{snake_case_attribute}}\")",
                  ruby_call(true),
                  if_else(
                      "(Boolean)JRubyAdapter.runScriptlet(rubyClassName + \".instance_methods(true).any?{|m| m.to_sym == :#{snake_case_attribute}}\")",
                      ruby_call(true),
                      # FIXME(uwe):  Can the method be unimplemented?  Is the Ruby instance always an instance of this class?
                      #if_else(
                      #    "(Boolean)JRubyAdapter.runScriptlet(rubyClassName + \".instance_methods(true).any?{|m| m.to_sym == :#{attribute('name')}}\")",
                      ruby_call,
                  #    [super_return]
                  #)
                  )
              )
          ),
          ('ScriptLoader.unloadScript(this);' if attribute('name') == 'onDestroy'),
      ]
  ).indent.join("\n")
end

#method_signatureObject



116
117
118
# File 'lib/ruboto/util/xml_element.rb', line 116

def method_signature
  "#{attribute('name')}(#{parameters.map { |i| i[1] }.join(',')})"
end

#nameObject



21
22
23
# File 'lib/ruboto/util/xml_element.rb', line 21

def name
  self['name']
end

#parametersObject



112
113
114
# File 'lib/ruboto/util/xml_element.rb', line 112

def parameters
  get_elements('parameter').map { |p| [p.attribute('name'), p.attribute('type').gsub('&lt;', '<').gsub('&gt;', '>')] }
end

#rootObject



17
18
19
# File 'lib/ruboto/util/xml_element.rb', line 17

def root
  Ruboto::API.api
end

#ruby_call(snake_case = false) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ruboto/util/xml_element.rb', line 158

def ruby_call(snake_case = false)
  params = parameters
  args = ''
  if params.size > 1
    args = ', new Object[]{' + params.map { |i| i[0] }.join(', ') + '}'
  elsif params.size > 0
    args = ', ' + params.map { |i| i[0] }.join(', ')
  end

  return_cast = ''
  convert_return = ''
  if attribute('return') && attribute('return') != 'void'
    if attribute('return').include?('.') || attribute('return') == 'int[]'
      return_class = attribute('return')
    elsif attribute('return') == 'int'
      return_class = 'Integer'
    else
      return_class = attribute('return').capitalize
    end
    return_cast = "return (#{return_class.gsub('&lt;', '<').gsub('&gt;', '>')}) " if return_class
    convert_return = "#{return_class.sub(/<.*>$/, '')}.class, "
  end

  method_name = snake_case ? snake_case_attribute : attribute('name')
  ["#{return_cast}JRubyAdapter.runRubyMethod(#{convert_return}scriptInfo.getRubyInstance(), \"#{method_name}\"#{args});"]
end

#service_super_guard(class_name, method_name) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/ruboto/util/xml_element.rb', line 232

def service_super_guard(class_name, method_name)
  if class_name == 'RubotoService'
    if method_name == 'onCreate'
      "preOnCreate();\n"
    elsif method_name == 'onStartCommand'
      "preOnStartCommand(intent);\n"
    elsif method_name == 'onBind'
      "preOnBind(intent);\n"
    end
  end
end

#snake_case_attributeObject



185
186
187
# File 'lib/ruboto/util/xml_element.rb', line 185

def snake_case_attribute
  attribute('name').gsub(/[A-Z]/) { |i| "_#{i}" }.downcase
end

#super_returnObject



152
153
154
155
156
# File 'lib/ruboto/util/xml_element.rb', line 152

def super_return
  rv = super_string
  return "{#{rv} return;}" unless attribute('return')
  rv ? "return #{rv}" : default_return
end

#super_stringObject



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ruboto/util/xml_element.rb', line 124

def super_string
  if attribute('api_added') and
      attribute('api_added').to_i > verify_min_sdk.to_i and
      attribute('api_added').to_i <= verify_target_sdk.to_i
    nil
  elsif attribute('abstract') == 'true'
    nil
  elsif name == 'method'
    "super.#{attribute('name')}(#{parameters.map { |i| i[0] }.join(', ')});"
  elsif name == 'constructor'
    "super(#{parameters.map { |i| i[0] }.join(', ')});"
  end
end