00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "DetectorDescription/Parser/interface/DDLParser.h"
00019 #include "DetectorDescription/Parser/interface/DDLDocumentProvider.h"
00020 #include "DetectorDescription/Parser/interface/DDLConfiguration.h"
00021 #include "DetectorDescription/Parser/interface/DDLSAX2Handler.h"
00022 #include "DetectorDescription/Parser/interface/DDLSAX2FileHandler.h"
00023 #include "DetectorDescription/Parser/interface/DDLSAX2ConfigHandler.h"
00024 #include "DetectorDescription/Parser/interface/DDLSAX2ExpressionHandler.h"
00025
00026 #include "DDLElementRegistry.h"
00027 #include "StrX.h"
00028
00029
00030 #include "DetectorDescription/Base/interface/DDdebug.h"
00031 #include "DetectorDescription/Base/interface/DDException.h"
00032 #include "DetectorDescription/Algorithm/src/AlgoInit.h"
00033
00034
00035 #include <xercesc/util/PlatformUtils.hpp>
00036 #include <xercesc/util/XMLUni.hpp>
00037 #include <xercesc/sax2/SAX2XMLReader.hpp>
00038 #include <xercesc/sax2/XMLReaderFactory.hpp>
00039 #include <xercesc/sax/SAXException.hpp>
00040
00041
00042 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00043 #include "FWCore/ParameterSet/interface/FileInPath.h"
00044
00045 #include <string>
00046 #include <iostream>
00047 #include <map>
00048
00049
00050
00051
00052 using namespace std;
00053 using namespace xercesc_2_7;
00054
00055
00056
00057
00059 DDLParser::~DDLParser()
00060 {
00061
00062 XMLPlatformUtils::Terminate();
00063 DCOUT_V('P', "DDLParser::~DDLParser(): destruct DDLParser");
00064 }
00065
00067 DDLParser::DDLParser( ) : nFiles_(0)
00068 {
00069
00070
00071
00072
00073
00074
00075
00076 XMLPlatformUtils::Initialize();
00077 AlgoInit();
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 SAX2Parser_ = XMLReaderFactory::createXMLReader();
00088
00089 SAX2Parser_->setFeature(XMLUni::fgSAX2CoreValidation, false);
00090 SAX2Parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, false);
00091
00092
00093
00094 expHandler_ = new DDLSAX2ExpressionHandler;
00095 fileHandler_ = new DDLSAX2FileHandler;
00096 errHandler_ = new DDLSAX2Handler;
00097 SAX2Parser_->setErrorHandler(errHandler_);
00098 SAX2Parser_->setContentHandler(fileHandler_);
00099
00100
00101
00102 DCOUT_V('P', "DDLParser::DDLParser(): new (and only) DDLParser");
00103 }
00104
00105
00106
00107
00108
00109
00110
00111 DDLParser* DDLParser::instance_ = 0;
00112
00113 DDLParser* DDLParser::instance()
00114 {
00115
00116 if ( instance_ == 0 ) {
00117 instance_ = new DDLParser();
00118 }
00119
00120 return instance_;
00121 }
00122
00127 SAX2XMLReader* DDLParser::getXMLParser() { return SAX2Parser_; }
00128
00129 DDLSAX2FileHandler* DDLParser::getDDLSAX2FileHandler() {
00130 return fileHandler_;
00131 }
00132
00133 size_t DDLParser::isFound(const std::string& filename)
00134 {
00135 FileNameHolder::const_iterator it = fileNames_.begin();
00136 size_t i = 1;
00137 bool foundFile = false;
00138 while (it != fileNames_.end() && !foundFile)
00139 {
00140 if (it->second.first == filename)
00141 {
00142 foundFile = true;
00143 }
00144 else ++i;
00145 ++it;
00146 }
00147 if (foundFile)
00148 return i;
00149 return 0;
00150 }
00151
00152 bool DDLParser::isParsed(const std::string& filename)
00153 {
00154 size_t found = isFound(filename);
00155 if (found)
00156 return parsed_[found];
00157 return false;
00158 }
00159
00160 bool DDLParser::parseOneFile(const std::string& fullname)
00161 {
00162
00163 std::string filename = expHandler_->extractFileName(fullname);
00164 edm::FileInPath fp(fullname);
00165 std::string absoluteFileName = fp.fullPath();
00166 size_t foundFile = isFound(filename);
00167 if (!foundFile)
00168 {
00169 int fIndex = foundFile;
00170 pair <std::string, std::string> pss;
00171 pss.first = filename;
00172 pss.second = absoluteFileName;
00173 fIndex = nFiles_;
00174 fileNames_[nFiles_] = pss;
00175 ++nFiles_;
00176 parsed_[fIndex]=false;
00177
00178 currFileName_ = fileNames_[fIndex].second;
00179
00180
00181
00182
00183
00184
00185 SAX2Parser_->setContentHandler(expHandler_);
00186 LogDebug ("DDLParser") << "ParseOneFile() Parsing: " << fileNames_[fIndex].second << std::endl;
00187 parseFile ( fIndex );
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 DCOUT_V('P', "DDLParser::ParseOneFile(): PASS2: Just before setting Xerces content and error handlers... ");
00201
00202
00203
00204
00205 SAX2Parser_->setContentHandler(fileHandler_);
00206
00207 parseFile ( fIndex );
00208 parsed_[fIndex] = true;
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 }
00219 else
00220 {
00221 DCOUT('P', " WARNING: DDLParser::ParseOneFile() file " + filename
00222 + " was already parsed as " + fileNames_[foundFile].second);
00223 return true;
00224 }
00225 return false;
00226 }
00227
00228 std::vector < std::string > DDLParser::getFileList(void)
00229 {
00230 std::vector<std::string> flist;
00231 for (FileNameHolder::const_iterator fit = fileNames_.begin(); fit != fileNames_.end(); ++fit)
00232 {
00233 flist.push_back(fit->second.first);
00234 }
00235 return flist;
00236 }
00237
00238 void DDLParser::dumpFileList(void) {
00239 edm::LogInfo ("DDLParser") << "File List:" << std::endl;
00240 for (FileNameHolder::const_iterator it = fileNames_.begin(); it != fileNames_.end(); ++it)
00241 edm::LogInfo ("DDLParser") << it->second.second << std::endl;
00242 }
00243
00244 void DDLParser::dumpFileList(ostream& co) {
00245 co << "File List:" << std::endl;
00246 for (FileNameHolder::const_iterator it = fileNames_.begin(); it != fileNames_.end(); ++it)
00247 co << it->second.second << std::endl;
00248 }
00249
00250 int DDLParser::parse(const DDLDocumentProvider& dp)
00251 {
00252
00253 edm::LogInfo ("DDLParser") << "Start Parsing. Validation is set off for the time being." << std::endl;
00254
00255
00256
00257
00258 if (dp.doValidation())
00259 {
00260
00261 SAX2Parser_->setFeature(XMLUni::fgSAX2CoreValidation, true);
00262 SAX2Parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
00263
00264 }
00265 else
00266
00267 SAX2Parser_->setFeature(XMLUni::fgSAX2CoreValidation, false);
00268 SAX2Parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, false);
00269
00270
00271
00272
00273
00274 size_t fileIndex = 0;
00275 std::vector<std::string> fullFileName;
00276
00277 for (; fileIndex < (dp.getFileList()).size(); ++fileIndex)
00278 {
00279 std::string ts = dp.getURLList()[fileIndex];
00280 std::string tf = dp.getFileList()[fileIndex];
00281 if ( ts.size() > 0 ) {
00282 if ( ts[ts.size() - 1] == '/') {
00283 fullFileName.push_back( ts + tf );
00284 } else {
00285 fullFileName.push_back( ts + "/" + tf );
00286 }
00287 } else {
00288 fullFileName.push_back( tf );
00289 }
00290 }
00291
00292 for (std::vector<std::string>::const_iterator fnit = fullFileName.begin();
00293 fnit != fullFileName.end();
00294 ++fnit)
00295 {
00296 size_t foundFile = isFound(expHandler_->extractFileName( *fnit ));
00297
00298 if (!foundFile)
00299 {
00300 pair <std::string, std::string> pss;
00301 pss.first = expHandler_->extractFileName( *fnit );
00302 pss.second = *fnit;
00303 fileNames_[nFiles_++] = pss;
00304 parsed_[nFiles_ - 1]=false;
00305 }
00306 }
00307
00308
00309
00310
00311
00312 DCOUT('P', "DDLParser::parse(): PASS1: Just before setting Xerces content and error handlers... ");
00313
00314
00315
00316
00317
00318
00319
00320 SAX2Parser_->setContentHandler(expHandler_);
00321 for (size_t i = 0; i < fileNames_.size(); ++i)
00322 {
00323
00324 if (!parsed_[i])
00325 {
00326 parseFile(i);
00327 }
00328 }
00329 expHandler_->dumpElementTypeCounter();
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 DCOUT('P', "DDLParser::parse(): PASS2: Just before setting Xerces content and error handlers... ");
00342
00343
00344
00345
00346
00347
00348 SAX2Parser_->setContentHandler(fileHandler_);
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 for (size_t i = 0; i < fileNames_.size(); ++i)
00360 {
00361
00362 parseFile(i);
00363 parsed_[i] = true;
00364 pair<std::string, std::string> namePair = fileNames_[i];
00365 LogDebug ("DDLParser") << "Completed parsing file " << namePair.second << std::endl;
00366 }
00367
00368
00369
00370
00371
00372
00373
00374
00375 return 0;
00376 }
00377
00378 void DDLParser::parseFile(const int& numtoproc)
00379 {
00380
00381 if (!parsed_[numtoproc])
00382 {
00383 const std::string & fname = fileNames_[numtoproc].second;
00384
00385
00386
00387
00388
00389
00390 currFileName_ = fname;
00391 SAX2Parser_->parse(currFileName_.c_str());
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 }
00402 else
00403 {
00404 DCOUT('P', "\nWARNING: File " + fileNames_[numtoproc].first
00405 + " has already been processed as " + fileNames_[numtoproc].second);
00406 }
00407 }
00408
00409
00410 std::string DDLParser::getCurrFileName()
00411 {
00412 return currFileName_;
00413 }
00414