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