hpp-util  4.13.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 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // 1. Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27 // DAMAGE.
28 
29 #ifndef HPP_MANIPULATION_PARSER_HH
30 #define HPP_MANIPULATION_PARSER_HH
31 
32 #include <tinyxml.h>
33 
34 #include <functional>
35 #include <iostream>
36 #include <list>
37 #include <map>
38 #include <string>
39 
40 namespace hpp {
41 namespace util {
42 namespace parser {
43 typedef TiXmlElement XMLElement;
44 typedef TiXmlDocument XMLDocument;
45 typedef TiXmlDeclaration XMLDeclaration;
46 typedef TiXmlAttribute XMLAttribute;
47 typedef TiXmlNode XMLNode;
48 typedef TiXmlText XMLText;
49 typedef TiXmlComment XMLComment;
50 
51 typedef TiXmlPrinter XMLPrinter;
52 
53 class RootFactory;
54 
61 
64 
90  public:
91  typedef std::list<ObjectFactory*> ObjectFactoryList;
92 
93  ObjectFactory(ObjectFactory* parent = NULL, const XMLElement* element = NULL);
94 
95  virtual ~ObjectFactory() {}
98 
101  virtual bool init();
102 
109  void setAttribute(const XMLAttribute* attr);
110 
112  virtual void addTextChild(const XMLText* text);
113 
116  virtual bool finishAttributes();
117 
119  virtual void finishTags();
120 
122  virtual void finishFile();
123 
125 
128 
130  ObjectFactory(const std::string& tagName, ObjectFactory* parent = NULL);
131 
133  void addAttribute(const std::string& name, const std::string& value);
134 
140  XMLNode* write(XMLNode* node) const;
141 
143 
146 
149  std::string tagName() const;
150 
153  std::string name() const;
154 
156  bool hasAttribute(const std::string& attr) const;
157 
159  std::string getAttribute(const std::string& attr) const;
160 
162  ObjectFactoryList getChildrenOfType(std::string type);
163 
169  bool getChildOfType(std::string type, ObjectFactory*& o);
170 
172 
176  void name(const std::string& n);
177 
179  void name(const char* n);
180 
182  template <typename T>
183  T* as() {
184  return static_cast<T*>(this);
185  }
186 
187  protected:
189 
191 
192  virtual ObjectFactory* root();
193 
194  bool hasParent() const;
195 
196  const XMLElement* XMLelement();
197 
198  virtual void impl_setAttribute(const XMLAttribute* attr);
199 
200  virtual void impl_write(XMLElement* element) const;
201 
202  void addChild(ObjectFactory* child);
203 
204  virtual std::ostream& print(std::ostream& os) const;
205 
206  private:
207  ObjectFactory* parent_;
208  ObjectFactory* root_;
209  typedef std::map<std::string, ObjectFactoryList> ChildrenMap;
210  ChildrenMap children_;
211 
212  const XMLElement* element_;
213 
214  typedef std::map<std::string, std::string> AttributeMap;
215  AttributeMap attrMap_;
216  std::string name_, tagName_;
217  int id_;
218 
219  friend std::ostream& operator<<(std::ostream&, const ObjectFactory&);
220 };
222 
225 template <typename T>
227  const XMLElement* element = NULL) {
228  return new T(parent, element);
229 }
230 
236 class Parser {
237  public:
238  typedef std::function<ObjectFactory*(ObjectFactory*, const XMLElement*)>
240 
243  Parser(FactoryType defaultFactory = create<ObjectFactory>);
244 
245  virtual ~Parser();
246 
247  void addObjectFactory(const std::string& tagname, FactoryType factory);
248 
249  virtual void parse(const char* xmlString);
250 
251  virtual void parseFile(const std::string& filename);
252 
253  ObjectFactory* root() const;
254 
255  private:
256  XMLDocument doc_;
257  ObjectFactory* root_;
258 
259  bool checkError() const;
260 
261  void loadString(const char* xmlstring);
262 
263  void parse();
264 
265  void parseElement(const XMLElement* element, ObjectFactory* parent);
266 
267  typedef std::map<std::string, FactoryType> ObjectFactoryMap;
268  typedef std::pair<std::string, FactoryType> ObjectFactoryPair;
269  typedef std::pair<ObjectFactoryMap::iterator, bool> ObjectFactoryInsertRet;
270  ObjectFactoryMap objFactoryMap_;
271  FactoryType defaultFactory_;
272 
273  typedef std::list<ObjectFactory*> ObjectFactoryList;
274  ObjectFactoryList objectFactories_;
275 
276  std::ostream& print(std::ostream&) const;
277  friend std::ostream& operator<<(std::ostream&, const Parser&);
278 };
279 
280 std::ostream& operator<<(std::ostream&, const ObjectFactory&);
281 std::ostream& operator<<(std::ostream&, const Parser&);
282 } // namespace parser
283 } // namespace util
284 } // namespace hpp
285 
286 #endif // HPP_MANIPULATION_PARSER_HH
virtual void finishFile()
Called when parsing is finished.
Definition: parser.cc:160
std::string name() const
Definition: parser.cc:196
ObjectFactory(ObjectFactory *parent=NULL, const XMLElement *element=NULL)
Definition: parser.cc:137
T * as()
Cast this class to any child class.
Definition: parser.hh:183
virtual bool finishAttributes()
Definition: parser.cc:156
TiXmlText XMLText
Definition: parser.hh:48
Definition: assertion.hh:45
std::function< ObjectFactory *(ObjectFactory *, const XMLElement *)> FactoryType
Definition: parser.hh:239
virtual ObjectFactory * root()
Definition: parser.cc:204
bool hasParent() const
Definition: parser.cc:206
Class that catch XML Parser events for a specific tag and build the corresponding Object...
Definition: parser.hh:89
void addChild(ObjectFactory *child)
Definition: parser.cc:214
ObjectFactory * create(ObjectFactory *parent=NULL, const XMLElement *element=NULL)
Definition: parser.hh:226
const XMLElement * XMLelement()
Definition: parser.cc:208
void addAttribute(const std::string &name, const std::string &value)
Add an attribute.
Definition: parser.cc:174
TiXmlNode XMLNode
Definition: parser.hh:47
std::string getAttribute(const std::string &attr) const
Return a given attributes.
Definition: parser.cc:267
TiXmlAttribute XMLAttribute
Definition: parser.hh:46
ObjectFactory * parent()
Definition: parser.cc:202
virtual ~ObjectFactory()
Definition: parser.hh:95
void setAttribute(const XMLAttribute *attr)
Definition: parser.cc:247
TiXmlComment XMLComment
Definition: parser.hh:49
ObjectFactoryList getChildrenOfType(std::string type)
Get a list of ObjectFactory whose tag name is type.
Definition: parser.cc:218
TiXmlElement XMLElement
Definition: parser.hh:43
std::string tagName() const
Definition: parser.cc:194
friend std::ostream & operator<<(std::ostream &, const ObjectFactory &)
Definition: parser.cc:133
TiXmlDocument XMLDocument
Definition: parser.hh:44
virtual std::ostream & print(std::ostream &os) const
Definition: parser.cc:237
XMLNode * write(XMLNode *node) const
Get a new XMLElement from the content of this factory.
Definition: parser.cc:180
TiXmlPrinter XMLPrinter
Definition: parser.hh:51
bool getChildOfType(std::string type, ObjectFactory *&o)
Definition: parser.cc:222
std::list< ObjectFactory * > ObjectFactoryList
Definition: parser.hh:91
Parse an XML document.
Definition: parser.hh:236
virtual void addTextChild(const XMLText *text)
Add Text child.
Definition: parser.cc:162
virtual bool init()
Definition: parser.cc:154
bool hasAttribute(const std::string &attr) const
Check if an attribute was set.
Definition: parser.cc:263
TiXmlDeclaration XMLDeclaration
Definition: parser.hh:45
virtual void impl_write(XMLElement *element) const
Definition: parser.cc:212
virtual void impl_setAttribute(const XMLAttribute *attr)
Definition: parser.cc:210
virtual void finishTags()
Called when all the child tags have been processed.
Definition: parser.cc:158