Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
42 ClassImp(TXMLSetup);
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 
102 TString TXMLSetup::DefaultXmlSetup()
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 /// creates TXMLSetup object getting values from string
117 
118 TXMLSetup::TXMLSetup(const char *opt)
119 {
120  ReadSetupFromStr(opt);
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// copy constructor of TXMLSetup class
125 
126 TXMLSetup::TXMLSetup(const TXMLSetup &src)
127  : fXmlLayout(src.fXmlLayout), fStoreStreamerInfos(src.fStoreStreamerInfos), fUseDtd(src.fUseDtd),
128  fUseNamespaces(src.fUseNamespaces)
129 {
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// assign operator
134 
135 TXMLSetup &TXMLSetup::operator=(const TXMLSetup &rhs)
136 {
137  fXmlLayout = rhs.fXmlLayout;
138  fStoreStreamerInfos = rhs.fStoreStreamerInfos;
139  fUseDtd = rhs.fUseDtd;
140  fUseNamespaces = rhs.fUseNamespaces;
141  return *this;
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// return setup values as string
146 
147 TString TXMLSetup::GetSetupAsString()
148 {
149  char setupstr[10] = "2xxx";
150 
151  setupstr[0] = char(48 + fXmlLayout);
152  setupstr[1] = fStoreStreamerInfos ? 'x' : 'o';
153  setupstr[2] = fUseDtd ? 'x' : 'o';
154  setupstr[3] = fUseNamespaces ? 'x' : 'o';
155 
156  return TString(setupstr);
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// checks if string is valid setup
161 
162 Bool_t TXMLSetup::IsValidXmlSetup(const char *setupstr)
163 {
164  if (!setupstr || (strlen(setupstr) != 4))
165  return kFALSE;
166  TString str = setupstr;
167  str.ToLower();
168  if ((str[0] < 48) || (str[0] > 53))
169  return kFALSE;
170  for (int n = 1; n < 4; n++)
171  if ((str[n] != 'o') && (str[n] != 'x'))
172  return kFALSE;
173  return kTRUE;
174 }
175 
176 ////////////////////////////////////////////////////////////////////////////////
177 /// get values from string
178 
179 Bool_t TXMLSetup::ReadSetupFromStr(const char *setupstr)
180 {
181  if (!setupstr || (strlen(setupstr) < 4))
182  return kFALSE;
183  Int_t lay = EXMLLayout(setupstr[0] - 48);
184  if (lay == kGeneralized)
185  fXmlLayout = kGeneralized;
186  else
187  fXmlLayout = kSpecialized;
188 
189  fStoreStreamerInfos = setupstr[1] == 'x';
190  fUseDtd = kFALSE;
191  fUseNamespaces = setupstr[3] == 'x';
192  return kTRUE;
193 }
194 
195 ////////////////////////////////////////////////////////////////////////////////
196 /// show setup values
197 
198 void TXMLSetup::PrintSetup()
199 {
200  std::cout << " *** Setup printout ***" << std::endl;
201  std::cout << "Attribute mode = " << fXmlLayout << std::endl;
202  std::cout << "Store streamer infos = " << (fStoreStreamerInfos ? "true" : "false") << std::endl;
203  std::cout << "Use dtd = " << (fUseDtd ? "true" : "false") << std::endl;
204  std::cout << "Use name spaces = " << (fUseNamespaces ? "true" : "false") << std::endl;
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// convert class name to exclude any special symbols like ':', '<' '>' ',' and spaces
209 
210 const char *TXMLSetup::XmlConvertClassName(const char *clname)
211 {
212  fStrBuf = clname;
213  fStrBuf.ReplaceAll("<", "_");
214  fStrBuf.ReplaceAll(">", "_");
215  fStrBuf.ReplaceAll(",", "_");
216  fStrBuf.ReplaceAll(" ", "_");
217  fStrBuf.ReplaceAll(":", "_");
218  return fStrBuf.Data();
219 }
220 
221 ////////////////////////////////////////////////////////////////////////////////
222 /// produce string which used as reference in class namespace definition
223 
224 const char *TXMLSetup::XmlClassNameSpaceRef(const TClass *cl)
225 {
226  TString clname = XmlConvertClassName(cl->GetName());
227  fStrBuf = fgNameSpaceBase;
228  fStrBuf += clname;
229  if (fgNameSpaceBase == "http://root.cern.ch/root/htmldoc/")
230  fStrBuf += ".html";
231  return fStrBuf.Data();
232 }
233 
234 ////////////////////////////////////////////////////////////////////////////////
235 /// return converted name for TStreamerElement
236 
237 const char *TXMLSetup::XmlGetElementName(const TStreamerElement *el)
238 {
239  if (!el)
240  return nullptr;
241  if (!el->InheritsFrom(TStreamerSTL::Class()))
242  return el->GetName();
243  if (strcmp(el->GetName(), el->GetClassPointer()->GetName()) != 0)
244  return el->GetName();
245  return XmlConvertClassName(el->GetName());
246 }
247 
248 ////////////////////////////////////////////////////////////////////////////////
249 /// get item name for given element
250 
251 const char *TXMLSetup::GetElItemName(TStreamerElement *el)
252 {
253  if (!el)
254  return nullptr;
255  fStrBuf = el->GetName();
256  fStrBuf += "_item";
257  return fStrBuf.Data();
258 }
259 
260 ////////////////////////////////////////////////////////////////////////////////
261 /// define class for the converted class name, where
262 /// special symbols were replaced by '_'
263 
264 TClass *TXMLSetup::XmlDefineClass(const char *xmlClassName)
265 {
266  if (strchr(xmlClassName, '_') == 0)
267  return TClass::GetClass(xmlClassName);
268 
269  TIter iter(gROOT->GetListOfClasses());
270  TClass *cl = nullptr;
271  while ((cl = (TClass *)iter()) != nullptr) {
272  const char *name = XmlConvertClassName(cl->GetName());
273  if (strcmp(xmlClassName, name) == 0)
274  return cl;
275  }
276  return nullptr;
277 }
278 
279 ////////////////////////////////////////////////////////////////////////////////
280 /// converts string to integer.
281 /// if error, returns default value
282 
283 Int_t TXMLSetup::AtoI(const char *sbuf, Int_t def, const char *errinfo)
284 {
285  if (sbuf)
286  return atoi(sbuf);
287  if (errinfo)
288  std::cerr << "<Error in TXMLSetup::AtoI>" << errinfo << " not valid integer: sbuf <NULL>" << std::endl;
289  return def;
290 }