require "rexml/document.rb"; include REXML

def dump(node)
  [node.prefix ? node.name : node.prefix+":"+node.name] <<
  node.attributes.collect{|n,v| [n,v]} << 
  node.children.collect do |child|
    if child.instance_of? Text or child.instance_of? CData
      child.value
    elsif child.instance_of? Element
      dump child
    end
  end
end

def load(data)
  e=Element.new(data[0])
  data[1].each{|n,v| e.add_attribute(n,v)}
  data[2].each{|child|
    if child.instance_of? String
      e.add_text(child)
    elsif child
      e.add_element(load(child))
    end
  }
  e
end

case ARGV[0]

when 'dump'
  for file in Dir['/home/rubys/tmp/atom/*.atom']
    xdump=file.sub(/\.atom$/,'.xdump')
    data=dump(Document.new(File.new(file)).root)
    File.open(xdump,'w') {|file| file.write(Marshal::dump(data))}
  end

when 'marshal'
  for file in Dir['/home/rubys/tmp/atom/*.xdump']
    Marshal::load(File.new(file))
  end

when 'load'
  for file in Dir['/home/rubys/tmp/atom/*.xdump']
    load(Marshal::load(File.new(file)))
  end

end
