hpp-util 6.0.0
Debugging tools for the HPP project.
Loading...
Searching...
No Matches
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 <tinyxml2.h>
33
34#include <functional>
35#include <iostream>
36#include <list>
37#include <map>
38#include <string>
39
40namespace hpp {
41namespace util {
42namespace parser {
43typedef tinyxml2::XMLElement XMLElement;
44typedef tinyxml2::XMLDocument XMLDocument;
45typedef tinyxml2::XMLDeclaration XMLDeclaration;
46typedef tinyxml2::XMLAttribute XMLAttribute;
47typedef tinyxml2::XMLNode XMLNode;
48typedef tinyxml2::XMLText XMLText;
49typedef tinyxml2::XMLComment XMLComment;
50
51typedef tinyxml2::XMLPrinter XMLPrinter;
52
53class RootFactory;
54
61
64
90 public:
91 typedef std::list<ObjectFactory*> ObjectFactoryList;
92
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
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
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
225template <typename T>
227 const XMLElement* element = NULL) {
228 return new T(parent, element);
229}
230
236class Parser {
237 public:
238 typedef std::function<ObjectFactory*(ObjectFactory*, const XMLElement*)>
240
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
280std::ostream& operator<<(std::ostream&, const ObjectFactory&);
281std::ostream& operator<<(std::ostream&, const Parser&);
282} // namespace parser
283} // namespace util
284} // namespace hpp
285
286#endif // HPP_MANIPULATION_PARSER_HH
Class that catch XML Parser events for a specific tag and build the corresponding Object.
Definition parser.hh:89
void setAttribute(const XMLAttribute *attr)
Definition parser.cc:244
bool hasParent() const
Definition parser.cc:203
ObjectFactoryList getChildrenOfType(std::string type)
Get a list of ObjectFactory whose tag name is type.
Definition parser.cc:215
virtual bool init()
Definition parser.cc:151
std::list< ObjectFactory * > ObjectFactoryList
Definition parser.hh:91
virtual std::ostream & print(std::ostream &os) const
Definition parser.cc:234
virtual void finishTags()
Called when all the child tags have been processed.
Definition parser.cc:155
virtual ~ObjectFactory()
Definition parser.hh:95
std::string tagName() const
Definition parser.cc:191
std::string name() const
Definition parser.cc:193
ObjectFactory * parent()
Definition parser.cc:199
virtual void finishFile()
Called when parsing is finished.
Definition parser.cc:157
void addChild(ObjectFactory *child)
Definition parser.cc:211
XMLNode * write(XMLNode *node) const
Get a new XMLElement from the content of this factory.
Definition parser.cc:177
virtual void impl_write(XMLElement *element) const
Definition parser.cc:209
virtual ObjectFactory * root()
Definition parser.cc:201
const XMLElement * XMLelement()
Definition parser.cc:205
void addAttribute(const std::string &name, const std::string &value)
Add an attribute.
Definition parser.cc:171
virtual bool finishAttributes()
Definition parser.cc:153
std::string getAttribute(const std::string &attr) const
Return a given attributes.
Definition parser.cc:264
virtual void impl_setAttribute(const XMLAttribute *attr)
Definition parser.cc:207
friend std::ostream & operator<<(std::ostream &, const ObjectFactory &)
Definition parser.cc:130
bool getChildOfType(std::string type, ObjectFactory *&o)
Definition parser.cc:219
T * as()
Cast this class to any child class.
Definition parser.hh:183
bool hasAttribute(const std::string &attr) const
Check if an attribute was set.
Definition parser.cc:260
virtual void addTextChild(const XMLText *text)
Add Text child.
Definition parser.cc:159
Parse an XML document.
Definition parser.hh:236
virtual void parseFile(const std::string &filename)
Definition parser.cc:54
friend std::ostream & operator<<(std::ostream &, const Parser &)
Definition parser.cc:126
virtual ~Parser()
Definition parser.cc:41
ObjectFactory * root() const
Definition parser.cc:118
std::function< ObjectFactory *(ObjectFactory *, const XMLElement *)> FactoryType
Definition parser.hh:239
virtual void parse(const char *xmlString)
Definition parser.cc:48
void addObjectFactory(const std::string &tagname, FactoryType factory)
Definition parser.cc:79
tinyxml2::XMLPrinter XMLPrinter
Definition parser.hh:51
tinyxml2::XMLDocument XMLDocument
Definition parser.hh:44
tinyxml2::XMLAttribute XMLAttribute
Definition parser.hh:46
tinyxml2::XMLComment XMLComment
Definition parser.hh:49
tinyxml2::XMLText XMLText
Definition parser.hh:48
ObjectFactory * create(ObjectFactory *parent=NULL, const XMLElement *element=NULL)
Definition parser.hh:226
tinyxml2::XMLElement XMLElement
Definition parser.hh:43
tinyxml2::XMLNode XMLNode
Definition parser.hh:47
tinyxml2::XMLDeclaration XMLDeclaration
Definition parser.hh:45
std::ostream & operator<<(std::ostream &, const ObjectFactory &)
Definition parser.cc:130
Definition assertion.hh:45
Class to ease exception creation.
Definition exception-factory.hh:73