def location(path)
if path.empty? || path == '/'
return @location_table[-1]
end
if path.is_a?(Array)
items = path.collect { |item| to_scalar(item) }
elsif path.is_a?(String)
items = path.split('/').collect { |item| to_scalar(item) }
items.shift if path[0] == ?/
else
raise ArgumentError.new("path should be Array or String.")
end
last_item = items.pop()
c = @doc
items.each do |item|
if c.is_a?(Array)
c = c[item.to_i]
elsif c.is_a?(Hash)
c = c[item]
elsif (table = @location_table[c.__id__]) && table[:classobj]
if c.respond_to?(item)
c = c.__send__(item)
elsif c.respond_to?("[]=")
c = c[item]
else
assert false
end
else
raise ArgumentError.new("#{path.inspect}: invalid path.")
end
end
collection = @location_table[c.__id__]
return nil if collection.nil?
index = c.is_a?(Array) ? last_item.to_i : last_item
return collection[index]
end