// @(#)root/xml:$Id$
// Author: Sergey Linev  10.05.2004

/*************************************************************************
 * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//________________________________________________________________________
//
// Class TXMLSetup is used as storage of xml file settings
// This class is used in TXMLFile and in TXmlBuffer classes.
// Xml settings can be codded via a string in following format
//
//   "2xoo"
//    ||| \ .
//    || \ usage of name spaces.
//    | \ usage of DTD;
//     \ storage of TStreamerInfo objects in file;
//      layout of xml file (= 2 - specialized (default), = 3 - generic)
//
// For last three boolean parameters "x" means true, "o" - false
//
// Such string can be set as argument of TXMLFile constructor. In that
// case new TXMLFile with such parameters will be created.
// These settings automatically stored in xml file.

//________________________________________________________________________


#include "TXMLSetup.h"

#include "TROOT.h"
#include "TClass.h"
#include "TStreamerElement.h"

#include "Riostream.h"
#include <stdlib.h>

ClassImp(TXMLSetup);

namespace xmlio {

   const char* Root        = "root";
   const char* Setup       = "setup";
   const char* ClassVersion= "version";
   const char* IOVersion   = "version";
   const char* OnlyVersion = "Version";
   const char* Ptr         = "ptr";
   const char* Ref         = "ref";
   const char* Null        = "null";
   const char* IdBase      = "id";
   const char* Size        = "size";
   const char* Xmlobject   = "XmlObject";
   const char* Xmlkey      = "XmlKey";
   const char* Cycle       = "cycle";
   const char* XmlBlock    = "XmlBlock";
   const char* Zip         = "zip";
   const char* Object      = "Object";
   const char* ObjClass    = "class";
   const char* Class       = "Class";
   const char* Member      = "Member";
   const char* Item        = "Item";
   const char* Name        = "name";
   const char* Title       = "title";
   const char* CreateTm    = "created";
   const char* ModifyTm    = "modified";
   const char* ObjectUUID  = "uuid";
   const char* Type        = "type";
   const char* Value       = "value";
   const char* v           = "v";
   const char* cnt         = "cnt";
   const char* True        = "true";
   const char* False       = "false";
   const char* SInfos      = "StreamerInfos";

   const char* Array       = "Array";
   const char* Bool        = "Bool_t";
   const char* Char        = "Char_t";
   const char* Short       = "Short_t";
   const char* Int         = "Int_t";
   const char* Long        = "Long_t";
   const char* Long64      = "Long64_t";
   const char* Float       = "Float_t";
   const char* Double      = "Double_t";
   const char* UChar       = "UChar_t";
   const char* UShort      = "UShort_t";
   const char* UInt        = "UInt_t";
   const char* ULong       = "ULong_t";
   const char* ULong64     = "ULong64_t";
   const char* String      = "string";
   const char* CharStar    = "CharStar";
};

TString TXMLSetup::fgNameSpaceBase = "http://root.cern.ch/root/htmldoc/";

//______________________________________________________________________________
TString TXMLSetup::DefaultXmlSetup()
{
   // return default value for XML setup

   return TString("2xoo");
}

//______________________________________________________________________________
void TXMLSetup::SetNameSpaceBase(const char* namespacebase)
{
   // set namespace base

   fgNameSpaceBase = namespacebase;
}

//______________________________________________________________________________
TXMLSetup::TXMLSetup() :
   fXmlLayout(kSpecialized),
   fStoreStreamerInfos(kTRUE),
   fUseDtd(kFALSE),
   fUseNamespaces(kFALSE),
   fRefCounter(0)
{
   // defaule constructor of TXMLSetup class
}

//______________________________________________________________________________
TXMLSetup::TXMLSetup(const char* opt) :
   fXmlLayout(kSpecialized),
   fStoreStreamerInfos(kTRUE),
   fUseDtd(kFALSE),
   fUseNamespaces(kFALSE),
   fRefCounter(0)
{
   // contsruct TXMLSetup object getting values from string

   ReadSetupFromStr(opt);
}

//______________________________________________________________________________
TXMLSetup::TXMLSetup(const TXMLSetup& src) :
   fXmlLayout(src.fXmlLayout),
   fStoreStreamerInfos(src.fStoreStreamerInfos),
   fUseDtd(src.fUseDtd),
   fUseNamespaces(src.fUseNamespaces),
   fRefCounter(0)
{
   // copy sonstructor of TXMLSetup class

}

//______________________________________________________________________________
TXMLSetup::~TXMLSetup()
{
   // TXMLSetup class destructor
}

//______________________________________________________________________________
TString TXMLSetup::GetSetupAsString()
{
   // return setup values as string

   char setupstr[10] = "2xxx";

   setupstr[0] = char(48+fXmlLayout);
   setupstr[1] = fStoreStreamerInfos ? 'x' : 'o';
   setupstr[2] = fUseDtd ? 'x' : 'o';
   setupstr[3] = fUseNamespaces ? 'x' : 'o';

   return TString(setupstr);
}

//______________________________________________________________________________
Bool_t TXMLSetup::IsValidXmlSetup(const char* setupstr)
{
   // checks if string is valid setup

   if ((setupstr==0) || (strlen(setupstr)!=4)) return kFALSE;
   TString str = setupstr;
   str.ToLower();
   if ((str[0]<48) || (str[0]>53)) return kFALSE;
   for (int n=1;n<4;n++)
      if ((str[n]!='o') && (str[n]!='x')) return kFALSE;
   return kTRUE;
}

//______________________________________________________________________________
Bool_t TXMLSetup::ReadSetupFromStr(const char* setupstr)
{
   // get values from string

   if ((setupstr==0) || (strlen(setupstr)<4)) return kFALSE;
   Int_t lay          = EXMLLayout(setupstr[0] - 48);
   if (lay==kGeneralized) fXmlLayout = kGeneralized;
                     else fXmlLayout = kSpecialized;

   fStoreStreamerInfos = setupstr[1]=='x';
   fUseDtd            = kFALSE;
   fUseNamespaces     = setupstr[3]=='x';
   return kTRUE;
}

//______________________________________________________________________________
void TXMLSetup::PrintSetup()
{
   // show setup values

   std::cout << " *** Setup printout ***" << std::endl;
   std::cout << "Attribute mode = " << fXmlLayout << std::endl;
   std::cout << "Store streamer infos = " << (fStoreStreamerInfos ? "true" : "false") << std::endl;
   std::cout << "Use dtd = " << (fUseDtd ? "true" : "false") << std::endl;
   std::cout << "Use name spaces = " << (fUseNamespaces ? "true" : "false") << std::endl;
}

//______________________________________________________________________________
const char* TXMLSetup::XmlConvertClassName(const char* clname)
{
   // convert class name to exclude any special symbols like ':', '<' '>' ',' and spaces

   fStrBuf = clname;
   fStrBuf.ReplaceAll("<","_");
   fStrBuf.ReplaceAll(">","_");
   fStrBuf.ReplaceAll(",","_");
   fStrBuf.ReplaceAll(" ","_");
   fStrBuf.ReplaceAll(":","_");
   return fStrBuf.Data();
}

//______________________________________________________________________________
const char* TXMLSetup::XmlClassNameSpaceRef(const TClass* cl)
{
   // produce string which used as reference in class namespace definition

   TString clname = XmlConvertClassName(cl->GetName());
   fStrBuf = fgNameSpaceBase;
   fStrBuf += clname;
   if (fgNameSpaceBase == "http://root.cern.ch/root/htmldoc/")
     fStrBuf += ".html";
   return fStrBuf.Data();
}

//______________________________________________________________________________
const char* TXMLSetup::XmlGetElementName(const TStreamerElement* el)
{
   //  return converted name for TStreamerElement

   if (el==0) return 0;
   if (!el->InheritsFrom(TStreamerSTL::Class())) return el->GetName();
   if (strcmp(el->GetName(), el->GetClassPointer()->GetName())!=0) return el->GetName();
   return XmlConvertClassName(el->GetName());
}

//______________________________________________________________________________
const char* TXMLSetup::GetElItemName(TStreamerElement* el)
{
   // get item name for given element

   if (el==0) return 0;
   fStrBuf = el->GetName();
   fStrBuf+="_item";
   return fStrBuf.Data();
}

//______________________________________________________________________________
TClass* TXMLSetup::XmlDefineClass(const char* xmlClassName)
{
   // define class for the converted class name, where
   // special symbols were replaced by '_'

   if (strchr(xmlClassName,'_')==0) return TClass::GetClass(xmlClassName);

   TIter iter(gROOT->GetListOfClasses());
   TClass* cl = 0;
   while ((cl = (TClass*) iter()) != 0) {
      const char* name = XmlConvertClassName(cl->GetName());
      if (strcmp(xmlClassName,name)==0) return cl;
   }
   return 0;
}

//______________________________________________________________________________
Int_t TXMLSetup::AtoI(const char* sbuf, Int_t def, const char* errinfo)
{
   // converts string to integer.
   // if error, returns default value

   if (sbuf) return atoi(sbuf);
   if (errinfo)
      std::cerr << "<Error in TXMLSetup::AtoI>" << errinfo << " not valid integer: sbuf <NULL>" << std::endl;
   return def;
}
 TXMLSetup.cxx:1
 TXMLSetup.cxx:2
 TXMLSetup.cxx:3
 TXMLSetup.cxx:4
 TXMLSetup.cxx:5
 TXMLSetup.cxx:6
 TXMLSetup.cxx:7
 TXMLSetup.cxx:8
 TXMLSetup.cxx:9
 TXMLSetup.cxx:10
 TXMLSetup.cxx:11
 TXMLSetup.cxx:12
 TXMLSetup.cxx:13
 TXMLSetup.cxx:14
 TXMLSetup.cxx:15
 TXMLSetup.cxx:16
 TXMLSetup.cxx:17
 TXMLSetup.cxx:18
 TXMLSetup.cxx:19
 TXMLSetup.cxx:20
 TXMLSetup.cxx:21
 TXMLSetup.cxx:22
 TXMLSetup.cxx:23
 TXMLSetup.cxx:24
 TXMLSetup.cxx:25
 TXMLSetup.cxx:26
 TXMLSetup.cxx:27
 TXMLSetup.cxx:28
 TXMLSetup.cxx:29
 TXMLSetup.cxx:30
 TXMLSetup.cxx:31
 TXMLSetup.cxx:32
 TXMLSetup.cxx:33
 TXMLSetup.cxx:34
 TXMLSetup.cxx:35
 TXMLSetup.cxx:36
 TXMLSetup.cxx:37
 TXMLSetup.cxx:38
 TXMLSetup.cxx:39
 TXMLSetup.cxx:40
 TXMLSetup.cxx:41
 TXMLSetup.cxx:42
 TXMLSetup.cxx:43
 TXMLSetup.cxx:44
 TXMLSetup.cxx:45
 TXMLSetup.cxx:46
 TXMLSetup.cxx:47
 TXMLSetup.cxx:48
 TXMLSetup.cxx:49
 TXMLSetup.cxx:50
 TXMLSetup.cxx:51
 TXMLSetup.cxx:52
 TXMLSetup.cxx:53
 TXMLSetup.cxx:54
 TXMLSetup.cxx:55
 TXMLSetup.cxx:56
 TXMLSetup.cxx:57
 TXMLSetup.cxx:58
 TXMLSetup.cxx:59
 TXMLSetup.cxx:60
 TXMLSetup.cxx:61
 TXMLSetup.cxx:62
 TXMLSetup.cxx:63
 TXMLSetup.cxx:64
 TXMLSetup.cxx:65
 TXMLSetup.cxx:66
 TXMLSetup.cxx:67
 TXMLSetup.cxx:68
 TXMLSetup.cxx:69
 TXMLSetup.cxx:70
 TXMLSetup.cxx:71
 TXMLSetup.cxx:72
 TXMLSetup.cxx:73
 TXMLSetup.cxx:74
 TXMLSetup.cxx:75
 TXMLSetup.cxx:76
 TXMLSetup.cxx:77
 TXMLSetup.cxx:78
 TXMLSetup.cxx:79
 TXMLSetup.cxx:80
 TXMLSetup.cxx:81
 TXMLSetup.cxx:82
 TXMLSetup.cxx:83
 TXMLSetup.cxx:84
 TXMLSetup.cxx:85
 TXMLSetup.cxx:86
 TXMLSetup.cxx:87
 TXMLSetup.cxx:88
 TXMLSetup.cxx:89
 TXMLSetup.cxx:90
 TXMLSetup.cxx:91
 TXMLSetup.cxx:92
 TXMLSetup.cxx:93
 TXMLSetup.cxx:94
 TXMLSetup.cxx:95
 TXMLSetup.cxx:96
 TXMLSetup.cxx:97
 TXMLSetup.cxx:98
 TXMLSetup.cxx:99
 TXMLSetup.cxx:100
 TXMLSetup.cxx:101
 TXMLSetup.cxx:102
 TXMLSetup.cxx:103
 TXMLSetup.cxx:104
 TXMLSetup.cxx:105
 TXMLSetup.cxx:106
 TXMLSetup.cxx:107
 TXMLSetup.cxx:108
 TXMLSetup.cxx:109
 TXMLSetup.cxx:110
 TXMLSetup.cxx:111
 TXMLSetup.cxx:112
 TXMLSetup.cxx:113
 TXMLSetup.cxx:114
 TXMLSetup.cxx:115
 TXMLSetup.cxx:116
 TXMLSetup.cxx:117
 TXMLSetup.cxx:118
 TXMLSetup.cxx:119
 TXMLSetup.cxx:120
 TXMLSetup.cxx:121
 TXMLSetup.cxx:122
 TXMLSetup.cxx:123
 TXMLSetup.cxx:124
 TXMLSetup.cxx:125
 TXMLSetup.cxx:126
 TXMLSetup.cxx:127
 TXMLSetup.cxx:128
 TXMLSetup.cxx:129
 TXMLSetup.cxx:130
 TXMLSetup.cxx:131
 TXMLSetup.cxx:132
 TXMLSetup.cxx:133
 TXMLSetup.cxx:134
 TXMLSetup.cxx:135
 TXMLSetup.cxx:136
 TXMLSetup.cxx:137
 TXMLSetup.cxx:138
 TXMLSetup.cxx:139
 TXMLSetup.cxx:140
 TXMLSetup.cxx:141
 TXMLSetup.cxx:142
 TXMLSetup.cxx:143
 TXMLSetup.cxx:144
 TXMLSetup.cxx:145
 TXMLSetup.cxx:146
 TXMLSetup.cxx:147
 TXMLSetup.cxx:148
 TXMLSetup.cxx:149
 TXMLSetup.cxx:150
 TXMLSetup.cxx:151
 TXMLSetup.cxx:152
 TXMLSetup.cxx:153
 TXMLSetup.cxx:154
 TXMLSetup.cxx:155
 TXMLSetup.cxx:156
 TXMLSetup.cxx:157
 TXMLSetup.cxx:158
 TXMLSetup.cxx:159
 TXMLSetup.cxx:160
 TXMLSetup.cxx:161
 TXMLSetup.cxx:162
 TXMLSetup.cxx:163
 TXMLSetup.cxx:164
 TXMLSetup.cxx:165
 TXMLSetup.cxx:166
 TXMLSetup.cxx:167
 TXMLSetup.cxx:168
 TXMLSetup.cxx:169
 TXMLSetup.cxx:170
 TXMLSetup.cxx:171
 TXMLSetup.cxx:172
 TXMLSetup.cxx:173
 TXMLSetup.cxx:174
 TXMLSetup.cxx:175
 TXMLSetup.cxx:176
 TXMLSetup.cxx:177
 TXMLSetup.cxx:178
 TXMLSetup.cxx:179
 TXMLSetup.cxx:180
 TXMLSetup.cxx:181
 TXMLSetup.cxx:182
 TXMLSetup.cxx:183
 TXMLSetup.cxx:184
 TXMLSetup.cxx:185
 TXMLSetup.cxx:186
 TXMLSetup.cxx:187
 TXMLSetup.cxx:188
 TXMLSetup.cxx:189
 TXMLSetup.cxx:190
 TXMLSetup.cxx:191
 TXMLSetup.cxx:192
 TXMLSetup.cxx:193
 TXMLSetup.cxx:194
 TXMLSetup.cxx:195
 TXMLSetup.cxx:196
 TXMLSetup.cxx:197
 TXMLSetup.cxx:198
 TXMLSetup.cxx:199
 TXMLSetup.cxx:200
 TXMLSetup.cxx:201
 TXMLSetup.cxx:202
 TXMLSetup.cxx:203
 TXMLSetup.cxx:204
 TXMLSetup.cxx:205
 TXMLSetup.cxx:206
 TXMLSetup.cxx:207
 TXMLSetup.cxx:208
 TXMLSetup.cxx:209
 TXMLSetup.cxx:210
 TXMLSetup.cxx:211
 TXMLSetup.cxx:212
 TXMLSetup.cxx:213
 TXMLSetup.cxx:214
 TXMLSetup.cxx:215
 TXMLSetup.cxx:216
 TXMLSetup.cxx:217
 TXMLSetup.cxx:218
 TXMLSetup.cxx:219
 TXMLSetup.cxx:220
 TXMLSetup.cxx:221
 TXMLSetup.cxx:222
 TXMLSetup.cxx:223
 TXMLSetup.cxx:224
 TXMLSetup.cxx:225
 TXMLSetup.cxx:226
 TXMLSetup.cxx:227
 TXMLSetup.cxx:228
 TXMLSetup.cxx:229
 TXMLSetup.cxx:230
 TXMLSetup.cxx:231
 TXMLSetup.cxx:232
 TXMLSetup.cxx:233
 TXMLSetup.cxx:234
 TXMLSetup.cxx:235
 TXMLSetup.cxx:236
 TXMLSetup.cxx:237
 TXMLSetup.cxx:238
 TXMLSetup.cxx:239
 TXMLSetup.cxx:240
 TXMLSetup.cxx:241
 TXMLSetup.cxx:242
 TXMLSetup.cxx:243
 TXMLSetup.cxx:244
 TXMLSetup.cxx:245
 TXMLSetup.cxx:246
 TXMLSetup.cxx:247
 TXMLSetup.cxx:248
 TXMLSetup.cxx:249
 TXMLSetup.cxx:250
 TXMLSetup.cxx:251
 TXMLSetup.cxx:252
 TXMLSetup.cxx:253
 TXMLSetup.cxx:254
 TXMLSetup.cxx:255
 TXMLSetup.cxx:256
 TXMLSetup.cxx:257
 TXMLSetup.cxx:258
 TXMLSetup.cxx:259
 TXMLSetup.cxx:260
 TXMLSetup.cxx:261
 TXMLSetup.cxx:262
 TXMLSetup.cxx:263
 TXMLSetup.cxx:264
 TXMLSetup.cxx:265
 TXMLSetup.cxx:266
 TXMLSetup.cxx:267
 TXMLSetup.cxx:268
 TXMLSetup.cxx:269
 TXMLSetup.cxx:270
 TXMLSetup.cxx:271
 TXMLSetup.cxx:272
 TXMLSetup.cxx:273
 TXMLSetup.cxx:274
 TXMLSetup.cxx:275
 TXMLSetup.cxx:276
 TXMLSetup.cxx:277
 TXMLSetup.cxx:278
 TXMLSetup.cxx:279
 TXMLSetup.cxx:280
 TXMLSetup.cxx:281
 TXMLSetup.cxx:282
 TXMLSetup.cxx:283
 TXMLSetup.cxx:284
 TXMLSetup.cxx:285
 TXMLSetup.cxx:286
 TXMLSetup.cxx:287
 TXMLSetup.cxx:288
 TXMLSetup.cxx:289
 TXMLSetup.cxx:290
 TXMLSetup.cxx:291