Index: trunk/zoo-project/zoo-kernel/service_conf.y
===================================================================
--- trunk/zoo-project/zoo-kernel/service_conf.y	(revision 788)
+++ trunk/zoo-project/zoo-kernel/service_conf.y	(revision 790)
@@ -34,4 +34,5 @@
 static bool wait_mainmetadata=false;
 static bool wait_metadata=false;
+static bool wait_nested=false;
 static bool wait_inputs=false;
 static bool wait_defaults=false;
@@ -47,4 +48,5 @@
 static int previous_data=0;
 static int current_data=0;
+static int nested_level=0;
 // namespace
 using namespace std;
@@ -145,5 +147,5 @@
   dumpMap(current_content);
 #endif
-  if(my_service->content==NULL){
+  if(my_service->content==NULL && current_content!=NULL){
 #ifdef DEBUG_SERVICE_CONF
     fprintf(stderr,"NO CONTENT\n");
@@ -156,4 +158,15 @@
     wait_maincontent=false;
   }
+  if(strncasecmp($2,"EndNested",9)==0){
+#ifdef DEBUG_SERVICE_CONF
+      fprintf(stderr,"(ENDNESTED - %d) \n",__LINE__);
+      fflush(stderr);
+#endif
+      nested_level-=1;
+      if(nested_level==0){
+	wait_nested=false;
+      }
+  }
+
   if(strncasecmp($2,"DataInputs",10)==0){
     if(current_element==NULL){
@@ -167,13 +180,5 @@
 #endif
       current_element=NULL;
-      current_element=(elements*)malloc(ELEMENTS_SIZE);
-      current_element->name=NULL;
-      current_element->content=NULL;
-      current_element->metadata=NULL;
-      current_element->format=NULL;
-      current_element->defaults=NULL;
-      current_element->supported=NULL;
-      current_element->child=NULL;
-      current_element->next=NULL;
+      current_element=createEmptyElements();
     }
     wait_inputs=true;
@@ -217,13 +222,5 @@
 	fflush(stderr);
 #endif
-	current_element=(elements*)malloc(ELEMENTS_SIZE);
-	current_element->name=NULL;
-	current_element->content=NULL;
-	current_element->metadata=NULL;
-	current_element->format=NULL;
-	current_element->defaults=NULL;
-	current_element->supported=NULL;
-	current_element->child=NULL;
-	current_element->next=NULL;
+	current_element=createEmptyElements();
       }
       wait_outputs=1;
@@ -269,4 +266,5 @@
 	  current_element->defaults=NULL;
 	  current_element->supported=NULL;
+	  current_element->child=NULL;
 	  current_content=NULL;
 	}
@@ -285,6 +283,6 @@
 	    }
 #ifdef DEBUG_SERVICE_CONF
-  printf("* Identifiant : %s\n",$2);
-  fflush(stdout);
+  fprintf(stderr,"* Identifiant : %s\n",$2);
+  fflush(stderr);
 #endif
 }
@@ -340,5 +338,50 @@
      current_content=NULL;
      current_element->supported=NULL;
+     current_element->child=NULL;
      current_element->next=NULL;
+   }
+   if(strncasecmp($2,"EndNested",9)==0){
+     if(current_data==1){
+       elements* cursor=my_service->inputs;
+       while(cursor->next!=NULL)
+	 cursor=cursor->next;
+       if(nested_level>1){
+	 for(int j=0;j<nested_level-1;j++){
+	   cursor=cursor->child;
+	   while(cursor->next!=NULL)
+	     cursor=cursor->next;
+	 }
+       }
+       if(cursor->child==NULL){
+	 cursor->child=dupElements(current_element);
+       }else{
+	 addToElements(&cursor->child,current_element);
+       }
+     }else{
+       if(current_element->name!=NULL){
+	 elements* cursor=my_service->outputs;
+	 while(cursor->next!=NULL)
+	   cursor=cursor->next;
+	 if(nested_level>1){
+	   for(int j=0;j<nested_level-1;j++){
+	     cursor=cursor->child;
+	     while(cursor->next!=NULL)
+	       cursor=cursor->next;
+	   }
+	 }
+	 if(cursor->child==NULL){
+	   cursor->child=dupElements(current_element);
+	 }else
+	   addToElements(&cursor->child,current_element);
+       }
+     }
+     freeElements(&current_element);
+     free(current_element);
+     current_element=NULL;
+     current_element=createEmptyElements();
+     nested_level-=1;
+     if(nested_level==0){
+       wait_nested=false;       
+     }
    }
  }
@@ -395,4 +438,5 @@
 	current_element->defaults=NULL;
 	current_element->supported=NULL;
+	current_element->child=NULL;
       }else{
 	if(wait_mainmetadata){
@@ -428,4 +472,5 @@
     current_content=NULL;
     current_element->supported=NULL;
+    current_element->child=NULL;
     current_element->next=NULL;
   }
@@ -443,4 +488,5 @@
       }else{
 	current_element->supported=NULL;
+	current_element->child=NULL;
 	current_element->next=NULL;
       }
@@ -553,20 +599,13 @@
   fprintf(stderr,"processid (%s %d) %s\n",__FILE__,__LINE__,$1);
 #endif
-//  if(data==-1){
-//    data=1;
   if(::data==-1){ // knut: add namespace to avoid ambiguous symbol
     ::data=1;	
     if($1!=NULL){
       char *cen=zStrdup($1);
-      my_service->name=(char*)malloc((strlen(cen)-1)*sizeof(char*));
       cen[strlen(cen)-1]=0;
       cen+=1;
-      sprintf(my_service->name,"%s",cen);
+      setServiceName(&my_service,cen);
       cen-=1;
       free(cen);
-      my_service->content=NULL;
-      my_service->metadata=NULL;
-      my_service->inputs=NULL;
-      my_service->outputs=NULL;
     }
   } else {
@@ -579,5 +618,42 @@
 	}
 	else{
-	  addToElements(&my_service->inputs,current_element);
+	  if(wait_nested){
+	    elements* cursor=my_service->inputs;
+	    while(cursor->next!=NULL)
+	      cursor=cursor->next;
+	    if(nested_level>1){
+	      for(int j=0;j<nested_level-1;j++){
+		cursor=cursor->child;
+		while(cursor->next!=NULL)
+		  cursor=cursor->next;
+	      }
+	    }
+	    if(cursor->child==NULL){
+	      cursor->child=dupElements(current_element);
+	    }else{
+	      addToElements(&cursor->child,current_element);
+	    }
+	  }else
+	    addToElements(&my_service->inputs,current_element);
+	}
+	if(current_element->format==NULL){
+	  wait_nested=true;
+	  nested_level+=1;
+	  if(current_content!=NULL){
+	    elements* cursor=my_service->inputs;
+	    while(cursor->next!=NULL)
+	      cursor=cursor->next;
+	    if(nested_level>1){
+	      for(int j=0;j<nested_level-1;j++){
+		cursor=cursor->child;
+		while(cursor->next!=NULL)
+		  cursor=cursor->next;
+	      }
+	    }
+	    addMapToMap(&cursor->content,current_content);
+	    freeMap(&current_content);
+	    free(current_content);
+	    current_content=NULL;
+	  }
 	}
 #ifdef DEBUG_SERVICE_CONF
@@ -593,12 +669,5 @@
 	fprintf(stderr,"(DATAINPUTS - 489) ALLOCATE current_element\n");
 #endif
-	current_element=(elements*)malloc(ELEMENTS_SIZE);
-	current_element->name=NULL;
-	current_element->content=NULL;
-	current_element->metadata=NULL;
-	current_element->format=NULL;
-	current_element->defaults=NULL;
-	current_element->supported=NULL;
-	current_element->next=NULL;
+	current_element=createEmptyElements();
       }
       if(current_element->name==NULL){
@@ -613,8 +682,7 @@
 	if($1!=NULL){ 
 	  char *cen=zStrdup($1);
-	  current_element->name=(char*)malloc((strlen(cen)-1)*sizeof(char*));
 	  cen[strlen(cen)-1]=0;
 	  cen+=1;
-	  sprintf(current_element->name,"%s",cen);
+	  setElementsName(&current_element,cen);
 	  cen-=1;
 	  free(cen);
@@ -622,10 +690,4 @@
 	  fprintf(stderr,"NAME IN %s (current - %s)\n",$1,current_element->name);
 #endif
-	  current_element->content=NULL;
-	  current_element->metadata=NULL;
-	  current_element->format=NULL;
-	  current_element->defaults=NULL;
-	  current_element->supported=NULL;
-	  current_element->next=NULL;
 #ifdef DEBUG_SERVICE_CONF
 	  fprintf(stderr,"NAME IN %s (current - %s)\n",$1,current_element->name);
@@ -659,12 +721,5 @@
 	    fprintf(stderr,"(DATAOUTPUTS -%d) ALLOCATE current_element %s \n",__LINE__,__FILE__);
 #endif
-	    current_element=(elements*)malloc(ELEMENTS_SIZE);
-	    current_element->name=NULL;
-	    current_element->content=NULL;
-	    current_element->metadata=NULL;
-	    current_element->format=NULL;
-	    current_element->defaults=NULL;
-	    current_element->supported=NULL;
-	    current_element->next=NULL;
+	    current_element=createEmptyElements();
 	  }
 	  if(current_element->name==NULL){
@@ -675,16 +730,9 @@
 	    if($1!=NULL){ 
 	      char *cen=zStrdup($1);
-	      current_element->name=(char*)malloc((strlen(cen)-1)*sizeof(char));
 	      cen[strlen(cen)-1]=0;
 	      cen+=1;
-	      sprintf(current_element->name,"%s",cen);
+	      setElementsName(&current_element,cen);
 	      cen-=1;
 	      free(cen);
-	      current_element->content=NULL;
-	      current_element->metadata=NULL;
-	      current_element->format=NULL;
-	      current_element->defaults=NULL;
-	      current_element->supported=NULL;
-	      current_element->next=NULL;
 	    }
 	  }
@@ -696,6 +744,44 @@
 	    if(my_service->outputs==NULL)
 	      my_service->outputs=dupElements(current_element);
-	    else
-	      addToElements(&my_service->outputs,current_element);
+	    else{
+	      if(wait_nested){
+		elements* cursor=my_service->outputs;
+		while(cursor->next!=NULL)
+		  cursor=cursor->next;
+		if(nested_level>1){
+		  for(int j=0;j<nested_level-1;j++){
+		    cursor=cursor->child;
+		    while(cursor->next!=NULL)
+		      cursor=cursor->next;
+		  }
+		}
+		if(cursor->child==NULL){
+		  cursor->child=dupElements(current_element);
+		}else
+		  addToElements(&cursor->child,current_element);
+	      }else
+		addToElements(&my_service->outputs,current_element);
+	    }
+	    if(current_element->format==NULL){
+	      wait_nested=true;
+	      nested_level+=1;
+	      if(current_content!=NULL){
+		elements* cursor=my_service->outputs;
+		while(cursor->next!=NULL)
+		  cursor=cursor->next;
+		if(nested_level>1){
+		  for(int j=0;j<nested_level-1;j++){
+		    cursor=cursor->child;
+		    while(cursor->next!=NULL)
+		      cursor=cursor->next;
+		  }
+		}
+		addMapToMap(&cursor->content,current_content);
+		freeMap(&current_content);
+		free(current_content);
+		current_content=NULL;
+	      }
+	    }
+
 #ifdef DEBUG_SERVICE_CONF
 	    fprintf(stderr,"ADD TO OUTPUTS Elements\n");
@@ -705,18 +791,11 @@
 	    free(current_element);
 	    current_element=NULL;
-	    current_element=(elements*)malloc(ELEMENTS_SIZE);
+	    
 	    char *cen=zStrdup($1);
-	    current_element->name=(char*)malloc((strlen(cen)-1)*sizeof(char));
 	    cen[strlen(cen)-1]=0;
 	    cen+=1;
-	    sprintf(current_element->name,"%s",cen);
+	    current_element=createElements(cen);
 	    cen-=1;
 	    free(cen);
-	    current_element->content=NULL;
-	    current_element->metadata=NULL;
-	    current_element->format=NULL;
-	    current_element->defaults=NULL;
-	    current_element->supported=NULL;
-	    current_element->next=NULL;
 	  }
 	  else{
@@ -727,5 +806,4 @@
 	    if($1!=NULL){ 
 	      char *cen=zStrdup($1);
-	      current_element->name=(char*)malloc((strlen(cen))*sizeof(char*));
 	      cen[strlen(cen)-1]=0;
 #ifdef DEBUG
@@ -733,13 +811,7 @@
 #endif
 	      cen+=1;
-	      sprintf(current_element->name,"%s",cen);
+	      setElementsName(&current_element,cen);
 	      cen-=1;
 	      free(cen);
-	      current_element->content=NULL;
-	      current_element->metadata=NULL;
-	      current_element->format=NULL;
-	      current_element->defaults=NULL;
-	      current_element->supported=NULL;
-	      current_element->next=NULL;
 	    }
 	  }
@@ -780,6 +852,8 @@
   }
 #ifdef DEBUG_SERVICE_CONF
-  fprintf(stderr,"(STARTING)FREE current_element\n");
-#endif
+  fprintf(stderr,"(STARTING)FREE current_element %s\n",file);
+#endif
+  fflush(stderr);
+  fflush(stdout);
   if(current_element!=NULL){
     freeElements(&current_element);
@@ -797,4 +871,5 @@
   wait_outputs=-1;
   wait_data=false;
+  wait_nested=false;
 //data=-1;
   ::data=-1; // knut: add namespace to avoid ambiguous symbol
@@ -831,5 +906,21 @@
       fprintf(stderr,"(DATAOUTPUTS - %d) COPY current_element\n",__LINE__);
 #endif
-      addToElements(&my_service->outputs,current_element);
+      if(wait_nested){
+	elements* cursor=my_service->outputs;
+	while(cursor->next!=NULL)
+	  cursor=cursor->next;
+	if(nested_level>1){
+	  for(int j=0;j<nested_level-1;j++){
+	    cursor=cursor->child;
+	    while(cursor->next!=NULL)
+	      cursor=cursor->next;
+	  }
+	}
+	if(cursor->child==NULL){
+	  cursor->child=dupElements(current_element);
+	}else
+	  addToElements(&cursor->child,current_element);
+      }else
+	addToElements(&my_service->outputs,current_element);
     }
 #ifdef DEBUG_SERVICE_CONF
