File indexing completed on 2025-08-05 08:19:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include "JetScapeXML.h"
0017 #include "JetScapeLogger.h"
0018 #include <stdlib.h>
0019
0020 using namespace std;
0021
0022 namespace Jetscape {
0023
0024 JetScapeXML *JetScapeXML::m_pInstance = NULL;
0025
0026 JetScapeXML *JetScapeXML::Instance() {
0027 if (!m_pInstance) {
0028 JSINFO << "Created JetScapeXML Instance";
0029 m_pInstance = new JetScapeXML();
0030 }
0031
0032 return m_pInstance;
0033 }
0034
0035
0036 void JetScapeXML::OpenXMLMasterFile() {
0037 JSWARN << "Deprecated function OpenXMLMasterFile(): Call OpenXMLMainFile() instead!";
0038 OpenXMLMainFile();
0039 }
0040
0041 void JetScapeXML::OpenXMLMainFile() {
0042
0043 if (!IsMainFileOpen()) {
0044
0045 xml_doc_main.LoadFile((char *)GetXMLMainFileName().c_str());
0046 VERBOSE(2) << "Trying XML Main file : " << GetXMLMainFileName();
0047
0048 if (xml_doc_main.ErrorID() < 1) {
0049 JSINFO << "Open XML Main file : " << GetXMLMainFileName();
0050 xml_root_main =
0051 (tinyxml2::XMLElement *)xml_doc_main.FirstChildElement("jetscape");
0052
0053 if (!xml_root_main) {
0054 JSWARN << "Not a valid JetScape XML Main file!";
0055 exit(-1);
0056 }
0057 } else {
0058 auto errCode = xml_doc_main.ErrorID();
0059 SetXMLMainFileName("../config/jetscape_master.xml");
0060 xml_doc_main.LoadFile((char *)GetXMLMainFileName().c_str());
0061 VERBOSE(2) << "Looking for Old XML Master file : " << GetXMLMainFileName();
0062
0063 if (xml_doc_main.ErrorID() < 1) {
0064 JSWARN << "Using Deprecated XML Master file : " << GetXMLMainFileName();
0065 xml_root_main =
0066 (tinyxml2::XMLElement *)xml_doc_main.FirstChildElement("jetscape");
0067
0068 if (!xml_root_main) {
0069 JSWARN << "Not a valid JetScape XML Main file!";
0070 exit(-1);
0071 }
0072 } else {
0073 JSWARN << "XML Main file not found/not properly opened! Error code : "
0074 << errCode;
0075 exit(-1);
0076 }
0077 }
0078
0079 xml_main_file_open = true;
0080 }
0081 }
0082
0083
0084 void JetScapeXML::OpenXMLMasterFile(string m_name) {
0085 JSWARN << "Deprecated function OpenXMLMasterFile(): Call OpenXMLMainFile() instead!";
0086 OpenXMLMainFile(m_name);
0087 }
0088
0089
0090 void JetScapeXML::OpenXMLMainFile(string m_name) {
0091 SetXMLMainFileName(m_name);
0092 OpenXMLMainFile();
0093 }
0094
0095
0096 void JetScapeXML::OpenXMLUserFile() {
0097
0098 if (!xml_user_file_open) {
0099
0100 xml_doc_user.LoadFile((char *)GetXMLUserFileName().c_str());
0101 VERBOSE(2) << "Trying XML User file : " << GetXMLUserFileName();
0102
0103 if (xml_doc_user.ErrorID() < 1) {
0104 JSINFO << "Open XML User file : " << GetXMLUserFileName();
0105 xml_root_user =
0106 (tinyxml2::XMLElement *)xml_doc_user.FirstChildElement("jetscape");
0107
0108 if (!xml_root_user) {
0109 JSWARN << "Not a valid JetScape XML User file!";
0110 exit(-1);
0111 }
0112 } else {
0113 JSWARN << "XML User file not found/not properly opened! Error code : "
0114 << xml_doc_user.ErrorID();
0115 exit(-1);
0116 }
0117
0118 xml_user_file_open = true;
0119 }
0120 }
0121
0122
0123 void JetScapeXML::OpenXMLUserFile(string m_name) {
0124 SetXMLUserFileName(m_name);
0125 OpenXMLUserFile();
0126 }
0127
0128
0129 tinyxml2::XMLElement *
0130 JetScapeXML::GetXMLElementMaster(std::initializer_list<const char *> &path) {
0131
0132 JSWARN << "Deprecated function GetXMLElementMaster(). Call GetXMLElementMain() instead!";
0133 return GetXMLElementMain(path);
0134 }
0135
0136
0137 tinyxml2::XMLElement *
0138 JetScapeXML::GetXMLElementMain(std::initializer_list<const char *> &path) {
0139
0140 VERBOSE(2) << "Looking for element in Main file: " << path;
0141
0142 OpenXMLMainFile();
0143
0144 tinyxml2::XMLElement *currentElement = nullptr;
0145 for (auto &elementName : path) {
0146 if (!currentElement) {
0147 currentElement = xml_root_main->FirstChildElement(elementName);
0148
0149 if (currentElement) {
0150 VERBOSE(3) << "Loaded " << elementName << " from xml_root_main";
0151 } else {
0152 VERBOSE(3) << elementName << " not found in xml_root_main";
0153 }
0154 } else {
0155 currentElement = currentElement->FirstChildElement(elementName);
0156
0157 if (currentElement) {
0158 VERBOSE(3) << "Loaded " << elementName << " from "
0159 << currentElement->Name();
0160 } else {
0161 VERBOSE(3) << elementName << " not found in " << elementName;
0162 }
0163 }
0164 }
0165
0166 if (currentElement) {
0167 VERBOSE(2) << "Found element.";
0168 } else {
0169 VERBOSE(2) << "Did not find element.";
0170 }
0171
0172 return currentElement;
0173 }
0174
0175
0176 tinyxml2::XMLElement *
0177 JetScapeXML::GetXMLElementUser(std::initializer_list<const char *> &path) {
0178
0179 VERBOSE(2) << "Looking for element in User file: " << path;
0180
0181 OpenXMLUserFile();
0182
0183 tinyxml2::XMLElement *currentElement = nullptr;
0184 for (auto &elementName : path) {
0185 if (!currentElement) {
0186 currentElement = xml_root_user->FirstChildElement(elementName);
0187
0188 if (currentElement) {
0189 VERBOSE(3) << "Loaded " << elementName << " from xml_root_user";
0190 } else {
0191 VERBOSE(3) << elementName << " not found in xml_root_user";
0192 }
0193 } else {
0194 currentElement = currentElement->FirstChildElement(elementName);
0195
0196 if (currentElement) {
0197 VERBOSE(3) << "Loaded " << elementName << " from "
0198 << currentElement->Name();
0199 } else {
0200 VERBOSE(3) << elementName << " not found in " << elementName;
0201 }
0202 }
0203 }
0204
0205 if (currentElement) {
0206 VERBOSE(2) << "Found element.";
0207 } else {
0208 VERBOSE(2) << "Did not find element.";
0209 }
0210
0211 return currentElement;
0212 }
0213
0214
0215 tinyxml2::XMLElement *
0216 JetScapeXML::GetElement(std::initializer_list<const char *> path,
0217 bool isRequired ) {
0218
0219
0220 tinyxml2::XMLElement *elementUser = GetXMLElementUser(path);
0221 if (elementUser) {
0222 return elementUser;
0223 }
0224
0225 else {
0226 tinyxml2::XMLElement *elementMain = GetXMLElementMain(path);
0227 if (elementMain) {
0228 return elementMain;
0229 } else {
0230 if (isRequired) {
0231 JSWARN << "XML element " << path << " not found, but is required.";
0232 exit(-1);
0233 }
0234 return nullptr;
0235 }
0236 }
0237 }
0238
0239
0240 std::string
0241 JetScapeXML::GetElementText(std::initializer_list<const char *> path,
0242 bool isRequired ) {
0243
0244 tinyxml2::XMLElement *element = GetElement(path, isRequired);
0245
0246 if (element) {
0247 return element->GetText();
0248 } else {
0249 return "";
0250 }
0251 }
0252
0253
0254 int JetScapeXML::GetElementInt(std::initializer_list<const char *> path,
0255 bool isRequired ) {
0256
0257 tinyxml2::XMLElement *element = GetElement(path, isRequired);
0258
0259 if (element) {
0260 int value = 0;
0261 element->QueryIntText(&value);
0262 return value;
0263 } else {
0264 return 0;
0265 }
0266 }
0267
0268
0269 double JetScapeXML::GetElementDouble(std::initializer_list<const char *> path,
0270 bool isRequired ) {
0271
0272 tinyxml2::XMLElement *element = GetElement(path, isRequired);
0273
0274 if (element) {
0275 double value = 0;
0276 element->QueryDoubleText(&value);
0277 return value;
0278 } else {
0279 return 0.;
0280 }
0281 }
0282
0283
0284 std::ostream &operator<<(std::ostream &os,
0285 std::initializer_list<const char *> path) {
0286
0287 int i = 0;
0288 int size = path.size();
0289
0290 os << "\"";
0291 for (auto name : path) {
0292 os << name;
0293 if (i < size - 1) {
0294 os << ":";
0295 }
0296 i++;
0297 }
0298 os << "\"";
0299
0300 return os;
0301 }
0302
0303 }