File indexing completed on 2025-01-18 10:17:34
0001
0002
0003
0004
0005
0006 #include <unistd.h>
0007
0008 #include <iostream>
0009 #include <fstream>
0010 #include <string>
0011
0012 using namespace std;
0013
0014 #include <JANA/JApplication.h>
0015 #include <JANA/Calibrations/JCalibrationManager.h>
0016 #include <JANA/Geometry/JGeometryXML.h>
0017 #include <JANA/Services/JParameterManager.h>
0018 #include <md5.h>
0019
0020 #if JANA2_HAVE_XERCES
0021 using namespace xercesc;
0022 #endif
0023
0024
0025
0026
0027
0028 JGeometryXML::JGeometryXML(string url, int run, string context):JGeometry(url,run,context)
0029 {
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 valid_xmlfile = false;
0046 md5_checksum = "";
0047 jcalib = NULL;
0048 #if JANA2_HAVE_XERCES
0049 parser = NULL;
0050 doc = NULL;
0051 #endif
0052
0053 string xmlfile;
0054 string xml;
0055
0056 if(url.find("xmlfile://")==0){
0057
0058
0059 xmlfile = url.substr(10);
0060
0061
0062 if( access(xmlfile.c_str(), R_OK)){
0063 jerr << " Unable to open \""<<xmlfile<<"\"! Geometry info not available!!"<<endl;
0064 run_min = run_max = -1;
0065 return;
0066 }
0067
0068 }else if(url.find("ccdb://")==0){
0069
0070
0071 xmlfile = url.substr(7);
0072 jcalib = japp->GetService<JCalibrationManager>()->GetJCalibration((uint32_t)run);
0073
0074
0075 vector< map<string, string> > vals;
0076 jcalib->GetCalib(xmlfile, vals);
0077 if(vals.empty()){
0078 jerr << " Failed to get XML for " << xmlfile << "! Geometry info not available!!" << endl;
0079 run_min = run_max = -1;
0080 return;
0081 }
0082 xml = vals[0].begin()->second;
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119 }else{
0120 _DBG_<<"Poorly formed URL. Should start with \"xmlfile://\" or \"ccdb://\"."<<endl;
0121 _DBG_<<"URL:"<<url<<endl;
0122 _DBG_<<"(Try setting you JANA_GEOMETRY_URL environment variable.)"<<endl;
0123 return;
0124 }
0125
0126
0127
0128 Init(xmlfile, xml);
0129
0130
0131
0132 run_min = run_max = run_found = GetRunRequested();
0133
0134 }
0135
0136
0137
0138
0139 void JGeometryXML::Init(string xmlfile, string xml)
0140 {
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150 #if !JANA2_HAVE_XERCES
0151 (void) xmlfile;
0152 (void) xml;
0153 jerr<<endl;
0154 jerr<<"This JANA library was compiled without XERCESC support. To enable"<<endl;
0155 jerr<<"XERCESC, install it on your system and set your XERCESCROOT enviro."<<endl;
0156 jerr<<"variable to point to it. Then, recompile and install JANA."<<endl;
0157 jerr<<endl;
0158 #else
0159
0160 XMLPlatformUtils::Initialize();
0161
0162
0163 parser = new XercesDOMParser();
0164 parser->setCreateEntityReferenceNodes(false);
0165 parser->setValidationScheme(XercesDOMParser::Val_Always);
0166 parser->setValidationSchemaFullChecking(true);
0167 parser->setDoSchema(true);
0168 parser->setDoNamespaces(true);
0169
0170
0171 JGeometryXML::ErrorHandler errorHandler;
0172 parser->setErrorHandler(&errorHandler);
0173
0174
0175 EntityResolver myEntityResolver(xmlfile, jcalib);
0176 parser->setEntityResolver(&myEntityResolver);
0177
0178
0179 parser->resetDocumentPool();
0180
0181
0182 if( xml != "" ){
0183
0184 xercesc::MemBufInputSource myxml_buf((const unsigned char*)xml.c_str(), xml.size(), "myxml (in memory)");
0185 parser->parse(myxml_buf);
0186 md5_checksum = myEntityResolver.GetMD5_checksum();
0187 }else{
0188
0189 parser->parse(xmlfile.c_str());
0190 md5_checksum = myEntityResolver.GetMD5_checksum();
0191 }
0192
0193
0194
0195
0196 doc = parser->getDocument();
0197
0198 valid_xmlfile = true;
0199
0200
0201 pthread_mutex_init(&found_xpaths_mutex, NULL);
0202
0203
0204 MapNodeNames(doc);
0205
0206 #endif
0207 }
0208
0209
0210
0211
0212 JGeometryXML::~JGeometryXML()
0213 {
0214 #if JANA2_HAVE_XERCES
0215
0216 if(valid_xmlfile){
0217
0218
0219
0220 XMLPlatformUtils::Terminate();
0221 }
0222 #endif
0223 }
0224
0225 #if JANA2_HAVE_XERCES
0226
0227
0228
0229 void JGeometryXML::MapNodeNames(xercesc::DOMNode *current_node)
0230 {
0231
0232 char *tmp = XMLString::transcode(current_node->getNodeName());
0233 node_names[current_node] = string(tmp);
0234 XMLString::release(&tmp);
0235
0236
0237 for(DOMNode *child = current_node->getFirstChild(); child != 0; child=child->getNextSibling()){
0238 current_node = child;
0239 MapNodeNames(current_node);
0240 }
0241 }
0242 #endif
0243
0244
0245
0246
0247 bool JGeometryXML::Get(string xpath, string &sval)
0248 {
0249
0250
0251
0252
0253
0254
0255 if(!valid_xmlfile){sval=""; return false;}
0256
0257
0258
0259
0260 pthread_mutex_lock(&found_xpaths_mutex);
0261 map<string, string>::iterator iter = found_xpaths.find(xpath);
0262 if(iter != found_xpaths.end()){
0263 sval = iter->second;
0264 pthread_mutex_unlock(&found_xpaths_mutex);
0265 return true;
0266 }
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281 #if JANA2_HAVE_XERCES
0282
0283
0284
0285 int oldstate, oldtype;
0286 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
0287 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
0288
0289
0290
0291 multimap<xercesc::DOMNode*, string> attributes;
0292 FindAttributeValues(xpath, attributes, 1);
0293
0294
0295 if(attributes.size()>0){
0296 sval = (attributes.begin())->second;
0297
0298
0299 found_xpaths[xpath] = sval;
0300 }
0301
0302
0303 pthread_mutex_unlock(&found_xpaths_mutex);
0304
0305 pthread_setcancelstate(oldstate, NULL);
0306 pthread_setcanceltype(oldtype, NULL);
0307
0308 if(attributes.size()>0)return true;
0309
0310 #endif
0311
0312 if( verbose > 0) _DBG_<<"Node or attribute not found for xpath \""<<xpath<<"\"."<<endl;
0313
0314
0315 return false;
0316 }
0317
0318
0319
0320
0321 bool JGeometryXML::Get(string xpath, map<string, string> &svals)
0322 {
0323
0324
0325
0326
0327
0328
0329
0330
0331 svals.clear();
0332
0333 if(!valid_xmlfile)return false;
0334
0335 #if JANA2_HAVE_XERCES
0336
0337
0338
0339 int oldstate, oldtype;
0340 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
0341 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
0342
0343 multimap<xercesc::DOMNode*, string> attributes;
0344 FindAttributeValues(xpath, attributes, 1);
0345
0346
0347 if(attributes.size()>0)GetAttributes((attributes.begin())->first, svals);
0348
0349 pthread_setcancelstate(oldstate, NULL);
0350 pthread_setcanceltype(oldtype, NULL);
0351
0352 if(attributes.size()>0)return true;
0353
0354 #endif
0355
0356 if( verbose > 0) _DBG_<<"Node or attribute not found for xpath \""<<xpath<<"\"."<<endl;
0357
0358
0359 return false;
0360 }
0361
0362
0363
0364
0365 bool JGeometryXML::GetMultiple(string xpath, vector<string> &vsval)
0366 {
0367
0368
0369
0370
0371
0372 vsval.clear();
0373
0374 if(!valid_xmlfile){return false;}
0375
0376 #if JANA2_HAVE_XERCES
0377
0378
0379
0380 int oldstate, oldtype;
0381 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
0382 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
0383
0384 multimap<xercesc::DOMNode*, string> attributes;
0385 FindAttributeValues(xpath, attributes, 0);
0386
0387 multimap<xercesc::DOMNode*, string>::iterator iter = attributes.begin();
0388 for(; iter!=attributes.end(); iter++){
0389 vsval.push_back(iter->second);
0390 }
0391
0392 pthread_setcancelstate(oldstate, NULL);
0393 pthread_setcanceltype(oldtype, NULL);
0394
0395 #else
0396 (void) xpath;
0397 #endif
0398
0399
0400 return vsval.size()>0;
0401 }
0402
0403
0404
0405
0406 bool JGeometryXML::GetMultiple(string xpath, vector<map<string, string> >&vsvals)
0407 {
0408
0409
0410
0411
0412
0413 vsvals.clear();
0414
0415 if(!valid_xmlfile){return false;}
0416
0417 #if JANA2_HAVE_XERCES
0418
0419
0420
0421 int oldstate, oldtype;
0422 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
0423 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
0424
0425 multimap<xercesc::DOMNode*, string> attributes;
0426 FindAttributeValues(xpath, attributes, 0);
0427
0428 multimap<xercesc::DOMNode*, string>::iterator iter = attributes.begin();
0429 for(; iter!=attributes.end(); iter++){
0430
0431 DOMNode *node = iter->first;
0432 if(!node)continue;
0433
0434 map<string, string> svals;
0435 GetAttributes(node, svals);
0436 vsvals.push_back(svals);
0437 }
0438
0439 pthread_setcancelstate(oldstate, NULL);
0440 pthread_setcanceltype(oldtype, NULL);
0441 #else
0442 (void) xpath;
0443 #endif
0444
0445
0446 return vsvals.size()>0;
0447 }
0448
0449
0450
0451
0452 void JGeometryXML::GetXPaths(vector<string> &xpaths, ATTR_LEVEL_t level, const string &filter)
0453 {
0454
0455
0456
0457
0458
0459
0460
0461 if(!valid_xmlfile){xpaths.clear(); return;}
0462
0463 #if JANA2_HAVE_XERCES
0464 AddNodeToList((DOMNode*)doc->getDocumentElement(), "", xpaths, level);
0465 #else
0466 (void) level;
0467 #endif
0468
0469
0470 if(filter=="")return;
0471
0472
0473 vector<node_t> filter_nodes;
0474 string dummy_str;
0475 unsigned int dummy_int;
0476 ParseXPath(filter, filter_nodes, dummy_str, dummy_int);
0477
0478
0479 if(filter_nodes.size()==0)return;
0480
0481
0482 vector<string> xpaths_to_keep;
0483 for(unsigned int i=0; i<xpaths.size(); i++){
0484
0485 vector<node_t> nodes;
0486 ParseXPath(xpaths[i], nodes, dummy_str, dummy_int);
0487
0488
0489 vector<node_t>::iterator iter;
0490 for(iter=nodes.begin(); iter!=nodes.end(); iter++){
0491 if(NodeCompare(filter_nodes.begin(), filter_nodes.end(), iter, nodes.end()))xpaths_to_keep.push_back(xpaths[i]);
0492 }
0493 }
0494
0495 xpaths = xpaths_to_keep;
0496 }
0497
0498
0499
0500
0501 bool JGeometryXML::NodeCompare(node_iter_t iter1, node_iter_t end1, node_iter_t iter2, node_iter_t end2)
0502 {
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514 for(; iter1!=end1; iter1++, iter2++){
0515
0516
0517 if(iter2==end2)return false;
0518
0519
0520 if(iter1->first!="*")
0521 if(iter1->first != iter2->first)return false;
0522
0523
0524 map<string,string> &attr1 = iter1->second;
0525 map<string,string> &attr2 = iter2->second;
0526 map<string,string>::iterator attr_iter1 = attr1.begin();
0527 for(; attr_iter1!= attr1.end(); attr_iter1++){
0528
0529
0530 map<string,string>::iterator attr_iter2 = attr2.find(attr_iter1->first);
0531 if(attr_iter2==attr2.end())return false;
0532
0533
0534 if(attr_iter1->second!=""){
0535 if(attr_iter1->second!=attr_iter2->second)return false;
0536 }
0537 }
0538 }
0539
0540 return true;
0541 }
0542
0543
0544
0545
0546
0547 void JGeometryXML::ParseXPath(string xpath, vector<pair<string, map<string,string> > > &nodes, string &attribute, unsigned int &attr_depth) const
0548 {
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571 attribute = "";
0572 attr_depth = 0xFFFFFFFF;
0573
0574
0575 vector<string> sections;
0576 string::size_type lastPos = xpath.find_first_not_of("/", 0);
0577 do{
0578 string::size_type pos = xpath.find_first_of("/", lastPos);
0579 if(pos == string::npos)break;
0580
0581 sections.push_back(xpath.substr(lastPos, pos-lastPos));
0582
0583 lastPos = pos+1;
0584 }while(lastPos!=string::npos && lastPos<xpath.size());
0585 sections.push_back(xpath.substr(lastPos, xpath.length()-lastPos));
0586
0587
0588 for(unsigned int i=0; i<sections.size(); i++){
0589 string &str = sections[i];
0590
0591
0592 string::size_type pos_node_end = str.find_first_of("[", 0);
0593 if(pos_node_end==string::npos)pos_node_end = str.length();
0594
0595
0596 string::size_type pos_node_start = str.find_first_of(":", 0);
0597 if(pos_node_start==string::npos || pos_node_start>pos_node_end){
0598 pos_node_start=0;
0599 }else{
0600 pos_node_start++;
0601 }
0602 if(str[pos_node_start]=='@')pos_node_start=pos_node_end;
0603 string nodeName = str.substr(pos_node_start, pos_node_end-pos_node_start);
0604
0605
0606 map<string,string> qualifiers;
0607 lastPos = str.find_first_of("@", 0);
0608 while(lastPos!=string::npos){
0609 lastPos++;
0610 string attr="";
0611 string val="";
0612 string::size_type pos_equals = str.find_first_of("=", lastPos);
0613 string::size_type next_attr = str.find_first_of("@", lastPos);
0614 if(pos_equals!=string::npos && (next_attr>pos_equals || next_attr==string::npos)){
0615
0616 attr = str.substr(lastPos, pos_equals-lastPos);
0617 string::size_type pos_end = str.find_first_of(" ", pos_equals);
0618 if(pos_end==string::npos)pos_end = str.size();
0619
0620
0621
0622 string::size_type pos_quote = str.find_first_of("'", lastPos);
0623 if(pos_quote!=string::npos){
0624 pos_quote = str.find_first_of("'", pos_quote+1);
0625 if(pos_quote!=string::npos && pos_quote>pos_end)pos_end = pos_quote;
0626 }
0627
0628
0629
0630
0631 string::size_type pos_start = pos_equals+1;
0632 if(str[pos_end-1]==']')pos_end--;
0633 if(str[pos_end-1]=='\'')pos_end--;
0634 if(str[pos_end-1]=='\"')pos_end--;
0635 if(str[pos_start]=='\'')pos_start++;
0636 if(str[pos_start]=='\"')pos_start++;
0637 val = str.substr(pos_start, pos_end - pos_start);
0638 }else if(pos_equals==string::npos){
0639
0640 string::size_type pos_end = str.find_first_of(" ", lastPos);
0641 if(pos_end==string::npos)pos_end = str.length();
0642 if(str[pos_end-1]==']')pos_end--;
0643 attr = str.substr(lastPos, pos_end-lastPos);
0644 }else{
0645
0646 break;
0647 }
0648 if(attr!=""){
0649 qualifiers[attr] = val;
0650 lastPos = str.find_first_of("@", lastPos);
0651 }else{
0652 break;
0653 }
0654 }
0655
0656
0657
0658
0659
0660
0661
0662
0663 if(nodeName=="" && i>0){
0664 if(qualifiers.size()==1){
0665 if(attribute!=""){
0666
0667
0668
0669
0670
0671
0672
0673 _DBG_<<"Multiple attribute targets specified in \""<<xpath<<"\""<<endl;
0674 }
0675 attribute = qualifiers.begin()->first;
0676 attr_depth = nodes.size()-1;
0677 map<string,string> &last_qualifiers = nodes[i-1].second;
0678 last_qualifiers[attribute] = "";
0679 }
0680 }else{
0681
0682 pair<string, map<string,string> > node(nodeName, qualifiers);
0683 nodes.push_back(node);
0684 }
0685 }
0686
0687 }
0688
0689 #if JANA2_HAVE_XERCES
0690
0691
0692
0693 void JGeometryXML::AddNodeToList(xercesc::DOMNode* start, string start_path, vector<string> &xpaths, ATTR_LEVEL_t level)
0694 {
0695
0696
0697
0698
0699
0700
0701
0702 string nodeName = node_names.at(start);
0703
0704
0705
0706
0707
0708 if(nodeName[0] == '#')return;
0709
0710
0711 map<string,string> attributes;
0712 if(start->hasAttributes()) {
0713 DOMNamedNodeMap *pAttributes = start->getAttributes();
0714 int nSize = pAttributes->getLength();
0715 for(int i=0;i<nSize;++i) {
0716 DOMAttr *pAttributeNode = (DOMAttr*) pAttributes->item(i);
0717 char *attrName = XMLString::transcode(pAttributeNode->getName());
0718 char *attrValue = XMLString::transcode(pAttributeNode->getValue());
0719 attributes[attrName] = attrValue;
0720 XMLString::release(&attrName);
0721 XMLString::release(&attrValue);
0722 }
0723 }
0724
0725
0726 string attr_qualifiers = "";
0727 if(level!=attr_level_none && attributes.size()>0){
0728 attr_qualifiers += "[";
0729 map<string,string>::iterator iter = attributes.begin();
0730 for(int i=0; iter!=attributes.end(); i++, iter++){
0731 if(i>0)attr_qualifiers += " and ";
0732 attr_qualifiers += "@"+iter->first+"='"+iter->second+"'";
0733 }
0734 attr_qualifiers += "]";
0735 }
0736
0737
0738 string xpath = start_path + "/" + nodeName;
0739 xpaths.push_back(xpath + attr_qualifiers);
0740
0741
0742
0743 for (DOMNode *child = start->getFirstChild(); child != 0; child=child->getNextSibling()){
0744 AddNodeToList(child, xpath + (level==attr_level_all ? attr_qualifiers:""), xpaths, level);
0745 }
0746 }
0747
0748
0749
0750
0751 void JGeometryXML::FindAttributeValues(string &xpath, multimap<DOMNode*, string> &attributes, unsigned int max_values)
0752 {
0753
0754
0755
0756 SearchParameters sp;
0757 ParseXPath(xpath, sp.nodes, sp.attribute_name, sp.attr_depth);
0758
0759
0760 sp.max_values = max_values;
0761 sp.depth = 0;
0762 sp.attr_value = "";
0763 sp.current_node = doc;
0764
0765
0766 sp.SearchTree(node_names);
0767
0768
0769 attributes = sp.attributes;
0770 }
0771
0772
0773
0774
0775 void JGeometryXML::SearchParameters::SearchTree(map<xercesc::DOMNode*, string> &node_names)
0776 {
0777
0778
0779
0780
0781
0782
0783
0784
0785
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798
0799
0800
0801
0802 if(depth>=nodes.size())return;
0803
0804
0805 const string &nodeName = node_names.at(current_node);
0806
0807
0808
0809
0810
0811
0812 if(nodeName!=nodes[depth].first && nodes[depth].first!="" && nodes[depth].first!="*"){
0813
0814
0815
0816
0817
0818
0819
0820 if(depth!=0)return;
0821
0822
0823
0824
0825 for(DOMNode *child = current_node->getFirstChild(); child != 0; child=child->getNextSibling()){
0826 current_node = child;
0827 SearchTree(node_names);
0828 if(max_values>0 && attributes.size()>=max_values)return;
0829 }
0830
0831
0832
0833
0834
0835 return;
0836 }
0837
0838
0839 map<string,string> my_attributes;
0840 JGeometryXML::GetAttributes(current_node, my_attributes);
0841
0842
0843
0844 unsigned int Npassed = 0;
0845 map<string, string> &qualifiers = nodes[depth].second;
0846 string my_attr_value = "";
0847 map<string, string>::iterator iter;
0848 for(iter = qualifiers.begin(); iter!=qualifiers.end(); iter++){
0849 const string &attr = iter->first;
0850 const string &val = iter->second;
0851
0852
0853 map<string, string>::iterator attr_iter;
0854 for(attr_iter = my_attributes.begin(); attr_iter!=my_attributes.end(); attr_iter++){
0855 const string &my_attr = attr_iter->first;
0856 const string &my_val = attr_iter->second;
0857
0858
0859 if(attr==attribute_name)my_attr_value = my_val;
0860
0861
0862 if(attr == my_attr){
0863 if(val=="" || val==my_val)Npassed++;
0864 break;
0865 }
0866 }
0867 }
0868
0869
0870 if(Npassed != qualifiers.size())return;
0871
0872
0873
0874
0875 if(depth==attr_depth)attr_value = my_attr_value;
0876
0877
0878 if(depth==nodes.size()-1){
0879
0880
0881 attributes.insert(pair<DOMNode*, string>(current_node, attr_value));
0882
0883
0884 return;
0885 }
0886
0887
0888
0889
0890
0891 DOMNode* save_current = current_node;
0892 depth++;
0893 for (DOMNode *child = current_node->getFirstChild(); child != 0; child=child->getNextSibling()){
0894 current_node = child;
0895 SearchTree(node_names);
0896 }
0897 depth--;
0898 current_node = save_current;
0899
0900 return;
0901 }
0902
0903
0904
0905
0906 void JGeometryXML::GetAttributes(xercesc::DOMNode* node, map<string,string> &attributes)
0907 {
0908 attributes.clear();
0909
0910 if(!node->hasAttributes())return;
0911
0912
0913 DOMNamedNodeMap *pAttributes = node->getAttributes();
0914 int nSize = pAttributes->getLength();
0915 for(int i=0;i<nSize;++i) {
0916 DOMAttr *pAttributeNode = (DOMAttr*) pAttributes->item(i);
0917 char *tmp1 = XMLString::transcode(pAttributeNode->getName());
0918 char *tmp2 = XMLString::transcode(pAttributeNode->getValue());
0919 attributes[tmp1] = tmp2;
0920 XMLString::release(&tmp1);
0921 XMLString::release(&tmp2);
0922 }
0923 }
0924
0925
0926
0927
0928
0929
0930 JGeometryXML::EntityResolver::EntityResolver(const string &xmlFile, JCalibration *jcalib)
0931 {
0932
0933
0934 xml_filenames.push_back(xmlFile);
0935 this->jcalib = jcalib;
0936
0937
0938 string fname = xmlFile;
0939 size_t pos = fname.find_last_of('/');
0940 if(pos != string::npos){
0941 path = fname.substr(0,pos) + "/";
0942 }
0943
0944 PRINT_CHECKSUM_INPUT_FILES = false;
0945
0946
0947
0948
0949
0950 }
0951
0952
0953
0954
0955 JGeometryXML::EntityResolver::~EntityResolver()
0956 {
0957
0958 }
0959
0960
0961
0962
0963 xercesc::InputSource* JGeometryXML::EntityResolver::resolveEntity(const XMLCh* const publicId, const XMLCh* const systemId)
0964 {
0965
0966
0967
0968
0969
0970
0971
0972 std::string my_publicId = "";
0973 std::string my_systemId = "";
0974 if(publicId){
0975 char *my_publicId_ptr = xercesc::XMLString::transcode(publicId);
0976 my_publicId = my_publicId_ptr;
0977 xercesc::XMLString::release(&my_publicId_ptr);
0978 }
0979 if(systemId){
0980 char *my_systemId_ptr = xercesc::XMLString::transcode(systemId);
0981 my_systemId = my_systemId_ptr;
0982 xercesc::XMLString::release(&my_systemId_ptr);
0983 }
0984
0985
0986 string fullpath = path + my_systemId;
0987 xml_filenames.push_back(fullpath);
0988
0989 if(jcalib){
0990
0991 vector< map<string, string> > vals;
0992 jcalib->GetCalib(fullpath, vals);
0993 if(vals.empty()){
0994 jerr << " Failed to get XML from Calib DB for " << fullpath << "! Geometry info not available!!" << endl;
0995 exit(-1);
0996 }
0997 string &xml = vals[0].begin()->second;
0998
0999
1000
1001
1002
1003 xml_content.push_back(xml);
1004 auto *myxml_buf = new xercesc::MemBufInputSource((const unsigned char*)xml_content.back().c_str(), xml_content.back().size(), "dummy string");
1005 return myxml_buf;
1006 }else{
1007
1008 return NULL;
1009 }
1010 }
1011
1012
1013
1014
1015 std::vector<std::string> JGeometryXML::EntityResolver::GetXMLFilenames(void)
1016 {
1017 return xml_filenames;
1018 }
1019
1020
1021
1022
1023 std::string JGeometryXML::EntityResolver::GetMD5_checksum(void)
1024 {
1025
1026
1027
1028
1029
1030 md5_state_t pms;
1031 md5_init(&pms);
1032 for(string fullpath : xml_filenames){
1033
1034 uint32_t fsize = 0;
1035
1036
1037 if(jcalib){
1038 vector< map<string, string> > vals;
1039 jcalib->GetCalib(fullpath, vals);
1040 if( !vals.empty() ){
1041 string &xml = vals[0].begin()->second;
1042 md5_append(&pms, (const md5_byte_t *)xml.c_str(), xml.size());
1043 fsize = xml.size();
1044 }
1045 }else{
1046 ifstream ifs(fullpath);
1047 if(!ifs.is_open())continue;
1048
1049
1050 ifs.seekg (0, ios::end);
1051 unsigned int length = ifs.tellg();
1052 ifs.seekg (0, ios::beg);
1053
1054
1055 char *buff = new char [length];
1056
1057
1058 ifs.read (buff,length);
1059 ifs.close();
1060
1061 md5_append(&pms, (const md5_byte_t *)buff, length);
1062 fsize = length;
1063
1064 delete[] buff;
1065 }
1066
1067 if(PRINT_CHECKSUM_INPUT_FILES) cerr << " .... Adding file to MD5 checksum : " << fullpath <<" (" << fsize << " bytes)" << endl;
1068
1069 }
1070
1071 md5_byte_t digest[16];
1072 md5_finish(&pms, digest);
1073
1074 const size_t str_len = 16*2 + 1;
1075 char hex_output[str_len];
1076 for(int di = 0; di < 16; ++di) {
1077 size_t buff_left = str_len - di * 2;
1078 snprintf(hex_output + di * 2, buff_left, "%02x", digest[di]);
1079 }
1080
1081 return hex_output;
1082 }
1083
1084
1085 #endif
1086