stdr_xml_parser.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  STDR Simulator - Simple Two DImensional Robot Simulator
3  Copyright (C) 2013 STDR Simulator
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 3 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 
16  Authors :
17  * Manos Tsardoulias, etsardou@gmail.com
18  * Aris Thallas, aris.thallas@gmail.com
19  * Chris Zalidis, zalidis@gmail.com
20 ******************************************************************************/
21 
23 
24 namespace stdr_parser
25 {
26 
32  {
33 
34  }
35 
41  void XmlParser::parse(std::string file_name, Node* base_node)
42  {
43  // Must destroy prev tree
44  std::string path = file_name;
45  TiXmlDocument doc;
46  doc.SetTabSize(2);
47  bool loadOkay = doc.LoadFile(path.c_str());
48  if (!loadOkay)
49  {
50  std::string error =
51  std::string("Failed to load file '") +
52  path + std::string("'") +
53  std::string("\nError was '") + std::string(doc.ErrorDesc()) +
54  std::string("'\nIf error was 'Error reading end tag' you have a \
55 malformed xml file");
56  throw ParserException(error);
57  }
58  base_node->file_origin = path;
59  base_node->file_row = doc.Row();
60  parseLow(&doc,base_node);
61  }
62 
69  void XmlParser::parseLow(TiXmlNode* node, Node* n)
70  {
71  Node* new_node = new Node();
72  TiXmlNode* pChild;
73  int type = node->Type();
74  std::string node_text(node->Value());
75  switch (type)
76  {
77  case 0 :
78  {
79  new_node = n;
80  break;
81  }
82  case 1 :
83  {
84  new_node->tag = node_text;
85  new_node->file_origin = n->file_origin;
86  n->file_row = node->Row();
87  n->elements.push_back(new_node);
88  break;
89  }
90  case 4 :
91  {
92 
93  if(std::string(node->Parent()->Value()) == "filename")
94  {
95  try
96  {
97  parse(ros::package::getPath("stdr_resources") +
98  std::string("/resources/") + std::string(node->Value()), n);
99  }
100  catch(ParserException ex)
101  {
102  try
103  {
104  // If not found on stdr_resources/resources,
105  // search on the directory containing parent file
106  parse(extractDirname(new_node->file_origin) +
107  std::string("/") + std::string(node->Value()), n);
108  }
109  catch(ParserException ex)
110  {
111  throw ex;
112  }
113  }
114  }
115  else
116  {
117  new_node->value = node_text;
118  new_node->file_origin = n->file_origin;
119  n->file_row = node->Row();
120  n->elements.push_back(new_node);
121  }
122  break;
123  }
124  }
125 
126  for (
127  pChild = node->FirstChild();
128  pChild != 0;
129  pChild = pChild->NextSibling())
130  {
131  parseLow( pChild , new_node );
132  }
133  }
134 
135 }
136 
std::string tag
The node value (if it not a tag node)
XmlParser(void)
Default constructor.
The main namespace for STDR GUI XML parser.
static void parse(std::string file_name, Node *n)
Private function that initiates the parsing of an xml file.
std::string value
The node children.
static void parseLow(TiXmlNode *node, Node *n)
Low-level recursive function for parsing the xml robot file.
ROSLIB_DECL std::string getPath(const std::string &package_name)
std::string extractDirname(std::string s)
Extracts the directory from an absolute path. It does the same functionality as libgen's dirname but ...
Implements the main functionalities of the stdr parser tree.
std::string file_origin
Row in the original file.
std::vector< Node * > elements
File it was into.
Provides a parser exception. Publicly inherits from std::runtime_error. Used in robot handler...


stdr_parser
Author(s): Manos Tsardoulias, Chris Zalidis, Aris Thallas
autogenerated on Mon Jun 10 2019 15:14:54