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 }
int parse(const DDLDocumentProvider &dp)
Parse all files. Return is meaningless.
Definition: DDLParser.cc:123
std::map< int, bool > parsed_
Parse status of a given file.
Definition: DDLParser.h:142
std::pair< ALIstring, ALIstring > pss
Definition: Fit.h:25
DDLParser()=delete
ret
prodAgent to be discontinued
void xercesTerminate()
Definition: Xerces.cc:23
XERCES_CPP_NAMESPACE::SAX2XMLReader SAX2XMLReader
Definition: DDLParser.h:65
DDLSAX2FileHandler * fileHandler_
Definition: DDLParser.h:154
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
assert(be >=bs)
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
~DDLParser()
Destructor terminates the XMLPlatformUtils (as required by Xerces)
Definition: DDLParser.cc:38
DDLSAX2FileHandler is the SAX2 Handler for XML files found in the configuration file.
std::string const extractFileName(const std::string &fullname)
Definition: DDLParser.cc:213
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
DDLSAX2ExpressionHandler is the first pass SAX2 Handler for XML files found in the configuration file...
DDLSAX2ExpressionHandler * expHandler_
Definition: DDLParser.h:155
bool parseOneFile(const std::string &filename)
Process a single files.
Definition: DDLParser.cc:80
void createDDConstants() const
creates all DDConstant from the evaluator which has been already &#39;filled&#39; in the first scan of the do...
DDLSAX2Handler * errHandler_
Definition: DDLParser.h:156
DDLElementRegistry * elementRegistry_
Definition: DDLParser.h:153
void clearFiles()
Clear the file list - see Warning!
Definition: DDLParser.cc:208
DDLSAX2FileHandler * getDDLSAX2FileHandler()
To get the parent this class allows access to the handler.
Definition: DDLParser.cc:53
Log< level::Info, false > LogInfo
void parseFile(const int &numtoproc)
Parse File. Just to hold some common looking code.
Definition: DDLParser.cc:199
size_t nFiles_
Number of files + 1.
Definition: DDLParser.h:145
SAX2XMLReader * getXMLParser()
Get the SAX2Parser from the DDLParser. USE WITH CAUTION. Set your own handler, etc.
Definition: DDLParser.cc:51
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:217
DDLSAX2Handler inherits from Xerces C++ DefaultHandler.
The main class for processing parsed elements.
#define LogDebug(id)
bool isParsed(const std::string &filename)
Is the file already parsed?
Definition: DDLParser.cc:71