stdr_xml_parser.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002    STDR Simulator - Simple Two DImensional Robot Simulator
00003    Copyright (C) 2013 STDR Simulator
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 3 of the License, or
00007    (at your option) any later version.
00008    This program is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011    GNU General Public License for more details.
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software Foundation,
00014    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015    
00016    Authors : 
00017    * Manos Tsardoulias, etsardou@gmail.com
00018    * Aris Thallas, aris.thallas@gmail.com
00019    * Chris Zalidis, zalidis@gmail.com 
00020 ******************************************************************************/
00021 
00022 #include "stdr_parser/stdr_xml_parser.h"
00023 
00024 namespace stdr_parser
00025 {
00026   
00031   XmlParser::XmlParser(void)
00032   {
00033 
00034   }
00035   
00041   void XmlParser::parse(std::string file_name, Node* base_node)
00042   {
00043     // Must destroy prev tree
00044     std::string path = file_name;
00045     TiXmlDocument doc;
00046     doc.SetTabSize(2);
00047     bool loadOkay = doc.LoadFile(path.c_str());
00048     if (!loadOkay)
00049     {
00050       std::string error = 
00051         std::string("Failed to load file '") + 
00052         path + std::string("'") +
00053         std::string("\nError was '") + std::string(doc.ErrorDesc()) + 
00054         std::string("'\nIf error was 'Error reading end tag' you have a \
00055 malformed xml file");
00056       throw ParserException(error);
00057     }
00058     base_node->file_origin = path;
00059     base_node->file_row = doc.Row();
00060     parseLow(&doc,base_node); 
00061   }
00062  
00069   void XmlParser::parseLow(TiXmlNode* node,Node* n)
00070   {
00071     Node* new_node = new Node();
00072     TiXmlNode* pChild;
00073     int type = node->Type();
00074     std::string node_text(node->Value());
00075     switch (type)
00076     {
00077       case 0 :    
00078       {
00079         new_node = n;
00080         break;
00081       }
00082       case 1 :    
00083       {
00084         new_node->tag = node_text;
00085         new_node->file_origin = n->file_origin;
00086         n->file_row = node->Row();
00087         n->elements.push_back(new_node);
00088         break;
00089       }
00090       case 4 :    
00091       {
00092         
00093         if(std::string(node->Parent()->Value()) == "filename")
00094         {
00095           try
00096           {
00097             parse(ros::package::getPath("stdr_resources") + 
00098               std::string("/resources/") + std::string(node->Value()) , n);
00099           }
00100           catch(ParserException ex)
00101           {
00102             throw ex;
00103           }
00104           
00105         }
00106         else
00107         {
00108           new_node->value = node_text;
00109           new_node->file_origin = n->file_origin;
00110           n->file_row = node->Row();
00111           n->elements.push_back(new_node);
00112         }
00113         break;
00114       }
00115     }
00116     
00117     for ( 
00118       pChild = node->FirstChild(); 
00119       pChild != 0; 
00120       pChild = pChild->NextSibling()) 
00121     {
00122       parseLow( pChild , new_node );
00123     }
00124   }
00125   
00126 }
00127 


stdr_parser
Author(s): Manos Tsardoulias, Chris Zalidis, Aris Thallas
autogenerated on Wed Sep 2 2015 03:36:18