ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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),
48  fSubdir(kFALSE)
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((void*)obj, obj ? obj->IsA() : 0);
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),
79  fSubdir(kFALSE)
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);
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)
181 {
182  TXMLFile* f = (TXMLFile*) GetFile();
183  TXMLEngine* xml = XMLEngine();
184  if ((f==0) || (xml==0) || (fKeyNode==0)) return;
185 
187 
189  if (f->GetIOVersion()==1)
191 
192  XMLNodePointer_t node = buffer.XmlWriteAny(obj, cl);
193 
194  if (node!=0)
195  xml->AddChildFirst(fKeyNode, node);
196 
197  buffer.XmlWriteBlock(fKeyNode);
198 
199  if (cl) fClassName = cl->GetName();
200 }
201 
202 ////////////////////////////////////////////////////////////////////////////////
203 /// update key attributes in key node
204 
206 {
207  TXMLEngine* xml = XMLEngine();
208  if ((xml==0) || (fKeyNode==0)) return;
209 
210  xml->FreeAllAttr(fKeyNode);
211 
213 }
214 
215 ////////////////////////////////////////////////////////////////////////////////
216 /// updates object, stored in the node
217 /// Used for TDirectory data update
218 
220 {
221  TXMLFile* f = (TXMLFile*) GetFile();
222  TXMLEngine* xml = XMLEngine();
223  if ((f==0) || (xml==0) || (obj==0) || (fKeyNode==0)) return;
224 
225  XMLNodePointer_t objnode = xml->GetChild(fKeyNode);
226  xml->SkipEmpty(objnode);
227 
228  if (objnode==0) return;
229 
230  xml->UnlinkNode(objnode);
231  xml->FreeNode(objnode);
232 
233  xml->FreeAllAttr(fKeyNode);
234 
235  StoreObject(obj, obj->IsA());
236 }
237 
238 ////////////////////////////////////////////////////////////////////////////////
239 /// To read an object from the file.
240 /// The object associated to this key is read from the file into memory.
241 /// Before invoking this function, obj has been created via the
242 /// default constructor.
243 
245 {
246  if (tobj==0) return 0;
247 
248  void* res = XmlReadAny(tobj, 0);
249 
250  return res==0 ? 0 : 1;
251 }
252 
253 ////////////////////////////////////////////////////////////////////////////////
254 /// read object derived from TObject class, from key
255 /// if it is not TObject or in case of error, return 0
256 
258 {
259  TObject* tobj = (TObject*) XmlReadAny(0, TObject::Class());
260 
261  if (tobj!=0) {
262  if (gROOT->GetForceStyle()) tobj->UseCurrentStyle();
263  if (tobj->IsA() == TDirectoryFile::Class()) {
264  TDirectoryFile *dir = (TDirectoryFile*) tobj;
265  dir->SetName(GetName());
266  dir->SetTitle(GetTitle());
267  dir->SetSeekDir(GetKeyId());
268  // set mother before reading keys
269  dir->SetMother(fMotherDir);
270  dir->ReadKeys();
271  fMotherDir->Append(dir);
272  fSubdir = kTRUE;
273  }
274  }
275 
276  return tobj;
277 }
278 
279 ////////////////////////////////////////////////////////////////////////////////
280 /// read object derived from TObject class, from key
281 /// if it is not TObject or in case of error, return 0
282 
283 TObject* TKeyXML::ReadObjWithBuffer(char * /*bufferRead*/)
284 {
285  TObject* tobj = (TObject*) XmlReadAny(0, TObject::Class());
286 
287  if (tobj!=0) {
288  if (gROOT->GetForceStyle()) tobj->UseCurrentStyle();
289  if (tobj->IsA() == TDirectoryFile::Class()) {
290  TDirectoryFile *dir = (TDirectoryFile*) tobj;
291  dir->SetName(GetName());
292  dir->SetTitle(GetTitle());
293  dir->SetSeekDir(GetKeyId());
294  // set mother before reading keys
295  dir->SetMother(fMotherDir);
296  dir->ReadKeys();
297  fMotherDir->Append(dir);
298  fSubdir = kTRUE;
299  }
300  }
301 
302  return tobj;
303 }
304 
305 ////////////////////////////////////////////////////////////////////////////////
306 /// read object of any type
307 
308 void* TKeyXML::ReadObjectAny(const TClass *expectedClass)
309 {
310  return XmlReadAny(0, expectedClass);
311 }
312 
313 ////////////////////////////////////////////////////////////////////////////////
314 /// read object from key and cast to expected class
315 
316 void* TKeyXML::XmlReadAny(void* obj, const TClass* expectedClass)
317 {
318  if (fKeyNode==0) return obj;
319 
320  TXMLFile* f = (TXMLFile*) GetFile();
321  TXMLEngine* xml = XMLEngine();
322  if ((f==0) || (xml==0)) return obj;
323 
325  if (f->GetIOVersion()==1)
327 
328  XMLNodePointer_t blocknode = xml->GetChild(fKeyNode);
329  xml->SkipEmpty(blocknode);
330  while (blocknode!=0) {
331  if (strcmp(xml->GetNodeName(blocknode), xmlio::XmlBlock)==0) break;
332  xml->ShiftToNext(blocknode);
333  }
334  buffer.XmlReadBlock(blocknode);
335 
336  XMLNodePointer_t objnode = xml->GetChild(fKeyNode);
337  xml->SkipEmpty(objnode);
338 
339  TClass* cl = 0;
340  void* res = buffer.XmlReadAny(objnode, obj, &cl);
341 
342  if ((cl==0) || (res==0)) return obj;
343 
344  Int_t delta = 0;
345 
346  if (expectedClass!=0) {
347  delta = cl->GetBaseClassOffset(expectedClass);
348  if (delta<0) {
349  if (obj==0) cl->Destructor(res);
350  return 0;
351  }
352  if (cl->GetState() > TClass::kEmulated && expectedClass->GetState() <= TClass::kEmulated) {
353  //we cannot mix a compiled class with an emulated class in the inheritance
354  Warning("XmlReadAny",
355  "Trying to read an emulated class (%s) to store in a compiled pointer (%s)",
356  cl->GetName(),expectedClass->GetName());
357  }
358  }
359 
360  return ((char*)res) + delta;
361 }
362 
363 ////////////////////////////////////////////////////////////////////////////////
364 /// return pointer on TXMLEngine object, used for xml conversion
365 
367 {
368  TXMLFile* f = (TXMLFile*) GetFile();
369  return f==0 ? 0 : f->XML();
370 }
EState GetState() const
Definition: TClass.h:442
void XmlReadBlock(XMLNodePointer_t node)
Read binary block of data from xml.
Definition: TBufferXML.cxx:491
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:316
virtual Int_t Read(TObject *tobj)
To read an object from the file.
Definition: TKeyXML.cxx:244
tuple buffer
Definition: tree.py:99
const char * ObjClass
Definition: TXMLSetup.cxx:63
long long Long64_t
Definition: RtypesCore.h:69
TXMLEngine * XML()
Definition: TXMLFile.h:122
TDirectory * GetMotherDir() const
Definition: TKey.h:87
void XmlWriteBlock(XMLNodePointer_t node)
Write binary data block from buffer to xml.
Definition: TBufferXML.cxx:433
void SetSeekDir(Long64_t v)
void Set()
Set Date/Time to current time as reported by the system.
Definition: TDatime.cxx:286
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:158
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)
Change (i.e.
Definition: TNamed.cxx:128
#define gROOT
Definition: TROOT.h:344
Int_t GetIOVersion() const
Definition: TXMLFile.h:80
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:283
TFile * GetFile() const
Returns file to which key belong.
Definition: TKey.cxx:569
int Int_t
Definition: RtypesCore.h:41
TXMLEngine * XMLEngine()
return pointer on TXMLEngine object, used for xml conversion
Definition: TKeyXML.cxx:366
const Bool_t kFALSE
Definition: Rtypes.h:92
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:246
virtual void UseCurrentStyle()
Set current style settings in this object This function is called when either TCanvas::UseCurrentStyl...
Definition: TObject.cxx:753
TDirectory * fMotherDir
!pointer to mother directory
Definition: TKey.h:53
TKeyXML()
default constructor
Definition: TKeyXML.cxx:33
TFile * f
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:732
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:2625
void FreeAllAttr(XMLNodePointer_t xmlnode)
Free all attributes of the node.
Definition: TXMLEngine.cxx:550
XMLNodePointer_t fKeyNode
Definition: TKeyXML.h:78
void Class()
Definition: Class.C:29
virtual void * ReadObjectAny(const TClass *expectedClass)
read object of any type
Definition: TKeyXML.cxx:308
TString fClassName
Object Class name.
Definition: TKey.h:48
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:30
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:43
virtual const char * GetTitle() const
Returns title (title can contain 32x32 xpm thumbnail/icon).
Definition: TKey.cxx:1522
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:80
virtual TObject * ReadObj()
read object derived from TObject class, from key if it is not TObject or in case of error...
Definition: TKeyXML.cxx:257
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:205
TPaveLabel title(3, 27.1, 15, 28.7,"ROOT Environment and Tools")
A ROOT file is structured in Directories (like a file system).
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:187
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:675
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition: TClass.cxx:4959
void UpdateObject(TObject *obj)
updates object, stored in the node Used for TDirectory data update
Definition: TKeyXML.cxx:219
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
Definition: TDirectory.cxx:151
virtual void Delete(Option_t *option="")
Delete key from current directory Note: TKeyXML object is not deleted.
Definition: TKeyXML.cxx:146
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
void * XMLNodePointer_t
Definition: TXMLEngine.h:19
void SkipEmpty(XMLNodePointer_t &xmlnode)
Skip all current empty nodes and locate on first "true" node.
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:127
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:45
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:415
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
void StoreObject(const void *obj, const TClass *cl)
convert object to xml structure and keep this structure in key
Definition: TKeyXML.cxx:180
Describe directory structure in memory.
Definition: TDirectory.h:44
void dir(char *path=0)
Definition: rootalias.C:30
void FreeNode(XMLNodePointer_t xmlnode)
release all memory, allocated fro this node and destroyes node itself
Definition: TXMLEngine.cxx:893
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
Long64_t GetKeyId() const
Definition: TKeyXML.h:64
ClassImp(TKeyXML)
Class for serializing/deserializing object to/from xml.
Definition: TBufferXML.h:44
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
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
TObject * obj
virtual void SetMother(TObject *mother)
Definition: TDirectory.h:192
const char * AsSQLString() const
Return the date & time in SQL compatible string format, like: 1997-01-15 20:16:28.
Definition: TDatime.cxx:149
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition: TDatime.h:39
void * XmlReadAny(XMLNodePointer_t node, void *obj, TClass **cl)
Recreate object from xml structure.
Definition: TBufferXML.cxx:262
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904