/**
 *
 * @author Nicola Onose
 *
    IF YOU ADD TESTS TO THIS FILE, YOU MUST ALSO ADD THEM TO: 
      examples/c_api/c-test.c
      examples/caml_api/ml-test.ml
 */

import galapi.*;

public class Test
{
    public static void main (String sysargs[]) 
	throws Exception {

	Galax.init ();      

	/* xs:int */	
	xsInt i = new xsInt (20);
	if (20 != i.toInt()) 
	    System.out.println ("Error! roundtripping int");
	ItemList il = ItemList.cons (i, new ItemList());
	String str = il.toString();
	System.out.println (str);

	/* xs:integer */	
	xsInteger i1 = new xsInteger (20);
	if (20 != i1.toInt()) 
	    System.out.println ("Error! roundtripping integer");
	System.out.println (i1);

	/* xs:decimal */	
	xsDecimal i2 = new xsDecimal (20);
	if (20 != i2.toInt()) 
	    System.out.println ("Error! roundtripping decimal");
	System.out.println (i2);

	/* xs:string */
	xsString s = new xsString ("user test");
	str = s.toString();
	if (!s.toString().equals("user test")) 
	    System.out.println ("Error! roundtripping string");	
	System.out.println (str);

	/* xs:boolean */
	xsBoolean b = new xsBoolean (false);
	if (b.toBoolean()) 
	    System.out.println ("Error! roundtripping boolean");	
	System.out.println (b);
	
	/* xs:float */
	xsFloat d = new xsFloat (98.549);
	if (98.549 != d.toDouble()) 
	    System.out.println ("Error! roundtripping float "+d.toDouble());	
	System.out.println (d);

	/* xs:double */
	xsDouble d2 = new xsDouble (98.549);
	if (98.549 != d2.toDouble()) 
	    System.out.println ("Error! roundtripping double "+d2.toDouble());	
	System.out.println (d2);	

	/* load standard library */
	ProcessingContext pc = Galax.defaultProcessingContext ();
	ModuleContext sc = Galax.loadStandardLibrary (pc);

	/* Intentionally raise an exception */
	try { 
	    ModuleContext cn = Galax.importLibraryModule (sc,"xq-example.xq");
	} catch (GalapiException ex) {
	    ModuleContext cn = Galax.importLibraryModule (sc,"xq-example1.xq");

	    /* xs:QName */
	    xsQName q = new xsQName (cn, "foo:bar");
	    System.out.println (q);

	    /* comment */
	    Comment c = new Comment (new xsString ("This is a comment"));
	    System.out.println (c.nodeKind());
	    System.out.println (Galax.serializeToString(ItemList.cons(c, new ItemList())));	

	    /* PI */
	    ProcessingInstruction p = 
		new ProcessingInstruction (new xsString ("target"),
					   new xsString ("This is a processing instruction"));	
	    System.out.println (Galax.serializeToString(ItemList.cons(p, new ItemList())));	
	
	    /* element */
	    Element e = new Element (q, new AttributeList(), new ItemList(), q);
	    System.out.println (Galax.serializeToString(ItemList.cons(e, new ItemList())));
	    
	    /* xs:QName */
	    xsQName q1 = new xsQName (cn, "baz");
	    System.out.println (q1);		
	    
            /* document */ 
	    ItemList el = 
		ItemList.cons (p,
  		  ItemList.cons (c,
  		    ItemList.cons (e,
			       ItemList.cons (e,new ItemList()))));
	    
	    Document dn = new Document ("http://db.bell-labs.com/galax",  el);
	    System.out.println (Galax.serializeToString(ItemList.cons(dn, new ItemList())));
	    /* attribute */
	    Attribute a = new Attribute (q1, new xsString ("attribute value"), q1);
	    System.out.println (Galax.serializeToString(ItemList.cons(a, new ItemList())));	
	    
	    /* element */
	    el = 
		ItemList.cons (e,
			       ItemList.cons (e,new ItemList()));
	    
	    e = new Element (q,  AttributeList.cons (a, new AttributeList()), el, q);
	    System.out.println (Galax.serializeToString(ItemList.cons(e, new ItemList())));

    
	    /* node name */
            AtomicList q1opt = e.nodeName(); 
	    if (q1opt.isEmpty()) 
		System.out.println ("Error! node name is empty\n");
	    else	
		System.out.println (Galax.serializeToString(q1opt));

	    /* children */
	    NodeList k = e.children ();
	    System.out.println (Galax.serializeToString(k));
	    
	    /* parent */
	    NodeList pr = (k.ItemsFirst().getNode()).parent(); 
	    System.out.println (Galax.serializeToString(pr));
	    
	    /* attributes */
	    AttributeList ats = e.attributes ();
	    System.out.println (Galax.serializeToString(ats));	
	    
	    /* text node */
	    Text t = new Text (new xsString("this is a text node"));
	    System.out.println (Galax.serializeToString(ItemList.cons(t, new ItemList())));
	    
	    /* serialization */
	    NodeList docitems = Galax.loadDocument ("xml-example.xml");
	    Galax.serializeToOutputChannel (docitems);
	    str = Galax.serializeToString (docitems);
	    System.out.println (str);		
	    
            /* Base uri of a document node */
	    Node doc = docitems.ItemsFirst().getNode();
            AtomicList bs = doc.baseURI();
	    if (bs.isEmpty()) {
		System.out.println ("Error! base uri is empty\n"); 
	    } else {
		System.out.println (Galax.serializeToString(bs));
	    }

            /* Base uri of an element node */
            bs = doc.children().ItemsFirst().getNode().baseURI();
	    if (bs.isEmpty()) {
		System.out.println ("Error! base uri is empty\n"); 
	    } else {
		System.out.println (Galax.serializeToString(bs));
	    }

            /* Quick Start Functions */

	    ItemList items = Galax.evalStatementWithContextItemFromDocument("xml-example.xml", "./html/ul/li"); 
	    System.out.println (Galax.serializeToString(items));

	    items = Galax.evalStatementWithContextItemFromXML((Item)doc, "./html/ul/li"); 
	    System.out.println (Galax.serializeToString(items));

	    items = Galax.evalStatementFromFileWithContextItemFromXML(doc, "htmlpath.xq"); 
	    System.out.println (Galax.serializeToString(items));

	    NameValuePair vars[] = new NameValuePair[1];
            vars[0] = new NameValuePair("v", docitems); 
	    items = Galax.evalStatementWithVariablesFromXML(vars, "$v/html/ul/li"); 
	    System.out.println (Galax.serializeToString(items));

	    ItemList args1[] = new ItemList[2];
	    args1[0] = ItemList.cons (i, new ItemList());
	    args1[1] = ItemList.cons (s, new ItemList());	
	    
	    /* Eval query function  */
	    items = Galax.evalQueryFunction (cn, "test2", args1);
	    System.out.println (Galax.serializeToString(items));
	    
	    
	    ItemList args2[] = new ItemList[4];
	    args2[0] = docitems;
	    args2[1] = ItemList.cons (i, new ItemList());
	    args2[2] = ItemList.cons (s, new ItemList());
	    args2[3] = ItemList.cons (d, new ItemList());	
	    
	    items = Galax.evalQueryFunction (cn, "to_html", args2);
	    System.out.println (Galax.serializeToString(items));			
	    
	    Node nv = items.ItemsFirst().getNode();
	    Galax.serializeToFile ("to_html.xml", items);

            /* Import main module */
	    QueryContext qc = Galax.importMainModule (cn, "xq-example2.xq");
	    
	    items = Galax.evalStatements (qc);	
	    System.out.println (Galax.serializeToString(items));

            /* Eval statement from string */	    
	    items = Galax.evalStatementFromString (cn, "test0()");	
	    System.out.println (Galax.serializeToString(items));
	    
	    Atomic av = items.ItemsFirst().getAtomic();
	    
	    items = Galax.evalStatementFromString (cn, "20.0 * 3.3");
	    System.out.println (Galax.serializeToString(items));
	    
            /* Import library module */
	    ModuleContext cn2 = Galax.importLibraryModule (sc, "xmlschema_context.xq");
	    ModuleContext cn3 = Galax.evalGlobalVariables (cn2);	
	    items = Galax.evalStatementFromString (cn3,"$po/HisPo:purchaseOrder/HisPo:items/HisPo:item/HisPo:quantity");
	    System.out.println (Galax.serializeToString(items));
	    
	    /* Typed value */
	    AtomicList al = items.ItemsFirst().getNode().typedValue();
	    System.out.println (Galax.serializeToString(al));
	    System.out.println (":" + al.ItemsFirst().itemKind());

            /* External context */	    
	    NameValuePair vars1[] = new NameValuePair[2];
	    vars1[0] = new NameValuePair ("x", args2[0]);
	    vars1[1] = new NameValuePair ("y", args2[1]);
	    ExternalContext ec = new ExternalContext (il, vars1);
	    ModuleContext cn4 = Galax.addExternalContext (cn3, ec);

	    items = Galax.evalStatementFromString (cn4,"$x");
	    System.out.println (items);
	    items = Galax.evalStatementFromString (cn4,"$y");
	    System.out.println (items);

	    try {
		NodeList invalid_items = Galax.loadDocument ("no-such-file.xml");
	    } catch (Exception ex2) {
		System.out.println ("Raise an error intentionally");
		System.out.println(ex2);
		System.out.flush ();
		throw ex2;
	    }
	}
	
    }// main()

}// class Test
