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