#!/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 " if file==nil graph = xp.parse file ############## # having got some rdf, let's do something with it. # here we assume an rss channel... query=<#{title} #{description}\n" end