XMLClassesRegistry.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014 Jose Luis Blanco Claraco (University of Almeria) |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under GNU General Public License version 3 |
7  | See <http://www.gnu.org/licenses/> |
8  +-------------------------------------------------------------------------+ */
9 
10 #include "XMLClassesRegistry.h"
11 
12 #include <iostream>
13 
14 #include <mrpt/version.h>
15 #if MRPT_VERSION<0x199
16 #include <mrpt/utils/utils_defs.h> // mrpt::format()
17 #else
18 #include <mrpt/core/format.h>
19 #include <mrpt/core/bits_math.h>
20 #endif
21 
22 #include <algorithm> // count()
23 
24 using namespace mvsim;
25 using namespace std;
26 
28  const std::string& xml_node_vehicle_class) const
29 {
30  map<string, TXMLData>::const_iterator it =
31  m_classes.find(xml_node_vehicle_class);
32  if (it == m_classes.end())
33  return NULL;
34  else
35  return it->second.xml_doc->first_node();
36 }
37 
38 void XmlClassesRegistry::add(const std::string& input_xml_node_vehicle_class)
39 {
40  // Parse the string as if it was an XML file:
41  std::string* xml_node_vehicle_class =
42  new std::string(input_xml_node_vehicle_class);
43 
44  char* input_str = const_cast<char*>(xml_node_vehicle_class->c_str());
46  try
47  {
48  xml->parse<0>(input_str);
49 
50  // sanity checks:
51  const rapidxml::xml_node<>* root_node =
52  xml->first_node(m_tagname.c_str()); //"vehicle:class"
53  if (!root_node)
54  throw runtime_error(
55  mrpt::format(
56  "[XmlClassesRegistry] Missing XML node <%s>",
57  m_tagname.c_str()));
58 
59  const rapidxml::xml_attribute<>* att_name =
60  root_node->first_attribute("name");
61  if (!att_name || !att_name->value())
62  throw runtime_error(
63  mrpt::format(
64  "[VehicleClassesRegistry] Missing mandatory attribute "
65  "'name' in node <%s>",
66  m_tagname.c_str()));
67 
68  const string sClassName = att_name->value();
69 
70  // All OK:
71  TXMLData& d = m_classes[sClassName];
72  d.xml_doc = xml;
73  d.xml_data = xml_node_vehicle_class;
74  }
75  catch (rapidxml::parse_error& e)
76  {
77  unsigned int line =
78  static_cast<long>(std::count(input_str, e.where<char>(), '\n') + 1);
79  delete xml;
80  throw std::runtime_error(
81  mrpt::format(
82  "[XmlClassesRegistry] XML parse error (Line %u): %s",
83  static_cast<unsigned>(line), e.what()));
84  }
85  catch (std::exception&)
86  {
87  delete xml;
88  throw;
89  }
90 }
d
Ch * where() const
Definition: rapidxml.hpp:94
Ch * value() const
Definition: rapidxml.hpp:692
void parse(Ch *text)
Definition: rapidxml.hpp:1381
xml_node< Ch > * first_node(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
Definition: rapidxml.hpp:936
virtual const char * what() const
Definition: rapidxml.hpp:85
xml_attribute< Ch > * first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
Definition: rapidxml.hpp:1025
const rapidxml::xml_node< char > * get(const std::string &xml_node_class) const
void add(const std::string &input_xml_node_class)


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 19:36:40