require 'RDF4R/Driver/RDFParser'
require 'saxdriver'
require 'uri'

# notes by danbri: 
# this subclasses XML::Parser to hook up to Expat. 
# much of the original is held in common with other RDF parsers that
# don't require Expat/XML::Parser. I've broken this out into RDFParser
# hence the 'include' mix-in. Most of the original Liber code is now 
# in RDF4R::Driver::RDFParser
# $Id: XMLParser.rb,v 1.11 2003/04/20 23:14:55 danbri Exp $

# RubyRDF / RDF Parser. Based on code contributed from
# RDF4R RDF Parser, Copyright(c) 2002 Brandt Kurowski (brandt@kurowski.net)
# packaged as part of RubyRDF, see http://www.w3.org/2001/12/rubyrdf/intro.html
# All Rights Reserved. This work is distributed under the W3C (c) Software 
# License [1] in the hope that it will be useful, but WITHOUT ANY 
# WARRANTY; without even the implied warranty of MERCHANTABILITY or 
# FITNESS FOR A PARTICULAR PURPOSE. 
# [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231

# ruby expat docs:
# http://www.yoshidam.net/xmlparser_en.txt
# todo: find out how to run parser with text input

module RDF4R
  module Driver
    class XMLParser < XML::Parser
      include RDF4R::Driver::RDFParser 

        # This left here as names the class. 
        #
        def XMLParser.process(source, base, consumer)
          p = XMLParser.new(nil, ';')

#          STDERR.puts "XMLParser setup, #{source} base: #{base} "
#          begin 
            p.base_uri = base.kind_of?(URI::Generic) ? base : URI.parse(base)
#          STDERR.puts "XMLParser: set base URI to: #{p.base_uri}"
#          rescue Exception
#            STDERR.puts "Error setting base."
#          end

          p.consumer = consumer 
          begin

            #source='<foo/>'
            #idanbri todo: parse text? source=File.read source
            #source=File.read(source) if source.kind_of? File
		#if source.length<30 #danbri bug / hack
	    #puts source
            p.parse(source)
          rescue XMLParserError
            $stderr.puts %Q{in #{source.path}} if source.kind_of? File
            $stderr.puts %Q{at #{source.lineno}} if source.kind_of? File
            raise $!
          end 
        p.consumer.models
      end
    end
  end
end

