forked from openframeworks/openFrameworks
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathofxXmlPoco.cpp
More file actions
991 lines (826 loc) · 27.1 KB
/
ofxXmlPoco.cpp
File metadata and controls
991 lines (826 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
#include "ofConstants.h"
#include "ofxXmlPoco.h"
#include "Poco/AutoPtr.h"
using std::string;
using std::vector;
ofxXmlPoco::~ofxXmlPoco(){
releaseAll();
}
ofxXmlPoco::ofxXmlPoco(const string & path){
document = new Poco::XML::Document(); // we create this so that they can be merged later
element = document->documentElement();
load(path);
}
ofxXmlPoco::ofxXmlPoco(const ofxXmlPoco & rhs){
document = new Poco::XML::Document();
Poco::XML::Node *n = document->importNode(rhs.getPocoDocument()->documentElement(), true);
document->appendChild(n);
element = document->documentElement();
}
const ofxXmlPoco & ofxXmlPoco::operator=(const ofxXmlPoco & rhs){
if(&rhs == this){
return *this;
}
releaseAll();
document = (Poco::XML::Document *)rhs.document->cloneNode(true);
element = document->documentElement();
return *this;
}
ofxXmlPoco::ofxXmlPoco(){
document = new Poco::XML::Document(); // we create this so that they can be merged later
element = document->documentElement();
}
bool ofxXmlPoco::load(const of::filesystem::path & filePath){
ofFile file(filePath, ofFile::ReadOnly);
if(!file.exists()){
ofLogError("ofxXmlPoco") << "couldn't load, \"" << file.getFileName() << "\" not found";
return false;
}
ofBuffer xmlBuffer(file);
return loadFromBuffer(xmlBuffer);
}
bool ofxXmlPoco::save(const of::filesystem::path & filePath){
ofBuffer buffer;
buffer.set(toString());
ofFile file(filePath, ofFile::WriteOnly);
return file.writeFromBuffer(buffer);
}
int ofxXmlPoco::getNumChildren() const{
if(!element){
return 0;
}
int numberOfChildren = 0;
Poco::XML::NodeList *list = element->childNodes();
for(unsigned long i=0; i < list->length(); i++){
if(list->item(i) && list->item(i)->nodeType() == Poco::XML::Node::ELEMENT_NODE){
numberOfChildren++;
}
}
return numberOfChildren;
}
int ofxXmlPoco::getNumChildren(const string& path) const{
if(!element){
return 0;
}
int numberOfChildren = 0;
Poco::XML::NodeList *list = element->childNodes();
for(unsigned long i=0; i < list->length(); i++){
if(list->item(i) && list->item(i)->nodeType() == Poco::XML::Node::ELEMENT_NODE){
string nodeName = list->item(i)->localName();
if(path.compare(nodeName) == 0){
numberOfChildren++;
}
}
}
return numberOfChildren;
}
string ofxXmlPoco::toString() const{
std::ostringstream stream;
Poco::XML::DOMWriter writer;
writer.setOptions(Poco::XML::XMLWriter::PRETTY_PRINT);
if(document){
try{
writer.writeNode( stream, getPocoDocument() );
}catch( std::exception & e ){
ofLogError("ofxXmlPoco") << "toString(): " << e.what();
}
} else if(element){
element->normalize();
writer.writeNode( stream, element );
}
string tmp = stream.str();
// don't know how else to get rid of the hidden <#text></#text> nodes :/
ofStringReplace(tmp, "<#text>", "");
ofStringReplace(tmp, "</#text>", "");
return tmp;
}
void ofxXmlPoco::addXml(ofxXmlPoco& xml, bool copyAll){
Poco::XML::Node *n = NULL;
if(copyAll){
n = document->importNode(xml.getPocoDocument()->documentElement(), true);
}else{
if(xml.getPocoElement() == 0 || xml.getPocoElement() == xml.getPocoDocument()->documentElement()){
n = document->importNode(xml.getPocoDocument()->documentElement(), true);
}else{
n = document->importNode( xml.getPocoElement(), true);
}
}
// we have an element, i.e. the document has child nodes
// or we don't, so append it directly to the document
if(element){
element->appendChild(n);
}else{
document->appendChild(n);
}
}
bool ofxXmlPoco::addChild(const string& path){
vector<string> tokens;
if(path.find('/') != string::npos){
tokens = tokenize(path, "/");
}
// is this a tokenized tag?
if(tokens.size() > 1){
// don't 'push' down into the new nodes
Poco::XML::Element *el = element;
vector<Poco::XML::Element*> toBeReleased;
for(std::size_t i = 0; i < tokens.size(); i++){
Poco::XML::Element *pe = getPocoDocument()->createElement(tokens.at(i));
el->appendChild(pe);
toBeReleased.push_back(pe);
el = pe;
}
if(element){
element->appendChild(el);
}else{
element = el;
}
return true;
}else{
Poco::XML::Element* pe = getPocoDocument()->createElement(path);
if(element){
element->appendChild(pe);
}else{
document->appendChild(pe);
element = document->documentElement();
}
}
return true;
}
string ofxXmlPoco::getValue() const{
//if we don't have a DOM element, return the default value
if(!element){
return "";
}
// firstChild() may return a NULL pointer
if(NULL == element->firstChild()){
// return default value in this case
return "";
}
// no NULL pointer -> save to call nodeType()
if(element->firstChild()->nodeType() == Poco::XML::Node::TEXT_NODE) {
return element->innerText();
}
return "";
}
string ofxXmlPoco::getValue(const string & path) const{
return getValue <string>(path, "");
}
int ofxXmlPoco::getIntValue() const {
return ofToInt(getValue());
}
int ofxXmlPoco::getIntValue(const string & path) const {
return getValue <int>(path, 0);
}
float ofxXmlPoco::getFloatValue() const {
return ofToFloat(getValue());
}
float ofxXmlPoco::getFloatValue(const string & path) const {
return getValue <float>(path, 0.0);
}
bool ofxXmlPoco::getBoolValue() const {
return ofToBool(getValue());
}
bool ofxXmlPoco::getBoolValue(const string & path) const {
return getValue <bool>(path, false);
}
int64_t ofxXmlPoco::getInt64Value() const {
return ofToInt64(getValue());
}
int64_t ofxXmlPoco::getInt64Value(const string & path) const {
return getValue <int64_t>(path, 0);
}
bool ofxXmlPoco::reset(){
if(element){
element = document->documentElement();
return true;
}
ofLogWarning("ofxXmlPoco") << "reset(): no element set yet";
return false;
}
bool ofxXmlPoco::setToChild(unsigned long index){
if(!element){
if((Poco::XML::Element*) document->documentElement()->firstChild()){
element = (Poco::XML::Element*) document->documentElement()->firstChild();
}else{
ofLogWarning("ofxXmlPoco") << "setToChild(): no element created yet";
return false;
}
}
unsigned long numberOfChildren = 0;
Poco::XML::NodeList *list = element->childNodes();
for(unsigned long i=0; i < list->length() && numberOfChildren < index + 1; i++){
if(list->item(i) && list->item(i)->nodeType() == Poco::XML::Node::ELEMENT_NODE){
if(numberOfChildren == index){
element = (Poco::XML::Element*) list->item(i);
return true;
}
numberOfChildren++;
}
}
return false;
}
bool ofxXmlPoco::setToParent(){
if(element->parentNode()){
element = (Poco::XML::Element*) element->parentNode();
}else{
ofLogWarning("ofxXmlPoco") << "setToParent(): current element has no parent";
return false;
}
return true;
}
bool ofxXmlPoco::setToParent(int numLevelsUp){
if(element){
int i = 0;
while( i < numLevelsUp ){
if(element->parentNode()){
element = (Poco::XML::Element*) element->parentNode();
}else{
ofLogWarning("ofxXmlPoco") << "setToParent(): too many parents: " << numLevelsUp;
return false;
}
i++;
}
return true;
}
ofLogWarning("ofxXmlPoco") << "setToParent(): no element set yet";
return false;
}
bool ofxXmlPoco::setToSibling(){
Poco::XML::Element *node;
if(element){
node = (Poco::XML::Element*) element->nextSibling();
}else{
ofLogWarning("ofxXmlPoco") << "setToSibling() << no element set yet";
return false;
}
/* If we get NULL for node, then we do not have a sibling.
We can only savely check the type on a non-Null node (thus
avoiding NULL-pointer dereferences). Empty space is treated
as a text node and we do not want that. We are also not
interessted in comments. If we find a non-TEXT_NODE or
non-COMMENT_NODE, we do not look further for a sibling. */
while(NULL != node){
if((node->nodeType() == Poco::XML::Node::TEXT_NODE)
|| (node->nodeType() == Poco::XML::Node::COMMENT_NODE)){
node = (Poco::XML::Element*) node->nextSibling();
}else{
break;
}
}
// make sure we actually got a sibling
if(NULL == node){
return false;
}
// we're cool now
element = node;
return true;
}
bool ofxXmlPoco::setToPrevSibling(){
Poco::XML::Element *node;
if(element){
node = (Poco::XML::Element*) element->previousSibling();
}else{
ofLogWarning("ofxXmlPoco") << "setToPrevSibling(): no element set yet";
return false;
}
// empty space in the XML doc is treated as text nodes. blerg.
while(node && node->nodeType() == Poco::XML::Node::TEXT_NODE){
node = (Poco::XML::Element*) node->previousSibling();
}
if(!node || node->nodeType() == Poco::XML::Node::TEXT_NODE){
return false;
}
element = node;
return true;
}
bool ofxXmlPoco::setValue(const string& path, const string& value){
Poco::XML::Element *e;
if(element){
e = (Poco::XML::Element*) element->getNodeByPath(path);
}else{
ofLogWarning("ofxXmlPoco") << "setValue(): no element set yet";
return false;
}
if(!e){
ofLogWarning("ofxXmlPoco") << "setValue(): path \"" + path + "\" doesn't exist";
return false;
}
if(!e->firstChild()){
Poco::XML::Text *node = getPocoDocument()->createTextNode(ofToString(value));
e->appendChild(node);
node->release();
return true;
}
if(e->firstChild()->nodeType() == Poco::XML::Node::TEXT_NODE){
Poco::XML::Text *node = getPocoDocument()->createTextNode(ofToString(value));
e->replaceChild( (Poco::XML::Node*) node, e->firstChild()); // swap out
node->release();
return true;
}else{
return false;
}
}
string ofxXmlPoco::getAttribute(const string& path) const{
Poco::XML::Node *e;
if(element){
if(path.find("[@") == string::npos){
// we need to create a proper path
string attributePath = "[@" + path + "]";
e = element->getNodeByPath(attributePath);
}else{
e = element->getNodeByPath(path);
}
}else{
ofLogWarning("ofxXmlPoco") << "getAttribute(): no element set yet";
return "";
}
if(e){
return e->getNodeValue(); // this will be the value of the attribute
}
return "";
}
bool ofxXmlPoco::removeAttribute(const string& path){
string attributeName, pathToAttribute;
Poco::XML::Element *e;
if(element){
bool hasPath = false;
// you can pass either /node[@attr] or just attr
if(path.find("[@") != string::npos){
int attrBegin = path.find("[@");
int start = attrBegin + 2;
int end = path.find("]", start);
attributeName = path.substr( start, end - start );
pathToAttribute = path.substr(0, attrBegin);
hasPath = true;
}else{
attributeName = path;
}
if(hasPath){
e = (Poco::XML::Element*) element->getNodeByPath(pathToAttribute);
}else{
e = element;
}
}else{
ofLogWarning("ofxXmlPoco") << "clearAttributes(): no element set yet";
return false;
}
if(e){
Poco::XML::NamedNodeMap *map = e->attributes();
for(unsigned long i = 0; i < map->length(); i++){
if(map->item(i)->nodeName() == attributeName){
e->removeAttribute(map->item(i)->nodeName());
}
}
map->release();
return true;
}
return false;
}
bool ofxXmlPoco::removeAttributes(const string& path){
Poco::XML::Element *e;
if(element){
if(path.find("[@") == string::npos){
// we need to create a proper path
string attributePath = "[@" + path + "]";
e = (Poco::XML::Element*) element->getNodeByPath(attributePath);
}else{
e = (Poco::XML::Element*) element->getNodeByPath(path);
}
}else{
ofLogWarning("ofxXmlPoco") << "clearAttributes(): no element set yet";
return false;
}
if(e){
Poco::XML::NamedNodeMap *map = e->attributes();
for(unsigned long i = 0; i < map->length(); i++){
e->removeAttribute(map->item(i)->nodeName());
}
map->release();
return true;
}
return false;
}
bool ofxXmlPoco::removeAttributes(){
if(element){
Poco::XML::NamedNodeMap *map = element->attributes();
for(unsigned long i = 0; i < map->length(); i++){
element->removeAttribute(map->item(i)->nodeName());
}
map->release();
return true;
}
ofLogWarning("ofxXmlPoco") << "clearAttributes(): no element set yet";
return false;
}
bool ofxXmlPoco::removeContents(){
if(element && element->hasChildNodes()){
Poco::XML::Node* swap;
Poco::XML::Node* n = element->firstChild();
while(n->nextSibling() != nullptr){
swap = n->nextSibling();
element->removeChild(n);
n = swap;
}
return true;
}
return false;
}
bool ofxXmlPoco::removeContents(const string& path){
Poco::XML::Element *e;
if(element){
e = (Poco::XML::Element*) element->getNodeByPath(path);
}else{
ofLogWarning("ofxXmlPoco") << "clearContents(): no element set yet";
return false;
}
if(e){
Poco::XML::NodeList *list = e->childNodes();
for(unsigned long i = 0; i < list->length(); i++) {
element->removeChild(list->item(i));
}
list->release();
return true;
}
return false;
}
void ofxXmlPoco::clear(){
releaseAll();
document = new Poco::XML::Document(); // we create this so that they can be merged later
element = document->documentElement();
}
void ofxXmlPoco::releaseAll(){
if(document){
document->release();
document = NULL;
}
element = NULL;
}
bool ofxXmlPoco::remove(const string & path){ // works for both attributes and tags
Poco::XML::Node * node;
if(element){
node = element->getNodeByPath(path);
}else{
ofLogWarning("ofxXmlPoco") << "remove(): no element set yet";
return false;
}
if(node){
Poco::XML::Node * n = node->parentNode()->removeChild(node);
n->release();
return true;
}
return false;
}
void ofxXmlPoco::remove(){
Poco::XML::Node * parent = element->parentNode();
if(parent){
parent->removeChild(element);
element->release();
element = (Poco::XML::Element *)parent;
}else{
clear();
}
}
bool ofxXmlPoco::exists(const string & path) const{ // works for both attributes and tags
Poco::XML::Node * node;
if(element){
node = element->getNodeByPath(path);
}else{
return false;
}
if(node){
return true;
}
return false;
}
std::map<string, string> ofxXmlPoco::getAttributes() const{ // works for both attributes and tags
std::map<string, string> attrMap;
if(element){
Poco::AutoPtr<Poco::XML::NamedNodeMap> attr = element->attributes();
for(unsigned long i = 0; i < attr->length(); i++){
attrMap[attr->item(i)->nodeName()] = attr->item(i)->nodeValue();
}
}else{
ofLogWarning("ofxXmlPoco") << "getAttribute(): no element set";
}
return attrMap;
}
bool ofxXmlPoco::setAttribute(const string& path, const string& value){
string attributeName, pathToAttribute;
bool hasPath = false;
// you can pass either /node[@attr] or just attr
if(path.find("[@") != string::npos){
size_t attrBegin = path.find("[@");
size_t start = attrBegin + 2;
size_t end = path.find("]", start);
attributeName = path.substr( start, end - start );
pathToAttribute = path.substr(0, attrBegin);
hasPath = true;
}else{
attributeName = path;
}
// we don't have a path to resolve
Poco::AutoPtr<Poco::XML::Attr> attr = getPocoDocument()->createAttribute(attributeName);
attr->setValue(value);
if(!hasPath){
Poco::AutoPtr<Poco::XML::NamedNodeMap> map = element->attributes();
map->setNamedItem(attr);
return true; // and we're done
}
// we have a path to resolve
Poco::XML::Element* curElement = getPocoElement(pathToAttribute);
if(!curElement){ // if it doesn't exist
vector<string> tokens;
if(path.find('/') != string::npos){
tokens = tokenize(pathToAttribute, "/");
}
// is this a tokenized tag?
if(tokens.size() > 1){
// don't 'push' down into the new nodes
curElement = element;
// find the last existing tag
size_t lastExistingTag = 0;
// can't use reverse_iterator b/c accumulate doesn't like it
for(vector<string>::iterator it = tokens.end(); it != tokens.begin(); it--){
string empty = "";
string concat = accumulate(tokens.begin(), it, std::string());
Poco::XML::Element* testElement = getPocoElement(concat);
if(testElement){
lastExistingTag++;
curElement = testElement;
break;
}
}
// create all the tags that don't exist
for(size_t i = lastExistingTag; i < tokens.size(); i++){
Poco::XML::Element *newElement = getPocoDocument()->createElement(tokens.at(i));
curElement->appendChild(newElement);
curElement = newElement;
}
curElement->setAttribute(attributeName, value);
return true;
}else{
Poco::XML::Element* testElement = getPocoElement(pathToAttribute);
if(testElement){
curElement = testElement;
}else{
Poco::XML::Element *newElement = getPocoDocument()->createElement(pathToAttribute);
curElement->appendChild(newElement);
curElement = newElement;
}
curElement->setAttribute(attributeName, value);
return true;
}
}
return false;
}
bool ofxXmlPoco::loadFromBuffer(const string & buffer){
Poco::XML::DOMParser parser;
// release and null out if we already have a document
if(document){
document->release();
}
try{
document = parser.parseString(buffer);
element = (Poco::XML::Element *)document->firstChild();
document->normalize();
return true;
}
catch(const Poco::XML::SAXException & e){
ofLogError("ofxXmlPoco") << "parse error: " << e.message();
document = new Poco::XML::Document;
element = document->documentElement();
return false;
}
catch(const std::exception & e){
short msg = atoi(e.what());
ofLogError("ofxXmlPoco") << "parse error: " << DOMErrorMessage(msg);
document = new Poco::XML::Document;
element = document->documentElement();
return false;
}
}
string ofxXmlPoco::getName() const {
if(element){
return element->nodeName();
}
return "";
}
bool ofxXmlPoco::setTo(const string& path){
if(!element){
if(document->documentElement()) {
element = document->documentElement();
}else{
ofLogWarning("ofxXmlPoco") << "setTo(): empty document";
return false;
}
}
// one case: we're at the root, but we don't know it yet:
if(element == document->documentElement() && element->nodeName() == path ){
return true;
}
//ofLogNotice("ofxXmlPoco") << path << " " << path.find("../");
// another: let's go up a little
if(path.find("../") != string::npos){
Poco::XML::Element* prev = element;
Poco::XML::Element* parent = nullptr;
size_t count = 0;
size_t offset;
for (offset = path.find("../");
offset != std::string::npos;
offset = path.find("../", offset + 3)){
if(count == 0){
parent = (Poco::XML::Element*) element->parentNode();
}else{
parent = (Poco::XML::Element*) parent->parentNode();
}
++count;
}
//ofLogNotice("ofxXmlPoco") << (count * 3) << " " << path.size();
if( (count * 3) > path.size() - 1 ){
element = parent;
return true;
}else if (parent){
string remainingPath = path.substr((count * 3), path.size() - (count * 3));
element = (Poco::XML::Element*) parent->getNodeByPath(remainingPath);
if(!element){
element = prev;
ofLogWarning("ofxXmlPoco") << "setCurrentElement(): passed invalid path \"" << remainingPath << "\"";
return false;
}
}else{
ofLogWarning("ofxXmlPoco") << "setCurrentElement(): parent is nullptr.";
return false;
}
}else if(path.find("//") != string::npos){
// another: we're looking all over
Poco::XML::Element* prev = element;
element = (Poco::XML::Element*) document->getNodeByPath(path);
if(!element){
element = prev;
ofLogWarning("ofxXmlPoco") << "setCurrentElement(): passed invalid path \"" << path << "\"";
return false;
}
}else{
// another: we're actually looking down into the thing :)
Poco::XML::Element* prev = element;
element = (Poco::XML::Element*) element->getNodeByPath(path);
if(!element){
element = prev;
ofLogWarning("ofxXmlPoco") << "setCurrentElement(): passed invalid path \"" << path << "\"";
return false;
}
}
return true;
}
const Poco::XML::Element * ofxXmlPoco::getPocoElement() const {
return element;
}
Poco::XML::Element * ofxXmlPoco::getPocoElement(){
return element;
}
Poco::XML::Element* ofxXmlPoco::getPocoElement(const string& path){
string copy = path;
// does it have an attribute? just in case
std::size_t ind = copy.find("[@");
if(ind != string::npos){
copy = path.substr(0, ind);
}
if(element){
return (Poco::XML::Element*) element->getNodeByPath(copy);
}else{
ofLogWarning("ofxXmlPoco") << "getPocoElement(): no element to get yet ";
return nullptr;
}
}
const Poco::XML::Element* ofxXmlPoco::getPocoElement(const string& path) const{
string copy = path;
// does it have an attribute? just in case
std::size_t ind = copy.find("[@");
if(ind != string::npos){
copy = path.substr(0, ind);
}
if(element){
return (Poco::XML::Element*) element->getNodeByPath(copy);
}else{
ofLogWarning("ofxXmlPoco") << "getPocoElement(): no element to get yet ";
return nullptr;
}
}
Poco::XML::Document * ofxXmlPoco::getPocoDocument(){
return document;
}
const Poco::XML::Document * ofxXmlPoco::getPocoDocument() const {
return document;
}
string ofxXmlPoco::DOMErrorMessage(short msg){
switch(msg){
case 1:
return "INDEX_SIZE_ERR";
break; /// index or size is negative or greater than allowed value
case 2:
return "DOMSTRING_SIZE_ERR"; /// the specified range of text does not fit into a DOMString (not used)
break;
case 3:
return "HIERARCHY_REQUEST_ERR"; /// a node is inserted somewhere it doesn't belong
break;
case 4:
return "WRONG_DOCUMENT_ERR"; /// a node is used in a different document than the one that created it
break;
case 5:
return "INVALID_CHARACTER_ERR"; /// an invalid character is specified (not used)
break;
case 6:
return "NO_DATA_ALLOWED_ERR"; /// data is specified for a node which does not support data
break;
case 7:
return "NO_MODIFICATION_ALLOWED_ERR"; /// an attempt is made to modify an object where modifications are not allowed
break;
case 8:
return "NOT_FOUND_ERR"; /// an attempt was made to reference a node in a context where it does not exist
break;
case 9:
return "NOT_SUPPORTED_ERR"; /// the implementation does not support the type of object requested
break;
case 10:
return "INUSE_ATTRIBUTE_ERR"; /// an attempt is made to add an attribute that is already in use elsewhere
break;
case 11:
return "INVALID_STATE_ERR"; /// a parameter or an operation is not supported by the underlying object
break;
case 12:
return "SYNTAX_ERR"; /// an invalid or illegal string is specified
break;
case 13:
return "INVALID_MODIFICATION_ERR"; /// an attempt is made to modify the type of the underlying object
break;
case 14:
return "NAMESPACE_ERR"; /// an attempt is made to create or change an object in a way which is incorrect with regard to namespaces
break;
case 15:
return "INVALID_ACCESS_ERR"; /// an attempt is made to use an object that is not or is no longer usable
break;
}
return "DOM ERROR";
}
void ofSerialize(ofxXmlPoco & xml, const ofAbstractParameter & parameter){
if(!parameter.isSerializable()){
return;
}
string name = parameter.getEscapedName();
if(name == ""){
name = "UnknownName";
}
if(parameter.type() == typeid(ofParameterGroup).name()){
const ofParameterGroup & group = static_cast <const ofParameterGroup &>(parameter);
if(!xml.exists(name)){
xml.addChild(name);
ofLogVerbose("ofxXmlPoco") << "creating group " << name;
}
xml.setTo(name);
ofLogVerbose("ofxXmlPoco") << "group " << name;
for(auto & p: group){
ofSerialize(xml, *p);
}
ofLogVerbose("ofxXmlPoco") << "end group " << name;
xml.setToParent();
}else{
string value = parameter.toString();
if(!xml.exists(name)){
xml.addChild(name);
ofLogVerbose("ofxXmlPoco") << "creating tag " << name;
}
ofLogVerbose("ofxXmlPoco") << "setting tag " << name << ": " << value;
xml.setValue(name, value);
}
}
void ofDeserialize(const ofxXmlPoco & xml, ofAbstractParameter & parameter){
if(!parameter.isSerializable()){
return;
}
string name = parameter.getEscapedName();
if(parameter.type() == typeid(ofParameterGroup).name()){
ofParameterGroup & group = static_cast <ofParameterGroup &>(parameter);
if(const_cast<ofxXmlPoco&>(xml).setTo(name)){
for(auto & p: group){
ofDeserialize(xml, *p);
}
const_cast<ofxXmlPoco&>(xml).setToParent();
}
}else{
if(xml.exists(name)){
if(parameter.type() == typeid(ofParameter <int> ).name()){
parameter.cast <int>() = xml.getIntValue(name);
}else if(parameter.type() == typeid(ofParameter <float> ).name()){
parameter.cast <float>() = xml.getFloatValue(name);
}else if(parameter.type() == typeid(ofParameter <bool> ).name()){
parameter.cast <bool>() = xml.getBoolValue(name);
}else if(parameter.type() == typeid(ofParameter <int64_t> ).name()){
parameter.cast <int64_t>() = xml.getInt64Value(name);
}else if(parameter.type() == typeid(ofParameter <string> ).name()){
parameter.cast <string>() = xml.getValue(name);
}else{
parameter.fromString(xml.getValue(name));
}
}
}
}