#!/usr/bin/env ruby
#
# utility to run the XSLT RDF Parser #5 from Ruby
# $Id: parse.rb,v 1.3 2002/09/26 10:32:34 danbri Exp $

#
# this script parses RDF from a file.
# it also currently has some RSS viewing code, but that's just for
# testing.
#

# Assumes use of xsltproc.
#
# Todo:
# - integrate this with the calls in basicrdf.rb
# - call xslt from API instead if possible
# - run without hitting the disk
# - can we store the .xsl content in memory (or generate .rb files?)
#   so we don't hit disk to load every time
# - rig to rdf test cases before worrying about optimisation 
# - clean up files afterwards

require 'basicrdf'
require 'squish' 


class XSLTParser
 
  attr_accessor :file, :nt_cache, :cache_dir, :base_uri
  
  def parse(file)
  id='001' #randomize

  cache_dir = './tmp'
  cfg='.'
  c14n_fn = "#{cache_dir}/rdf-#{id}.c14.rdf"
  nt_cache = "#{cache_dir}/rdf-#{id}.nt"   
  ctext= `xsltproc '#{cfg}/rdfc14n.xsl' '#{file}'` # run xslt 1st time

  begin
    c14n = File::new( c14n_fn, File::CREAT|File::RDWR, 0644 )
    c14n.puts ctext
    c14n.close
    puts "stored in c14n_nf: #{c14n_fn} file:#{c14n.inspect}"   # \n\nctext: #{ctext}"
  rescue
    puts "Problem storing canonicalised RDF data in cache. $!"
  end

  begin 
    nt_text = `xsltproc  --stringparam base '#{base_uri}' '#{cfg}/rdfc2nt.xsl' '#{c14n_fn}'`
    puts "N-Triples: #{nt}"
  rescue
    puts "Problem loading canonicalised RDF data from cache. $!"
  end

  begin
    nt = File::new( nt_cache, File::CREAT|File::RDWR, 0644 )
    nt.puts nt_text
    nt.close
  rescue
    puts "Problem storing N-Triples data. $!"
  end

  return Loader.ntfile2graph nt_cache
  end
end

def esc_utf8(text)
  return(text)
end


xp = XSLTParser.new
file = ARGV[0] 
raise "Usage: parse.rb <rdf_file>" if file==nil 


graph = xp.parse file



##############

# having got some rdf, let's do something with it.
# here we assume an rss channel...
   
query=<<EOQ;

	SELECT ?i, ?t, ?d,  
 	 WHERE 
	 (rss::title ?i ?t)
	 (rdf::type ?i rss::item)
	USING
	 rdf for http://www.w3.org/1999/02/22-rdf-syntax-ns#
	 rss for http://purl.org/rss/1.0/ 

EOQ

x=''
u='http://www.w3.org/2000/08/w3c-synd/home.rss'

q=SquishQuery.new.parseFromText(query)
resultset = SquishQuery.ask q, graph

chan_node = Node.getResource u, graph	
resultset.each do |chan|
  puts "#{chan.inspect} \n"
  title = esc_utf8 chan.values['t']
  item = esc_utf8 chan.values['i']
  item_node = Node.getResource item, graph	
  description =''# esc_utf8 item_node.rss_description if item_node
  x += "<li><a href=\"#{item}\">#{title}</a> <em>#{description}</em></li>\n"
end



