XMLClassesRegistry.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2020 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #include "XMLClassesRegistry.h"
11 
12 #include <mrpt/core/bits_math.h>
13 #include <mrpt/core/format.h>
14 
15 #include <algorithm> // count()
16 
17 using namespace mvsim;
18 using namespace std;
19 
21  const std::string& xml_node_class) const
22 {
23  map<string, TXMLData>::const_iterator it = m_classes.find(xml_node_class);
24  if (it == m_classes.end())
25  return nullptr;
26  else
27  return it->second.xml_doc->first_node();
28 }
29 
30 void XmlClassesRegistry::add(const std::string& input_xml_node_class)
31 {
32  // Parse the string as if it was an XML file:
33  std::string* xml_node_class = new std::string(input_xml_node_class);
34 
35  char* input_str = const_cast<char*>(xml_node_class->c_str());
37  try
38  {
39  xml->parse<0>(input_str);
40 
41  // sanity checks:
42  // e.g. "vehicle:class"
43  const rapidxml::xml_node<>* root_node =
44  xml->first_node(m_tagname.c_str());
45  if (!root_node)
46  throw runtime_error(mrpt::format(
47  "[XmlClassesRegistry] Missing XML node <%s>",
48  m_tagname.c_str()));
49 
50  const rapidxml::xml_attribute<>* att_name =
51  root_node->first_attribute("name");
52  if (!att_name || !att_name->value())
53  throw runtime_error(mrpt::format(
54  "[XmlClassesRegistry] Missing mandatory attribute "
55  "'name' in node <%s>",
56  m_tagname.c_str()));
57 
58  const string sClassName = att_name->value();
59 
60  // All OK:
61  TXMLData& d = m_classes[sClassName];
62  d.xml_doc = xml;
63  d.xml_data = xml_node_class;
64  }
65  catch (const rapidxml::parse_error& e)
66  {
67  unsigned int line =
68  static_cast<long>(std::count(input_str, e.where<char>(), '\n') + 1);
69  delete xml;
70  throw std::runtime_error(mrpt::format(
71  "[XmlClassesRegistry] XML parse error (Line %u): %s",
72  static_cast<unsigned>(line), e.what()));
73  }
74  catch (const std::exception&)
75  {
76  delete xml;
77  throw;
78  }
79 }
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
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
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 Fri May 7 2021 03:05:51