/*********************************************************

Command : SxPathTestOnePath


This command which accepts one parameter shows various mode
  of using Xpath

   -c  only compile
   -f  compile and interprets in first mode
   -s  compile and interprets in set mode
   -F  interprets from Xml representation of SxPath

 *********************************************************/

#include "SxPath.h"
#include "string.h"
#include "Buffer.h"
#include <stdlib.h>
#include "DilibKernel.h"

 extern char *optarg;
int getopt ();
void modeCompile()
{
  Buffer *bufPath;
  SxmlParser *xp1;
  SxmlNode *p1;

  bufPath=BufferCreate(50,50);
  xp1=SxmlParserCreate();
  BufferGets(bufPath);
  printf ("================= test compile ==\n%s\n-----------\n",
	  BufferString(bufPath));
  p1=SxPathExpressionParse(xp1,BufferString(bufPath));
  SxmlPrint (p1); 
  putchar ('\n');
  SxmlParserFree(xp1);
  SxmlFree(p1);
  BufferFree(bufPath);
 
}

void modeSet()
{
  Buffer *bufPath;

  bufPath=BufferCreate(100,100);

  if(BufferGets(bufPath))
    {  
      SxmlNode *doc;
      SxmlNode *p1;

      p1=SxPathSetCompile(BufferString(bufPath));
      printf ("================= test set ==\n%s\n-----------\n",
	  BufferString(bufPath));

      while((doc=SxmlInputNextDocumentElement()))
	{
	  SxmlNode *iterP;
	  SxmlNode *item;

	  iterP=SxPathSetResultListCreate(p1,doc);
	  SxmlReset(iterP);
	  printf("------  doc -----------\n");
	  SxmlPrint (doc);
	  putchar ('\n');
	  printf("------ selected elements  ----\n");
	  while((item=SxmlNodeListNextNode(iterP)))
	    {
	      SxmlPrint (item); 
	      putchar ('\n');
	    }
	  SxmlFree(iterP);
	}
      SxmlFree(p1);
    }
  BufferFree(bufPath);
}

void modeFirst()
{
  Buffer *bufPath;

  bufPath=BufferCreate(50,50);
  if(BufferGets(bufPath))
    {  
      SxmlNode *doc;
      SxmlNode *p1;
      printf ("================= test first ==\n%s\n-----------\n",
	  BufferString(bufPath));
      p1=SxPathFirstCompile(BufferString(bufPath));
      while((doc=SxmlInputNextDocumentElement()))
	{
	  SxmlNode *res;
	  printf("------ new doc -----------\n");
	  SxmlPrint (doc);
	  putchar('\n');
	  if((res=SxPathFirstResultNode(p1,doc)))
	    {
	      SxmlPrint(res);putchar('\n');
	    }
	  else printf("(null)\n");
	}
    }
}

void modeFirstOnSxml()
{
  SxmlNode *xp1;
  if((xp1=SxmlClone(SxmlInputNextDocumentElement())))
    {
      SxPathResult *r1;
      SxmlNode *doc;
      printf ("================= test first from Sxml ==\n%s\n-----------\n",
	  SxmlToString(xp1));
      r1=SxPathFirstCompileOnSxml(xp1);
      while((doc=SxmlInputNextDocumentElement()))
	{
	  SxmlNode *res;
	  printf("------ new doc -----------\n");
	  SxmlPrint (doc);
	  putchar('\n');
	  if((res=SxPathFirstResultNode(r1,doc)))
	    {
	      SxmlPrint(res);putchar('\n');
	    }
	  else printf("(null)\n");
	}
    }
}

void modeSetOnSxml()
{
  SxmlNode *xp1;
  if((xp1=SxmlClone(SxmlInputNextDocumentElement())))
    {
      SxPathResult *r1;
      SxmlNode *doc;
      printf ("================= test first from Sxml ==\n%s\n-----------\n",
	  SxmlToString(xp1));
      r1=SxPathSetCompileOnSxml(xp1);
      while((doc=SxmlInputNextDocumentElement()))
	{
	  SxmlNode *iterP;
	  SxmlNode *item;
	  
	  iterP=SxPathSetResultListCreate(r1,doc);
	  SxmlReset(iterP);
	  printf("------ doc -----------\n");
	  SxmlPrint (doc);
	  putchar ('\n');
	  printf("------ selected elements  ----\n");
	  while((item=SxmlNodeListNextNode(iterP)))
	    {
	      SxmlPrint (item); 
	      putchar ('\n');
	    }
	}while((doc=SxmlInputNextDocumentElement()));
    }
}

int main(argc,argv)
     int argc;
     char **argv;
{
  int cod_arg;
  
  while ((cod_arg = getopt(argc,argv,"cfsFST:"))!=EOF)
    {
      switch(cod_arg)
	{ 
	case 'c':
	  modeCompile();
	  break;
	case 'f':
	  modeFirst();
	  break;
	case 's':
	  modeSet();
	  break;
	case 'F':
	  modeFirstOnSxml();
	  break;
	case 'S':
	  modeSetOnSxml();
	  break;
	case 'T':
	  DilibSetTraceLevel(atoi(optarg));
	  break;
	}
    }
  exit(0);
}
