Logo ROOT   6.12/07
Reference Guide
TKeyXML.cxx
Go to the documentation of this file.
1 // @(#)root/xml:$Id$
2 // Author: Sergey Linev, Rene Brun 10.05.2004
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //________________________________________________________________________
13 //
14 // TKeyXML is represents one block of data in TXMLFile
15 // Normally this block corresponds to data of single object like histogram,
16 // TObjArray and so on.
17 //________________________________________________________________________
18 
19 #include "TKeyXML.h"
20 
21 #include "TBufferXML.h"
22 #include "TXMLFile.h"
23 #include "TClass.h"
24 #include "TROOT.h"
25 #include "TBrowser.h"
26 
28 
29 ////////////////////////////////////////////////////////////////////////////////
30 /// default constructor
31 
32 TKeyXML::TKeyXML() : TKey(), fKeyNode(0), fKeyId(0), fSubdir(kFALSE)
33 {
34 }
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 /// Creates TKeyXML and convert object data to xml structures
38 
39 TKeyXML::TKeyXML(TDirectory *mother, Long64_t keyid, const TObject *obj, const char *name, const char *title)
40  : TKey(mother), fKeyNode(0), fKeyId(keyid), fSubdir(kFALSE)
41 {
42  if (name)
43  SetName(name);
44  else if (obj != 0) {
45  SetName(obj->GetName());
46  fClassName = obj->ClassName();
47  } else
48  SetName("Noname");
49 
50  if (title)
51  SetTitle(title);
52 
53  fCycle = GetMotherDir()->AppendKey(this);
54 
55  TXMLEngine *xml = XMLEngine();
56  if (xml != 0)
57  fKeyNode = xml->NewChild(0, 0, xmlio::Xmlkey, 0);
58 
59  fDatime.Set();
60 
61  StoreObject(obj, 0, kTRUE);
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Creates TKeyXML and convert object data to xml structures
66 
67 TKeyXML::TKeyXML(TDirectory *mother, Long64_t keyid, const void *obj, const TClass *cl, const char *name,
68  const char *title)
69  : TKey(mother), fKeyNode(0), fKeyId(keyid), fSubdir(kFALSE)
70 {
71  if (name && *name)
72  SetName(name);
73  else
74  SetName(cl ? cl->GetName() : "Noname");
75 
76  if (title)
77  SetTitle(title);
78 
79  fCycle = GetMotherDir()->AppendKey(this);
80 
81  TXMLEngine *xml = XMLEngine();
82  if (xml != 0)
83  fKeyNode = xml->NewChild(0, 0, xmlio::Xmlkey, 0);
84 
85  fDatime.Set();
86 
87  StoreObject(obj, cl, kFALSE);
88 }
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 /// Creates TKeyXML and takes ownership over xml node, from which object can be restored
92 
94  : TKey(mother), fKeyNode(keynode), fKeyId(keyid), fSubdir(kFALSE)
95 {
96  TXMLEngine *xml = XMLEngine();
97 
98  SetName(xml->GetAttr(keynode, xmlio::Name));
99 
100  if (xml->HasAttr(keynode, xmlio::Title))
101  SetTitle(xml->GetAttr(keynode, xmlio::Title));
102 
103  fCycle = xml->GetIntAttr(keynode, xmlio::Cycle);
104 
105  if (xml->HasAttr(keynode, xmlio::CreateTm)) {
106  TDatime tm(xml->GetAttr(keynode, xmlio::CreateTm));
107  fDatime = tm;
108  }
109 
110  XMLNodePointer_t objnode = xml->GetChild(keynode);
111  xml->SkipEmpty(objnode);
112 
113  fClassName = xml->GetAttr(objnode, xmlio::ObjClass);
114 }
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// TKeyXML destructor
118 
120 {
121  if (fKeyNode) {
122  TXMLEngine *xml = XMLEngine();
123  if (xml) {
124  xml->FreeNode(fKeyNode);
125  } else {
126  TXMLEngine xml_;
127  xml_.FreeNode(fKeyNode);
128  }
129  }
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// Delete key from current directory
134 /// Note: TKeyXML object is not deleted. You still have to call "delete key"
135 
136 void TKeyXML::Delete(Option_t * /*option*/)
137 {
138  TXMLEngine *xml = XMLEngine();
139  if (fKeyNode && xml) {
140  xml->FreeNode(fKeyNode);
141  fKeyNode = 0;
142  }
143 
144  fMotherDir->GetListOfKeys()->Remove(this);
145 }
146 
147 ////////////////////////////////////////////////////////////////////////////////
148 /// Stores keys attributes in key node
149 
151 {
152  TXMLEngine *xml = XMLEngine();
153  TXMLFile *f = (TXMLFile *)GetFile();
154  if ((f == 0) || (xml == 0) || (fKeyNode == 0))
155  return;
156 
157  xml->NewAttr(fKeyNode, 0, xmlio::Name, GetName());
158 
160 
161  if (f->GetIOVersion() > 1) {
162  if (strlen(GetTitle()) > 0)
163  xml->NewAttr(fKeyNode, 0, xmlio::Title, GetTitle());
165  }
166 }
167 
168 ////////////////////////////////////////////////////////////////////////////////
169 /// convert object to xml structure and keep this structure in key
170 
171 void TKeyXML::StoreObject(const void *obj, const TClass *cl, Bool_t check_tobj)
172 {
173  TXMLFile *f = (TXMLFile *)GetFile();
174  TXMLEngine *xml = XMLEngine();
175  if ((f == 0) || (xml == 0) || (fKeyNode == 0))
176  return;
177 
178  if (obj && check_tobj) {
179  TClass *actual = TObject::Class()->GetActualClass((TObject *)obj);
180  if (!actual)
181  actual = TObject::Class();
182  else if (actual != TObject::Class())
183  obj = (void *)((Long_t)obj - actual->GetBaseClassOffset(TObject::Class()));
184  cl = actual;
185  }
186 
188 
189  TBufferXML buffer(TBuffer::kWrite, f);
190  if (f->GetIOVersion() == 1)
192 
193  XMLNodePointer_t node = buffer.XmlWriteAny(obj, cl);
194 
195  if (node != 0)
196  xml->AddChildFirst(fKeyNode, node);
197 
198  buffer.XmlWriteBlock(fKeyNode);
199 
200  if (cl)
201  fClassName = cl->GetName();
202 }
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// update key attributes in key node
206 
208 {
209  TXMLEngine *xml = XMLEngine();
210  if ((xml == 0) || (fKeyNode == 0))
211  return;
212 
213  xml->FreeAllAttr(fKeyNode);
214 
216 }
217 
218 ////////////////////////////////////////////////////////////////////////////////
219 /// updates object, stored in the node
220 /// Used for TDirectory data update
221 
223 {
224  TXMLFile *f = (TXMLFile *)GetFile();
225  TXMLEngine *xml = XMLEngine();
226  if ((f == 0) || (xml == 0) || (obj == 0) || (fKeyNode == 0))
227  return;
228 
229  XMLNodePointer_t objnode = xml->GetChild(fKeyNode);
230  xml->SkipEmpty(objnode);
231 
232  if (objnode == 0)
233  return;
234 
235  xml->UnlinkNode(objnode);
236  xml->FreeNode(objnode);
237 
238  xml->FreeAllAttr(fKeyNode);
239 
240  StoreObject(obj, 0, kTRUE);
241 }
242 
243 ////////////////////////////////////////////////////////////////////////////////
244 /// To read an object from the file.
245 /// The object associated to this key is read from the file into memory.
246 /// Before invoking this function, obj has been created via the
247 /// default constructor.
248 
250 {
251  if (tobj == 0)
252  return 0;
253 
254  void *res = XmlReadAny(tobj, 0);
255 
256  return res == 0 ? 0 : 1;
257 }
258 
259 ////////////////////////////////////////////////////////////////////////////////
260 /// read object derived from TObject class, from key
261 /// if it is not TObject or in case of error, return 0
262 
264 {
265  TObject *tobj = (TObject *)XmlReadAny(0, TObject::Class());
266 
267  if (tobj != 0) {
268  if (gROOT->GetForceStyle())
269  tobj->UseCurrentStyle();
270  if (tobj->IsA() == TDirectoryFile::Class()) {
271  TDirectoryFile *dir = (TDirectoryFile *)tobj;
272  dir->SetName(GetName());
273  dir->SetTitle(GetTitle());
274  dir->SetSeekDir(GetKeyId());
275  // set mother before reading keys
276  dir->SetMother(fMotherDir);
277  dir->ReadKeys();
278  fMotherDir->Append(dir);
279  fSubdir = kTRUE;
280  }
281  }
282 
283  return tobj;
284 }
285 
286 ////////////////////////////////////////////////////////////////////////////////
287 /// read object derived from TObject class, from key
288 /// if it is not TObject or in case of error, return 0
289 
290 TObject *TKeyXML::ReadObjWithBuffer(char * /*bufferRead*/)
291 {
292  TObject *tobj = (TObject *)XmlReadAny(0, TObject::Class());
293 
294  if (tobj != 0) {
295  if (gROOT->GetForceStyle())
296  tobj->UseCurrentStyle();
297  if (tobj->IsA() == TDirectoryFile::Class()) {
298  TDirectoryFile *dir = (TDirectoryFile *)tobj;
299  dir->SetName(GetName());
300  dir->SetTitle(GetTitle());
301  dir->SetSeekDir(GetKeyId());
302  // set mother before reading keys
303  dir->SetMother(fMotherDir);
304  dir->ReadKeys();
305  fMotherDir->Append(dir);
306  fSubdir = kTRUE;
307  }
308  }
309 
310  return tobj;
311 }
312 
313 ////////////////////////////////////////////////////////////////////////////////
314 /// read object of any type
315 
316 void *TKeyXML::ReadObjectAny(const TClass *expectedClass)
317 {
318  void *res = XmlReadAny(0, expectedClass);
319 
320  if (res && (expectedClass == TDirectoryFile::Class())) {
321  TDirectoryFile *dir = (TDirectoryFile *)res;
322  dir->SetName(GetName());
323  dir->SetTitle(GetTitle());
324  dir->SetSeekDir(GetKeyId());
325  // set mother before reading keys
326  dir->SetMother(fMotherDir);
327  dir->ReadKeys();
328  fMotherDir->Append(dir);
329  fSubdir = kTRUE;
330  }
331 
332  return res;
333 }
334 
335 ////////////////////////////////////////////////////////////////////////////////
336 /// read object from key and cast to expected class
337 
338 void *TKeyXML::XmlReadAny(void *obj, const TClass *expectedClass)
339 {
340  if (fKeyNode == 0)
341  return obj;
342 
343  TXMLFile *f = (TXMLFile *)GetFile();
344  TXMLEngine *xml = XMLEngine();
345  if ((f == 0) || (xml == 0))
346  return obj;
347 
348  TBufferXML buffer(TBuffer::kRead, f);
349  if (f->GetIOVersion() == 1)
351 
352  XMLNodePointer_t blocknode = xml->GetChild(fKeyNode);
353  xml->SkipEmpty(blocknode);
354  while (blocknode != 0) {
355  if (strcmp(xml->GetNodeName(blocknode), xmlio::XmlBlock) == 0)
356  break;
357  xml->ShiftToNext(blocknode);
358  }
359  buffer.XmlReadBlock(blocknode);
360 
361  XMLNodePointer_t objnode = xml->GetChild(fKeyNode);
362  xml->SkipEmpty(objnode);
363 
364  TClass *cl = 0;
365  void *res = buffer.XmlReadAny(objnode, obj, &cl);
366 
367  if ((cl == 0) || (res == 0))
368  return obj;
369 
370  Int_t delta = 0;
371 
372  if (expectedClass != 0) {
373  delta = cl->GetBaseClassOffset(expectedClass);
374  if (delta < 0) {
375  if (obj == 0)
376  cl->Destructor(res);
377  return 0;
378  }
379  if (cl->GetState() > TClass::kEmulated && expectedClass->GetState() <= TClass::kEmulated) {
380  // we cannot mix a compiled class with an emulated class in the inheritance
381  Warning("XmlReadAny", "Trying to read an emulated class (%s) to store in a compiled pointer (%s)",
382  cl->GetName(), expectedClass->GetName());
383  }
384  }
385 
386  return ((char *)res) + delta;
387 }
388 
389 ////////////////////////////////////////////////////////////////////////////////
390 /// return pointer on TXMLEngine object, used for xml conversion
391 
393 {
394  TXMLFile *f = (TXMLFile *)GetFile();
395  return f == 0 ? 0 : f->XML();
396 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
void XmlReadBlock(XMLNodePointer_t node)
Read binary block of data from xml.
Definition: TBufferXML.cxx:470
const char * XmlBlock
Definition: TXMLSetup.cxx:59
void * XmlReadAny(void *obj, const TClass *expectedClass)
read object from key and cast to expected class
Definition: TKeyXML.cxx:338
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:150
virtual Int_t Read(TObject *tobj)
To read an object from the file.
Definition: TKeyXML.cxx:249
const char * ObjClass
Definition: TXMLSetup.cxx:62
long long Long64_t
Definition: RtypesCore.h:69
TXMLEngine * XML()
Definition: TXMLFile.h:110
void XmlWriteBlock(XMLNodePointer_t node)
Write binary data block from buffer to xml.
Definition: TBufferXML.cxx:409
void SetSeekDir(Long64_t v)
void Set()
Set Date/Time to current time as reported by the system.
Definition: TDatime.cxx:288
Long64_t GetKeyId() const
Definition: TKeyXML.h:61
virtual ~TKeyXML()
TKeyXML destructor.
Definition: TKeyXML.cxx:119
const char * Title
Definition: TXMLSetup.cxx:67
const char Option_t
Definition: RtypesCore.h:62
void StoreKeyAttributes()
Stores keys attributes in key node.
Definition: TKeyXML.cxx:150
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
#define gROOT
Definition: TROOT.h:402
virtual TObject * ReadObjWithBuffer(char *bufferRead)
read object derived from TObject class, from key if it is not TObject or in case of error...
Definition: TKeyXML.cxx:290
Long64_t fKeyId
node with stored object
Definition: TKeyXML.h:76
int Int_t
Definition: RtypesCore.h:41
TXMLEngine * XMLEngine()
return pointer on TXMLEngine object, used for xml conversion
Definition: TKeyXML.cxx:392
bool Bool_t
Definition: RtypesCore.h:59
XMLNodePointer_t XmlWriteAny(const void *obj, const TClass *cl)
Convert object of any class to xml structures Return pointer on top xml element.
Definition: TBufferXML.cxx:229
virtual void UseCurrentStyle()
Set current style settings in this object This function is called when either TCanvas::UseCurrentStyl...
Definition: TObject.cxx:715
Int_t GetIOVersion() const
Definition: TXMLFile.h:71
TDirectory * GetMotherDir() const
Definition: TKey.h:81
TDirectory * fMotherDir
!pointer to mother directory
Definition: TKey.h:47
TKeyXML()
default constructor
Definition: TKeyXML.cxx:32
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:694
XMLAttrPointer_t NewIntAttr(XMLNodePointer_t xmlnode, const char *name, Int_t value)
create node attribute with integer value
Definition: TXMLEngine.cxx:604
const char * Name
Definition: TXMLSetup.cxx:66
Int_t GetBaseClassOffset(const TClass *toBase, void *address=0, bool isDerivedObject=true)
Definition: TClass.cxx:2710
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:128
void FreeAllAttr(XMLNodePointer_t xmlnode)
Free all attributes of the node.
Definition: TXMLEngine.cxx:639
XMLNodePointer_t fKeyNode
Definition: TKeyXML.h:75
void Class()
Definition: Class.C:29
EState GetState() const
Definition: TClass.h:453
virtual void * ReadObjectAny(const TClass *expectedClass)
read object of any type
Definition: TKeyXML.cxx:316
TString fClassName
Object Class name.
Definition: TKey.h:42
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
const char * Cycle
Definition: TXMLSetup.cxx:58
const char * Xmlkey
Definition: TXMLSetup.cxx:57
TDatime fDatime
Date/Time of insertion in file.
Definition: TKey.h:37
const char * GetNodeName(XMLNodePointer_t xmlnode)
returns name of xmlnode
Bool_t fSubdir
unique identifier of key for search methods
Definition: TKeyXML.h:77
virtual TObject * ReadObj()
read object derived from TObject class, from key if it is not TObject or in case of error...
Definition: TKeyXML.cxx:263
void ShiftToNext(XMLNodePointer_t &xmlnode, Bool_t realnode=kTRUE)
shifts specified node to next if realnode==kTRUE, any special nodes in between will be skipped ...
const char * CreateTm
Definition: TXMLSetup.cxx:68
void UpdateAttributes()
update key attributes in key node
Definition: TKeyXML.cxx:207
A ROOT file is structured in Directories (like a file system).
TFile * GetFile() const
Returns file to which key belong.
Definition: TKey.cxx:572
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:818
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition: TClass.cxx:5149
const char * AsSQLString() const
Return the date & time in SQL compatible string format, like: 1997-01-15 20:16:28.
Definition: TDatime.cxx:151
void UpdateObject(TObject *obj)
updates object, stored in the node Used for TDirectory data update
Definition: TKeyXML.cxx:222
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
Definition: TDirectory.cxx:190
void StoreObject(const void *obj, const TClass *cl, Bool_t check_tobj=kFALSE)
convert object to xml structure and keep this structure in key
Definition: TKeyXML.cxx:171
virtual void Delete(Option_t *option="")
Delete key from current directory Note: TKeyXML object is not deleted.
Definition: TKeyXML.cxx:136
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:75
void * XMLNodePointer_t
Definition: TXMLEngine.h:17
void SkipEmpty(XMLNodePointer_t &xmlnode)
Skip all current empty nodes and locate on first "true" node.
const Bool_t kFALSE
Definition: RtypesCore.h:88
Bool_t HasAttr(XMLNodePointer_t xmlnode, const char *name)
checks if node has attribute of specified name
Definition: TXMLEngine.cxx:531
virtual Int_t AppendKey(TKey *)
Definition: TDirectory.h:119
long Long_t
Definition: RtypesCore.h:50
void AddChildFirst(XMLNodePointer_t parent, XMLNodePointer_t child)
add node as first child
Definition: TXMLEngine.cxx:812
XMLAttrPointer_t NewAttr(XMLNodePointer_t xmlnode, XMLNsPointer_t, const char *name, const char *value)
creates new attribute for xmlnode, namespaces are not supported for attributes
Definition: TXMLEngine.cxx:578
virtual void SetName(const char *newname)
Set the name for directory If the directory name is changed after the directory was written once...
Short_t fCycle
Cycle number.
Definition: TKey.h:39
#define ClassImp(name)
Definition: Rtypes.h:359
virtual Int_t ReadKeys(Bool_t forceRead=kTRUE)
Read the linked list of keys.
const char * GetAttr(XMLNodePointer_t xmlnode, const char *name)
returns value of attribute for xmlnode
Definition: TXMLEngine.cxx:547
Describe directory structure in memory.
Definition: TDirectory.h:34
virtual const char * GetTitle() const
Returns title (title can contain 32x32 xpm thumbnail/icon).
Definition: TKey.cxx:1517
void FreeNode(XMLNodePointer_t xmlnode)
release all memory, allocated from this node and destroys node itself
Definition: TXMLEngine.cxx:986
Mother of all ROOT objects.
Definition: TObject.h:37
Class for serializing/deserializing object to/from xml.
Definition: TBufferXML.h:32
XMLNodePointer_t GetChild(XMLNodePointer_t xmlnode, Bool_t realnode=kTRUE)
returns first child of xmlnode
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=0)
create new child element for parent node
Definition: TXMLEngine.cxx:707
void UnlinkNode(XMLNodePointer_t node)
unlink (detach) xmlnode from parent
Definition: TXMLEngine.cxx:957
Int_t GetIntAttr(XMLNodePointer_t node, const char *name)
returns value of attribute as integer
Definition: TXMLEngine.cxx:563
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
virtual void SetMother(TObject *mother)
Definition: TDirectory.h:184
const Bool_t kTRUE
Definition: RtypesCore.h:87
char name[80]
Definition: TGX11.cxx:109
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:866
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition: TDatime.h:37
void * XmlReadAny(XMLNodePointer_t node, void *obj, TClass **cl)
Recreate object from xml structure.
Definition: TBufferXML.cxx:246