/*   -*- coding: utf-8 -*-  */

/***********************************************************************
*
*      Module  : Sxml
*      Fichier : SxmlAtt.c
*      Auteur  : J. Ducloy
*
*      Derniere mise a jour Decembre 2000
*      $Id: SxmlAtt.c,v 1.2 2003/06/02 09:57:29 parmentf Exp $
*
************************************************************************
* 
*     Copyright (C) 2001 CNRS INIST
*
************************************************************************/
#include "SxmlNode.h"
#include "Except.h"
/*

*/

SxmlNode *SxmlAttributeCreate(char *n1, char *v1)
{
  SxmlNode *a1;
  char *dv1;
  char *dn1;
  dn1=strdup(n1);
  if(v1)dv1=strdup(v1);
  else dv1=strdup("");
  if((!dv1)||(!dn1))
    {
      	ExceptSetError("SxmlNode","MA", "memory allocation failed","","",2);
    }
  a1=SxmlNodeCreate(XML_NODE_ATTRIBUTE);
  SxmlSetNodeName(a1,dn1);
  SxmlSetNodeNameToFree(a1);
  SxmlSetNodeValue(a1,dv1);
  SxmlSetNodeValueToFree(a1);
  return a1;
}

SxmlListAttributes *SxmlListAttributesCreate()
{
  SxmlNode *l1;
  l1=SxmlNodeCreate(XML_NODE_LIST_ATTRIBUTES);
  SxmlSetNodeName(l1,"#listAttributes");
  return l1;
}

void SxmlAttributeFilePrint( SxmlNode *a1, FILE *f1)
{
  fprintf(f1, " %s=\"%s\"", SxmlNodeName(a1), SxmlNodeValue(a1));
}

void SxmlListAttributesFilePrint(SxmlNode *l1, FILE *f1)
{
  SxmlNode *a1;
  SxmlReset(l1);
  while((a1=SxmlNextNode(l1)))SxmlFilePrint(a1,f1);
}

char *SxmlAttributeToString(Buffer  *b1, SxmlNode *a1)
{
  BufferStrcat(b1," ");
  BufferStrcat(b1,SxmlNodeName(a1));
  BufferStrcat(b1,"=\"");
  BufferStrcat(b1,SxmlNodeValue(a1));
  BufferStrcat(b1,"\"");
  return BufferString(b1);
}

char *SxmlListAttributesToString(Buffer  *b1, SxmlNode *l1)
{
  SxmlNode *a1;
  SxmlReset(l1);
  while((a1=SxmlNextNode(l1)))SxmlAttributeToString(b1,a1);
  return BufferString(b1);
}

SxmlNode *SxmlGetAttributeNode(SxmlNode *e1, char *n1)
{
  SxmlNode *l1;
  if ((l1=SxmlGetListAttributes(e1)))
    {
      return SxmlGetFirstChildByTagName(l1,n1);
    }
  else return NULL;
}

SxmlNode *SxmlSetListAttributes(e1,l1)
     SxmlNode *l1;
     SxmlNode *e1;
{
  e1->attributes=l1;
  SxmlSetParent(l1,e1);
  return e1;
}

SxmlNode *SxmlSetAttribute( SxmlNode *e1, char *n1, char *v1)
{
  SxmlNode *a1;
  SxmlNode *l1;
  if ((l1=SxmlGetListAttributes(e1)))
    {
      if((a1=SxmlGetFirstChildByTagName(l1,n1)))
	{ 
	  char *dv1;
	  if(!(dv1=strdup(v1)))
	    {
	      ExceptSetError("SxmlNode","MA", "memory allocation failed","","",2);
	    }
	  if (SxmlNodeValueToFree(a1))free(SxmlNodeValue(a1));
	  SxmlSetNodeValue(a1,dv1);
	  SxmlSetNodeValueToFree(a1);
	  return a1;
	}
      else
	{
	  a1=SxmlAttributeCreate(n1,v1);
	  SxmlAppendChild(l1,a1);
	  return a1;
	}
    }
  else
    {
      l1=SxmlListAttributesCreate();
      a1=SxmlAttributeCreate(n1,v1);
      SxmlAppendChild(l1,a1);
      SxmlSetListAttributes(e1,l1);
      return (a1);
    }
}

SxmlNode *SxmlSetIntAttribute( SxmlNode *e1, char *n1, int v1)
{
  static char  strInt[10];
  sprintf(strInt, "%d", v1);
  return SxmlSetAttribute( e1, n1, strInt);
}

char *SxmlGetAttribute(SxmlNode *x1, char *a1)
{
  char *v1;
  SxmlNode *av1;
  if((av1=SxmlGetAttributeNode(x1,a1)))v1=SxmlNodeValue(av1);
  else v1=NULL;
  return v1;
}

char *SxmlHasAttribute(SxmlNode *x1, char *a1,  char *v1)
{
  char *v2;
  if (!x1)return NULL;
  if((v2=SxmlGetAttribute(x1,a1)))
    {
      if (strcmp(v1,v2)==0)return v1;
      else return NULL;
    }
  else return NULL;
}
