Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

XMLTools.hpp

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00019 #ifndef _XMLTools_hpp
00020 #define _XMLTools_hpp
00021 
00022 #include<libxml++/libxml++.h>
00023 #include<ImLib3D/CppTools.hpp>
00024 #include <ctype.h>
00025 
00026 extern string GetAttribute(xmlpp::Element *node,string prop,bool fail=false);
00027 
00028 
00029 extern xmlpp::Element *Child(xmlpp::Element *parent,string name,bool fail=false);
00030 
00031 inline vector<xmlpp::Element *> 
00032 ChildElements(xmlpp::Node *node,const string &name="")
00033 {
00034     vector<xmlpp::Element *> res;
00035     xmlpp::Node::NodeList nodeList=node->get_children(name);
00036     for(xmlpp::Element::NodeList::iterator p=nodeList.begin();p!=nodeList.end();++p)
00037     {
00038         if(dynamic_cast<xmlpp::Element*>(*p) && 
00039            (name=="" || (*p)->get_name()==name))
00040         {res.push_back(dynamic_cast<xmlpp::Element*>(*p));}
00041     }
00042     return res;
00043 }
00044 
00045 template<class Type>
00046 void
00047 ReadVector(xmlpp::Element *node,string name,vector<Type> &res,bool downCase=false)
00048 {
00049     //mprintf("ReadVector<%s>: node %x:%s name:%s downCase:%d\n",typeid(Type).name(),node,string(node->get_name()),name,downCase);
00050     string varName=name+"s";
00051     //  if(isupper(varName[0])){varName[0]+='a'-'A';}
00052     // if there is a named subnode for this vector, go into subnode
00053     if(Child(node,varName)){node=Child(node,varName);}
00054     res.clear();
00055     vector<xmlpp::Element *>  nodeList=ChildElements(node,name);
00056     for(vector<xmlpp::Element *>::iterator p=nodeList.begin();p!=nodeList.end();++p)
00057     {
00058         //mprintf("ReadVector<>: %x child: %x:%s\n",node,*p,string((*p)->get_name()));
00059         res.push_back(Type(*p));
00060     }
00061 }
00062 
00063 inline 
00064 void
00065 ReadVector(xmlpp::Element *node,string name,vector<string> &res)
00066 {
00067     string varName=name+"s";
00068     //  if(isupper(varName[0])){varName[0]+='a'-'A';}
00069     // if there is a named subnode for this vector, go into subnode
00070     if(Child(node,varName)){node=Child(node,varName);}
00071     res.clear();
00072     vector<xmlpp::Element *>  nodeList=ChildElements(node);
00073     for(vector<xmlpp::Element *>::iterator p=nodeList.begin();p!=nodeList.end();++p)
00074     {
00075         res.push_back(GetAttribute(*p,"name",true));
00076     }
00077 }
00078 
00079 void ReadText(xmlpp::Element *node,string textNodeName,string &res,bool fail=false);
00080 
00081 #define ReadAttribute(p) p=GetAttribute(node,#p,true)
00082 #define ReadOptAttribute(p) p=GetAttribute(node,#p,false)
00083 
00084 void  RecursiveShowXMLElement(xmlpp::Element *node,int depth=0);
00085 
00086 #endif// _XMLTools_hpp

Generated on Fri Jun 17 13:36:10 2005 for ImLib3D by  doxygen 1.4.2