Logo ROOT  
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 "TList.h"
37 #include "TClass.h"
38 #include "TStreamerElement.h"
39 
40 #include <iostream>
41 #include <cstdlib>
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 /// creates TXMLSetup object getting values from string
118 
119 TXMLSetup::TXMLSetup(const char *opt)
120 {
121  ReadSetupFromStr(opt);
122 }
123 
124 ////////////////////////////////////////////////////////////////////////////////
125 /// copy constructor of TXMLSetup class
126 
128  : fXmlLayout(src.fXmlLayout), fStoreStreamerInfos(src.fStoreStreamerInfos), fUseDtd(src.fUseDtd),
129  fUseNamespaces(src.fUseNamespaces)
130 {
131 }
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// assign operator
135 
137 {
138  fXmlLayout = rhs.fXmlLayout;
140  fUseDtd = rhs.fUseDtd;
142  return *this;
143 }
144 
145 ////////////////////////////////////////////////////////////////////////////////
146 /// return setup values as string
147 
149 {
150  char setupstr[10] = "2xxx";
151 
152  setupstr[0] = char(48 + fXmlLayout);
153  setupstr[1] = fStoreStreamerInfos ? 'x' : 'o';
154  setupstr[2] = fUseDtd ? 'x' : 'o';
155  setupstr[3] = fUseNamespaces ? 'x' : 'o';
156 
157  return TString(setupstr);
158 }
159 
160 ////////////////////////////////////////////////////////////////////////////////
161 /// checks if string is valid setup
162 
163 Bool_t TXMLSetup::IsValidXmlSetup(const char *setupstr)
164 {
165  if (!setupstr || (strlen(setupstr) != 4))
166  return kFALSE;
167  TString str = setupstr;
168  str.ToLower();
169  if ((str[0] < 48) || (str[0] > 53))
170  return kFALSE;
171  for (int n = 1; n < 4; n++)
172  if ((str[n] != 'o') && (str[n] != 'x'))
173  return kFALSE;
174  return kTRUE;
175 }
176 
177 ////////////////////////////////////////////////////////////////////////////////
178 /// get values from string
179 
180 Bool_t TXMLSetup::ReadSetupFromStr(const char *setupstr)
181 {
182  if (!setupstr || (strlen(setupstr) < 4))
183  return kFALSE;
184  Int_t lay = EXMLLayout(setupstr[0] - 48);
185  if (lay == kGeneralized)
187  else
189 
190  fStoreStreamerInfos = setupstr[1] == 'x';
191  fUseDtd = kFALSE;
192  fUseNamespaces = setupstr[3] == 'x';
193  return kTRUE;
194 }
195 
196 ////////////////////////////////////////////////////////////////////////////////
197 /// show setup values
198 
200 {
201  std::cout << " *** Setup printout ***" << std::endl;
202  std::cout << "Attribute mode = " << fXmlLayout << std::endl;
203  std::cout << "Store streamer infos = " << (fStoreStreamerInfos ? "true" : "false") << std::endl;
204  std::cout << "Use dtd = " << (fUseDtd ? "true" : "false") << std::endl;
205  std::cout << "Use name spaces = " << (fUseNamespaces ? "true" : "false") << std::endl;
206 }
207 
208 ////////////////////////////////////////////////////////////////////////////////
209 /// convert class name to exclude any special symbols like ':', '<' '>' ',' and spaces
210 
211 const char *TXMLSetup::XmlConvertClassName(const char *clname)
212 {
213  fStrBuf = clname;
214  fStrBuf.ReplaceAll("<", "_");
215  fStrBuf.ReplaceAll(">", "_");
216  fStrBuf.ReplaceAll(",", "_");
217  fStrBuf.ReplaceAll(" ", "_");
218  fStrBuf.ReplaceAll(":", "_");
219  return fStrBuf.Data();
220 }
221 
222 ////////////////////////////////////////////////////////////////////////////////
223 /// produce string which used as reference in class namespace definition
224 
226 {
227  TString clname = XmlConvertClassName(cl->GetName());
229  fStrBuf += clname;
230  if (fgNameSpaceBase == "http://root.cern.ch/root/htmldoc/")
231  fStrBuf += ".html";
232  return fStrBuf.Data();
233 }
234 
235 ////////////////////////////////////////////////////////////////////////////////
236 /// return converted name for TStreamerElement
237 
239 {
240  if (!el)
241  return nullptr;
242  if (!el->InheritsFrom(TStreamerSTL::Class()))
243  return el->GetName();
244  if (strcmp(el->GetName(), el->GetClassPointer()->GetName()) != 0)
245  return el->GetName();
246  return XmlConvertClassName(el->GetName());
247 }
248 
249 ////////////////////////////////////////////////////////////////////////////////
250 /// get item name for given element
251 
253 {
254  if (!el)
255  return nullptr;
256  fStrBuf = el->GetName();
257  fStrBuf += "_item";
258  return fStrBuf.Data();
259 }
260 
261 ////////////////////////////////////////////////////////////////////////////////
262 /// define class for the converted class name, where
263 /// special symbols were replaced by '_'
264 
265 TClass *TXMLSetup::XmlDefineClass(const char *xmlClassName)
266 {
267  if (strchr(xmlClassName, '_') == 0)
268  return TClass::GetClass(xmlClassName);
269 
270  TIter iter(gROOT->GetListOfClasses());
271  TClass *cl = nullptr;
272  while ((cl = (TClass *)iter()) != nullptr) {
273  const char *name = XmlConvertClassName(cl->GetName());
274  if (strcmp(xmlClassName, name) == 0)
275  return cl;
276  }
277  return nullptr;
278 }
279 
280 ////////////////////////////////////////////////////////////////////////////////
281 /// converts string to integer.
282 /// if error, returns default value
283 
284 Int_t TXMLSetup::AtoI(const char *sbuf, Int_t def, const char *errinfo)
285 {
286  if (sbuf)
287  return atoi(sbuf);
288  if (errinfo)
289  std::cerr << "<Error in TXMLSetup::AtoI>" << errinfo << " not valid integer: sbuf <NULL>" << std::endl;
290  return def;
291 }
xmlio::CreateTm
const char * CreateTm
Definition: TXMLSetup.cxx:69
TXMLSetup::PrintSetup
void PrintSetup()
show setup values
Definition: TXMLSetup.cxx:199
n
const Int_t n
Definition: legend1.C:16
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TXMLSetup::XmlDefineClass
TClass * XmlDefineClass(const char *xmlClassName)
define class for the converted class name, where special symbols were replaced by '_'
Definition: TXMLSetup.cxx:265
TXMLSetup::SetNameSpaceBase
static void SetNameSpaceBase(const char *namespacebase)
set namespace base
Definition: TXMLSetup.cxx:111
xmlio::Float
const char * Float
Definition: TXMLSetup.cxx:87
xmlio::v
const char * v
Definition: TXMLSetup.cxx:74
xmlio::Zip
const char * Zip
Definition: TXMLSetup.cxx:61
xmlio::ClassVersion
const char * ClassVersion
Definition: TXMLSetup.cxx:49
xmlio
Definition: TXMLSetup.h:28
xmlio::IdBase
const char * IdBase
Definition: TXMLSetup.cxx:55
TString::Data
const char * Data() const
Definition: TString.h:369
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
xmlio::Object
const char * Object
Definition: TXMLSetup.cxx:62
TStreamerElement.h
xmlio::Int
const char * Int
Definition: TXMLSetup.cxx:84
xmlio::Class
const char * Class
Definition: TXMLSetup.cxx:64
xmlio::ModifyTm
const char * ModifyTm
Definition: TXMLSetup.cxx:70
xmlio::Double
const char * Double
Definition: TXMLSetup.cxx:88
xmlio::ObjClass
const char * ObjClass
Definition: TXMLSetup.cxx:63
xmlio::Title
const char * Title
Definition: TXMLSetup.cxx:68
TClass.h
TXMLSetup::fStoreStreamerInfos
Bool_t fStoreStreamerInfos
Definition: TXMLSetup.h:126
TList.h
xmlio::CharStar
const char * CharStar
Definition: TXMLSetup.cxx:95
TXMLSetup::IsValidXmlSetup
Bool_t IsValidXmlSetup(const char *setupstr)
checks if string is valid setup
Definition: TXMLSetup.cxx:163
xmlio::Xmlobject
const char * Xmlobject
Definition: TXMLSetup.cxx:57
xmlio::Bool
const char * Bool
Definition: TXMLSetup.cxx:81
TString
Basic string class.
Definition: TString.h:136
xmlio::Item
const char * Item
Definition: TXMLSetup.cxx:66
TXMLSetup
Definition: TXMLSetup.h:82
TObject::InheritsFrom
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:445
xmlio::Long64
const char * Long64
Definition: TXMLSetup.cxx:86
bool
TString::ReplaceAll
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
TXMLSetup::XmlClassNameSpaceRef
const char * XmlClassNameSpaceRef(const TClass *cl)
produce string which used as reference in class namespace definition
Definition: TXMLSetup.cxx:225
xmlio::Xmlkey
const char * Xmlkey
Definition: TXMLSetup.cxx:58
TROOT.h
TXMLSetup::XmlGetElementName
const char * XmlGetElementName(const TStreamerElement *el)
return converted name for TStreamerElement
Definition: TXMLSetup.cxx:238
xmlio::Root
const char * Root
Definition: TXMLSetup.cxx:47
xmlio::ObjectUUID
const char * ObjectUUID
Definition: TXMLSetup.cxx:71
TXMLSetup::DefaultXmlSetup
static TString DefaultXmlSetup()
return default value for XML setup
Definition: TXMLSetup.cxx:103
TXMLSetup::operator=
TXMLSetup & operator=(const TXMLSetup &rhs)
assign operator
Definition: TXMLSetup.cxx:136
TXMLSetup.h
TXMLSetup::GetSetupAsString
TString GetSetupAsString()
return setup values as string
Definition: TXMLSetup.cxx:148
TXMLSetup::kGeneralized
@ kGeneralized
Definition: TXMLSetup.h:84
xmlio::IOVersion
const char * IOVersion
Definition: TXMLSetup.cxx:50
TXMLSetup::fXmlLayout
EXMLLayout fXmlLayout
Definition: TXMLSetup.h:125
xmlio::UShort
const char * UShort
Definition: TXMLSetup.cxx:90
xmlio::ULong
const char * ULong
Definition: TXMLSetup.cxx:92
xmlio::Char
const char * Char
Definition: TXMLSetup.cxx:82
TXMLSetup::fUseNamespaces
Bool_t fUseNamespaces
Definition: TXMLSetup.h:128
TStreamerElement::GetClassPointer
virtual TClass * GetClassPointer() const
Returns a pointer to the TClass of this element.
Definition: TStreamerElement.cxx:292
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
xmlio::XmlBlock
const char * XmlBlock
Definition: TXMLSetup.cxx:60
xmlio::Short
const char * Short
Definition: TXMLSetup.cxx:83
xmlio::String
const char * String
Definition: TXMLSetup.cxx:94
TXMLSetup::XmlConvertClassName
const char * XmlConvertClassName(const char *name)
convert class name to exclude any special symbols like ':', '<' '>' ',' and spaces
Definition: TXMLSetup.cxx:211
TXMLSetup::fUseDtd
Bool_t fUseDtd
Definition: TXMLSetup.h:127
xmlio::Member
const char * Member
Definition: TXMLSetup.cxx:65
Value
Definition: functioncalls.h:15
xmlio::UInt
const char * UInt
Definition: TXMLSetup.cxx:91
TClass::GetClass
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:2938
xmlio::Null
const char * Null
Definition: TXMLSetup.cxx:54
xmlio::Size
const char * Size
Definition: TXMLSetup.cxx:56
xmlio::OnlyVersion
const char * OnlyVersion
Definition: TXMLSetup.cxx:51
TXMLSetup::fStrBuf
TString fStrBuf
counter , used to build id of xml references
Definition: TXMLSetup.h:132
xmlio::Name
const char * Name
Definition: TXMLSetup.cxx:67
TXMLSetup::kSpecialized
@ kSpecialized
Definition: TXMLSetup.h:84
TXMLSetup::ReadSetupFromStr
Bool_t ReadSetupFromStr(const char *setupstr)
get values from string
Definition: TXMLSetup.cxx:180
xmlio::SInfos
const char * SInfos
Definition: TXMLSetup.cxx:78
TXMLSetup::fgNameSpaceBase
static TString fgNameSpaceBase
buffer, used in XmlDefineClass() function
Definition: TXMLSetup.h:134
xmlio::Type
const char * Type
Definition: TXMLSetup.cxx:72
TClass
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:80
TXMLSetup::GetElItemName
const char * GetElItemName(TStreamerElement *el)
get item name for given element
Definition: TXMLSetup.cxx:252
TXMLSetup::EXMLLayout
EXMLLayout
Definition: TXMLSetup.h:84
xmlio::Setup
const char * Setup
Definition: TXMLSetup.cxx:48
xmlio::Ptr
const char * Ptr
Definition: TXMLSetup.cxx:52
name
char name[80]
Definition: TGX11.cxx:110
xmlio::ULong64
const char * ULong64
Definition: TXMLSetup.cxx:93
xmlio::Ref
const char * Ref
Definition: TXMLSetup.cxx:53
xmlio::Array
const char * Array
Definition: TXMLSetup.cxx:80
TIter
Definition: TCollection.h:233
xmlio::UChar
const char * UChar
Definition: TXMLSetup.cxx:89
xmlio::cnt
const char * cnt
Definition: TXMLSetup.cxx:75
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
xmlio::True
const char * True
Definition: TXMLSetup.cxx:76
Class
void Class()
Definition: Class.C:29
TString::ToLower
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1125
xmlio::Long
const char * Long
Definition: TXMLSetup.cxx:85
TStreamerElement
Definition: TStreamerElement.h:33
xmlio::False
const char * False
Definition: TXMLSetup.cxx:77
xmlio::Cycle
const char * Cycle
Definition: TXMLSetup.cxx:59
TXMLSetup::TXMLSetup
TXMLSetup()=default
gROOT
#define gROOT
Definition: TROOT.h:406
int
TXMLSetup::AtoI
Int_t AtoI(const char *sbuf, Int_t def=0, const char *errinfo=nullptr)
converts string to integer.
Definition: TXMLSetup.cxx:284