Logo ROOT   6.10/09
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 
20 #include "TKeyXML.h"
21 
22 #include "TBufferXML.h"
23 #include "TXMLFile.h"
24 #include "TClass.h"
25 #include "TROOT.h"
26 #include "TBrowser.h"
27 
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// default constructor
32 
34  TKey(),
35  fKeyNode(0),
36  fKeyId(0),
37  fSubdir(kFALSE)
38 {
39 }
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// Creates TKeyXML and convert obj data to xml structures
43 
44 TKeyXML::TKeyXML(TDirectory* mother, Long64_t keyid, const TObject* obj, const char* name, const char* title) :
45  TKey(mother),
46  fKeyNode(0),
47  fKeyId(keyid),
49 {
50  if (name)
51  SetName(name);
52  else
53  if (obj!=0) {
54  SetName(obj->GetName());
55  fClassName=obj->ClassName();
56  } else
57  SetName("Noname");
58 
59  if (title) SetTitle(title);
60 
61  fCycle = GetMotherDir()->AppendKey(this);
62 
63  TXMLEngine* xml = XMLEngine();
64  if (xml!=0)
65  fKeyNode = xml->NewChild(0, 0, xmlio::Xmlkey, 0);
66 
67  fDatime.Set();
68 
69  StoreObject(obj, 0, kTRUE);
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Creates TKeyXML and convert obj data to xml structures
74 
75 TKeyXML::TKeyXML(TDirectory* mother, Long64_t keyid, const void* obj, const TClass* cl, const char* name, const char* title) :
76  TKey(mother),
77  fKeyNode(0),
78  fKeyId(keyid),
80 {
81  if (name && *name) SetName(name);
82  else SetName(cl ? cl->GetName() : "Noname");
83 
84  if (title) SetTitle(title);
85 
86  fCycle = GetMotherDir()->AppendKey(this);
87 
88  TXMLEngine* xml = XMLEngine();
89  if (xml!=0)
90  fKeyNode = xml->NewChild(0, 0, xmlio::Xmlkey, 0);
91 
92  fDatime.Set();
93 
94  StoreObject(obj, cl, kFALSE);
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Creates TKeyXML and takes ownership over xml node, from which object can be restored
99 
101  TKey(mother),
102  fKeyNode(keynode),
103  fKeyId(keyid),
104  fSubdir(kFALSE)
105 {
106  TXMLEngine* xml = XMLEngine();
107 
108  SetName(xml->GetAttr(keynode, xmlio::Name));
109 
110  if (xml->HasAttr(keynode, xmlio::Title))
111  SetTitle(xml->GetAttr(keynode, xmlio::Title));
112 
113  fCycle = xml->GetIntAttr(keynode, xmlio::Cycle);
114 
115  if (xml->HasAttr(keynode, xmlio::CreateTm)) {
116  TDatime tm(xml->GetAttr(keynode, xmlio::CreateTm));
117  fDatime = tm;
118  }
119 
120  XMLNodePointer_t objnode = xml->GetChild(keynode);
121  xml->SkipEmpty(objnode);
122 
123  fClassName = xml->GetAttr(objnode, xmlio::ObjClass);
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// TKeyXML destructor
128 
130 {
131  if (fKeyNode) {
132  TXMLEngine* xml = XMLEngine();
133  if (xml) {
134  xml->FreeNode(fKeyNode);
135  } else {
136  TXMLEngine xml_;
137  xml_.FreeNode(fKeyNode);
138  }
139  }
140 }
141 
142 ////////////////////////////////////////////////////////////////////////////////
143 /// Delete key from current directory
144 /// Note: TKeyXML object is not deleted. You still have to call "delete key"
145 
146 void TKeyXML::Delete(Option_t * /*option*/)
147 {
148  TXMLEngine* xml = XMLEngine();
149  if (fKeyNode && xml) {
150  xml->FreeNode(fKeyNode);
151  fKeyNode = 0;
152  }
153 
154  fMotherDir->GetListOfKeys()->Remove(this);
155 }
156 
157 ////////////////////////////////////////////////////////////////////////////////
158 /// Stores keys attributes in key node
159 
161 {
162  TXMLEngine* xml = XMLEngine();
163  TXMLFile* f = (TXMLFile*) GetFile();
164  if ((f==0) || (xml==0) || (fKeyNode==0)) return;
165 
166  xml->NewAttr(fKeyNode, 0, xmlio::Name, GetName());
167 
169 
170  if (f->GetIOVersion()>1) {
171  if (strlen(GetTitle())>0)
172  xml->NewAttr(fKeyNode, 0, xmlio::Title, GetTitle());
174  }
175 }
176 
177 ////////////////////////////////////////////////////////////////////////////////
178 /// convert object to xml structure and keep this structure in key
179 
180 void TKeyXML::StoreObject(const void* obj, const TClass* cl, Bool_t check_tobj)
181 {
182  TXMLFile* f = (TXMLFile*) GetFile();
183  TXMLEngine* xml = XMLEngine();
184  if ((f==0) || (xml==0) || (fKeyNode==0)) return;
185 
186  if (obj && check_tobj) {
187  TClass* actual = TObject::Class()->GetActualClass((TObject*) obj);
188  if (!actual) actual = TObject::Class(); else
189  if (actual != TObject::Class())
190  obj = (void *) ((Long_t) obj - actual->GetBaseClassOffset(TObject::Class()));
191  cl = actual;
192  }
193 
195 
196  TBufferXML buffer(TBuffer::kWrite, f);
197  if (f->GetIOVersion()==1)
199 
200  XMLNodePointer_t node = buffer.XmlWriteAny(obj, cl);
201 
202  if (node!=0)
203  xml->AddChildFirst(fKeyNode, node);
204 
205  buffer.XmlWriteBlock(fKeyNode);
206 
207  if (cl) fClassName = cl->GetName();
208 }
209 
210 ////////////////////////////////////////////////////////////////////////////////
211 /// update key attributes in key node
212 
214 {
215  TXMLEngine* xml = XMLEngine();
216  if ((xml==0) || (fKeyNode==0)) return;
217 
218  xml->FreeAllAttr(fKeyNode);
219 
221 }
222 
223 ////////////////////////////////////////////////////////////////////////////////
224 /// updates object, stored in the node
225 /// Used for TDirectory data update
226 
228 {
229  TXMLFile* f = (TXMLFile*) GetFile();
230  TXMLEngine* xml = XMLEngine();
231  if ((f==0) || (xml==0) || (obj==0) || (fKeyNode==0)) return;
232 
233  XMLNodePointer_t objnode = xml->GetChild(fKeyNode);
234  xml->SkipEmpty(objnode);
235 
236  if (objnode==0) return;
237 
238  xml->UnlinkNode(objnode);
239  xml->FreeNode(objnode);
240 
241  xml->FreeAllAttr(fKeyNode);
242 
243  StoreObject(obj, 0, kTRUE);
244 }
245 
246 ////////////////////////////////////////////////////////////////////////////////
247 /// To read an object from the file.
248 /// The object associated to this key is read from the file into memory.
249 /// Before invoking this function, obj has been created via the
250 /// default constructor.
251 
253 {
254  if (tobj==0) return 0;
255 
256  void* res = XmlReadAny(tobj, 0);
257 
258  return res==0 ? 0 : 1;
259 }
260 
261 ////////////////////////////////////////////////////////////////////////////////
262 /// read object derived from TObject class, from key
263 /// if it is not TObject or in case of error, return 0
264 
266 {
267  TObject* tobj = (TObject*) XmlReadAny(0, TObject::Class());
268 
269  if (tobj!=0) {
270  if (gROOT->GetForceStyle()) tobj->UseCurrentStyle();
271  if (tobj->IsA() == TDirectoryFile::Class()) {
272  TDirectoryFile *dir = (TDirectoryFile*) tobj;
273  dir->SetName(GetName());
274  dir->SetTitle(GetTitle());
275  dir->SetSeekDir(GetKeyId());
276  // set mother before reading keys
277  dir->SetMother(fMotherDir);
278  dir->ReadKeys();
279  fMotherDir->Append(dir);
280  fSubdir = kTRUE;
281  }
282  }
283 
284  return tobj;
285 }
286 
287 ////////////////////////////////////////////////////////////////////////////////
288 /// read object derived from TObject class, from key
289 /// if it is not TObject or in case of error, return 0
290 
291 TObject* TKeyXML::ReadObjWithBuffer(char * /*bufferRead*/)
292 {
293  TObject* tobj = (TObject*) XmlReadAny(0, TObject::Class());
294 
295  if (tobj!=0) {
296  if (gROOT->GetForceStyle()) 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  return XmlReadAny(0, expectedClass);
319 }
320 
321 ////////////////////////////////////////////////////////////////////////////////
322 /// read object from key and cast to expected class
323 
324 void* TKeyXML::XmlReadAny(void* obj, const TClass* expectedClass)
325 {
326  if (fKeyNode==0) return obj;
327 
328  TXMLFile* f = (TXMLFile*) GetFile();
329  TXMLEngine* xml = XMLEngine();
330  if ((f==0) || (xml==0)) return obj;
331 
332  TBufferXML buffer(TBuffer::kRead, f);
333  if (f->GetIOVersion()==1)
335 
336  XMLNodePointer_t blocknode = xml->GetChild(fKeyNode);
337  xml->SkipEmpty(blocknode);
338  while (blocknode!=0) {
339  if (strcmp(xml->GetNodeName(blocknode), xmlio::XmlBlock)==0) break;
340  xml->ShiftToNext(blocknode);
341  }
342  buffer.XmlReadBlock(blocknode);
343 
344  XMLNodePointer_t objnode = xml->GetChild(fKeyNode);
345  xml->SkipEmpty(objnode);
346 
347  TClass* cl = 0;
348  void* res = buffer.XmlReadAny(objnode, obj, &cl);
349 
350  if ((cl==0) || (res==0)) return obj;
351 
352  Int_t delta = 0;
353 
354  if (expectedClass!=0) {
355  delta = cl->GetBaseClassOffset(expectedClass);
356  if (delta<0) {
357  if (obj==0) cl->Destructor(res);
358  return 0;
359  }
360  if (cl->GetState() > TClass::kEmulated && expectedClass->GetState() <= TClass::kEmulated) {
361  //we cannot mix a compiled class with an emulated class in the inheritance
362  Warning("XmlReadAny",
363  "Trying to read an emulated class (%s) to store in a compiled pointer (%s)",
364  cl->GetName(),expectedClass->GetName());
365  }
366  }
367 
368  return ((char*)res) + delta;
369 }
370 
371 ////////////////////////////////////////////////////////////////////////////////
372 /// return pointer on TXMLEngine object, used for xml conversion
373 
375 {
376  TXMLFile* f = (TXMLFile*) GetFile();
377  return f==0 ? 0 : f->XML();
378 }
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:505
const char * XmlBlock
Definition: TXMLSetup.cxx:60
void * XmlReadAny(void *obj, const TClass *expectedClass)
read object from key and cast to expected class
Definition: TKeyXML.cxx:324
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:148
virtual Int_t Read(TObject *tobj)
To read an object from the file.
Definition: TKeyXML.cxx:252
const char * ObjClass
Definition: TXMLSetup.cxx:63
long long Long64_t
Definition: RtypesCore.h:69
TXMLEngine * XML()
Definition: TXMLFile.h:116
void XmlWriteBlock(XMLNodePointer_t node)
Write binary data block from buffer to xml.
Definition: TBufferXML.cxx:447
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:60
virtual ~TKeyXML()
TKeyXML destructor.
Definition: TKeyXML.cxx:129
const char * Title
Definition: TXMLSetup.cxx:68
const char Option_t
Definition: RtypesCore.h:62
void StoreKeyAttributes()
Stores keys attributes in key node.
Definition: TKeyXML.cxx:160
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:131
#define gROOT
Definition: TROOT.h:375
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:291
Long64_t fKeyId
node with stored object
Definition: TKeyXML.h:75
int Int_t
Definition: RtypesCore.h:41
TXMLEngine * XMLEngine()
return pointer on TXMLEngine object, used for xml conversion
Definition: TKeyXML.cxx:374
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:260
virtual void UseCurrentStyle()
Set current style settings in this object This function is called when either TCanvas::UseCurrentStyl...
Definition: TObject.cxx:708
Int_t GetIOVersion() const
Definition: TXMLFile.h:74
TDirectory * GetMotherDir() const
Definition: TKey.h:81
TDirectory * fMotherDir
!pointer to mother directory
Definition: TKey.h:47
TKeyXML()
default constructor
Definition: TKeyXML.cxx:33
Float_t delta
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:687
XMLAttrPointer_t NewIntAttr(XMLNodePointer_t xmlnode, const char *name, Int_t value)
create node attribute with integer value
Definition: TXMLEngine.cxx:514
const char * Name
Definition: TXMLSetup.cxx:67
Int_t GetBaseClassOffset(const TClass *toBase, void *address=0, bool isDerivedObject=true)
Definition: TClass.cxx:2709
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:135
void FreeAllAttr(XMLNodePointer_t xmlnode)
Free all attributes of the node.
Definition: TXMLEngine.cxx:550
XMLNodePointer_t fKeyNode
Definition: TKeyXML.h:74
void Class()
Definition: Class.C:29
EState GetState() const
Definition: TClass.h:433
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:59
const char * Xmlkey
Definition: TXMLSetup.cxx:58
TDatime fDatime
Date/Time of insertion in file.
Definition: TKey.h:37
const char * GetNodeName(XMLNodePointer_t xmlnode)
returns name of xmlnode
Definition: TXMLEngine.cxx:930
Bool_t fSubdir
unique identifier of key for search methods
Definition: TKeyXML.h:76
virtual TObject * ReadObj()
read object derived from TObject class, from key if it is not TObject or in case of error...
Definition: TKeyXML.cxx:265
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:69
void UpdateAttributes()
update key attributes in key node
Definition: TKeyXML.cxx:213
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:679
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition: TClass.cxx:5063
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:227
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
Definition: TDirectory.cxx:153
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:180
virtual void Delete(Option_t *option="")
Delete key from current directory Note: TKeyXML object is not deleted.
Definition: TKeyXML.cxx:146
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:71
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:92
Bool_t HasAttr(XMLNodePointer_t xmlnode, const char *name)
checks if node has attribute of specified name
Definition: TXMLEngine.cxx:446
virtual Int_t AppendKey(TKey *)
Definition: TDirectory.h:117
long Long_t
Definition: RtypesCore.h:50
void AddChildFirst(XMLNodePointer_t parent, XMLNodePointer_t child)
add node as first child
Definition: TXMLEngine.cxx:717
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:488
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:336
double f(double x)
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:460
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:1525
void FreeNode(XMLNodePointer_t xmlnode)
release all memory, allocated fro this node and destroyes node itself
Definition: TXMLEngine.cxx:893
Mother of all ROOT objects.
Definition: TObject.h:37
Class for serializing/deserializing object to/from xml.
Definition: TBufferXML.h:34
XMLNodePointer_t GetChild(XMLNodePointer_t xmlnode, Bool_t realnode=kTRUE)
returns first child of xml node
Definition: TXMLEngine.cxx:993
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:614
void UnlinkNode(XMLNodePointer_t node)
unlink (dettach) xml node from parent
Definition: TXMLEngine.cxx:867
Int_t GetIntAttr(XMLNodePointer_t node, const char *name)
returns value of attribute as integer
Definition: TXMLEngine.cxx:475
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:364
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
virtual void SetMother(TObject *mother)
Definition: TDirectory.h:182
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:859
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:276