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