00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
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     
00050     string varName=name+"s";
00051     
00052     
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         
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     
00069     
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