CMS 3D CMS Logo

DDLParser.cc
Go to the documentation of this file.
9 #include <xercesc/framework/MemBufInputSource.hpp>
10 #include <xercesc/sax2/XMLReaderFactory.hpp>
11 #include <xercesc/util/XMLUni.hpp>
12 
13 #include <iostream>
14 
15 class DDCompactView;
16 
18 
19 using namespace std;
20 
23  : cpv_( cpv ),
24  nFiles_( 0 )
25 {
27  SAX2Parser_ = XMLReaderFactory::createXMLReader();
28 
29  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreValidation, false); // optional
30  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, false); // optional
31 
35  SAX2Parser_->setErrorHandler(errHandler_);
36  SAX2Parser_->setContentHandler(fileHandler_);
37 }
38 
41 {
42  // clean up and leave
43  delete expHandler_;
44  delete fileHandler_;
45  delete errHandler_;
47 }
48 
55 {
56  return SAX2Parser_;
57 }
58 
61 {
62  return fileHandler_;
63 }
64 
65 size_t
67 {
68  FileNameHolder::const_iterator it = fileNames_.begin();
69  size_t i = 1;
70  bool foundFile = false;
71  while( it != fileNames_.end() && !foundFile )
72  {
73  if( it->second.first == filename )
74  {
75  foundFile = true;
76  }
77  else ++i;
78  ++it;
79  }
80  if( foundFile )
81  return i;
82  return 0;
83 }
84 
85 bool
87 {
88  size_t found = isFound(filename);
89  if (found)
90  return parsed_[found];
91  return false;
92 }
93 
94 // Must receive a filename and path relative to the src directory of a CMSSW release
95 // e.g. DetectorDescription/test/myfile.xml
96 bool
98 {
99  std::string filename = extractFileName( fullname );
100  edm::FileInPath fp( fullname );
101  std::string absoluteFileName = fp.fullPath();
102  size_t foundFile = isFound( filename );
103  if( !foundFile )
104  {
105  pair< std::string, std::string > pss;
106  pss.first = filename;
107  pss.second = absoluteFileName; //url+filename;
108  int fIndex = nFiles_;
110  ++nFiles_;
111  parsed_[fIndex] = false;
112 
113  currFileName_ = fileNames_[fIndex].second;
114 
115  SAX2Parser_->setContentHandler( expHandler_ );
116  expHandler_->setNameSpace( getNameSpace( filename ));
117 
118  LogDebug ("DDLParser") << "ParseOneFile() Parsing: " << fileNames_[fIndex].second << std::endl;
119  parseFile( fIndex );
120 
121  // PASS 2:
122 
123  SAX2Parser_->setContentHandler(fileHandler_);
125  parseFile ( fIndex );
126  parsed_[fIndex] = true;
127  }
128  else // was found and is parsed...
129  {
130  return true;
131  }
132  return false;
133 }
134 
135 // This is for parsing the content of a blob stored in the conditions system of CMS.
136 void
137 DDLParser::parse( const std::vector<unsigned char>& ablob, unsigned int bsize )
138 {
139  char* dummy(nullptr);
140  MemBufInputSource mbis( &*ablob.begin(), bsize, dummy );
141  SAX2Parser_->parse(mbis);
142 }
143 
144 int
146 {
147  edm::LogInfo ("DDLParser") << "Start Parsing. Validation is set off for the time being." << std::endl;
148  // prep for pass 1 through DDD XML
149  SAX2Parser_->setFeature( XMLUni::fgSAX2CoreValidation, false );
150  SAX2Parser_->setFeature( XMLUni::fgSAX2CoreNameSpaces, false );
151 
152  // This need be only done once, so might as well to it here.
153  size_t fileIndex = 0;
154  std::vector<std::string> fullFileName;
155  const std::vector < std::string >& fileList = dp.getFileList();
156  const std::vector < std::string >& urlList = dp.getURLList();
157 
158  for(; fileIndex < fileList.size(); ++fileIndex )
159  {
160  std::string ts = urlList[fileIndex];
161  std::string tf = fileList[fileIndex];
162  if ( !ts.empty() ) {
163  if ( ts[ts.size() - 1] == '/') {
164  fullFileName.emplace_back( ts + tf );
165  } else {
166  fullFileName.emplace_back( ts + "/" + tf );
167  }
168  } else {
169  fullFileName.emplace_back( tf );
170  }
171  }
172 
173  for( const auto& fnit : fullFileName )
174  {
175  size_t foundFile = isFound( extractFileName( fnit ));
176 
177  if( !foundFile )
178  {
179  pair <std::string, std::string> pss;
180  pss.first = extractFileName( fnit );
181  pss.second = fnit;
182  fileNames_[nFiles_++] = pss;
183  parsed_[nFiles_ - 1] = false;
184  }
185  }
186 
187  // Start processing the files found in the config file.
188  assert( fileNames_.size() == nFiles_ );
189 
190  // PASS 1: This was added later (historically) to implement the DDD
191  // requirement for Expressions.
192 
193  SAX2Parser_->setContentHandler(expHandler_);
194  for( size_t i = 0; i < nFiles_; ++i )
195  {
196  if( !parsed_[i])
197  {
198  currFileName_ = fileNames_[i].second;
200  parseFile(i);
201  }
202  }
203 
204  // PASS 2:
205 
206  SAX2Parser_->setContentHandler(fileHandler_);
207 
208  // No need to validate (regardless of user's doValidation
209  // because the files have already been validated on the first pass.
210  // This optimization suggested by Martin Liendl.
211 
212  // Process files again.
213  for( size_t i = 0; i < nFiles_; ++i )
214  {
215  if( !parsed_[i]) {
216  currFileName_ = fileNames_[i].second;
218  parseFile(i);
219  parsed_[i] = true;
220  pair<std::string, std::string> namePair = fileNames_[i];
221  LogDebug ("DDLParser") << "Completed parsing file " << namePair.second << std::endl;
222  }
223  }
224  return 0;
225 }
226 
227 void
228 DDLParser::parseFile( const int& numtoproc )
229 {
230  if (!parsed_[numtoproc])
231  {
232  const std::string & fname = fileNames_[numtoproc].second;
233 
235  SAX2Parser_->parse(currFileName_.c_str());
236  }
237 }
238 
239 void
241 {
242  fileNames_.clear();
243  parsed_.clear();
244 }
245 
246 std::string const
248 {
249  return( fullname.substr( fullname.rfind( '/' ) + 1 ));
250 }
251 
252 std::string const
254 {
255  size_t j = 0;
256  std::string ret="";
257  while (j < fname.size() && fname[j] != '.')
258  ++j;
259  if (j < fname.size() && fname[j] == '.')
260  ret = fname.substr(0, j);
261  return ret;
262 }
#define LogDebug(id)
int parse(const DDLDocumentProvider &dp)
Parse all files. Return is meaningless.
Definition: DDLParser.cc:145
std::map< int, bool > parsed_
Parse status of a given file.
Definition: DDLParser.h:142
std::pair< ALIstring, ALIstring > pss
Definition: Fit.h:27
DDLParser()=delete
void xercesTerminate()
Definition: Xerces.cc:23
XERCES_CPP_NAMESPACE::SAX2XMLReader SAX2XMLReader
Definition: DDLParser.h:65
DDLSAX2FileHandler * fileHandler_
Definition: DDLParser.h:153
SAX2XMLReader * SAX2Parser_
SAX2XMLReader is one way of parsing.
Definition: DDLParser.h:151
std::string currFileName_
Which file is currently being processed.
Definition: DDLParser.h:148
FileNameHolder fileNames_
List of files to be processed, obtained from the DDLDocumentProvider.
Definition: DDLParser.h:139
void xercesInitialize()
Definition: Xerces.cc:18
type of data representation of DDCompactView
Definition: DDCompactView.h:90
~DDLParser()
Destructor terminates the XMLPlatformUtils (as required by Xerces)
Definition: DDLParser.cc:40
DDLSAX2FileHandler is the SAX2 Handler for XML files found in the configuration file.
std::string const extractFileName(const std::string &fullname)
Definition: DDLParser.cc:247
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:66
virtual const std::vector< std::string > & getURLList(void) const =0
Return a list of urls as a vector of strings.
DDLSAX2ExpressionHandler is the first pass SAX2 Handler for XML files found in the configuration file...
DDLSAX2ExpressionHandler * expHandler_
Definition: DDLParser.h:154
bool parseOneFile(const std::string &filename)
Process a single files.
Definition: DDLParser.cc:97
DDLSAX2Handler * errHandler_
Definition: DDLParser.h:155
void clearFiles()
Clear the file list - see Warning!
Definition: DDLParser.cc:240
DDLSAX2FileHandler * getDDLSAX2FileHandler()
To get the parent this class allows access to the handler.
Definition: DDLParser.cc:60
void parseFile(const int &numtoproc)
Parse File. Just to hold some common looking code.
Definition: DDLParser.cc:228
virtual const std::vector< std::string > & getFileList(void) const =0
Return a list of files as a vector of strings.
size_t nFiles_
Number of files + 1.
Definition: DDLParser.h:145
auto dp
Definition: deltaR.h:22
SAX2XMLReader * getXMLParser()
Get the SAX2Parser from the DDLParser. USE WITH CAUTION. Set your own handler, etc.
Definition: DDLParser.cc:54
string fname
main script
virtual void setNameSpace(const std::string &nms)
DDLDocumentProvider provides a set of URLs and filenames.
std::string const getNameSpace(const std::string &fname)
Definition: DDLParser.cc:253
DDLSAX2Handler inherits from Xerces C++ DefaultHandler.
std::string fullPath() const
Definition: FileInPath.cc:184
bool isParsed(const std::string &filename)
Is the file already parsed?
Definition: DDLParser.cc:86