Class: IEParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ieparser_extension.rb

Instance Method Summary collapse

Constructor Details

#initialize(win32ole_object, xpath_expression) ⇒ IEParser

Returns a new instance of IEParser.



140
141
142
143
# File 'lib/ieparser_extension.rb', line 140

def initialize(win32ole_object,xpath_expression)
  @win32ole_object = win32ole_object
  @finder_info = Tokenize.new(xpath_expression).populate
end

Instance Method Details

#check_xpathObject

def find_starting_id_node_with_frames(id)

  result = nil
  number_of_frames = @win32ole_object.document.frames.length.to_i - 1
  number_of_frames.times{|frame|
    lookup = @win32ole_object.document.frames[frame.to_s].document.getElementById(id)
    result = lookup unless lookup.nil?
  }
  result
end


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/ieparser_extension.rb', line 183

def check_xpath
  found_elements = []
  find_base_elements.each{|element|
    path = @finder_info.path.reverse
    
    if path_has_id?
      element_id = @finder_info.tag_id
      begin
        node = element
        eval("node = #{find_parent_node(path.size)}") 
          if node.getAttribute("id") == element_id
            found_elements << element
          end
      rescue
      end
    else
      begin
        node = element
        eval("node = #{find_parent_node(path.size - 1)}") if element_has_path?
        if node.nodename == path[-1].upcase
          found_elements << element
        end
      rescue
      end
    end
  }
  found_elements
end

#find_attribute_by_selector(selector, node, attribute, attribute_value) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/ieparser_extension.rb', line 261

def find_attribute_by_selector(selector, node, attribute, attribute_value)

  if attribute == 'text'
    node_attribute = node.innerHTML
  else
    node_attribute = node.getAttribute(attribute)
  end
  
  if selector == '=='
    result = node_attribute == attribute_value
  elsif selector == '=~'
    result = node_attribute.match(/#{attribute_value}/) != nil
  elsif selector == '=*'
    attribute_array = node_attribute.split
    result = attribute_array.include?(attribute_value)
  elsif selector == '=^'
    result = node_attribute.match(/^#{attribute_value}/) != nil
  elsif selector == '=$'
    result = node_attribute.match(/#{attribute_value}$/) != nil
  end
 result
end

#find_base_elementsObject



218
219
220
221
222
223
224
225
# File 'lib/ieparser_extension.rb', line 218

def find_base_elements
#    if has_frames?
#      #TODO
#    else
    element_array = find_element_with_no_frames
#    end
#    element_array
end

#find_by_name_or_id_when_no_pathObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ieparser_extension.rb', line 153

def find_by_name_or_id_when_no_path
  finder_detail = @finder_info.path.first
  finder_detail.match(/^\[(.+)\]$/)
  info = $1.to_s
  info.match(/^@(\w+)=."(.+)"$/)
  attribute = $1.to_s
  attribute_value = $2.to_s  

  if attribute == 'name'
    element_list = @win32ole_object.find_by_name(attribute_value)
  elsif attribute == 'id'
    element_list = @win32ole_object.find_by_id(attribute_value)
  else
    element_list = nil
  end
  [element_list]
end

#find_element_with_no_framesObject



227
228
229
230
231
232
233
234
235
236
# File 'lib/ieparser_extension.rb', line 227

def find_element_with_no_frames
  if has_finder_info?
    element = @finder_info.finder[:element].upcase
    element_list = @win32ole_object.document.getElementsByTagName(element)
    element_array = find_elements_by_finder_info(element_list)
  else
    element_array = find_plain_elements
  end
  element_array
end

#find_elements_by_finder_info(element_list) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/ieparser_extension.rb', line 247

def find_elements_by_finder_info(element_list)
  attribute = @finder_info.finder[:criteria]
  attribute_value = @finder_info.finder[:value]
  selector = @finder_info.finder[:selector]
 
  potential_elements = []
  element_list.each{|node|
     if find_attribute_by_selector(selector, node, attribute, attribute_value)
      potential_elements << node
    end
  }
  potential_elements
end

#find_parent_node(path_size) ⇒ Object



212
213
214
215
216
# File 'lib/ieparser_extension.rb', line 212

def find_parent_node(path_size)
  eval_string = []
  path_size.times{|node| eval_string << 'parentNode'}
  eval_string.unshift('element').join('.')
end

#find_plain_elementsObject



238
239
240
241
242
243
244
245
# File 'lib/ieparser_extension.rb', line 238

def find_plain_elements
  potential_elements = []
  element = @finder_info.path[-1].upcase
  @win32ole_object.document.getElementsByTagName(element).each{|node|
    potential_elements << node
  }
  potential_elements
end

#select_nodesObject



145
146
147
148
149
150
151
# File 'lib/ieparser_extension.rb', line 145

def select_nodes
  if element_has_path? == false
    find_by_name_or_id_when_no_path    
  else
    check_xpath
  end
end