/* ImLib3D
 * Copyright (c) 2001, ULP-IPB Strasbourg.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/*! \file XMLTools.hpp */
#ifndef _XMLTools_hpp
#define _XMLTools_hpp

#include<ImLib3D/xml++.h>

extern string Property(XMLNode *node,string prop,bool fail=false);
extern XMLNode *DuplicateXMLNode(XMLNode *src);
extern XMLNode *Child(XMLNode *parent,string name,bool fail=false);
template<class Type>
void
ReadVector(XMLNode *node,string name,vector<Type> &res)
{
	// if there is a named subnode for this vector, go into subnode
	if(Child(node,name+"s")){node=Child(node,name+"s");}
	res.clear();
	XMLNodeList nodeList=node->children(name);
	for(XMLNodeList::iterator p=nodeList.begin();p!=nodeList.end();++p)
	{
		res.push_back(Type(*p));
	}
}

template<>
inline void
ReadVector<string>(XMLNode *node,string name,vector<string> &res)
{
	// if there is a named subnode for this vector, go into subnode
	if(Child(node,name+"s")){node=Child(node,name+"s");}
	res.clear();
	XMLNodeList nodeList=node->children(name);
	for(XMLNodeList::iterator p=nodeList.begin();p!=nodeList.end();++p)
	{
		res.push_back(Property(*p,"name",true));
	}
}

inline void 
ReadText(XMLNode *node,string textNodeName,string &res)
{
	XMLNode *child=Child(node,textNodeName,true);
	if(!child){res="";return ;}
	XMLNodeList nlist=child->children();
	if(nlist.size()==0){res="";return;}
	XMLNode *textNode=*(nlist.begin());
	res=textNode->content();
}
#define ReadProperty(p) p=Property(node,#p,true)
#define ReadOptProperty(p) p=Property(node,#p,false)

#endif// _XMLTools_hpp
