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

XMLTools.cpp

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  */
00020 #include<ImLib3D/XMLTools.hpp>
00021 #include<ImLib3D/CppTools.hpp>
00022 
00023 string
00024 GetAttribute(xmlpp::Element *node0,string attribute,bool fail)
00025 {
00026     xmlpp::Element *node=dynamic_cast<xmlpp::Element*>(node0);
00027     if(!node && !fail){return "";}
00028     if(!node && fail){ThrowError("GetAttribute: NULL node");}
00029     if(node->get_attribute(attribute))
00030     {
00031         return node->get_attribute(attribute)->get_value();
00032     }
00033     else
00034     {
00035         if(fail){ThrowError("did not find attribute \"%s\" for node \"%s\"",attribute,string(node->get_name()));}
00036         return "";
00037     }
00038 }
00039 
00040 xmlpp::Element *
00041 Child(xmlpp::Element *parent,string name,bool fail)
00042 {
00043     if(!parent && !fail){return NULL;}
00044     if(!parent && fail){ThrowError("Child: NULL parent");}
00045     vector<xmlpp::Element *>  nodeList=ChildElements(parent,name);
00046     if(nodeList.size()==0)
00047     {
00048         if(fail){ThrowError("Child: did not find child \"%s\" for parent \"%s\"",name,(string)parent->get_name());}
00049         return NULL;
00050     }
00051     else
00052     {
00053         return *(nodeList.begin());
00054     }
00055 }
00056 
00057 string
00058 Spaces(int nspaces)
00059 {
00060     string res;
00061     for(int i=0;i<nspaces;i++){res+=" ";}
00062     return res;
00063 }
00064 
00065 void 
00066 RecursiveShowXMLElement(xmlpp::Element *node,int depth)
00067 {
00068     std::cout << std::endl; //Separate nodes by an empty line.
00069   
00070     const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
00071     const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
00072     const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);
00073 
00074     if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
00075     return;
00076     
00077     Glib::ustring nodename = node->get_name();
00078 
00079     if(!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text".
00080     {
00081         cout << Spaces(4*depth);
00082         std::cout << "Node name = " << node << " " << node->get_name() << std::endl;
00083         std::cout << "Node name = " << nodename << std::endl;
00084     }
00085     else if(nodeText) //Let's say when it's text. - e.g. let's say what that white space is.
00086     {
00087         cout << Spaces(4*depth);
00088         std::cout << "Text Node" << std::endl;
00089     }
00090 
00091     //Treat the various node types differently: 
00092     if(nodeText)
00093     {
00094         cout << Spaces(4*depth);
00095         std::cout << "text = \"" << nodeText->get_content() << "\"" << std::endl;
00096     }
00097     else if(nodeComment)
00098     {
00099         cout << Spaces(4*depth);
00100         std::cout << "comment = " << nodeComment->get_content() << std::endl;
00101     }
00102     else if(nodeContent)
00103     {
00104         cout << Spaces(4*depth);
00105         std::cout << "content = " << nodeContent->get_content() << std::endl;
00106     }
00107     else if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node))
00108     {
00109         //A normal Element node:
00110 
00111         //line() works only for ElementNodes.
00112         cout << Spaces(4*depth);
00113         std::cout << "     line = " << node->get_line() << std::endl;
00114 
00115         //Print attributes:
00116         const xmlpp::Element::AttributeList& attributes = nodeElement->get_attributes();
00117         for(xmlpp::Element::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
00118         {
00119             const xmlpp::Attribute* attribute = *iter;
00120             cout << Spaces(4*depth);
00121             std::cout << "  Attribute " << attribute->get_name() << " = " << attribute->get_value() << std::endl;
00122         }
00123 
00124         const xmlpp::Attribute* attribute = nodeElement->get_attribute("title");
00125         if(attribute)
00126         {
00127             std::cout << "title found: =" << attribute->get_value() << std::endl;
00128       
00129         }
00130     }
00131   
00132     if(!nodeContent)
00133     {
00134         //Recurse through child nodes:
00135         vector<xmlpp::Element *>  list=ChildElements(node);
00136         for(vector<xmlpp::Element *>::iterator iter = list.begin(); iter != list.end(); ++iter)
00137         {
00138             RecursiveShowXMLElement(*iter, depth + 1); //recursive
00139         }
00140     }
00141 }
00142 // {
00143 //  if(!node)
00144 //  {
00145 //      ThrowError("RecursiveShowXMLElement: NULL node");
00146 //  }
00147 //  int nspaces=depth*4;
00148 //  string spaces;
00149 //  for(int i=0;i<nspaces;i++){spaces+=" ";}
00150     
00151 //  mprintf("%sNODE:\"%s\"   children:",spaces,node->get_name().c_str());
00152 
00153 //  const xmlpp::Element::NodeList &children=node->get_children();
00154 //  XMLNodeConstIterator it;
00155 //  for(it=children.begin();it!=children.end();++it)
00156 //  {
00157 //      mprintf("%s,",(*it)->get_name().c_str());
00158 //  }
00159 //  mprintf("\n");
00160 //  XMLPropertyList properties=node->properties();
00161 //  XMLPropertyIterator ip;
00162 
00163 //  if(properties.size()>0)
00164 //  {
00165 //      mprintf("%sproperties:",spaces);
00166 //      for(ip=properties.begin();ip!=properties.end();++ip)
00167 //      {
00168 //          mprintf("%s:%s , ",(*ip)->get_name().c_str(),(*ip)->get_value().c_str());
00169 //      }
00170 //      mprintf("\n");
00171 //  }
00172 //  if(node->content()!=""){mprintf("%scontents:\"%s\"\n",spaces,node->content());}
00173 //  for(it=children.begin();it!=children.end();++it)
00174 //  {
00175 //      RecursiveShowXMLElement(*it,depth+1);
00176 //  }
00177 // }
00178 
00179 void 
00180 ReadText(xmlpp::Element *node,string textNodeName,string &res,bool fail)
00181 {
00182     xmlpp::Element *child=Child(node,textNodeName,false);
00183     if(!child)
00184     {
00185         if(fail){ThrowError("ReadText: could not find child node:\"%s\"",textNodeName);}
00186         res="";
00187         return ;
00188     }
00189     res="";
00190     xmlpp::TextNode *text=child->get_child_text();
00191     if(text){res=text->get_content();}
00192     return;
00193 }

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