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