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