/**
 *
 * @author Nicola Onose
 *
 */


import galapi.*;

public class Example
{

/* 
   Example 1 

   Build a query context from a file.
   Load a document.
   Add a global variable to query context, bound to the loaded document.
   Evaluate a query contained in a string.
   Serialize result to standard output

*/
    
    static void scenario1 (ModuleContext mc) throws GalapiException {
    
	ModuleContext mc1 = Galax.evalGlobalVariables (mc);
	ItemList r = Galax.evalStatementFromString (mc1, "for $s in $report//section[section.title = \"Procedure\"] return ($s//incision)[2]/instrument");
	Galax.serializeToOutputChannel (r);


    }// scenario1()


/* 
   Example 2

   Build a query context from a file.
   Load a document.
   Evaluate a query contained in a file.
   Serialize result to standard output.

*/
    static void scenario2 (ModuleContext mc) throws GalapiException  {

	ModuleContext mc1 = Galax.evalGlobalVariables (mc);
	QueryContext qc = Galax.importMainModule (mc1, "seq.xq");
	ItemList r = Galax.evalStatements (qc);
	Galax.serializeToOutputChannel (r);

    }// scenario2()


/* 
   Example 3

   Build a query context from a file.
   Create some XML values. 
   Evaluate a query function contained in the query context by
   applying to XML values.
   Serialize result to a string & print. 

*/
    static void scenario3 (ModuleContext mc, ItemList xmldoc)
	throws GalapiException  {

	ModuleContext mc1 = Galax.evalGlobalVariables (mc);
	ModuleContext mc2 = Galax.importLibraryModule (mc1, "xq-example1.xq"); 
	xsString xmls = new xsString ("a string"); 
	xsInt xi = new xsInt (12);
	ItemList []args = new ItemList[2];	
	args[0] = ItemList.cons (xi, new ItemList()); 
	args[1] = ItemList.cons (xmls, new ItemList()); 

	ItemList r = Galax.evalQueryFunction (mc2, "test2", args); 
	Galax.serializeToOutputChannel (r);
	
    }// scenario3()


    public static void main (String args[]) 
    	throws GalapiException  {
	
	Galax.init ();

	ProcessingContext pc = Galax.defaultProcessingContext ();
	ModuleContext mc = Galax.loadStandardLibrary (pc);
	ModuleContext mc1 = Galax.importLibraryModule (mc, "seq_context.xq");
	ItemList xmldoc = Galax.loadDocument ("report.xml");
	
	scenario1 (mc1);
	scenario2 (mc1);
	scenario3 (mc1, xmldoc);

    }// main()

}// class Example
