reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofXml.h
Go to the documentation of this file.
1#pragma once
2
3#include "ofParameter.h"
4#include "pugixml.hpp"
5
6template<typename It>
7class ofXmlIterator;
10
11class ofXml{
12public:
13 class Search{
14 public:
16
17 // Get collection type
18 pugi::xpath_node_set::type_t type() const;
19
20 // Get collection size
21 size_t size() const;
22
23 // Indexing operator
24 ofXml operator[](size_t index) const;
25
26 // Collection iterators
29
30 // Sort the collection in ascending/descending order by document order
31 void sort(bool reverse = false);
32
33 // Get first node in the collection by document order
34 ofXml getFirst() const;
35
36 // Check if collection is empty
37 bool empty() const;
38 private:
39 Search(std::shared_ptr<pugi::xml_document> doc, pugi::xpath_node_set set);
40 std::shared_ptr<pugi::xml_document> doc;
41 pugi::xpath_node_set search;
42 friend class ofXml;
43 };
44
45 class Attribute{
46 public:
48 std::string getValue() const;
49
50 void setName(const std::string & name);
51 std::string getName() const;
52
53 int getIntValue() const;
54 unsigned int getUintValue() const;
55 float getFloatValue() const;
56 double getDoubleValue() const;
57 bool getBoolValue() const;
58 operator bool() const;
59
62
63 template<typename T>
64 ofXml::Attribute & operator=(const T & value){
65 this->attr = ofToString(value);
66 return *this;
67 }
68
69 template<typename T>
70 Attribute & set(const T & value){
71 this->attr.set_value(ofToString(value).c_str());
72 return *this;
73 }
74 private:
75 Attribute(const pugi::xml_attribute & attr);
76 pugi::xml_attribute attr;
77 friend class ofXml;
78 };
79
80 template<class It>
81 class Range{
82 public:
83 It begin() const {
84 if(range.begin() != range.end()){
85 return It(doc, *range.begin());
86 }else{
87 return It(doc, typename It::Node());
88 }
89 }
90 It end() const { return It(doc, typename It::Node()); }
91
92 private:
93 Range(std::shared_ptr<pugi::xml_document> doc, pugi::xml_object_range<typename It::Base> range)
94 :doc(doc), range(range){}
95 std::shared_ptr<pugi::xml_document> doc;
96 pugi::xml_object_range<typename It::Base> range;
97 friend class ofXml;
98 };
99
100 ofXml();
101
102 bool load(const of::filesystem::path & file);
103 bool load(const ofBuffer & buffer);
104 bool parse(const std::string & xmlStr);
105 bool save(const of::filesystem::path & file) const;
106 void clear();
107 std::string toString(const std::string & indent = "\t") const;
108
109 ofXml getChild(const std::string & name) const;
112
113 ofXml appendChild(const ofXml & xml);
114 ofXml prependChild(const ofXml & xml);
115 bool removeChild(const ofXml & node);
116
117#if PUGIXML_VERSION>=170
118 ofXml appendChild(ofXml && xml);
119 ofXml prependChild(ofXml && xml);
120 bool removeChild(ofXml && node);
121#endif
122
123 ofXml appendChild(const std::string & name);
124 ofXml prependChild(const std::string & name);
125 bool removeChild(const std::string & name);
126
127 ofXml insertChildAfter(const std::string & name, const ofXml & after);
128 ofXml insertChildBefore(const std::string & name, const ofXml & after);
129
130 ofXml getNextSibling() const;
132 ofXml getNextSibling(const std::string & name) const;
133 ofXml getPreviousSibling(const std::string & name) const;
134
135 ofXml getFirstChild() const;
136 ofXml getLastChild() const;
137
138 ofXml getParent() const;
139
140
141 Attribute getAttribute(const std::string & name) const;
145 Attribute appendAttribute(const std::string & name);
146 Attribute prependAttribute(const std::string & name);
147 bool removeAttribute(const std::string & name);
148 bool removeAttribute(const Attribute & attr);
149 bool removeAttribute(Attribute && attr);
150
151 template<typename T>
152 Attribute setAttribute(const std::string & name, const T & value){
153 auto attr = getAttribute(name);
154 if(!attr){
155 attr = appendAttribute(name);
156 }
157 attr.set(value);
158 return attr;
159 }
160
161 ofXml findFirst(const std::string & path) const;
162 Search find(const std::string & path) const;
163
164 template<typename T>
165 T getValue() const{
166 return ofFromString<T>(this->xml.text().as_string());
167 }
168
169 std::string getValue() const;
170 std::string getName() const;
171
172 template<typename T>
173 void set(const T & value){
174 if(!xml){
175 xml = doc->append_child(pugi::node_element);
176 }
177 auto child = this->xml.first_child();
178 if(!child){
179 child = this->xml.append_child(pugi::node_pcdata);
180 }
181 if(child.type() == pugi::node_pcdata || child.type() == pugi::node_cdata){
182 child.set_value(ofToString(value).c_str());
183 }
184 }
185
186 void set(const unsigned char & value){
187 if(!xml){
188 xml = doc->append_child(pugi::node_element);
189 }
190 auto child = this->xml.first_child();
191 if(!child){
192 child = this->xml.append_child(pugi::node_pcdata);
193 }
194 if(child.type() == pugi::node_pcdata || child.type() == pugi::node_cdata){
195 child.set_value(ofToString(int(value)).c_str());
196 }
197 }
198
199
200 void setName(const std::string & name);
201
202 int getIntValue() const;
203 unsigned int getUintValue() const;
204 float getFloatValue() const;
205 double getDoubleValue() const;
206 bool getBoolValue() const;
207
208 operator bool() const;
209
210private:
211 ofXml(std::shared_ptr<pugi::xml_document> doc, const pugi::xml_node & xml);
212 std::shared_ptr<pugi::xml_document> doc;
213 pugi::xml_node xml;
214
215 template<typename It>
216 friend class ofXmlIterator;
219};
220
221template<typename It>
223public:
225
226 // Iterator operators
227 bool operator==(const ofXmlIterator& rhs) const{
228 return this->xml.xml == rhs.xml.xml;
229 }
230
231 bool operator!=(const ofXmlIterator& rhs) const{
232 return this->xml.xml != rhs.xml.xml;
233 }
234
235 const ofXml& operator*() const{
236 return this->xml;
237 }
238
239 const ofXml* operator->() const{
240 return &this->xml;
241 }
242
244 return this->xml;
245 }
246
248 return &this->xml;
249 }
250
252 if( std::is_same<Base, pugi::xml_named_node_iterator>::value ){
253 this->xml = xml.getNextSibling( xml.getName() );
254 }else{
255 this->xml = xml.getNextSibling();
256 }
257 return *this;
258 }
259
261 auto now = xml;
262 if( std::is_same<Base, pugi::xml_named_node_iterator>::value ){
263 this->xml = xml.getNextSibling( xml.getName() );
264 }else{
265 this->xml = xml.getNextSibling();
266 }
267 return now;
268 }
269
271 if( std::is_same<Base, pugi::xml_named_node_iterator>::value ){
272 this->xml = xml.getPreviousSibling( xml.getName() );
273 }else{
274 this->xml = xml.getPreviousSibling();
275 }
276 return *this;
277 }
278
280 auto now = xml;
281 if( std::is_same<Base, pugi::xml_named_node_iterator>::value ){
282 this->xml = xml.getPreviousSibling( xml.getName() );
283 }else{
284 this->xml = xml.getPreviousSibling();
285 }
286 return now;
287 }
288 typedef It Base;
289 typedef pugi::xml_node Node;
290private:
291
292 // Construct an iterator which points to the specified node
293 ofXmlIterator(std::shared_ptr<pugi::xml_document> doc, const pugi::xml_node & xml)
294 :xml(doc, xml){
295
296 }
297
298 // Construct an iterator which points to the specified node
299 ofXmlIterator(ofXml && xml)
300 :xml(xml){
301
302 }
303 mutable ofXml xml;
304 friend class ofXml;
305};
306
308public:
310
311 // Iterator operators
312 bool operator==(const ofXmlAttributeIterator& rhs) const{
313 return this->attr == rhs.attr;
314 }
315
316 bool operator!=(const ofXmlAttributeIterator& rhs) const{
317 return this->attr != rhs.attr;
318 }
319
321 return this->attr;
322 }
323
325 return &this->attr;
326 }
327
329 return this->attr;
330 }
331
333 return &this->attr;
334 }
335
337 this->attr = attr.getNextAttribute();
338 return *this;
339 }
340
342 auto now = attr;
343 this->attr = attr.getNextAttribute();
344 return now;
345 }
346
348 this->attr = attr.getPreviousAttribute();
349 return *this;
350 }
351
353 auto now = attr;
354 this->attr = attr.getPreviousAttribute();
355 return now;
356 }
357
358 typedef pugi::xml_attribute_iterator Base;
359 typedef pugi::xml_attribute Node;
360private:
361
362 // Construct an iterator which points to the specified node
363 ofXmlAttributeIterator(std::shared_ptr<pugi::xml_document>, const ofXml::Attribute & attr)
364 :attr(attr){
365
366 }
367
369 :attr(attr){
370
371 }
372 ofXml::Attribute attr;
373 friend class ofXml;
374};
375
376
378public:
380
381 // Iterator operators
382 bool operator==(const ofXmlSearchIterator& rhs) const;
383 bool operator!=(const ofXmlSearchIterator& rhs) const;
384
385 ofXml & operator*() const;
386 ofXml * operator->() const;
387
390
393
394private:
395 ofXmlSearchIterator(std::shared_ptr<pugi::xml_document> doc, const pugi::xpath_node * node)
396 :node(node)
397 {
398 if(node){
399 xml = ofXml(doc, node->node());
400 }
401 }
402 const pugi::xpath_node * node = nullptr;
403 mutable ofXml xml;
404 friend ofXml::Search;
405};
406// serializer
407void ofSerialize(ofXml & xml, const ofAbstractParameter & parameter);
408void ofDeserialize(const ofXml & xml, ofAbstractParameter & parameter);
Base class for ofParameter, ofReadOnlyParameter and ofParameterGroup.
Definition ofParameter.h:24
Definition ofFileUtils.h:15
Definition ofXml.h:45
std::string getValue() const
Definition ofXml.cpp:281
float getFloatValue() const
Definition ofXml.cpp:301
bool getBoolValue() const
Definition ofXml.cpp:317
void setName(const std::string &name)
Definition ofXml.cpp:285
ofXml::Attribute & operator=(const T &value)
Definition ofXml.h:64
int getIntValue() const
Definition ofXml.cpp:293
std::string getName() const
Definition ofXml.cpp:289
double getDoubleValue() const
Definition ofXml.cpp:309
Attribute getPreviousAttribute() const
Definition ofXml.cpp:329
Attribute getNextAttribute() const
Definition ofXml.cpp:325
unsigned int getUintValue() const
Definition ofXml.cpp:297
Attribute()
Definition ofXml.h:47
Attribute & set(const T &value)
Definition ofXml.h:70
Definition ofXml.h:81
It begin() const
Definition ofXml.h:83
It end() const
Definition ofXml.h:90
Definition ofXml.h:13
void sort(bool reverse=false)
Definition ofXml.cpp:362
Search()
Definition ofXml.h:15
bool empty() const
Definition ofXml.cpp:372
ofXmlSearchIterator end() const
Definition ofXml.cpp:357
size_t size() const
Definition ofXml.cpp:343
ofXml operator[](size_t index) const
Definition ofXml.cpp:348
ofXml getFirst() const
Definition ofXml.cpp:367
pugi::xpath_node_set::type_t type() const
Definition ofXml.cpp:338
ofXmlSearchIterator begin() const
Definition ofXml.cpp:353
Definition ofXml.h:307
ofXml::Attribute * operator->()
Definition ofXml.h:332
const ofXml::Attribute & operator*() const
Definition ofXml.h:320
bool operator!=(const ofXmlAttributeIterator &rhs) const
Definition ofXml.h:316
pugi::xml_attribute Node
Definition ofXml.h:359
ofXmlAttributeIterator operator++(int)
Definition ofXml.h:341
bool operator==(const ofXmlAttributeIterator &rhs) const
Definition ofXml.h:312
ofXmlAttributeIterator operator--(int)
Definition ofXml.h:352
pugi::xml_attribute_iterator Base
Definition ofXml.h:358
const ofXmlAttributeIterator & operator++()
Definition ofXml.h:336
ofXml::Attribute & operator*()
Definition ofXml.h:328
const ofXmlAttributeIterator & operator--()
Definition ofXml.h:347
ofXmlAttributeIterator()
Definition ofXml.h:309
const ofXml::Attribute * operator->() const
Definition ofXml.h:324
Definition ofXml.h:11
ofXml getLastChild() const
Definition ofXml.cpp:160
ofXml appendChild(const ofXml &xml)
Definition ofXml.cpp:94
bool load(const of::filesystem::path &file)
Definition ofXml.cpp:18
bool save(const of::filesystem::path &file) const
Definition ofXml.cpp:51
Attribute prependAttribute(const std::string &name)
Definition ofXml.cpp:188
Attribute getAttribute(const std::string &name) const
Definition ofXml.cpp:168
ofXml getChild(const std::string &name) const
Definition ofXml.cpp:82
ofXml findFirst(const std::string &path) const
Definition ofXml.cpp:209
Attribute appendAttribute(const std::string &name)
Definition ofXml.cpp:184
unsigned int getUintValue() const
Definition ofXml.cpp:245
ofXml insertChildAfter(const std::string &name, const ofXml &after)
Definition ofXml.cpp:124
void clear()
Definition ofXml.cpp:66
void set(const T &value)
Definition ofXml.h:173
float getFloatValue() const
Definition ofXml.cpp:249
bool getBoolValue() const
Definition ofXml.cpp:265
Attribute getLastAttribute() const
Definition ofXml.cpp:180
bool parse(const std::string &xmlStr)
Definition ofXml.cpp:36
void setName(const std::string &name)
Definition ofXml.cpp:234
bool removeAttribute(const std::string &name)
Definition ofXml.cpp:192
int getIntValue() const
Definition ofXml.cpp:241
Attribute getFirstAttribute() const
Definition ofXml.cpp:176
ofXml prependChild(const ofXml &xml)
Definition ofXml.cpp:98
ofXml getParent() const
Definition ofXml.cpp:164
ofXml getNextSibling() const
Definition ofXml.cpp:140
void set(const unsigned char &value)
Definition ofXml.h:186
ofXml getPreviousSibling() const
Definition ofXml.cpp:144
T getValue() const
Definition ofXml.h:165
bool removeChild(const ofXml &node)
Definition ofXml.cpp:136
Range< ofXmlIterator< pugi::xml_node_iterator > > getChildren() const
Definition ofXml.cpp:86
ofXml()
Definition ofXml.cpp:7
Search find(const std::string &path) const
Definition ofXml.cpp:217
ofXml insertChildBefore(const std::string &name, const ofXml &after)
Definition ofXml.cpp:128
std::string toString(const std::string &indent="\t") const
Definition ofXml.cpp:71
Range< ofXmlAttributeIterator > getAttributes() const
Definition ofXml.cpp:172
ofXml getFirstChild() const
Definition ofXml.cpp:156
std::string getName() const
Definition ofXml.cpp:230
double getDoubleValue() const
Definition ofXml.cpp:257
Attribute setAttribute(const std::string &name, const T &value)
Definition ofXml.h:152
Definition ofXml.h:222
ofXmlIterator()
Definition ofXml.h:224
ofXml & operator*()
Definition ofXml.h:243
pugi::xml_node Node
Definition ofXml.h:289
It Base
Definition ofXml.h:288
ofXmlIterator operator--(int)
Definition ofXml.h:279
bool operator!=(const ofXmlIterator &rhs) const
Definition ofXml.h:231
const ofXml * operator->() const
Definition ofXml.h:239
ofXmlIterator operator++(int)
Definition ofXml.h:260
const ofXmlIterator & operator--()
Definition ofXml.h:270
bool operator==(const ofXmlIterator &rhs) const
Definition ofXml.h:227
ofXml * operator->()
Definition ofXml.h:247
const ofXml & operator*() const
Definition ofXml.h:235
const ofXmlIterator & operator++()
Definition ofXml.h:251
Definition ofXml.h:377
bool operator==(const ofXmlSearchIterator &rhs) const
Definition ofXml.cpp:386
const ofXmlSearchIterator & operator++()
Definition ofXml.cpp:402
ofXmlSearchIterator()
Definition ofXml.cpp:383
bool operator!=(const ofXmlSearchIterator &rhs) const
Definition ofXml.cpp:390
const ofXmlSearchIterator & operator--()
Definition ofXml.cpp:422
ofXml & operator*() const
Definition ofXml.cpp:394
ofXml * operator->() const
Definition ofXml.cpp:398
std::string ofToString(const T &)
Convert a value to a string.
Definition ofUtils.h:657
void ofDeserialize(const ofXml &xml, ofAbstractParameter &parameter)
Definition ofXml.cpp:471
void ofSerialize(ofXml &xml, const ofAbstractParameter &parameter)
Definition ofXml.cpp:442