Skip to content

Commit

Permalink
parse compare ready for a blog entry, results as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed Sep 25, 2011
1 parent 2a81f2d commit 22484f4
Showing 1 changed file with 68 additions and 26 deletions.
94 changes: 68 additions & 26 deletions test/parse_cmp.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
#!/usr/bin/env ruby -wW1

$: << '.'
$: << '..'
$: << '../lib'
$: << '../ext'

if __FILE__ == $0
while (i = ARGV.index('-I'))
x,path = ARGV.slice!(i, 2)
$: << path
end
end

require 'optparse'
require 'pp'
require 'stringio'
require 'ox'

Expand All @@ -28,19 +18,60 @@

### XML conversion to Hash using in memory Ox parsing ###

def node_to_dict(node)
def node_to_dict(element)
dict = Hash.new
key = nil
node.each do |n|
if n.is_a?(::Ox::Element)
# if name is key...
# if dict
# TBD
elsif n.is_a?(String)
# TBD
element.nodes.each do |n|
raise "A dict can only contain elements." unless n.is_a?(::Ox::Element)
if key.nil?
raise "Expected a key, not a #{n.name}." unless 'key' == n.name
key = first_text(n)
else
dict[key] = node_to_value(n)
key = nil
end
end
return dict
dict
end

def node_to_array(element)
a = Array.new
element.nodes.each do |n|
a.push(node_to_value(n))
end
a
end

def node_to_value(node)
raise "A dict can only contain elements." unless node.is_a?(::Ox::Element)
case node.name
when 'key'
raise "Expected a value, not a key."
when 'string'
value = first_text(node)
when 'dict'
value = node_to_dict(node)
when 'array'
value = node_to_array(node)
when 'integer'
value = first_text(node).to_i
when 'real'
value = first_text(node).to_f
when 'true'
value = true
when 'false'
value = false
else
raise "#{node.name} is not a know element type."
end
value
end

def first_text(node)
node.nodes.each do |n|
return n if n.is_a?(String)
end
nil
end

def parse_gen(xml)
Expand All @@ -53,7 +84,7 @@ def parse_gen(xml)
break
end
end
pp dict
dict
end

### XML conversion to Hash using Ox SAX parser ###
Expand Down Expand Up @@ -133,12 +164,8 @@ def parse_sax(xml)
io = StringIO.new(xml)
start = Time.now
handler = Handler.new()
begin
Ox.sax_parse(handler, io)
rescue Exception => e
puts "#{e.class}: #{e.message}"
e.backtrace.each { |line| puts line }
end
Ox.sax_parse(handler, io)
handler.plist
end

### XML conversion to Hash using Ox Object parsing with gsub! replacements ###
Expand Down Expand Up @@ -187,6 +214,13 @@ def plist_to_obj_xml(xml)
files.each do |filename|
xml = File.read(filename)

if 0 < $verbose
d1 = parse_gen(xml)
d2 = parse_sax(xml)
d3 = convert_parse_obj(xml)
puts "--- It is #{d1 == d2 and d2 == d3} that all parsers yield the same Hash. ---"
end

start = Time.now
$iter.times do
parse_gen(xml)
Expand Down Expand Up @@ -217,3 +251,11 @@ def plist_to_obj_xml(xml)
puts "XML gsub Object parsing and conversion took #{conv_obj_time} for #{$iter} iterations."
puts "Object parsing and conversion took #{obj_time} for #{$iter} iterations."
end

# Results for a run:
#
# > parse_cmp.rb Sample.graffle -i 1000
# In memory parsing and conversion took 4.135701 for 1000 iterations.
# SAX parsing and conversion took 3.731695 for 1000 iterations.
# XML gsub Object parsing and conversion took 3.292397 for 1000 iterations.
# Object parsing and conversion took 0.808877 for 1000 iterations.

0 comments on commit 22484f4

Please sign in to comment.