hpp-manipulation-urdf  4.9.0
Implementation of a parser for hpp-manipulation.
parser.hh
Go to the documentation of this file.
1 // Copyright (c) 2014, LAAS-CNRS
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4 // This file is part of hpp-manipulation-urdf.
5 // hpp-manipulation-urdf is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // hpp-manipulation-urdf is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // hpp-manipulation-urdf. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HPP_MANIPULATION_PARSER_HH
18 # define HPP_MANIPULATION_PARSER_HH
19 
20 # include <map>
21 # include <list>
22 # include <string>
23 # include <iostream>
24 # include <tinyxml.h>
25 
26 # include <hpp/manipulation/fwd.hh>
27 
28 namespace hpp {
29  namespace manipulation {
30  namespace parser {
31  typedef TiXmlElement XMLElement;
32  typedef TiXmlDocument XMLDocument;
33  typedef TiXmlAttribute XMLAttribute;
34  typedef TiXmlNode XMLNode;
35  typedef TiXmlText XMLText;
36 
37  class RootFactory;
38 
45 
48 
73  class ObjectFactory {
74  public:
75  typedef std::vector <ObjectFactory*> ObjectFactoryList;
76 
77  ObjectFactory (ObjectFactory* parent = NULL, const XMLElement* element = NULL);
78 
79  virtual ~ObjectFactory ()
80  {}
81 
84 
87  virtual bool init ();
88 
95  void setAttribute (const XMLAttribute* attr);
96 
98  virtual void addTextChild (const XMLText* text);
99 
102  virtual bool finishAttributes ();
103 
105  virtual void finishTags ();
106 
108  virtual void finishFile ();
109 
111 
114 
117  std::string tagName () const;
118 
121  std::string name () const;
122 
124  bool hasAttribute (const std::string& attr) const;
125 
127  std::string getAttribute (const std::string& attr) const;
128 
130  ObjectFactoryList getChildrenOfType (std::string type);
131 
136  bool getChildOfType (std::string type, ObjectFactory*& o);
137 
139 
143  void name (const std::string& n);
144 
146  void name (const char* n);
147 
149  template <typename T> T* as ()
150  {
151  if (!dynamic_cast <T*> (this)) {
152  std::ostringstream oss;
153  oss << "Unexpected tag: " << this->tagName ();
154  throw std::invalid_argument (oss.str ().c_str ());
155  }
156  return static_cast <T*> (this);
157  }
158 
159  protected:
161 
162  ObjectFactory* parent ();
163 
164  RootFactory* root ();
165 
166  bool hasParent () const;
167 
168  const XMLElement* XMLelement ();
169 
170  virtual void impl_setAttribute (const XMLAttribute* attr);
171 
172  void addChild (ObjectFactory* child);
173 
174  virtual std::ostream& print (std::ostream& os) const;
175 
176  private:
177  ObjectFactory* parent_;
178  RootFactory* root_;
179  typedef std::map <std::string, ObjectFactoryList > ChildrenMap;
180  ChildrenMap children_;
181 
182  const XMLElement* element_;
183 
184  typedef std::map <std::string, std::string> AttributeMap;
185  AttributeMap attrMap_;
186  std::string name_;
187  int id_;
188 
189  friend std::ostream& operator<< (std::ostream&, const ObjectFactory&);
190  };
191 
193  class RootFactory : public ObjectFactory {
194  public:
195  virtual ~RootFactory () {}
196  RootFactory (const DevicePtr_t dev = DevicePtr_t ());
197 
198  DevicePtr_t device () const;
199 
200  inline std::string prependPrefix (const std::string& in) const
201  {
202  if (prefix_.empty ()) return in;
203  return prefix_ + in;
204  }
205 
206  inline std::string removePrefix (const std::string& in) const
207  {
208  if (prefix_.empty ()) return in;
209  assert (in.compare (0, prefix_.size (), prefix_) == 0);
210  return in.substr (prefix_.size ());
211  }
212 
213  void prefix (const std::string& prefix)
214  {
215  if (prefix.empty ()) return;
216  prefix_ = prefix + "/";
217  }
218 
219  private:
220  DevicePtr_t device_;
221  std::string prefix_;
222  };
223 
225 
228  template <typename T>
229  ObjectFactory* create (ObjectFactory* parent = NULL, const XMLElement* element = NULL)
230  {
231  return new T (parent, element);
232  }
233 
239  class Parser {
240  public:
241  typedef ObjectFactory* (*FactoryType) (ObjectFactory*, const XMLElement*);
243 
248  Parser (bool fillWithDefaultFactories = true, FactoryType defaultFactory = create <ObjectFactory>);
249 
250  ~Parser ();
251 
252  void addObjectFactory (const std::string& tagname, FactoryType factory);
253 
254  void parseString (const std::string& xmlString, DevicePtr_t robot);
255 
256  void parseFile (const std::string& filename, DevicePtr_t robot);
257 
258  const ObjectFactoryList& objectFactories () const
259  {
260  return objectFactories_;
261  }
262 
264  void prefix (const std::string& prefix)
265  {
266  prefix_ = prefix;
267  }
268 
269  private:
270  XMLDocument doc_;
271  RootFactory* root_;
272  DevicePtr_t device_;
273 
274  void loadFile (const char* filename);
275 
276  void loadString (const char* xmlstring);
277 
278  void parse ();
279 
280  void parseElement (const XMLElement* element, ObjectFactory* parent);
281 
282  typedef std::map <std::string, FactoryType> ObjectFactoryMap;
283  typedef std::pair <std::string, FactoryType> ObjectFactoryPair;
284  typedef std::pair <ObjectFactoryMap::iterator, bool> ObjectFactoryInsertRet;
285  ObjectFactoryMap objFactoryMap_;
286  FactoryType defaultFactory_;
287 
288  ObjectFactoryList objectFactories_;
289 
290  std::string prefix_;
291 
292  std::ostream& print (std::ostream&) const;
293  friend std::ostream& operator<< (std::ostream&, const Parser&);
294  };
295 
296  std::ostream& operator<< (std::ostream&, const ObjectFactory&);
297  std::ostream& operator<< (std::ostream&, const Parser&);
298  } // namespace parser
299  } // namespace manipulation
300 } // namespace hpp
301 
302 #endif // HPP_MANIPULATION_PARSER_HH
void prefix(const std::string &prefix)
Definition: parser.hh:213
TiXmlElement XMLElement
Definition: parser.hh:31
std::string prependPrefix(const std::string &in) const
Definition: parser.hh:200
Vec3f n
pinocchio::DevicePtr_t DevicePtr_t
bool hasAttribute(const std::string &attr) const
Check if an attribute was set.
virtual ~RootFactory()
Definition: parser.hh:195
virtual void addTextChild(const XMLText *text)
Add Text child.
TiXmlAttribute XMLAttribute
Definition: parser.hh:33
friend std::ostream & operator<<(std::ostream &, const ObjectFactory &)
TiXmlText XMLText
Definition: parser.hh:35
virtual void finishTags()
Called when all the child tags have been processed.
ObjectFactory::ObjectFactoryList ObjectFactoryList
Definition: parser.hh:242
void setAttribute(const XMLAttribute *attr)
void prefix(const std::string &prefix)
Set the prefix of all joints.
Definition: parser.hh:264
Represent a XML document.
Definition: parser.hh:193
assert(d.lhs()._blocks()==d.rhs()._blocks())
virtual void impl_setAttribute(const XMLAttribute *attr)
virtual ~ObjectFactory()
Definition: parser.hh:79
Parse an XML document.
Definition: parser.hh:239
TiXmlNode XMLNode
Definition: parser.hh:34
virtual std::ostream & print(std::ostream &os) const
std::vector< ObjectFactory * > ObjectFactoryList
Definition: parser.hh:75
std::string getAttribute(const std::string &attr) const
Return a given attributes.
void addChild(ObjectFactory *child)
virtual void finishFile()
Called when parsing is finished.
TiXmlDocument XMLDocument
Definition: parser.hh:32
T * as()
Cast this class to any child class.
Definition: parser.hh:149
ObjectFactory(ObjectFactory *parent=NULL, const XMLElement *element=NULL)
ObjectFactory * create(ObjectFactory *parent=NULL, const XMLElement *element=NULL)
Definition: parser.hh:229
Class that catch XML Parser events for a specific tag and build the corresponding Object...
Definition: parser.hh:73
std::string removePrefix(const std::string &in) const
Definition: parser.hh:206
ObjectFactoryList getChildrenOfType(std::string type)
Get a list of ObjectFactory whose tag name is type.
const ObjectFactoryList & objectFactories() const
Definition: parser.hh:258
bool getChildOfType(std::string type, ObjectFactory *&o)
Vec3f o