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 
00014 #include <mrpt/version.h>
00015 #if MRPT_VERSION<0x199
00016 #include <mrpt/utils/utils_defs.h>  // mrpt::format()
00017 #else
00018 #include <mrpt/core/format.h>
00019 #include <mrpt/core/bits_math.h>
00020 #endif
00021 
00022 #include <algorithm> // count()
00023 
00024 using namespace mvsim;
00025 using namespace std;
00026 
00027 const rapidxml::xml_node<char>* XmlClassesRegistry::get(
00028         const std::string& xml_node_vehicle_class) const
00029 {
00030         map<string, TXMLData>::const_iterator it =
00031                 m_classes.find(xml_node_vehicle_class);
00032         if (it == m_classes.end())
00033                 return NULL;
00034         else
00035                 return it->second.xml_doc->first_node();
00036 }
00037 
00038 void XmlClassesRegistry::add(const std::string& input_xml_node_vehicle_class)
00039 {
00040         // Parse the string as if it was an XML file:
00041         std::string* xml_node_vehicle_class =
00042                 new std::string(input_xml_node_vehicle_class);
00043 
00044         char* input_str = const_cast<char*>(xml_node_vehicle_class->c_str());
00045         rapidxml::xml_document<>* xml = new rapidxml::xml_document<>();
00046         try
00047         {
00048                 xml->parse<0>(input_str);
00049 
00050                 // sanity checks:
00051                 const rapidxml::xml_node<>* root_node =
00052                         xml->first_node(m_tagname.c_str());  //"vehicle:class"
00053                 if (!root_node)
00054                         throw runtime_error(
00055                                 mrpt::format(
00056                                         "[XmlClassesRegistry] Missing XML node <%s>",
00057                                         m_tagname.c_str()));
00058 
00059                 const rapidxml::xml_attribute<>* att_name =
00060                         root_node->first_attribute("name");
00061                 if (!att_name || !att_name->value())
00062                         throw runtime_error(
00063                                 mrpt::format(
00064                                         "[VehicleClassesRegistry] Missing mandatory attribute "
00065                                         "'name' in node <%s>",
00066                                         m_tagname.c_str()));
00067 
00068                 const string sClassName = att_name->value();
00069 
00070                 // All OK:
00071                 TXMLData& d = m_classes[sClassName];
00072                 d.xml_doc = xml;
00073                 d.xml_data = xml_node_vehicle_class;
00074         }
00075         catch (rapidxml::parse_error& e)
00076         {
00077                 unsigned int line =
00078                         static_cast<long>(std::count(input_str, e.where<char>(), '\n') + 1);
00079                 delete xml;
00080                 throw std::runtime_error(
00081                         mrpt::format(
00082                                 "[XmlClassesRegistry] XML parse error (Line %u): %s",
00083                                 static_cast<unsigned>(line), e.what()));
00084         }
00085         catch (std::exception&)
00086         {
00087                 delete xml;
00088                 throw;
00089         }
00090 }


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 22:08:35