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 
22 DDLParser::DDLParser(DDCompactView& cpv) : cpv_(cpv), nFiles_(0) {
24  SAX2Parser_ = XMLReaderFactory::createXMLReader();
25 
26  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreValidation, false); // optional
27  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, false); // optional
28 
33  SAX2Parser_->setErrorHandler(errHandler_);
34  SAX2Parser_->setContentHandler(fileHandler_);
35 }
36 
39  // clean up and leave
40  delete expHandler_;
41  delete fileHandler_;
42  delete errHandler_;
43  delete elementRegistry_;
45 }
46 
52 
54 
56  FileNameHolder::const_iterator it = fileNames_.begin();
57  size_t i = 1;
58  bool foundFile = false;
59  while (it != fileNames_.end() && !foundFile) {
60  if (it->second.first == filename) {
61  foundFile = true;
62  } else
63  ++i;
64  ++it;
65  }
66  if (foundFile)
67  return i;
68  return 0;
69 }
70 
72  size_t found = isFound(filename);
73  if (found)
74  return parsed_[found];
75  return false;
76 }
77 
78 // Must receive a filename and path relative to the src directory of a CMSSW release
79 // e.g. DetectorDescription/test/myfile.xml
80 bool DDLParser::parseOneFile(const std::string& fullname) {
82  edm::FileInPath fp(fullname);
83  std::string absoluteFileName = fp.fullPath();
84  size_t foundFile = isFound(filename);
85  if (!foundFile) {
86  pair<std::string, std::string> pss;
87  pss.first = filename;
88  pss.second = absoluteFileName; //url+filename;
89  int fIndex = nFiles_;
91  ++nFiles_;
92  parsed_[fIndex] = false;
93 
94  currFileName_ = fileNames_[fIndex].second;
95 
96  SAX2Parser_->setContentHandler(expHandler_);
98 
99  LogDebug("DDLParser") << "ParseOneFile() Parsing: " << fileNames_[fIndex].second << std::endl;
100  parseFile(fIndex);
102  // PASS 2:
103 
104  SAX2Parser_->setContentHandler(fileHandler_);
106  parseFile(fIndex);
107  parsed_[fIndex] = true;
108  } else // was found and is parsed...
109  {
110  return true;
111  }
112  return false;
113 }
114 
115 // This is for parsing the content of a blob stored in the conditions system of CMS.
116 void DDLParser::parse(const std::vector<unsigned char>& ablob, unsigned int bsize) {
117  char* dummy(nullptr);
118  MemBufInputSource mbis(&*ablob.begin(), bsize, dummy);
119  SAX2Parser_->parse(mbis);
121 }
122 
124  edm::LogInfo("DDLParser") << "Start Parsing. Validation is set off for the time being." << std::endl;
125  // prep for pass 1 through DDD XML
126  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreValidation, false);
127  SAX2Parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, false);
128 
129  // This need be only done once, so might as well to it here.
130  size_t fileIndex = 0;
131  std::vector<std::string> fullFileName;
132  const std::vector<std::string>& fileList = dp.getFileList();
133  const std::vector<std::string>& urlList = dp.getURLList();
134 
135  for (; fileIndex < fileList.size(); ++fileIndex) {
136  std::string ts = urlList[fileIndex];
137  std::string tf = fileList[fileIndex];
138  if (!ts.empty()) {
139  if (ts[ts.size() - 1] == '/') {
140  fullFileName.emplace_back(ts + tf);
141  } else {
142  fullFileName.emplace_back(ts + "/" + tf);
143  }
144  } else {
145  fullFileName.emplace_back(tf);
146  }
147  }
148 
149  for (const auto& fnit : fullFileName) {
150  size_t foundFile = isFound(extractFileName(fnit));
151 
152  if (!foundFile) {
153  pair<std::string, std::string> pss;
154  pss.first = extractFileName(fnit);
155  pss.second = fnit;
156  fileNames_[nFiles_++] = pss;
157  parsed_[nFiles_ - 1] = false;
158  }
159  }
160 
161  // Start processing the files found in the config file.
162  assert(fileNames_.size() == nFiles_);
163 
164  // PASS 1: This was added later (historically) to implement the DDD
165  // requirement for Expressions.
166 
167  SAX2Parser_->setContentHandler(expHandler_);
168  for (size_t i = 0; i < nFiles_; ++i) {
169  if (!parsed_[i]) {
170  currFileName_ = fileNames_[i].second;
172  parseFile(i);
173  }
174  }
176 
177  // PASS 2:
178 
179  SAX2Parser_->setContentHandler(fileHandler_);
180 
181  // No need to validate (regardless of user's doValidation
182  // because the files have already been validated on the first pass.
183  // This optimization suggested by Martin Liendl.
184 
185  // Process files again.
186  for (size_t i = 0; i < nFiles_; ++i) {
187  if (!parsed_[i]) {
188  currFileName_ = fileNames_[i].second;
190  parseFile(i);
191  parsed_[i] = true;
192  pair<std::string, std::string> namePair = fileNames_[i];
193  LogDebug("DDLParser") << "Completed parsing file " << namePair.second << std::endl;
194  }
195  }
196  return 0;
197 }
198 
199 void DDLParser::parseFile(const int& numtoproc) {
200  if (!parsed_[numtoproc]) {
201  const std::string& fname = fileNames_[numtoproc].second;
202 
204  SAX2Parser_->parse(currFileName_.c_str());
205  }
206 }
207 
209  fileNames_.clear();
210  parsed_.clear();
211 }
212 
214  return (fullname.substr(fullname.rfind('/') + 1));
215 }
216 
218  size_t j = 0;
219  std::string ret = "";
220  while (j < fname.size() && fname[j] != '.')
221  ++j;
222  if (j < fname.size() && fname[j] == '.')
223  ret = fname.substr(0, j);
224  return ret;
225 }
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:543
DDLParser::elementRegistry_
DDLElementRegistry * elementRegistry_
Definition: DDLParser.h:153
mps_fire.i
i
Definition: mps_fire.py:428
MessageLogger.h
DDLParser::parse
int parse(const DDLDocumentProvider &dp)
Parse all files. Return is meaningless.
Definition: DDLParser.cc:123
DDLParser::SAX2XMLReader
XERCES_CPP_NAMESPACE::SAX2XMLReader SAX2XMLReader
Definition: DDLParser.h:65
DDLParser::isParsed
bool isParsed(const std::string &filename)
Is the file already parsed?
Definition: DDLParser.cc:71
DDLParser::SAX2Parser_
SAX2XMLReader * SAX2Parser_
SAX2XMLReader is one way of parsing.
Definition: DDLParser.h:151
pss
std::pair< ALIstring, ALIstring > pss
Definition: Fit.h:25
DDLParser::isFound
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:55
DDLDocumentProvider.h
DDLParser::currFileName_
std::string currFileName_
Which file is currently being processed.
Definition: DDLParser.h:148
DDLSAX2Handler.h
cms::cuda::assert
assert(be >=bs)
DDLElementRegistry
The main class for processing parsed elements.
Definition: DDLElementRegistry.h:23
personalPlayback.fp
fp
Definition: personalPlayback.py:523
XERCES_CPP_NAMESPACE_USE
Definition: XMLConfigWriter.cc:40
DDLParser.h
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
DDLParser::errHandler_
DDLSAX2Handler * errHandler_
Definition: DDLParser.h:156
DDLParser::~DDLParser
~DDLParser()
Destructor terminates the XMLPlatformUtils (as required by Xerces)
Definition: DDLParser.cc:38
newFWLiteAna.found
found
Definition: newFWLiteAna.py:118
DDLSAX2FileHandler
DDLSAX2FileHandler is the SAX2 Handler for XML files found in the configuration file.
Definition: DDLSAX2FileHandler.h:35
DDLDocumentProvider
DDLDocumentProvider provides a set of URLs and filenames.
Definition: DDLDocumentProvider.h:20
DDLParser::getNameSpace
const std::string getNameSpace(const std::string &fname)
Definition: DDLParser.cc:217
DDLParser::clearFiles
void clearFiles()
Clear the file list - see Warning!
Definition: DDLParser.cc:208
edm::FileInPath
Definition: FileInPath.h:61
DDLParser::nFiles_
size_t nFiles_
Number of files + 1.
Definition: DDLParser.h:145
DDLParser::getXMLParser
SAX2XMLReader * getXMLParser()
Get the SAX2Parser from the DDLParser. USE WITH CAUTION. Set your own handler, etc.
Definition: DDLParser.cc:51
DDLSAX2Handler
DDLSAX2Handler inherits from Xerces C++ DefaultHandler.
Definition: DDLSAX2Handler.h:27
Calorimetry_cff.dp
dp
Definition: Calorimetry_cff.py:158
DDCompactView
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
DDLParser::getDDLSAX2FileHandler
DDLSAX2FileHandler * getDDLSAX2FileHandler()
To get the parent this class allows access to the handler.
Definition: DDLParser.cc:53
DDLParser::extractFileName
const std::string extractFileName(const std::string &fullname)
Definition: DDLParser.cc:213
FileInPath.h
DDLSAX2ExpressionHandler
DDLSAX2ExpressionHandler is the first pass SAX2 Handler for XML files found in the configuration file...
Definition: DDLSAX2ExpressionHandler.h:25
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:233
Xerces.h
cms::concurrency::xercesTerminate
void xercesTerminate()
Definition: Xerces.cc:23
DDLParser::parsed_
std::map< int, bool > parsed_
Parse status of a given file.
Definition: DDLParser.h:142
DDLSAX2ExpressionHandler.h
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
DDLSAX2Handler::setNameSpace
virtual void setNameSpace(const std::string &nms)
Definition: DDLSAX2Handler.cc:81
alignmentValidation.fname
string fname
main script
Definition: alignmentValidation.py:959
std
Definition: JetResolutionObject.h:76
DDLParser::fileHandler_
DDLSAX2FileHandler * fileHandler_
Definition: DDLParser.h:154
DDLParser::fileNames_
FileNameHolder fileNames_
List of files to be processed, obtained from the DDLDocumentProvider.
Definition: DDLParser.h:139
DDLParser::parseOneFile
bool parseOneFile(const std::string &filename)
Process a single files.
Definition: DDLParser.cc:80
DDLParser::DDLParser
DDLParser()=delete
DDLParser::expHandler_
DDLSAX2ExpressionHandler * expHandler_
Definition: DDLParser.h:155
dummy
Definition: DummySelector.h:38
DDLParser::parseFile
void parseFile(const int &numtoproc)
Parse File. Just to hold some common looking code.
Definition: DDLParser.cc:199
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
DDLSAX2FileHandler.h
cms::concurrency::xercesInitialize
void xercesInitialize()
Definition: Xerces.cc:18
DDLSAX2FileHandler::createDDConstants
void createDDConstants() const
creates all DDConstant from the evaluator which has been already 'filled' in the first scan of the do...
Definition: DDLSAX2FileHandler.cc:99