Logo ROOT   6.08/07
Reference Guide
TXMLSetup.cxx
Go to the documentation of this file.
1 // @(#)root/xml:$Id$
2 // Author: Sergey Linev 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 // Class TXMLSetup is used as storage of xml file settings
15 // This class is used in TXMLFile and in TXmlBuffer classes.
16 // Xml settings can be codded via a string in following format
17 //
18 // "2xoo"
19 // ||| \ .
20 // || \ usage of name spaces.
21 // | \ usage of DTD;
22 // \ storage of TStreamerInfo objects in file;
23 // layout of xml file (= 2 - specialized (default), = 3 - generic)
24 //
25 // For last three boolean parameters "x" means true, "o" - false
26 //
27 // Such string can be set as argument of TXMLFile constructor. In that
28 // case new TXMLFile with such parameters will be created.
29 // These settings automatically stored in xml file.
30 
31 //________________________________________________________________________
32 
33 
34 #include "TXMLSetup.h"
35 
36 #include "TROOT.h"
37 #include "TClass.h"
38 #include "TStreamerElement.h"
39 
40 #include "Riostream.h"
41 #include <stdlib.h>
42 
44 
45 namespace xmlio {
46 
47  const char* Root = "root";
48  const char* Setup = "setup";
49  const char* ClassVersion= "version";
50  const char* IOVersion = "version";
51  const char* OnlyVersion = "Version";
52  const char* Ptr = "ptr";
53  const char* Ref = "ref";
54  const char* Null = "null";
55  const char* IdBase = "id";
56  const char* Size = "size";
57  const char* Xmlobject = "XmlObject";
58  const char* Xmlkey = "XmlKey";
59  const char* Cycle = "cycle";
60  const char* XmlBlock = "XmlBlock";
61  const char* Zip = "zip";
62  const char* Object = "Object";
63  const char* ObjClass = "class";
64  const char* Class = "Class";
65  const char* Member = "Member";
66  const char* Item = "Item";
67  const char* Name = "name";
68  const char* Title = "title";
69  const char* CreateTm = "created";
70  const char* ModifyTm = "modified";
71  const char* ObjectUUID = "uuid";
72  const char* Type = "type";
73  const char* Value = "value";
74  const char* v = "v";
75  const char* cnt = "cnt";
76  const char* True = "true";
77  const char* False = "false";
78  const char* SInfos = "StreamerInfos";
79 
80  const char* Array = "Array";
81  const char* Bool = "Bool_t";
82  const char* Char = "Char_t";
83  const char* Short = "Short_t";
84  const char* Int = "Int_t";
85  const char* Long = "Long_t";
86  const char* Long64 = "Long64_t";
87  const char* Float = "Float_t";
88  const char* Double = "Double_t";
89  const char* UChar = "UChar_t";
90  const char* UShort = "UShort_t";
91  const char* UInt = "UInt_t";
92  const char* ULong = "ULong_t";
93  const char* ULong64 = "ULong64_t";
94  const char* String = "string";
95  const char* CharStar = "CharStar";
96 };
97 
98 TString TXMLSetup::fgNameSpaceBase = "http://root.cern.ch/root/htmldoc/";
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// return default value for XML setup
102 
104 {
105  return TString("2xoo");
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// set namespace base
110 
111 void TXMLSetup::SetNameSpaceBase(const char* namespacebase)
112 {
113  fgNameSpaceBase = namespacebase;
114 }
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// defaule constructor of TXMLSetup class
118 
120  fXmlLayout(kSpecialized),
121  fStoreStreamerInfos(kTRUE),
122  fUseDtd(kFALSE),
123  fUseNamespaces(kFALSE),
124  fRefCounter(0)
125 {
126 }
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 /// contsruct TXMLSetup object getting values from string
130 
131 TXMLSetup::TXMLSetup(const char* opt) :
134  fUseDtd(kFALSE),
136  fRefCounter(0)
137 {
138  ReadSetupFromStr(opt);
139 }
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// copy sonstructor of TXMLSetup class
143 
145  fXmlLayout(src.fXmlLayout),
147  fUseDtd(src.fUseDtd),
149  fRefCounter(0)
150 {
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// TXMLSetup class destructor
155 
157 {
158 }
159 
160 ////////////////////////////////////////////////////////////////////////////////
161 /// return setup values as string
162 
164 {
165  char setupstr[10] = "2xxx";
166 
167  setupstr[0] = char(48+fXmlLayout);
168  setupstr[1] = fStoreStreamerInfos ? 'x' : 'o';
169  setupstr[2] = fUseDtd ? 'x' : 'o';
170  setupstr[3] = fUseNamespaces ? 'x' : 'o';
171 
172  return TString(setupstr);
173 }
174 
175 ////////////////////////////////////////////////////////////////////////////////
176 /// checks if string is valid setup
177 
178 Bool_t TXMLSetup::IsValidXmlSetup(const char* setupstr)
179 {
180  if ((setupstr==0) || (strlen(setupstr)!=4)) return kFALSE;
181  TString str = setupstr;
182  str.ToLower();
183  if ((str[0]<48) || (str[0]>53)) return kFALSE;
184  for (int n=1;n<4;n++)
185  if ((str[n]!='o') && (str[n]!='x')) return kFALSE;
186  return kTRUE;
187 }
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 /// get values from string
191 
192 Bool_t TXMLSetup::ReadSetupFromStr(const char* setupstr)
193 {
194  if ((setupstr==0) || (strlen(setupstr)<4)) return kFALSE;
195  Int_t lay = EXMLLayout(setupstr[0] - 48);
197  else fXmlLayout = kSpecialized;
198 
199  fStoreStreamerInfos = setupstr[1]=='x';
200  fUseDtd = kFALSE;
201  fUseNamespaces = setupstr[3]=='x';
202  return kTRUE;
203 }
204 
205 ////////////////////////////////////////////////////////////////////////////////
206 /// show setup values
207 
209 {
210  std::cout << " *** Setup printout ***" << std::endl;
211  std::cout << "Attribute mode = " << fXmlLayout << std::endl;
212  std::cout << "Store streamer infos = " << (fStoreStreamerInfos ? "true" : "false") << std::endl;
213  std::cout << "Use dtd = " << (fUseDtd ? "true" : "false") << std::endl;
214  std::cout << "Use name spaces = " << (fUseNamespaces ? "true" : "false") << std::endl;
215 }
216 
217 ////////////////////////////////////////////////////////////////////////////////
218 /// convert class name to exclude any special symbols like ':', '<' '>' ',' and spaces
219 
220 const char* TXMLSetup::XmlConvertClassName(const char* clname)
221 {
222  fStrBuf = clname;
223  fStrBuf.ReplaceAll("<","_");
224  fStrBuf.ReplaceAll(">","_");
225  fStrBuf.ReplaceAll(",","_");
226  fStrBuf.ReplaceAll(" ","_");
227  fStrBuf.ReplaceAll(":","_");
228  return fStrBuf.Data();
229 }
230 
231 ////////////////////////////////////////////////////////////////////////////////
232 /// produce string which used as reference in class namespace definition
233 
235 {
236  TString clname = XmlConvertClassName(cl->GetName());
238  fStrBuf += clname;
239  if (fgNameSpaceBase == "http://root.cern.ch/root/htmldoc/")
240  fStrBuf += ".html";
241  return fStrBuf.Data();
242 }
243 
244 ////////////////////////////////////////////////////////////////////////////////
245 /// return converted name for TStreamerElement
246 
248 {
249  if (el==0) return 0;
250  if (!el->InheritsFrom(TStreamerSTL::Class())) return el->GetName();
251  if (strcmp(el->GetName(), el->GetClassPointer()->GetName())!=0) return el->GetName();
252  return XmlConvertClassName(el->GetName());
253 }
254 
255 ////////////////////////////////////////////////////////////////////////////////
256 /// get item name for given element
257 
259 {
260  if (el==0) return 0;
261  fStrBuf = el->GetName();
262  fStrBuf+="_item";
263  return fStrBuf.Data();
264 }
265 
266 ////////////////////////////////////////////////////////////////////////////////
267 /// define class for the converted class name, where
268 /// special symbols were replaced by '_'
269 
270 TClass* TXMLSetup::XmlDefineClass(const char* xmlClassName)
271 {
272  if (strchr(xmlClassName,'_')==0) return TClass::GetClass(xmlClassName);
273 
274  TIter iter(gROOT->GetListOfClasses());
275  TClass* cl = 0;
276  while ((cl = (TClass*) iter()) != 0) {
277  const char* name = XmlConvertClassName(cl->GetName());
278  if (strcmp(xmlClassName,name)==0) return cl;
279  }
280  return 0;
281 }
282 
283 ////////////////////////////////////////////////////////////////////////////////
284 /// converts string to integer.
285 /// if error, returns default value
286 
287 Int_t TXMLSetup::AtoI(const char* sbuf, Int_t def, const char* errinfo)
288 {
289  if (sbuf) return atoi(sbuf);
290  if (errinfo)
291  std::cerr << "<Error in TXMLSetup::AtoI>" << errinfo << " not valid integer: sbuf <NULL>" << std::endl;
292  return def;
293 }
const char * ULong
Definition: TXMLSetup.cxx:92
Int_t fRefCounter
Definition: TXMLSetup.h:135
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
const char * Object
Definition: TXMLSetup.cxx:62
const char * Long64
Definition: TXMLSetup.cxx:86
const char * XmlBlock
Definition: TXMLSetup.cxx:60
const char * UInt
Definition: TXMLSetup.cxx:91
const char * Ref
Definition: TXMLSetup.cxx:53
const char * ObjClass
Definition: TXMLSetup.cxx:63
const char * ModifyTm
Definition: TXMLSetup.cxx:70
const char * Double
Definition: TXMLSetup.cxx:88
static void SetNameSpaceBase(const char *namespacebase)
set namespace base
Definition: TXMLSetup.cxx:111
const char * Title
Definition: TXMLSetup.cxx:68
const char * Size
Definition: TXMLSetup.cxx:56
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
const char * v
Definition: TXMLSetup.cxx:74
virtual TClass * GetClassPointer() const
Returns a pointer to the TClass of this element.
const char * ULong64
Definition: TXMLSetup.cxx:93
void PrintSetup()
show setup values
Definition: TXMLSetup.cxx:208
static TString DefaultXmlSetup()
return default value for XML setup
Definition: TXMLSetup.cxx:103
#define gROOT
Definition: TROOT.h:364
Basic string class.
Definition: TString.h:137
const char * Setup
Definition: TXMLSetup.cxx:48
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1089
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
const char * Item
Definition: TXMLSetup.cxx:66
const char * Class
Definition: TXMLSetup.cxx:64
const char * False
Definition: TXMLSetup.cxx:77
const char * Type
Definition: TXMLSetup.cxx:72
const char * Name
Definition: TXMLSetup.cxx:67
TXMLSetup()
defaule constructor of TXMLSetup class
Definition: TXMLSetup.cxx:119
const char * String
Definition: TXMLSetup.cxx:94
const char * Float
Definition: TXMLSetup.cxx:87
Int_t AtoI(const char *sbuf, Int_t def=0, const char *errinfo=0)
converts string to integer.
Definition: TXMLSetup.cxx:287
const char * UChar
Definition: TXMLSetup.cxx:89
const char * Char
Definition: TXMLSetup.cxx:82
const char * Cycle
Definition: TXMLSetup.cxx:59
const char * Xmlkey
Definition: TXMLSetup.cxx:58
const char * GetElItemName(TStreamerElement *el)
get item name for given element
Definition: TXMLSetup.cxx:258
TString fStrBuf
counter , used to build id of xml references
Definition: TXMLSetup.h:137
const char * XmlGetElementName(const TStreamerElement *el)
return converted name for TStreamerElement
Definition: TXMLSetup.cxx:247
const char * CreateTm
Definition: TXMLSetup.cxx:69
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:488
const char * Array
Definition: TXMLSetup.cxx:80
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
Bool_t fStoreStreamerInfos
Definition: TXMLSetup.h:131
virtual ~TXMLSetup()
TXMLSetup class destructor.
Definition: TXMLSetup.cxx:156
const char * XmlConvertClassName(const char *name)
convert class name to exclude any special symbols like &#39;:&#39;, &#39;<&#39; &#39;>&#39; &#39;,&#39; and spaces ...
Definition: TXMLSetup.cxx:220
const char * Ptr
Definition: TXMLSetup.cxx:52
const char * Root
Definition: TXMLSetup.cxx:47
const char * IOVersion
Definition: TXMLSetup.cxx:50
#define ClassImp(name)
Definition: Rtypes.h:279
const char * XmlClassNameSpaceRef(const TClass *cl)
produce string which used as reference in class namespace definition
Definition: TXMLSetup.cxx:234
const char * ObjectUUID
Definition: TXMLSetup.cxx:71
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition: TClass.cxx:2893
const char * Null
Definition: TXMLSetup.cxx:54
Bool_t fUseDtd
Definition: TXMLSetup.h:132
TString GetSetupAsString()
return setup values as string
Definition: TXMLSetup.cxx:163
const char * CharStar
Definition: TXMLSetup.cxx:95
static TString fgNameSpaceBase
buffer, used in XmlDefineClass() function
Definition: TXMLSetup.h:139
const char * Member
Definition: TXMLSetup.cxx:65
const char * Int
Definition: TXMLSetup.cxx:84
Bool_t ReadSetupFromStr(const char *setupstr)
get values from string
Definition: TXMLSetup.cxx:192
EXMLLayout fXmlLayout
Definition: TXMLSetup.h:130
TClass * XmlDefineClass(const char *xmlClassName)
define class for the converted class name, where special symbols were replaced by &#39;_&#39; ...
Definition: TXMLSetup.cxx:270
const char * Short
Definition: TXMLSetup.cxx:83
const char * OnlyVersion
Definition: TXMLSetup.cxx:51
const Bool_t kTRUE
Definition: Rtypes.h:91
const char * ClassVersion
Definition: TXMLSetup.cxx:49
const char * True
Definition: TXMLSetup.cxx:76
const char * UShort
Definition: TXMLSetup.cxx:90
const char * SInfos
Definition: TXMLSetup.cxx:78
const char * IdBase
Definition: TXMLSetup.cxx:55
const Int_t n
Definition: legend1.C:16
const char * Zip
Definition: TXMLSetup.cxx:61
char name[80]
Definition: TGX11.cxx:109
const char * cnt
Definition: TXMLSetup.cxx:75
const char * Xmlobject
Definition: TXMLSetup.cxx:57
const char * Long
Definition: TXMLSetup.cxx:85
const char * Value
Definition: TXMLSetup.cxx:73
Bool_t IsValidXmlSetup(const char *setupstr)
checks if string is valid setup
Definition: TXMLSetup.cxx:178
const char * Bool
Definition: TXMLSetup.cxx:81
const char * Data() const
Definition: TString.h:349
Bool_t fUseNamespaces
Definition: TXMLSetup.h:133