def generate_XPath_for_example(next_page_example=false)
case @example_type
when EXAMPLE_TYPE_XPATH
@xpath = @example
when EXAMPLE_TYPE_STRING
@temp_sink = SimpleExampleLookup.find_node_from_text(@parent_pattern.extractor.get_hpricot_doc,
@example,
next_page_example)
return if @temp_sink == nil
if @temp_sink.is_a? String
@final_result = @temp_sink
return
end
mark_changing_ranges = lambda { |element, range|
element.instance_eval do
@changing_ranges ||= [] << range
def changing_ranges
@changing_ranges
end
end
}
mark_changing_ranges.call(@temp_sink, @temp_sink.match_data.begin(0)..@temp_sink.match_data.end(0))
write_indices = next_page_example ? true : !@parent_pattern.generalize
@xpath = XPathUtils.generate_XPath(@temp_sink, nil, write_indices)
when EXAMPLE_TYPE_CHILDREN
current_example_index = 0
loop do
all_child_temp_sinks = []
@parent_pattern.children.each do |child_pattern|
all_child_temp_sinks << child_pattern.filters[current_example_index].temp_sink if child_pattern.filters[current_example_index].temp_sink
end
result = all_child_temp_sinks.pop
if all_child_temp_sinks.empty?
result = result.parent
else
all_child_temp_sinks.each do |child_sink|
result = XPathUtils.lowest_common_ancestor(result, child_sink)
end
end
xpath = @parent_pattern.generalize ? XPathUtils.generate_XPath(result, nil, false) :
XPathUtils.generate_XPath(result, nil, true)
if @parent_pattern.filters.size < current_example_index + 1
@parent_pattern.filters << Scrubyt::BaseFilter.create(@parent_pattern)
end
@parent_pattern.filters[current_example_index].xpath = xpath
@parent_pattern.filters[current_example_index].temp_sink = result
@parent_pattern.children.each do |child_pattern|
next if child_pattern.type == :detail_page
child_pattern.filters[current_example_index].xpath =
child_pattern.generalize ? XPathUtils.generate_generalized_relative_XPath(child_pattern.filters[current_example_index].temp_sink, result) :
XPathUtils.generate_relative_XPath(child_pattern.filters[current_example_index].temp_sink, result)
end
break if @parent_pattern.children[0].filters.size == current_example_index + 1
current_example_index += 1
end
when EXAMPLE_TYPE_IMAGE
@temp_sink = XPathUtils.find_image(@parent_pattern.extractor.get_hpricot_doc, @example)
@xpath = XPathUtils.generate_XPath(@temp_sink, nil, true)
when EXAMPLE_TYPE_COMPOUND
@temp_sink = CompoundExampleLookup.find_node_from_compund_example(@parent_pattern.extractor.get_hpricot_doc,
@example,
next_page_example)
@xpath = @parent_pattern.generalize ? XPathUtils.generate_XPath(@temp_sink, nil, false) :
XPathUtils.generate_XPath(@temp_sink, nil, true)
end
end