stdr_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 
28  Node* Parser::base_node_ = new Node();
29 
35  {
36 
37  }
38 
44  void Parser::parse(std::string file_name)
45  {
46  Parser::base_node_ = new Node();
47  Parser::base_node_->tag = "STDR_Parser_Root_Node";
48 
49  // Must destroy prev tree
50  try
51  {
52  if(file_name.find(".xml") != std::string::npos)
53  {
54  XmlParser::parse(file_name,base_node_);
55  }
56  else if(file_name.find(".yaml") != std::string::npos)
57  {
58  YamlParser::parse(file_name,base_node_);
59  }
60  //~ base_node_->printParsedXml(base_node_,"");
62 
64  while(!mergeNodes(base_node_));
66 
67  Validator::validate(file_name, base_node_);
68 
70  //~ base_node_->printParsedXml(base_node_,"");
71  }
72  catch(ParserException ex)
73  {
74  throw ex;
75  }
76  catch(YAML::ParserException& e)
77  {
78  std::string error =
79  std::string("Failed to load file '") +
80  file_name + std::string("'") +
81  std::string("\nError was '") + std::string(e.what());
82 
83  delete base_node_;
84 
85  throw ParserException(error);
86  }
87  }
88 
95  {
96  if(n->value != "")
97  {
98  return true;
99  }
100  for(unsigned int i = 0 ; i < n->elements.size() ; i++)
101  {
102  if(n->elements[i]->tag == "filename")
103  {
104  if(!n->elements[i]->checkForFilename(n->tag))
105  {
106  std::string error =
107  std::string("STDR parser : ") + n->tag + std::string(" has a \
108 filename of wrong type specified\n") +
109  std::string("\nError was in line ") + SSTR( n->file_row ) +
110  std::string(" of file '") + n->file_origin + std::string("'");
111  throw ParserException(error);
112  }
113  Node* child = n->elements[i]->elements[0];
114  n->elements.erase(n->elements.begin() + i);
115  for(unsigned int j = 0 ; j < child->elements.size() ; j++)
116  {
117  child->elements[j]->increasePriority() ;
118  n->elements.push_back(child->elements[j]);
119  }
120  return false;
121  }
122  else
123  {
124  try
125  {
126  if(!eliminateFilenames(n->elements[i]))
127  {
128  return false;
129  }
130  }
131  catch(ParserException ex)
132  {
133  throw ex;
134  }
135  }
136  }
137  return true;
138  }
139 
146  {
148  bool pure_values = true;
149  for(unsigned int i = 0 ; i < n->elements.size() ; i++)
150  {
151  if(n->elements[i]->value == "")
152  {
153  pure_values = false;
154  break;
155  }
156  }
157 
158  if(pure_values)
159  {
161  if(n->elements.size() <= 1)
162  {
163  return;
164  }
165 
167  int min_priority = n->elements[0]->priority;
168  unsigned int index = 0;
169  for(unsigned int i = 1 ; i < n->elements.size() ; i++)
170  {
171  if(n->elements[i]->priority < min_priority)
172  {
173  min_priority = n->elements[i]->priority;
174  index = i;
175  }
176  }
177  Node* proper_child = n->elements[index];
178  n->elements.clear();
179  n->elements.push_back(proper_child);
180  }
181  else
182  {
183  for(unsigned int i = 0 ; i < n->elements.size() ; i++)
184  {
185  mergeNodesValues(n->elements[i]);
186  }
187  }
188  }
189 
196  {
197  if(n->value != "")
198  {
199  return true;
200  }
201  for(unsigned int i = 0 ; i < n->elements.size() ; i++)
202  {
204  if(n->elements[i]->value != "")
205  {
206  continue;
207  }
208  std::string tag = n->elements[i]->tag;
209 
212  {
213  std::vector<int> num = n->getTag(tag);
214 
216  if(num.size() != 1)
217  {
218  for(int i = num.size()-1 ; i > 0 ; i --)
219  {
221  for (unsigned int j = 0 ;
222  j < n->elements[num[i]]->elements.size() ; j++)
223  {
224  n->elements[num[0]]->elements.push_back(
225  n->elements[num[i]]->elements[j]);
226  }
227  n->elements.erase(n->elements.begin() + num[i]);
228  }
229  return false;
230  }
231  }
232 
233  if(!mergeNodes(n->elements[i]))
234  {
235  return false;
236  }
237  }
238  return true;
239  }
240 }
241 
std::string tag
The node value (if it not a tag node)
Parser(void)
Default constructor.
Definition: stdr_parser.cpp:34
std::vector< int > getTag(std::string tag)
Searches for a tag in the specific node.
static Node * base_node_
< Base node of the parsed file
Definition: stdr_parser.h:47
The main namespace for STDR GUI XML parser.
static std::set< std::string > non_mergable_tags
#define SSTR(x)
< Transforms a float number to string
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 parse(std::string file_name)
Parses an xml file.
Definition: stdr_parser.cpp:44
static void parseMergableSpecifications(void)
Parses the mergable specifications file.
static bool eliminateFilenames(Node *n)
Recursive function - Expands the &#39;filename&#39; nodes and eliminates them.
Definition: stdr_parser.cpp:94
static void validate(std::string file_name, Node *n)
Performs a required / allowed - validity check on the xml tree.
static void parse(std::string file_name, Node *n)
Private function that initiates the parsing of an xml file.
static void mergeNodesValues(Node *n)
Merges the leaves of the xml tree, which are the value nodes.
static bool mergeNodes(Node *n)
Recursive function - Merges the nodes that do not exist in non_mergable_tags_.
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