/*
 *
 * [desc.]
 */
#include "Tools/Viewer/Parser/include/parser.h"
using namespace morpheo::tools::viewer::bdd;
namespace morpheo{
namespace tools{
namespace viewer{
namespace parser{     

  void Parse::parser_pos(QDomNode n,int level){
    string fichier;
       while( !n.isNull() )
      {
	QDomElement e = n.toElement();
	if( !e.isNull() )
	  {
	    
	    if( e.tagName() == "comment" )
	      {
		#ifdef DBUG_PARSE
		cout << "comment" <<endl;
		cout <<  e.text().toStdString() <<endl;
		#endif
		//BaseP
		pos->setComment(e.text().toStdString());
		pos->setLevel(level);
		
	      }
	    if (e.tagName() == "entity" )
	      {
		#ifdef DBUG_PARSE
		cout << "entity" <<endl;
		cout << e.attribute("name","").toStdString() <<endl;
		#endif

		ent[level]=new morpheo::tools::viewer::bdd::Entity\
		  (e.attribute("name","").toStdString(),\
		   e.attribute("type","").toStdString(),\
		   e.attribute("schema","").toInt(0,10));
		s=e.attribute("name","").toStdString();
		pos->setEntity(ent[level]);

		parser_pos(n.firstChild(),level);
		
		if((n.nextSiblingElement()).tagName() !="architecture")
      		  {
		    #ifdef DBUG_PARSE
		    cout << "pas d'archi" <<endl;
		    #endif
		    base->addBaseP(pos,s);
		    delete pos;
		  }
	      } 
	    if (e.tagName() == "port" )
	      {
		#ifdef DBUG_PARSE
		cout << "port" <<endl;
		#endif
		morpheo::tools::viewer::bdd::Port \
		  *p=new morpheo::tools::viewer::bdd::\
		  Port(e.attribute("name","").toStdString(),\
		       e.attribute("direction","").toStdString(),\
		       e.attribute("localisation","").toStdString());
		p->setComment(e.text().toStdString());
		ent[level]->addport(p);
	      } 
	    if (e.tagName() == "architecture" )
	      {
                #ifdef DBUG_PARSE
		cout << "architecture" <<endl;
		#endif
		arch[level]=new morpheo::tools::viewer::bdd::Architecture();
		pos->setArchitecture(arch[level]);
		
		base->addBaseP(pos,s);
		delete pos;
		parser_pos(n.firstChild(),level);
	      } 
	    if (e.tagName() == "component" )
	      {
		#ifdef DBUG_PARSE
		cout << "component" <<endl;
		#endif
		morpheo::tools::viewer::bdd::Component \
		  *comp=new morpheo::tools::viewer::bdd::\
		  Component(e.attribute("name","").toStdString(),\
			    e.attribute("type","").toStdString(),\
			    e.attribute("pos_x","").toInt(0,10),\
			    e.attribute("pos_y","").toInt(0,10),\
			    e.attribute("size_x","").toInt(0,10),\
			    e.attribute("size_y","").toInt(0,10));
		ctemp[level]=comp;
		arch[level]->addComponent(ctemp[level], e.attribute("name","").toStdString());
		
		fichier=absolutePath;
		fichier.append(ent[level]->getName());
		fichier.append("-");
		fichier.append((e.attribute("type","").toLower()).toStdString());
		fichier.append(".pos");
          #ifdef DBUG_PARSE
		cout << fichier <<endl; 
          #endif
		if(open_file(fichier,level+1)==-1)
		  exit(1);
		
		parser_pos(n.firstChild(),level);    
		
		//ajout ptr Port du sous composant
		BaseP pt=base->get_BasePos((e.attribute("type","").toLower()).toStdString());
		QList<Port> *sspor= (pt.getEntity())->getPort();
		for(QList<Port>::iterator it=sspor->begin();it!=sspor->end();++it)
		  ctemp[level]->addport(new Port(*it));
		
		
	      } 
	    if (e.tagName() == "port_map" )
	      {
                #ifdef DBUG_PARSE
		cout << "port_map" <<endl;
                #endif
		morpheo::tools::viewer::bdd::PortMap \
		  *p=new morpheo::tools::viewer::bdd::\
		  PortMap(e.attribute("name","").toStdString(),\
			  e.attribute("component","").toStdString(),\
			  e.attribute("port","").toStdString());
		
		ctemp[level]->addPortMap(p);
		delete p;
	      } 
      
	  }
	n = n.nextSibling(); //next channel
	    
      }	
      
  }

};//end parser
};//end viewer
};//end tools
};//end morpheo

