-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathTBufferXML.cxx
3174 lines (2538 loc) · 89.4 KB
/
TBufferXML.cxx
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
992
993
994
995
996
997
998
999
1000
// @(#)root/:$Id: 5400e36954e1dc109fcfc306242c30234beb7312 $
// Author: Sergey Linev, Rene Brun 10.05.2004
/*************************************************************************
* Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
/**
\class TBufferXML
\ingroup IO
Class for serializing/deserializing object to/from xml.
The simple way to create XML representation is:
~~~{.cpp}
TNamed *obj = new TNamed("name", "title");
TString xml = TBufferXML::ToXML(obj);
~~~
Produced xml can be decoded into new object:
~~~{.cpp}
TNamed *obj2 = nullptr;
TBufferXML::FromXML(obj2, xml);
~~~
TBufferXML class uses streaming mechanism, provided by ROOT system,
therefore most of ROOT and user classes can be stored to xml.
There are limitations for complex objects like TTree, which can not be converted to xml.
*/
#include "TBufferXML.h"
#include "Compression.h"
#include "TXMLFile.h"
#include "TROOT.h"
#include "TError.h"
#include "TClass.h"
#include "TClassTable.h"
#include "TDataType.h"
#include "TExMap.h"
#include "TStreamerInfo.h"
#include "TStreamerElement.h"
#include "TMemberStreamer.h"
#include "TStreamer.h"
#include "RZip.h"
#include "snprintf.h"
#include <memory>
ClassImp(TBufferXML);
////////////////////////////////////////////////////////////////////////////////
/// Creates buffer object to serialize/deserialize data to/from xml.
/// Mode should be either TBuffer::kRead or TBuffer::kWrite.
TBufferXML::TBufferXML(TBuffer::EMode mode) : TBufferText(mode)
{
}
////////////////////////////////////////////////////////////////////////////////
/// Creates buffer object to serialize/deserialize data to/from xml.
/// This constructor should be used, if data from buffer supposed to be stored in file.
/// Mode should be either TBuffer::kRead or TBuffer::kWrite.
TBufferXML::TBufferXML(TBuffer::EMode mode, TXMLFile *file)
: TBufferText(mode, file), TXMLSetup(*file)
{
// this is for the case when StreamerInfo reads elements from
// buffer as ReadFastArray. When it checks if size of buffer is
// too small and skip reading. Actually, more improved method should
// be used here.
if (XmlFile()) {
SetXML(XmlFile()->XML());
SetCompressionSettings(XmlFile()->GetCompressionSettings());
SetIOVersion(XmlFile()->GetIOVersion());
}
}
////////////////////////////////////////////////////////////////////////////////
/// Destroy xml buffer.
TBufferXML::~TBufferXML()
{
}
////////////////////////////////////////////////////////////////////////////////
/// Returns pointer to TXMLFile object.
/// Access to file is necessary to produce unique identifier for object references.
TXMLFile *TBufferXML::XmlFile()
{
return dynamic_cast<TXMLFile *>(GetParent());
}
////////////////////////////////////////////////////////////////////////////////
/// Converts object, inherited from TObject class, to XML string
/// GenericLayout defines layout choice for XML file
/// UseNamespaces allow XML namespaces.
/// See TXMLSetup class for details
TString TBufferXML::ConvertToXML(const TObject *obj, Bool_t GenericLayout, Bool_t UseNamespaces)
{
TClass *clActual = nullptr;
void *ptr = (void *)obj;
if (obj) {
clActual = TObject::Class()->GetActualClass(obj);
if (!clActual)
clActual = TObject::Class();
else if (clActual != TObject::Class())
ptr = (void *)((Longptr_t)obj - clActual->GetBaseClassOffset(TObject::Class()));
}
return ConvertToXML(ptr, clActual, GenericLayout, UseNamespaces);
}
////////////////////////////////////////////////////////////////////////////////
/// Converts any type of object to XML string.
/// GenericLayout defines layout choice for XML file
/// UseNamespaces allow XML namespaces.
/// See TXMLSetup class for details
TString TBufferXML::ConvertToXML(const void *obj, const TClass *cl, Bool_t GenericLayout, Bool_t UseNamespaces)
{
TXMLEngine xml;
TBufferXML buf(TBuffer::kWrite);
buf.SetXML(&xml);
buf.InitMap();
buf.SetXmlLayout(GenericLayout ? TXMLSetup::kGeneralized : TXMLSetup::kSpecialized);
buf.SetUseNamespaces(UseNamespaces);
XMLNodePointer_t xmlnode = buf.XmlWriteAny(obj, cl);
TString res;
xml.SaveSingleNode(xmlnode, &res);
xml.FreeNode(xmlnode);
return res;
}
////////////////////////////////////////////////////////////////////////////////
/// Read object from XML, produced by ConvertToXML() method.
/// If object does not inherit from TObject class, return 0.
/// GenericLayout and UseNamespaces should be the same as in ConvertToXML()
TObject *TBufferXML::ConvertFromXML(const char *str, Bool_t GenericLayout, Bool_t UseNamespaces)
{
TClass *cl = nullptr;
void *obj = ConvertFromXMLAny(str, &cl, GenericLayout, UseNamespaces);
if (!cl || !obj)
return nullptr;
Int_t delta = cl->GetBaseClassOffset(TObject::Class());
if (delta < 0) {
cl->Destructor(obj);
return nullptr;
}
return (TObject *)(((char *)obj) + delta);
}
////////////////////////////////////////////////////////////////////////////////
/// Read object of any class from XML, produced by ConvertToXML() method.
/// If cl!=0, return actual class of object.
/// GenericLayout and UseNamespaces should be the same as in ConvertToXML()
void *TBufferXML::ConvertFromXMLAny(const char *str, TClass **cl, Bool_t GenericLayout, Bool_t UseNamespaces)
{
TXMLEngine xml;
TBufferXML buf(TBuffer::kRead);
buf.SetXML(&xml);
buf.InitMap();
buf.SetXmlLayout(GenericLayout ? TXMLSetup::kGeneralized : TXMLSetup::kSpecialized);
buf.SetUseNamespaces(UseNamespaces);
XMLNodePointer_t xmlnode = xml.ReadSingleNode(str);
void *obj = buf.XmlReadAny(xmlnode, nullptr, cl);
xml.FreeNode(xmlnode);
return obj;
}
////////////////////////////////////////////////////////////////////////////////
/// Convert from XML and check if object derived from specified class
/// When possible, cast to given class
void *TBufferXML::ConvertFromXMLChecked(const char *xml, const TClass *expectedClass, Bool_t GenericLayout,
Bool_t UseNamespaces)
{
TClass *objClass = nullptr;
void *res = ConvertFromXMLAny(xml, &objClass, GenericLayout, UseNamespaces);
if (!res || !objClass)
return nullptr;
if (objClass == expectedClass)
return res;
Int_t offset = objClass->GetBaseClassOffset(expectedClass);
if (offset < 0) {
::Error("TBufferXML::ConvertFromXMLChecked", "expected class %s is not base for read class %s",
expectedClass->GetName(), objClass->GetName());
objClass->Destructor(res);
return nullptr;
}
return (char *)res - offset;
}
////////////////////////////////////////////////////////////////////////////////
/// Convert object of any class to xml structures
/// Return pointer on top xml element
XMLNodePointer_t TBufferXML::XmlWriteAny(const void *obj, const TClass *cl)
{
fErrorFlag = 0;
if (!fXML)
return nullptr;
XMLNodePointer_t res = XmlWriteObject(obj, cl, kTRUE);
return res;
}
////////////////////////////////////////////////////////////////////////////////
/// Recreate object from xml structure.
/// Return pointer to read object.
/// if (cl!=0) returns pointer to class of object
void *TBufferXML::XmlReadAny(XMLNodePointer_t node, void *obj, TClass **cl)
{
if (!node)
return nullptr;
if (cl)
*cl = nullptr;
fErrorFlag = 0;
if (!fXML)
return nullptr;
PushStack(node, kTRUE);
void *res = XmlReadObject(obj, cl);
PopStack();
return res;
}
// TXMLStackObj is used to keep stack of object hierarchy,
// stored in TBuffer. For example, data for parent class(es)
// stored in subnodes, but initial object node will be kept.
class TXMLStackObj {
public:
TXMLStackObj(XMLNodePointer_t node) : fNode(node)
{
}
~TXMLStackObj()
{
if (fIsElemOwner)
delete fElem;
}
Bool_t IsStreamerInfo() const { return fIsStreamerInfo; }
XMLNodePointer_t fNode{nullptr};
TStreamerInfo *fInfo{nullptr};
TStreamerElement *fElem{nullptr};
Int_t fElemNumber{0};
Bool_t fCompressedClassNode{kFALSE};
XMLNsPointer_t fClassNs{nullptr};
Bool_t fIsStreamerInfo{kFALSE};
Bool_t fIsElemOwner{kFALSE};
};
////////////////////////////////////////////////////////////////////////////////
/// Add new level to xml stack.
TXMLStackObj *TBufferXML::PushStack(XMLNodePointer_t current, Bool_t simple)
{
if (IsReading() && !simple) {
current = fXML->GetChild(current);
fXML->SkipEmpty(current);
}
fStack.emplace_back(std::make_unique<TXMLStackObj>(current));
return fStack.back().get();
}
////////////////////////////////////////////////////////////////////////////////
/// Remove one level from xml stack.
TXMLStackObj *TBufferXML::PopStack()
{
if (fStack.size() > 0)
fStack.pop_back();
return fStack.size() > 0 ? fStack.back().get() : nullptr;
}
////////////////////////////////////////////////////////////////////////////////
/// Return pointer on current xml node.
XMLNodePointer_t TBufferXML::StackNode()
{
TXMLStackObj *stack = Stack();
return stack ? stack->fNode : nullptr;
}
////////////////////////////////////////////////////////////////////////////////
/// Shift stack node to next.
void TBufferXML::ShiftStack(const char *errinfo)
{
TXMLStackObj *stack = Stack();
if (stack) {
fXML->ShiftToNext(stack->fNode);
if (gDebug > 4)
Info("ShiftStack", "%s to node %s", errinfo, fXML->GetNodeName(stack->fNode));
}
}
////////////////////////////////////////////////////////////////////////////////
/// See comments for function SetCompressionSettings.
void TBufferXML::SetCompressionAlgorithm(Int_t algorithm)
{
if (algorithm < 0 || algorithm >= ROOT::RCompressionSetting::EAlgorithm::kUndefined)
algorithm = 0;
if (fCompressLevel < 0) {
fCompressLevel = 100 * algorithm + ROOT::RCompressionSetting::ELevel::kUseMin;
} else {
int level = fCompressLevel % 100;
fCompressLevel = 100 * algorithm + level;
}
}
////////////////////////////////////////////////////////////////////////////////
/// See comments for function SetCompressionSettings.
void TBufferXML::SetCompressionLevel(Int_t level)
{
if (level < 0)
level = 0;
if (level > 99)
level = 99;
if (fCompressLevel < 0) {
// if the algorithm is not defined yet use 0 as a default
fCompressLevel = level;
} else {
int algorithm = fCompressLevel / 100;
if (algorithm >= ROOT::RCompressionSetting::EAlgorithm::kUndefined)
algorithm = 0;
fCompressLevel = 100 * algorithm + level;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Used to specify the compression level and algorithm.
///
/// See TFile constructor for the details.
void TBufferXML::SetCompressionSettings(Int_t settings)
{
fCompressLevel = settings;
}
////////////////////////////////////////////////////////////////////////////////
/// Write binary data block from buffer to xml.
/// This data can be produced only by direct call of TBuffer::WriteBuf() functions.
void TBufferXML::XmlWriteBlock(XMLNodePointer_t node)
{
if (!node || (Length() == 0))
return;
const char *src = Buffer();
int srcSize = Length();
char *fZipBuffer = nullptr;
Int_t compressionLevel = GetCompressionLevel();
ROOT::RCompressionSetting::EAlgorithm::EValues compressionAlgorithm =
static_cast<ROOT::RCompressionSetting::EAlgorithm::EValues>(GetCompressionAlgorithm());
if ((Length() > 512) && (compressionLevel > 0)) {
int zipBufferSize = Length();
fZipBuffer = new char[zipBufferSize];
int dataSize = Length();
int compressedSize = 0;
R__zipMultipleAlgorithm(compressionLevel, &dataSize, Buffer(), &zipBufferSize, fZipBuffer, &compressedSize,
compressionAlgorithm);
if (compressedSize > 0) {
src = fZipBuffer;
srcSize = compressedSize;
} else {
delete[] fZipBuffer;
fZipBuffer = nullptr;
}
}
TString res;
constexpr std::size_t sbufSize = 500;
char sbuf[sbufSize];
int block = 0;
char *tgt = sbuf;
int srcCnt = 0;
while (srcCnt++ < srcSize) {
tgt += snprintf(tgt, sbufSize - (tgt - sbuf), " %02x", (unsigned char)*src);
src++;
if (block++ == 100) {
res += sbuf;
block = 0;
tgt = sbuf;
}
}
if (block > 0)
res += sbuf;
XMLNodePointer_t blocknode = fXML->NewChild(node, nullptr, xmlio::XmlBlock, res);
fXML->NewIntAttr(blocknode, xmlio::Size, Length());
if (fZipBuffer) {
fXML->NewIntAttr(blocknode, xmlio::Zip, srcSize);
delete[] fZipBuffer;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Read binary block of data from xml.
void TBufferXML::XmlReadBlock(XMLNodePointer_t blocknode)
{
if (!blocknode)
return;
Int_t blockSize = fXML->GetIntAttr(blocknode, xmlio::Size);
Bool_t blockCompressed = fXML->HasAttr(blocknode, xmlio::Zip);
char *fUnzipBuffer = nullptr;
if (gDebug > 2)
Info("XmlReadBlock", "Block size = %d, Length = %d, Compressed = %d", blockSize, Length(), blockCompressed);
if (blockSize > BufferSize())
Expand(blockSize);
char *tgt = Buffer();
Int_t readSize = blockSize;
TString content = fXML->GetNodeContent(blocknode);
if (blockCompressed) {
Int_t zipSize = fXML->GetIntAttr(blocknode, xmlio::Zip);
fUnzipBuffer = new char[zipSize];
tgt = fUnzipBuffer;
readSize = zipSize;
}
char *ptr = (char *)content.Data();
if (gDebug > 3)
Info("XmlReadBlock", "Content %s", ptr);
for (int i = 0; i < readSize; i++) {
while ((*ptr < 48) || ((*ptr > 57) && (*ptr < 97)) || (*ptr > 102))
ptr++;
int b_hi = (*ptr > 57) ? *ptr - 87 : *ptr - 48;
ptr++;
int b_lo = (*ptr > 57) ? *ptr - 87 : *ptr - 48;
ptr++;
*tgt = b_hi * 16 + b_lo;
tgt++;
if (gDebug > 4)
Info("XmlReadBlock", " Buf[%d] = %d", i, b_hi * 16 + b_lo);
}
if (fUnzipBuffer) {
int srcsize(0), tgtsize(0), unzipRes(0);
int status = R__unzip_header(&srcsize, (UChar_t *)fUnzipBuffer, &tgtsize);
if (status == 0)
R__unzip(&readSize, (unsigned char *)fUnzipBuffer, &blockSize, (unsigned char *)Buffer(), &unzipRes);
if (status != 0 || unzipRes != blockSize)
Error("XmlReadBlock", "Decompression error %d", unzipRes);
else if (gDebug > 2)
Info("XmlReadBlock", "Unzip ok");
delete[] fUnzipBuffer;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Add "ptr" attribute to node, if ptr is null or
/// if ptr is pointer on object, which is already saved in buffer
/// Automatically add "ref" attribute to node, where referenced object is stored
Bool_t TBufferXML::ProcessPointer(const void *ptr, XMLNodePointer_t node)
{
if (!node)
return kFALSE;
TString refvalue;
if (!ptr) {
refvalue = xmlio::Null; // null
} else {
XMLNodePointer_t refnode = (XMLNodePointer_t)(Longptr_t)GetObjectTag(ptr);
if (!refnode)
return kFALSE;
if (fXML->HasAttr(refnode, xmlio::Ref)) {
refvalue = fXML->GetAttr(refnode, xmlio::Ref);
} else {
refvalue = xmlio::IdBase;
if (XmlFile())
refvalue += XmlFile()->GetNextRefCounter();
else
refvalue += GetNextRefCounter();
fXML->NewAttr(refnode, nullptr, xmlio::Ref, refvalue.Data());
}
}
if (refvalue.Length() > 0) {
fXML->NewAttr(node, nullptr, xmlio::Ptr, refvalue.Data());
return kTRUE;
}
return kFALSE;
}
////////////////////////////////////////////////////////////////////////////////
/// Searches for "ptr" attribute and returns pointer to object and class,
/// if "ptr" attribute reference to read object
Bool_t TBufferXML::ExtractPointer(XMLNodePointer_t node, void *&ptr, TClass *&cl)
{
cl = nullptr;
if (!fXML->HasAttr(node, xmlio::Ptr))
return kFALSE;
const char *ptrid = fXML->GetAttr(node, xmlio::Ptr);
if (!ptrid)
return kFALSE;
// null
if (strcmp(ptrid, xmlio::Null) == 0) {
ptr = nullptr;
return kTRUE;
}
if (strncmp(ptrid, xmlio::IdBase, strlen(xmlio::IdBase)) != 0) {
Error("ExtractPointer", "Pointer tag %s not started from %s", ptrid, xmlio::IdBase);
return kFALSE;
}
Int_t id = TString(ptrid + strlen(xmlio::IdBase)).Atoi();
GetMappedObject(id + 1, ptr, cl);
if (!ptr || !cl)
Error("ExtractPointer", "not found ptr %s result %p %s", ptrid, ptr, (cl ? cl->GetName() : "null"));
return ptr && cl;
}
////////////////////////////////////////////////////////////////////////////////
/// Analyze if node has "ref" attribute and register it to object map
void TBufferXML::ExtractReference(XMLNodePointer_t node, const void *ptr, const TClass *cl)
{
if (!node || !ptr)
return;
const char *refid = fXML->GetAttr(node, xmlio::Ref);
if (!refid)
return;
if (strncmp(refid, xmlio::IdBase, strlen(xmlio::IdBase)) != 0) {
Error("ExtractReference", "Reference tag %s not started from %s", refid, xmlio::IdBase);
return;
}
Int_t id = TString(refid + strlen(xmlio::IdBase)).Atoi();
MapObject(ptr, cl, id + 1);
if (gDebug > 2)
Info("ExtractReference", "Find reference %s for object %p class %s", refid, ptr, (cl ? cl->GetName() : "null"));
}
////////////////////////////////////////////////////////////////////////////////
/// Check if node has specified name
Bool_t TBufferXML::VerifyNode(XMLNodePointer_t node, const char *name, const char *errinfo)
{
if (!name || !node)
return kFALSE;
if (strcmp(fXML->GetNodeName(node), name) != 0) {
if (errinfo) {
Error("VerifyNode", "Reading XML file (%s). Get: %s, expects: %s", errinfo, fXML->GetNodeName(node), name);
fErrorFlag = 1;
}
return kFALSE;
}
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Check, if stack node has specified name
Bool_t TBufferXML::VerifyStackNode(const char *name, const char *errinfo)
{
return VerifyNode(StackNode(), name, errinfo);
}
////////////////////////////////////////////////////////////////////////////////
/// Checks, that attribute of specified name exists and has specified value
Bool_t TBufferXML::VerifyAttr(XMLNodePointer_t node, const char *name, const char *value, const char *errinfo)
{
if (!node || !name || !value)
return kFALSE;
const char *cont = fXML->GetAttr(node, name);
if ((!cont || (strcmp(cont, value) != 0))) {
if (errinfo) {
Error("VerifyAttr", "%s : attr %s = %s, expected: %s", errinfo, name, cont, value);
fErrorFlag = 1;
}
return kFALSE;
}
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Checks stack attribute
Bool_t TBufferXML::VerifyStackAttr(const char *name, const char *value, const char *errinfo)
{
return VerifyAttr(StackNode(), name, value, errinfo);
}
////////////////////////////////////////////////////////////////////////////////
/// Create item node of specified name
XMLNodePointer_t TBufferXML::CreateItemNode(const char *name)
{
XMLNodePointer_t node = nullptr;
if (GetXmlLayout() == kGeneralized) {
node = fXML->NewChild(StackNode(), nullptr, xmlio::Item);
fXML->NewAttr(node, nullptr, xmlio::Name, name);
} else
node = fXML->NewChild(StackNode(), nullptr, name);
return node;
}
////////////////////////////////////////////////////////////////////////////////
/// Checks, if stack node is item and has specified name
Bool_t TBufferXML::VerifyItemNode(const char *name, const char *errinfo)
{
Bool_t res = kTRUE;
if (GetXmlLayout() == kGeneralized)
res = VerifyStackNode(xmlio::Item, errinfo) && VerifyStackAttr(xmlio::Name, name, errinfo);
else
res = VerifyStackNode(name, errinfo);
return res;
}
////////////////////////////////////////////////////////////////////////////////
/// Create xml node correspondent to TStreamerElement object
void TBufferXML::CreateElemNode(const TStreamerElement *elem)
{
XMLNodePointer_t elemnode = nullptr;
const char *elemxmlname = XmlGetElementName(elem);
if (GetXmlLayout() == kGeneralized) {
elemnode = fXML->NewChild(StackNode(), nullptr, xmlio::Member);
fXML->NewAttr(elemnode, nullptr, xmlio::Name, elemxmlname);
} else {
// take namesapce for element only if it is not a base class or class name
XMLNsPointer_t ns = Stack()->fClassNs;
if ((elem->GetType() == TStreamerInfo::kBase) ||
((elem->GetType() == TStreamerInfo::kTNamed) && !strcmp(elem->GetName(), TNamed::Class()->GetName())) ||
((elem->GetType() == TStreamerInfo::kTObject) && !strcmp(elem->GetName(), TObject::Class()->GetName())) ||
((elem->GetType() == TStreamerInfo::kTString) && !strcmp(elem->GetName(), TString::Class()->GetName())))
ns = nullptr;
elemnode = fXML->NewChild(StackNode(), ns, elemxmlname);
}
TXMLStackObj *curr = PushStack(elemnode);
curr->fElem = (TStreamerElement *)elem;
}
////////////////////////////////////////////////////////////////////////////////
/// Checks if stack node correspond to TStreamerElement object
Bool_t TBufferXML::VerifyElemNode(const TStreamerElement *elem)
{
const char *elemxmlname = XmlGetElementName(elem);
if (GetXmlLayout() == kGeneralized) {
if (!VerifyStackNode(xmlio::Member))
return kFALSE;
if (!VerifyStackAttr(xmlio::Name, elemxmlname))
return kFALSE;
} else {
if (!VerifyStackNode(elemxmlname))
return kFALSE;
}
PerformPreProcessing(elem, StackNode());
TXMLStackObj *curr = PushStack(StackNode()); // set pointer to first data inside element
curr->fElem = (TStreamerElement *)elem;
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Write object to buffer
/// If object was written before, only pointer will be stored
/// Return pointer to top xml node, representing object
XMLNodePointer_t TBufferXML::XmlWriteObject(const void *obj, const TClass *cl, Bool_t cacheReuse)
{
XMLNodePointer_t objnode = fXML->NewChild(StackNode(), nullptr, xmlio::Object);
if (!cl)
obj = nullptr;
if (ProcessPointer(obj, objnode))
return objnode;
TString clname = XmlConvertClassName(cl->GetName());
fXML->NewAttr(objnode, nullptr, xmlio::ObjClass, clname);
if (cacheReuse)
fMap->Add(Void_Hash(obj), (Longptr_t)obj, (Longptr_t)objnode);
PushStack(objnode);
((TClass *)cl)->Streamer((void *)obj, *this);
PopStack();
if (gDebug > 1)
Info("XmlWriteObject", "Done write for class: %s", cl ? cl->GetName() : "null");
return objnode;
}
////////////////////////////////////////////////////////////////////////////////
/// Read object from the buffer
void *TBufferXML::XmlReadObject(void *obj, TClass **cl)
{
if (cl)
*cl = nullptr;
XMLNodePointer_t objnode = StackNode();
if (fErrorFlag > 0)
return obj;
if (!objnode)
return obj;
if (!VerifyNode(objnode, xmlio::Object, "XmlReadObjectNew"))
return obj;
TClass *objClass = nullptr;
if (ExtractPointer(objnode, obj, objClass)) {
ShiftStack("readobjptr");
if (cl)
*cl = objClass;
return obj;
}
TString clname = fXML->GetAttr(objnode, xmlio::ObjClass);
objClass = XmlDefineClass(clname);
if (objClass == TDirectory::Class())
objClass = TDirectoryFile::Class();
if (!objClass) {
Error("XmlReadObject", "Cannot find class %s", clname.Data());
ShiftStack("readobjerr");
return obj;
}
if (gDebug > 1)
Info("XmlReadObject", "Reading object of class %s", clname.Data());
if (!obj)
obj = objClass->New();
ExtractReference(objnode, obj, objClass);
PushStack(objnode);
objClass->Streamer((void *)obj, *this);
PopStack();
ShiftStack("readobj");
if (gDebug > 1)
Info("XmlReadObject", "Reading object of class %s done", clname.Data());
if (cl)
*cl = objClass;
return obj;
}
////////////////////////////////////////////////////////////////////////////////
/// Function is called from TStreamerInfo WriteBuffer and ReadBuffer functions
/// and indent new level in xml structure.
/// This call indicates, that TStreamerInfo functions starts streaming
/// object data of correspondent class
void TBufferXML::IncrementLevel(TVirtualStreamerInfo *info)
{
WorkWithClass((TStreamerInfo *)info);
}
////////////////////////////////////////////////////////////////////////////////
/// Prepares buffer to stream data of specified class.
void TBufferXML::WorkWithClass(TStreamerInfo *sinfo, const TClass *cl)
{
fCanUseCompact = kFALSE;
if (sinfo)
cl = sinfo->GetClass();
if (!cl)
return;
TString clname = XmlConvertClassName(cl->GetName());
if (gDebug > 2)
Info("IncrementLevel", "Class: %s", clname.Data());
Bool_t compressClassNode = (fExpectedBaseClass == cl);
fExpectedBaseClass = nullptr;
TXMLStackObj *stack = Stack();
if (IsWriting()) {
XMLNodePointer_t classnode = nullptr;
if (compressClassNode) {
classnode = StackNode();
} else {
if (GetXmlLayout() == kGeneralized) {
classnode = fXML->NewChild(StackNode(), nullptr, xmlio::Class);
fXML->NewAttr(classnode, nullptr, "name", clname);
} else
classnode = fXML->NewChild(StackNode(), nullptr, clname);
stack = PushStack(classnode);
}
if (fVersionBuf >= -1) {
if (fVersionBuf == -1)
fVersionBuf = 1;
fXML->NewIntAttr(classnode, xmlio::ClassVersion, fVersionBuf);
fVersionBuf = -111;
}
if (IsUseNamespaces() && (GetXmlLayout() != kGeneralized))
stack->fClassNs = fXML->NewNS(classnode, XmlClassNameSpaceRef(cl), clname);
} else {
if (!compressClassNode) {
if (GetXmlLayout() == kGeneralized) {
if (!VerifyStackNode(xmlio::Class, "StartInfo"))
return;
if (!VerifyStackAttr("name", clname, "StartInfo"))
return;
} else if (!VerifyStackNode(clname, "StartInfo"))
return;
stack = PushStack(StackNode());
}
}
stack->fCompressedClassNode = compressClassNode;
stack->fInfo = sinfo;
stack->fIsStreamerInfo = kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Function is called from TStreamerInfo WriteBuffer and ReadBuffer functions
/// and decrease level in xml structure.
void TBufferXML::DecrementLevel(TVirtualStreamerInfo *info)
{
CheckVersionBuf();
fCanUseCompact = kFALSE;
if (gDebug > 2)
Info("DecrementLevel", "Class: %s", (info ? info->GetClass()->GetName() : "custom"));
TXMLStackObj *stack = Stack();
if (!stack->IsStreamerInfo()) {
PerformPostProcessing();
stack = PopStack(); // remove stack of last element
}
if (stack->fCompressedClassNode) {
stack->fInfo = nullptr;
stack->fIsStreamerInfo = kFALSE;
stack->fCompressedClassNode = kFALSE;
} else {
PopStack(); // back from data of stack info
if (IsReading())
ShiftStack("declevel"); // shift to next element after streamer info
}
}
////////////////////////////////////////////////////////////////////////////////
/// Function is called from TStreamerInfo WriteBuffer and ReadBuffer functions
/// and add/verify next element of xml structure
/// This calls allows separate data, correspondent to one class member, from another
void TBufferXML::SetStreamerElementNumber(TStreamerElement *elem, Int_t comptype)
{
WorkWithElement(elem, comptype);
}
////////////////////////////////////////////////////////////////////////////////
/// This function is a part of SetStreamerElementNumber method.
/// It is introduced for reading of data for specified data member of class.
/// Used also in ReadFastArray methods to resolve problem of compressed data,
/// when several data members of the same basic type streamed with single ...FastArray call
void TBufferXML::WorkWithElement(TStreamerElement *elem, Int_t comp_type)
{
CheckVersionBuf();
fCanUseCompact = kFALSE;
fExpectedBaseClass = nullptr;
TXMLStackObj *stack = Stack();
if (!stack) {
Error("SetStreamerElementNumber", "stack is empty");
return;
}
if (!stack->IsStreamerInfo()) { // this is not a first element
PerformPostProcessing();
PopStack(); // go level back
if (IsReading())
ShiftStack("startelem"); // shift to next element, only for reading
stack = Stack();
}
if (!stack) {
Error("SetStreamerElementNumber", "Lost of stack");
return;
}
if (!elem) {
Error("SetStreamerElementNumber", "Problem in Inc/Dec level");
return;