parser.rb revision 022df7f893582e7e8efffce816ae27f6d1015766
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallaghermodule Hets
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher module DG
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher class Parser
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher attr_accessor :resource, :callback
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher def initialize(resource)
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher self.resource = resource
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher end
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher def parse(callback: nil)
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher input = resource.respond_to?(:close) ? resource : File.open(resource)
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher parser(callback).parse(input)
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher input.close
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher end
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher private
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher def parser(callback)
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher listener = NokogiriListener.new(callback)
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher Nokogiri::XML::SAX::Parser.new(listener)
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher end
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher end
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher end
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagherend
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher