XMLClassesRegistry.cpp
Go to the documentation of this file.
00001 /*+-------------------------------------------------------------------------+
00002   |                       MultiVehicle simulator (libmvsim)                 |
00003   |                                                                         |
00004   | Copyright (C) 2014  Jose Luis Blanco Claraco (University of Almeria)    |
00005   | Copyright (C) 2017  Borys Tymchenko (Odessa Polytechnic University)     |
00006   | Distributed under GNU General Public License version 3                  |
00007   |   See <http://www.gnu.org/licenses/>                                    |
00008   +-------------------------------------------------------------------------+ */
00009 
00010 #include "XMLClassesRegistry.h"
00011 
00012 #include <iostream>
00013 #include <mrpt/utils/utils_defs.h>  // mrpt::format()
00014 
00015 using namespace mvsim;
00016 using namespace std;
00017 
00018 const rapidxml::xml_node<char>* XmlClassesRegistry::get(
00019         const std::string& xml_node_vehicle_class) const
00020 {
00021         map<string, TXMLData>::const_iterator it =
00022                 m_classes.find(xml_node_vehicle_class);
00023         if (it == m_classes.end())
00024                 return NULL;
00025         else
00026                 return it->second.xml_doc->first_node();
00027 }
00028 
00029 void XmlClassesRegistry::add(const std::string& input_xml_node_vehicle_class)
00030 {
00031         // Parse the string as if it was an XML file:
00032         std::string* xml_node_vehicle_class =
00033                 new std::string(input_xml_node_vehicle_class);
00034 
00035         char* input_str = const_cast<char*>(xml_node_vehicle_class->c_str());
00036         rapidxml::xml_document<>* xml = new rapidxml::xml_document<>();
00037         try
00038         {
00039                 xml->parse<0>(input_str);
00040 
00041                 // sanity checks:
00042                 const rapidxml::xml_node<>* root_node =
00043                         xml->first_node(m_tagname.c_str());  //"vehicle:class"
00044                 if (!root_node)
00045                         throw runtime_error(
00046                                 mrpt::format(
00047                                         "[XmlClassesRegistry] Missing XML node <%s>",
00048                                         m_tagname.c_str()));
00049 
00050                 const rapidxml::xml_attribute<>* att_name =
00051                         root_node->first_attribute("name");
00052                 if (!att_name || !att_name->value())
00053                         throw runtime_error(
00054                                 mrpt::format(
00055                                         "[VehicleClassesRegistry] Missing mandatory attribute "
00056                                         "'name' in node <%s>",
00057                                         m_tagname.c_str()));
00058 
00059                 const string sClassName = att_name->value();
00060 
00061                 // All OK:
00062                 TXMLData& d = m_classes[sClassName];
00063                 d.xml_doc = xml;
00064                 d.xml_data = xml_node_vehicle_class;
00065         }
00066         catch (rapidxml::parse_error& e)
00067         {
00068                 unsigned int line =
00069                         static_cast<long>(std::count(input_str, e.where<char>(), '\n') + 1);
00070                 delete xml;
00071                 throw std::runtime_error(
00072                         mrpt::format(
00073                                 "[XmlClassesRegistry] XML parse error (Line %u): %s",
00074                                 static_cast<unsigned>(line), e.what()));
00075         }
00076         catch (std::exception&)
00077         {
00078                 delete xml;
00079                 throw;
00080         }
00081 }


mvsim
Author(s):
autogenerated on Thu Sep 7 2017 09:27:48