hpp-util  4.12.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 <functional>
25 
26 # include <tinyxml.h>
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 std::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
hpp::util::parser::ObjectFactory::setAttribute
void setAttribute(const XMLAttribute *attr)
Definition: parser.cc:292
hpp::util::parser::ObjectFactory::getChildrenOfType
ObjectFactoryList getChildrenOfType(std::string type)
Get a list of ObjectFactory whose tag name is type.
Definition: parser.cc:261
hpp::util::parser::Parser::FactoryType
std::function< ObjectFactory *(ObjectFactory *, const XMLElement *)> FactoryType
Definition: parser.hh:226
hpp::util::parser::operator<<
std::ostream & operator<<(std::ostream &, const ObjectFactory &)
Definition: parser.cc:133
hpp::util::parser::ObjectFactory
Class that catch XML Parser events for a specific tag and build the corresponding Object.
Definition: parser.hh:77
hpp::util::parser::Parser::~Parser
virtual ~Parser()
Definition: parser.cc:30
hpp::util::parser::ObjectFactory::operator<<
friend std::ostream & operator<<(std::ostream &, const ObjectFactory &)
Definition: parser.cc:133
hpp::util::parser::create
ObjectFactory * create(ObjectFactory *parent=NULL, const XMLElement *element=NULL)
Definition: parser.hh:213
hpp::util::parser::ObjectFactory::addTextChild
virtual void addTextChild(const XMLText *text)
Add Text child.
Definition: parser.cc:173
hpp::util::parser::ObjectFactory::finishTags
virtual void finishTags()
Called when all the child tags have been processed.
Definition: parser.cc:167
hpp::util::parser::ObjectFactory::hasAttribute
bool hasAttribute(const std::string &attr) const
Check if an attribute was set.
Definition: parser.cc:309
hpp::util::parser::ObjectFactory::print
virtual std::ostream & print(std::ostream &os) const
Definition: parser.cc:281
hpp::util::parser::ObjectFactory::tagName
std::string tagName() const
Definition: parser.cc:210
hpp::util::parser::Parser
Parse an XML document.
Definition: parser.hh:223
hpp::util::parser::ObjectFactory::as
T * as()
Cast this class to any child class.
Definition: parser.hh:169
hpp::util::parser::XMLText
TiXmlText XMLText
Definition: parser.hh:36
hpp::util::parser::ObjectFactory::finishFile
virtual void finishFile()
Called when parsing is finished.
Definition: parser.cc:170
hpp::util::parser::ObjectFactory::ObjectFactoryList
std::list< ObjectFactory * > ObjectFactoryList
Definition: parser.hh:79
hpp::util::parser::ObjectFactory::init
virtual bool init()
Definition: parser.cc:157
hpp::util::parser::ObjectFactory::root
virtual ObjectFactory * root()
Definition: parser.cc:235
hpp::util::parser::ObjectFactory::finishAttributes
virtual bool finishAttributes()
Definition: parser.cc:162
hpp::util::parser::ObjectFactory::getChildOfType
bool getChildOfType(std::string type, ObjectFactory *&o)
Definition: parser.cc:266
hpp::util::parser::Parser::Parser
Parser(FactoryType defaultFactory=create< ObjectFactory >)
Definition: parser.cc:26
hpp::util::parser::XMLPrinter
TiXmlPrinter XMLPrinter
Definition: parser.hh:39
hpp::util::parser::ObjectFactory::impl_write
virtual void impl_write(XMLElement *element) const
Definition: parser.cc:253
hpp
Definition: assertion.hh:22
hpp::util::parser::Parser::parse
virtual void parse(const char *xmlString)
Definition: parser.cc:38
hpp::util::parser::ObjectFactory::hasParent
bool hasParent() const
Definition: parser.cc:240
hpp::util::parser::ObjectFactory::addChild
void addChild(ObjectFactory *child)
Definition: parser.cc:256
hpp::util::parser::XMLElement
TiXmlElement XMLElement
Definition: parser.hh:31
hpp::util::parser::Parser::root
ObjectFactory * root() const
Definition: parser.cc:116
hpp::util::parser::XMLAttribute
TiXmlAttribute XMLAttribute
Definition: parser.hh:34
hpp::util::parser::ObjectFactory::impl_setAttribute
virtual void impl_setAttribute(const XMLAttribute *attr)
Definition: parser.cc:250
hpp::util::parser::XMLNode
TiXmlNode XMLNode
Definition: parser.hh:35
hpp::util::parser::ObjectFactory::~ObjectFactory
virtual ~ObjectFactory()
Definition: parser.hh:83
hpp::util::parser::Parser::addObjectFactory
void addObjectFactory(const std::string &tagname, FactoryType factory)
Definition: parser.cc:75
hpp::util::parser::Parser::parseFile
virtual void parseFile(const std::string &filename)
Definition: parser.cc:46
hpp::util::parser::ObjectFactory::addAttribute
void addAttribute(const std::string &name, const std::string &value)
Add an attribute.
Definition: parser.cc:188
hpp::util::parser::ObjectFactory::getAttribute
std::string getAttribute(const std::string &attr) const
Return a given attributes.
Definition: parser.cc:314
hpp::util::parser::ObjectFactory::ObjectFactory
ObjectFactory(ObjectFactory *parent=NULL, const XMLElement *element=NULL)
Definition: parser.cc:138
hpp::util::parser::Parser::operator<<
friend std::ostream & operator<<(std::ostream &, const Parser &)
Definition: parser.cc:128
hpp::util::parser::ObjectFactory::XMLelement
const XMLElement * XMLelement()
Definition: parser.cc:245
hpp::util::parser::XMLComment
TiXmlComment XMLComment
Definition: parser.hh:37
hpp::util::parser::XMLDocument
TiXmlDocument XMLDocument
Definition: parser.hh:32
hpp::util::parser::ObjectFactory::name
std::string name() const
Definition: parser.cc:215
hpp::util::parser::XMLDeclaration
TiXmlDeclaration XMLDeclaration
Definition: parser.hh:33
hpp::util::parser::ObjectFactory::write
XMLNode * write(XMLNode *node) const
Get a new XMLElement from the content of this factory.
Definition: parser.cc:195
hpp::util::parser::ObjectFactory::parent
ObjectFactory * parent()
Definition: parser.cc:230