#!/usr/bin/env ruby
#
#
# script and rough-cut of classes to support 
# the conversion of RDF queries (in Squish-ese) into XUL Templates to 
# use in Mozilla's RDF-based data binding system.
# 
# See http://www.mozilla.org/rdf/doc/ for Mozilla RDF docs.
# and http://www.w3.org/2001/12/rubyrdf/intro.html for RubyRDF Squish code
#
# Author: danbri@w3.org $Id: squish2xul.rb,v 1.3 2002/11/03 19:00:34 danbri Exp $
# 

require 'squish' 			# rubyrdf query api


def xultest

  query = SquishQuery.new().parseFromText <<EOQ
   SELECT ?uri, ?bugs, ?bug,
   WHERE	(b::bugs ?uri ?bugs)
  		(rdfs::member ?bugs ?bug)
		(b::id ?bug ?id)
		(b::component ?bug ?component)
		(rss::title ?bug ?rsst)
 USING 	 	b FOR http://www.bugzilla.org/rdf#
	rss FOR http://purl.org/rss/1.0/
 	rdfs FOR http://www.w3.org/2000/01/rdf-schema#

EOQ


  xul = XULData.new
  xul.src_data = 'http://bugzilla.mozilla.org/duplicates.cgi' #what is this?
 
  xul.columns = [
              {'id'=>'id', 'label'=>'ID'},
              {'id'=>'component', 'label'=>'Component Name'},
              {'id'=>'rsst', 'label'=>'Title'},
             ]
  xul.root='?uri' 
  xul.itemvar='?bug' 		# what do these do?

  top = xul.wrapper
  tree = xul.toXULTree query 
  window = top + tree + "\n</window>\n\n"
  return window

end





########################################################################
#
# We define one class which knows about XUL and (currently) Squish
# 

class XULData

  # columns is a list of hashes
  # root and itemvar: we don't quite know what these bits of xul do yet

  attr_accessor :src_data, :columns, :query_graph, :root, :itemvar

  def initialize
    # puts "Initializing XUL data binder"
  end

  def toXULTree(query)

    # XUL also needs a bunch of header info
 
    tree_element = "<tree id=\"results-tree\" flex=\"1\"
        flags=\"dont-build-content\"
        enableColumnDrag=\"true\"
        datasources=\"rdf:null\"
        ref=\"#{src_data}\"
        onselect=\"this.treeBoxObject.view.selectionChanged();\" > \n\n"

    treecols_element = "<treecols>\n"
    self.columns.each do |col|
      treecols_element += "<treecol id=\"#{col['id']}_column\" label=\"#{col['label']}\" flex=\"1\" sort=\"?#{col['id']}\" /> \n"
      treecols_element += "<splitter class=\"tree-splitter\" /> \n\n"
    end 
    treecols_element += "</treecols>\n\n" 


#   Binding conditions (taken from query)
    template_element='<template>
      <rule>
        <conditions>'

    template_element +="\n   <treeitem uri=\"#{root}\" />\n"


    query.full_clauses.each do |t|
      # puts t.inspect + "\n\n"
      if t[0] == 'http://www.w3.org/2000/01/rdf-schema#member'
       template_element += "   <member container=\"#{t[1]}\" child=\"#{t[2]}\" />\n"
      else
        template_element += "    <triple subject=\"#{t[1]}\" predicate=\"#{t[0]}\" object=\"#{t[2]}\" />\n"
      end
    end

    template_element += "    </conditions>\n\n"

    #omitting the <bindings> ... </bindings> may-bind triples 

    template_element += "     <action>
          <treechildren>
            <treeitem uri=\"#{itemvar}\">\n\n"
	
    template_element += "         <treerow>\n"
    self.columns.each do |col|
      template_element += "             <treecell  ref=\"#{col['id']}_column\" label=\"?#{col['id']}\" /> \n"
    end 
    template_element += "         </treerow>\n"
 
   template_element += '
            </treeitem>
          </treechildren>
        </action>
      </rule>
    </template>'
    return tree_element+treecols_element+template_element+"\n</tree>";
  end

  def wrapper



    xultop=<<EOX
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<?xml-stylesheet href="duplicates.css" type="text/css"?>
<window id="duplicates_report"
        xmlns:html="http://www.w3.org/1999/xhtml"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        title="XUL Report" onload="loadData();">

  <script type="application/x-javascript">
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    var gRDFService = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);

    function loadData()
    {
      var scriptURL = window.location.href;

	//file:///C:/cygwin/home/danbri/WWW/2001/sw/Europe/200209/xul/simple/simple.xul

      var dataURL="file:///C:/cygwin/home/danbri/WWW/2001/sw/Europe/200209/xul/simple/simple.xml";

      netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
      var dataSource = gRDFService.GetDataSource(dataURL);
 
     var resultsTree = document.getElementById('results-tree');
      resultsTree.database.AddDataSource(dataSource);
      resultsTree.builder.rebuild();
    }
  </script>

EOX

    return xultop

  end

end

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


# generate a window
puts xultest()





