CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DDLParser.cc
Go to the documentation of this file.
1 /***************************************************************************
2  DDLParser.cc - description
3  -------------------
4  begin : Mon Oct 22 2001
5  email : case@ucdhep.ucdavis.edu
6 ***************************************************************************/
7 
8 /***************************************************************************
9  * *
10  * DDDParser sub-component of DDD *
11  * *
12  ***************************************************************************/
13 
18 
21 
22 #include <xercesc/framework/MemBufInputSource.hpp>
24 
25 #include <iostream>
26 
27 using namespace std;
28 
29 using namespace XERCES_CPP_NAMESPACE;
30 
33  : cpv_( cpv ),
34  nFiles_( 0 )
35 {
36  XMLPlatformUtils::Initialize();
37  AlgoInit();
38  SAX2Parser_ = XMLReaderFactory::createXMLReader();
39 
40  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreValidation, false); // optional
41  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, false); // optional
42  // Specify other parser features, e.g.
43  // SAX2Parser_->setFeature(XMLUni::fgXercesSchemaFullChecking, false);
44 
48  SAX2Parser_->setErrorHandler(errHandler_);
49  SAX2Parser_->setContentHandler(fileHandler_);
50 
51  DCOUT_V('P', "DDLParser::DDLParser(): new (and only) DDLParser");
52 }
53 
56 {
57  // clean up and leave
58  delete expHandler_;
59  delete fileHandler_;
60  delete errHandler_;
61  XMLPlatformUtils::Terminate();
62  DCOUT_V('P', "DDLParser::~DDLParser(): destruct DDLParser");
63 }
64 
69 SAX2XMLReader*
71 {
72  return SAX2Parser_;
73 }
74 
77 {
78  return fileHandler_;
79 }
80 
81 size_t
82 DDLParser::isFound( const std::string& filename )
83 {
84  FileNameHolder::const_iterator it = fileNames_.begin();
85  size_t i = 1;
86  bool foundFile = false;
87  while( it != fileNames_.end() && !foundFile )
88  {
89  if( it->second.first == filename )
90  {
91  foundFile = true;
92  }
93  else ++i;
94  ++it;
95  }
96  if( foundFile )
97  return i;
98  return 0;
99 }
100 
101 bool
102 DDLParser::isParsed( const std::string& filename )
103 {
104  size_t found = isFound(filename);
105  if (found)
106  return parsed_[found];
107  return false;
108 }
109 
110 // Must receive a filename and path relative to the src directory of a CMSSW release
111 // e.g. DetectorDescription/test/myfile.xml
112 bool
113 DDLParser::parseOneFile( const std::string& fullname ) //, const std::string& url)
114 {
115  // std::string filename = expHandler_->extractFileName(fullname);
116  std::string filename = extractFileName(fullname);
117  // std::cout << "parseOneFile - fullname = " << fullname << std::endl;
118  // std::cout << "parseOneFile - filename = " << filename << std::endl;
119  edm::FileInPath fp(fullname);
120  std::string absoluteFileName = fp.fullPath();
121  size_t foundFile = isFound(filename);
122  if (!foundFile)
123  {
124  int fIndex = foundFile;
125  pair <std::string, std::string> pss;
126  pss.first = filename;
127  pss.second = absoluteFileName; //url+filename;
128  fIndex = nFiles_;
130  ++nFiles_;
131  parsed_[fIndex]=false;
132 
133  currFileName_ = fileNames_[fIndex].second;
134 
135  // in cleaning up try-catch blocks 2007-06-26 I decided to remove
136  // this because of CMSSW rules. but keep the commented way I used to
137  // do it...
138  // DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! try
139  // {
140  SAX2Parser_->setContentHandler(expHandler_);
141  expHandler_->setNameSpace( getNameSpace(filename) );
142  // std::cout << "0) namespace = " << getNameSpace(filename) << std::endl;
143  LogDebug ("DDLParser") << "ParseOneFile() Parsing: " << fileNames_[fIndex].second << std::endl;
144  parseFile ( fIndex );
145 
146  // }
147  // DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! catch (const XMLException& toCatch) {
148  // edm::LogError ("DDLParser") << "\nDDLParser::ParseOneFile, PASS1: XMLException while processing files... \n"
149  // << "Exception message is: \n"
150  // << StrX(toCatch.getMessage()) << "\n" ;
151  // XMLPlatformUtils::Terminate();
152  // throw (DDException(" See XMLException above. "));
153  // }
154 
155  // PASS 2:
156 
157  DCOUT_V('P', "DDLParser::ParseOneFile(): PASS2: Just before setting Xerces content and error handlers... ");
158 
159  // DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! try
160  // {
161 
162  SAX2Parser_->setContentHandler(fileHandler_);
163  // std::cout << "currFileName = " << currFileName_ << std::endl;
165  // std::cout << "1) namespace = " << getNameSpace(currFileName_) << std::endl;
166  parseFile ( fIndex );
167  parsed_[fIndex] = true;
168 
169  // }
170  // DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! catch (const XMLException& toCatch) {
171  // edm::LogError ("DDLParser") << "\nDDLParser::ParseOneFile, PASS2: XMLException while processing files... \n"
172  // << "Exception message is: \n"
173  // << StrX(toCatch.getMessage()) << "\n" ;
174  // XMLPlatformUtils::Terminate();
175  // throw (DDException(" See XMLException above."));
176  // }
177  }
178  else // was found and is parsed...
179  {
180  DCOUT('P', " WARNING: DDLParser::ParseOneFile() file " + filename
181  + " was already parsed as " + fileNames_[foundFile].second);
182  return true;
183  }
184  return false;
185 }
186 
187 // This is for parsing the content of a blob stored in the conditions system of CMS.
188 void
189 DDLParser::parse( const std::vector<unsigned char>& ablob, unsigned int bsize )
190 {
191  char* dummy(0);
192  MemBufInputSource mbis( &*ablob.begin(), bsize, dummy );
193  SAX2Parser_->parse(mbis);
194 }
195 
196 std::vector < std::string >
198 {
199  std::vector<std::string> flist;
200  for (FileNameHolder::const_iterator fit = fileNames_.begin(); fit != fileNames_.end(); ++fit)
201  {
202  flist.push_back(fit->second.first); // was .second (mec: 2003:02:19
203  }
204  return flist;
205 }
206 
207 void
209 {
210  edm::LogInfo ("DDLParser") << "File List:" << std::endl;
211  for (FileNameHolder::const_iterator it = fileNames_.begin(); it != fileNames_.end(); ++it)
212  edm::LogInfo ("DDLParser") << it->second.second << std::endl;
213 }
214 
215 void
217 {
218  co << "File List:" << std::endl;
219  for (FileNameHolder::const_iterator it = fileNames_.begin(); it != fileNames_.end(); ++it)
220  co << it->second.second << std::endl;
221 }
222 
223 int
225 {
226  // edm::LogInfo ("DDLParser") << "Start Parsing. Validation is set to " << dp.doValidation() << "." << std::endl;
227  edm::LogInfo ("DDLParser") << "Start Parsing. Validation is set off for the time being." << std::endl;
228  // prep for pass 1 through DDD XML
229  // // Since this block does nothing for CMSSW right now, I have taken it all out
230  // This clean-up involves interface changes such as the removal of doValidation() everywhere (OR NOT
231  // if I decide to keep it for other testing reasons.)
232  // if (dp.doValidation())
233  // {
234  // // DCOUT_V('P', "WARNING: PARSER VALIDATION IS TURNED OFF REGARDLESS OF <Schema... ELEMENT");
235  // SAX2Parser_->setFeature(XMLUni::fgSAX2CoreValidation, true);
236  // SAX2Parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
237  // // // SAX2Parser_->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
238  // }
239  // else
240  // {
241  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreValidation, false);
242  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, false);
243  // // SAX2Parser_->setFeature(XMLUni::fgXercesSchemaFullChecking, false);
244 
245  // }
246 
247  // This need be only done once, so might as well to it here.
248  size_t fileIndex = 0;
249  std::vector<std::string> fullFileName;
250 
251  for (; fileIndex < (dp.getFileList()).size(); ++fileIndex)
252  {
253  std::string ts = dp.getURLList()[fileIndex];
254  std::string tf = dp.getFileList()[fileIndex];
255  if ( ts.size() > 0 ) {
256  if ( ts[ts.size() - 1] == '/') {
257  fullFileName.push_back( ts + tf );
258  } else {
259  fullFileName.push_back( ts + "/" + tf );
260  }
261  } else {
262  fullFileName.push_back( tf );
263  }
264  }
265 
266  for (std::vector<std::string>::const_iterator fnit = fullFileName.begin();
267  fnit != fullFileName.end();
268  ++fnit)
269  {
270  size_t foundFile = isFound(extractFileName( *fnit ));
271 
272  if (!foundFile)
273  {
274  pair <std::string, std::string> pss;
275  pss.first = extractFileName( *fnit );
276  pss.second = *fnit;
277  fileNames_[nFiles_++] = pss;
278  parsed_[nFiles_ - 1]=false;
279  }
280  }
281 
282  // Start processing the files found in the config file.
283 
284  // PASS 1: This was added later (historically) to implement the DDD
285  // requirement for Expressions.
286  DCOUT('P', "DDLParser::parse(): PASS1: Just before setting Xerces content and error handlers... ");
287 
288 
289  // in cleaning up try-catch blocks 2007-06-26 I decided to remove
290  // this because of CMSSW rules. but keep the commented way I used to
291  // do it...
292  // DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! try
293  // {
294  SAX2Parser_->setContentHandler(expHandler_);
295  for (size_t i = 0; i < fileNames_.size(); ++i)
296  {
297  // seal::SealTimer t("DDLParser: parsing expressions of file " +fileNames_[i].first);
298  if (!parsed_[i])
299  {
300  currFileName_ = fileNames_[i].second;
301  // std::cout << "currFileName = " << currFileName_ << std::endl;
303  // std::cout << "2) namespace = " << getNameSpace(extractFileName(currFileName_)) << std::endl;
304  parseFile(i);
305  }
306  }
308  // }
309  // DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! catch (const XMLException& toCatch) {
310  // edm::LogInfo ("DDLParser") << "\nPASS1: XMLException while processing files... \n"
311  // << "Exception message is: \n"
312  // << StrX(toCatch.getMessage()) << "\n" ;
313  // XMLPlatformUtils::Terminate();
314  // // FIX use this after DEPRECATED stuff removed: throw(DDException("See XML Exception above"));
315  // return -1;
316  // }
317  // PASS 2:
318 
319  DCOUT('P', "DDLParser::parse(): PASS2: Just before setting Xerces content and error handlers... ");
320 
321  // in cleaning up try-catch blocks 2007-06-26 I decided to remove
322  // this because of CMSSW rules. but keep the commented way I used to
323  // do it...
324  // DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! try
325  // {
326  SAX2Parser_->setContentHandler(fileHandler_);
327 
328  // No need to validate (regardless of user's doValidation
329  // because the files have already been validated on the first pass.
330  // This optimization suggested by Martin Liendl.
331  // SAX2Parser_->setFeature(StrX("http://xml.org/sax/features/validation"), false); // optional
332  // SAX2Parser_->setFeature(StrX("http://xml.org/sax/features/namespaces"), false); // optional
333  // SAX2Parser_->setFeature(StrX("http://apache.org/xml/features/validation/dynamic"), false);
334 
335 
336  // Process files again.
337  for (size_t i = 0; i < fileNames_.size(); ++i)
338  {
339  // seal::SealTimer t("DDLParser: parsing all elements of file " +fileNames_[i].first);
340  if (!parsed_[i]) {
341  currFileName_ = fileNames_[i].second;
342  // std::cout << "currFileName = " << currFileName_ << std::endl;
344  // std::cout << "3) namespace = " << getNameSpace(extractFileName(currFileName_)) << std::endl;
345  parseFile(i);
346  parsed_[i] = true;
347  pair<std::string, std::string> namePair = fileNames_[i];
348  LogDebug ("DDLParser") << "Completed parsing file " << namePair.second << std::endl;
349  }
350  }
351  // }
352  // DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! catch (const XMLException& toCatch) {
353  // edm::LogError ("DDLParser") << "\nPASS2: XMLException while processing files... \n"
354  // << "Exception message is: \n"
355  // << StrX(toCatch.getMessage()) << "\n" ;
356  // XMLPlatformUtils::Terminate();
357  // return -1;
358  // }
359  return 0;
360 }
361 
362 void
363 DDLParser::parseFile( const int& numtoproc )
364 {
365  if (!parsed_[numtoproc])
366  {
367  const std::string & fname = fileNames_[numtoproc].second;
368 
369  // in cleaning up try-catch blocks 2007-06-26 I decided to remove
370  // this because of CMSSW rules. but keep the commented way I used to
371  // do it...
372  // DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! try
373  // {
375  SAX2Parser_->parse(currFileName_.c_str());
376  // }
377  // DO NOT UNCOMMENT FOR ANY RELEASE; ONLY FOR DEBUGGING! catch (const XMLException& toCatch)
378  // {
379  // std::string e("\nWARNING: DDLParser::parseFile, File: '");
380  // e += currFileName_ + "'\n"
381  // + "Exception message is: \n"
382  // + std::string(StrX(toCatch.getMessage()).localForm()) + "\n";
383  // throw(DDException(e));
384  // }
385  }
386  else
387  {
388  DCOUT('P', "\nWARNING: File " + fileNames_[numtoproc].first
389  + " has already been processed as " + fileNames_[numtoproc].second);
390  }
391 }
392 
393 // Return the name of the Current file being processed by the parser.
394 std::string
396 {
397  return currFileName_;
398 }
399 
400 void
402 {
403  fileNames_.clear();
404  parsed_.clear();
405 }
406 
407 std::string
408 DDLParser::extractFileName( std::string fullname )
409 {
410  std::string ret = "";
411  size_t bit = fullname.rfind('/');
412  if ( bit < fullname.size() - 2 ) {
413  ret=fullname.substr(bit+1);
414  }
415  return ret;
416 }
417 
418 std::string
419 DDLParser::getNameSpace( const std::string& fname )
420 {
421  size_t j = 0;
422  std::string ret="";
423  while (j < fname.size() && fname[j] != '.')
424  ++j;
425  if (j < fname.size() && fname[j] == '.')
426  ret = fname.substr(0, j);
427  return ret;
428 }
#define LogDebug(id)
int i
Definition: DBlmapReader.cc:9
int parse(const DDLDocumentProvider &dp)
Parse all files. Return is meaningless.
Definition: DDLParser.cc:224
std::map< int, bool > parsed_
Parse status of a given file.
Definition: DDLParser.h:161
std::pair< ALIstring, ALIstring > pss
Definition: Fit.h:27
std::string getNameSpace(const std::string &fname)
Definition: DDLParser.cc:419
std::string extractFileName(std::string fullname)
Definition: DDLParser.cc:408
DDLSAX2FileHandler * fileHandler_
Definition: DDLParser.h:175
SAX2XMLReader * SAX2Parser_
SAX2XMLReader is one way of parsing.
Definition: DDLParser.h:173
std::string getCurrFileName()
Report which file currently being processed (or last processed).
Definition: DDLParser.cc:395
std::string currFileName_
Which file is currently being processed.
Definition: DDLParser.h:170
FileNameHolder fileNames_
List of files to be processed, obtained from the DDLDocumentProvider.
Definition: DDLParser.h:158
type of data representation of DDCompactView
Definition: DDCompactView.h:81
~DDLParser()
Destructor terminates the XMLPlatformUtils (as required by Xerces)
Definition: DDLParser.cc:55
DDLSAX2FileHandler is the SAX2 Handler for XML files found in the configuration file.
virtual const std::vector< std::string > & getFileList(void) const =0
Return a list of files as a vector of strings.
U second(std::pair< T, U > const &p)
void dumpElementTypeCounter()
This dumps some statistics on elements encountered in the file.
size_t isFound(const std::string &filename)
Is the file already known by the DDLParser? Returns 0 if not found, and index if found.
Definition: DDLParser.cc:82
void AlgoInit()
Definition: AlgoInit.cc:28
DDLSAX2ExpressionHandler is the first pass SAX2 Handler for XML files found in the configuration file...
DDLSAX2ExpressionHandler * expHandler_
Definition: DDLParser.h:176
bool parseOneFile(const std::string &filename)
Process a single files.
Definition: DDLParser.cc:113
DDLSAX2Handler * errHandler_
Definition: DDLParser.h:177
int j
Definition: DBlmapReader.cc:9
bool first
Definition: L1TdeRCT.cc:79
void clearFiles()
Clear the file list - see Warning!
Definition: DDLParser.cc:401
DDLSAX2FileHandler * getDDLSAX2FileHandler()
To get the parent this class allows access to the handler.
Definition: DDLParser.cc:76
virtual const std::vector< std::string > & getURLList(void) const =0
Return a list of urls as a vector of strings.
void parseFile(const int &numtoproc)
Parse File. Just to hold some common looking code.
Definition: DDLParser.cc:363
#define DCOUT_V(M_v_Y, M_v_S)
Definition: DDdebug.h:54
std::vector< std::string > getFileList()
Return list of files.
Definition: DDLParser.cc:197
SAX2XMLReader * getXMLParser()
Get the SAX2Parser from the DDLParser. USE WITH CAUTION. Set your own handler, etc.
Definition: DDLParser.cc:70
string fname
main script
virtual void setNameSpace(const std::string &nms)
DDLDocumentProvider provides a set of URLs and filenames.
tuple filename
Definition: lut2db_cfg.py:20
DDLSAX2Handler inherits from Xerces C++ DefaultHandler.
void dumpFileList()
Print out the list of files.
Definition: DDLParser.cc:208
std::string fullPath() const
Definition: FileInPath.cc:170
#define DCOUT(M_v_Y, M_v_S)
Definition: DDdebug.h:53
tuple size
Write out results.
bool isParsed(const std::string &filename)
Is the file already parsed?
Definition: DDLParser.cc:102
int nFiles_
Number of files + 1.
Definition: DDLParser.h:164