hpp-util  4.9.0
Debugging tools for the HPP project.
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 <boost/function.hpp>
27 
28 namespace hpp {
29  namespace util {
30  namespace parser {
31  typedef TiXmlElement XMLElement;
32  typedef TiXmlDocument XMLDocument;
33  typedef TiXmlDeclaration XMLDeclaration;
34  typedef TiXmlAttribute XMLAttribute;
35  typedef TiXmlNode XMLNode;
36  typedef TiXmlText XMLText;
37  typedef TiXmlComment XMLComment;
38 
39  typedef TiXmlPrinter XMLPrinter;
40 
41  class RootFactory;
42 
49 
52 
77  class ObjectFactory {
78  public:
79  typedef std::list <ObjectFactory*> ObjectFactoryList;
80 
81  ObjectFactory (ObjectFactory* parent = NULL, const XMLElement* element = NULL);
82 
83  virtual ~ObjectFactory() {}
86 
89  virtual bool init ();
90 
97  void setAttribute (const XMLAttribute* attr);
98 
100  virtual void addTextChild (const XMLText* text);
101 
104  virtual bool finishAttributes ();
105 
107  virtual void finishTags ();
108 
110  virtual void finishFile ();
111 
113 
116 
118  ObjectFactory (const std::string& tagName, ObjectFactory* parent = NULL);
119 
121  void addAttribute (const std::string& name, const std::string& value);
122 
128  XMLNode* write (XMLNode* node) const;
129 
131 
134 
137  std::string tagName () const;
138 
141  std::string name () const;
142 
144  bool hasAttribute (const std::string& attr) const;
145 
147  std::string getAttribute (const std::string& attr) const;
148 
150  ObjectFactoryList getChildrenOfType (std::string type);
151 
156  bool getChildOfType (std::string type, ObjectFactory*& o);
157 
159 
163  void name (const std::string& n);
164 
166  void name (const char* n);
167 
169  template <typename T> T* as ()
170  {
171  return static_cast <T*> (this);
172  }
173 
174  protected:
176 
177  ObjectFactory* parent ();
178 
179  virtual ObjectFactory* root ();
180 
181  bool hasParent () const;
182 
183  const XMLElement* XMLelement ();
184 
185  virtual void impl_setAttribute (const XMLAttribute* attr);
186 
187  virtual void impl_write (XMLElement* element) const;
188 
189  void addChild (ObjectFactory* child);
190 
191  virtual std::ostream& print (std::ostream& os) const;
192 
193  private:
194  ObjectFactory* parent_;
195  ObjectFactory* root_;
196  typedef std::map <std::string, ObjectFactoryList > ChildrenMap;
197  ChildrenMap children_;
198 
199  const XMLElement* element_;
200 
201  typedef std::map <std::string, std::string> AttributeMap;
202  AttributeMap attrMap_;
203  std::string name_, tagName_;
204  int id_;
205 
206  friend std::ostream& operator<< (std::ostream&, const ObjectFactory&);
207  };
209 
212  template <typename T>
213  ObjectFactory* create (ObjectFactory* parent = NULL, const XMLElement* element = NULL)
214  {
215  return new T (parent, element);
216  }
217 
223  class Parser {
224  public:
225  typedef boost::function <ObjectFactory* (ObjectFactory*, const XMLElement*)>
227 
230  Parser (FactoryType defaultFactory = create <ObjectFactory>);
231 
232  virtual ~Parser ();
233 
234  void addObjectFactory (const std::string& tagname, FactoryType factory);
235 
236  virtual void parse (const char* xmlString);
237 
238  virtual void parseFile (const std::string& filename);
239 
240  ObjectFactory* root () const;
241 
242  private:
243  XMLDocument doc_;
244  ObjectFactory* root_;
245 
246  bool checkError () const;
247 
248  void loadString (const char* xmlstring);
249 
250  void parse ();
251 
252  void parseElement (const XMLElement* element, ObjectFactory* parent);
253 
254  typedef std::map <std::string, FactoryType> ObjectFactoryMap;
255  typedef std::pair <std::string, FactoryType> ObjectFactoryPair;
256  typedef std::pair <ObjectFactoryMap::iterator, bool> ObjectFactoryInsertRet;
257  ObjectFactoryMap objFactoryMap_;
258  FactoryType defaultFactory_;
259 
260  typedef std::list <ObjectFactory*> ObjectFactoryList;
261  ObjectFactoryList objectFactories_;
262 
263  std::ostream& print (std::ostream&) const;
264  friend std::ostream& operator<< (std::ostream&, const Parser&);
265  };
266 
267  std::ostream& operator<< (std::ostream&, const ObjectFactory&);
268  std::ostream& operator<< (std::ostream&, const Parser&);
269  } // namespace parser
270  } // namespace manipulation
271 } // namespace hpp
272 
273 #endif // HPP_MANIPULATION_PARSER_HH
virtual void finishFile()
Called when parsing is finished.
Definition: parser.cc:170
std::string name() const
Definition: parser.cc:215
ObjectFactory(ObjectFactory *parent=NULL, const XMLElement *element=NULL)
Definition: parser.cc:138
T * as()
Cast this class to any child class.
Definition: parser.hh:169
virtual bool finishAttributes()
Definition: parser.cc:162
TiXmlText XMLText
Definition: parser.hh:36
Definition: assertion.hh:24
virtual ObjectFactory * root()
Definition: parser.cc:235
bool hasParent() const
Definition: parser.cc:240
Class that catch XML Parser events for a specific tag and build the corresponding Object...
Definition: parser.hh:77
void addChild(ObjectFactory *child)
Definition: parser.cc:256
ObjectFactory * create(ObjectFactory *parent=NULL, const XMLElement *element=NULL)
Definition: parser.hh:213
const XMLElement * XMLelement()
Definition: parser.cc:245
void addAttribute(const std::string &name, const std::string &value)
Add an attribute.
Definition: parser.cc:188
TiXmlNode XMLNode
Definition: parser.hh:35
std::string getAttribute(const std::string &attr) const
Return a given attributes.
Definition: parser.cc:314
TiXmlAttribute XMLAttribute
Definition: parser.hh:34
ObjectFactory * parent()
Definition: parser.cc:230
virtual ~ObjectFactory()
Definition: parser.hh:83
void setAttribute(const XMLAttribute *attr)
Definition: parser.cc:292
TiXmlComment XMLComment
Definition: parser.hh:37
ObjectFactoryList getChildrenOfType(std::string type)
Get a list of ObjectFactory whose tag name is type.
Definition: parser.cc:261
TiXmlElement XMLElement
Definition: parser.hh:31
std::string tagName() const
Definition: parser.cc:210
friend std::ostream & operator<<(std::ostream &, const ObjectFactory &)
Definition: parser.cc:133
TiXmlDocument XMLDocument
Definition: parser.hh:32
virtual std::ostream & print(std::ostream &os) const
Definition: parser.cc:281
XMLNode * write(XMLNode *node) const
Get a new XMLElement from the content of this factory.
Definition: parser.cc:195
TiXmlPrinter XMLPrinter
Definition: parser.hh:39
bool getChildOfType(std::string type, ObjectFactory *&o)
Definition: parser.cc:266
std::list< ObjectFactory * > ObjectFactoryList
Definition: parser.hh:79
Parse an XML document.
Definition: parser.hh:223
boost::function< ObjectFactory *(ObjectFactory *, const XMLElement *)> FactoryType
Definition: parser.hh:226
virtual void addTextChild(const XMLText *text)
Add Text child.
Definition: parser.cc:173
virtual bool init()
Definition: parser.cc:157
bool hasAttribute(const std::string &attr) const
Check if an attribute was set.
Definition: parser.cc:309
TiXmlDeclaration XMLDeclaration
Definition: parser.hh:33
virtual void impl_write(XMLElement *element) const
Definition: parser.cc:253
virtual void impl_setAttribute(const XMLAttribute *attr)
Definition: parser.cc:250
virtual void finishTags()
Called when all the child tags have been processed.
Definition: parser.cc:167