CMS 3D CMS Logo

HcalPatternXMLParser.cc
Go to the documentation of this file.
1 #include "xercesc/sax2/SAX2XMLReader.hpp"
2 #include <xercesc/sax2/XMLReaderFactory.hpp>
3 #include "xercesc/sax2/DefaultHandler.hpp"
4 #include "xercesc/sax2/Attributes.hpp"
6 #include <xercesc/framework/MemBufInputSource.hpp>
9 #include <ostream>
10 
12 public:
13  std::unique_ptr<SAX2XMLReader> parser;
14 };
15 
18  if (m_parser != nullptr)
19  delete m_parser;
20 }
21 
22 /*
23  Example
24  <pre>
25  <CFGBrick>
26  <Parameter name='IETA' type='int'>14</Parameter>
27  <Parameter name='IPHI' type='int'>2</Parameter>
28  <Parameter name='DEPTH' type='int'>1</Parameter>
29  <Parameter name='CRATE' type='int'>1</Parameter>
30  <Parameter name='SLOT' type='int'>8</Parameter>
31  <Parameter name='TOPBOTTOM' type='int'>0</Parameter>
32  <Parameter name='CHANNEL' type='int'>6</Parameter>
33  <Parameter name='LUT_TYPE' type='int'>1</Parameter>
34  <Parameter name='CREATIONTAG' type='string'>Identity</Parameter>
35  <Parameter name='CREATIONSTAMP' type='string'>2005-03-08 11:44:34</Parameter>
36  <Parameter name='FORMATREVISION' type='string'>1</Parameter>
37  <Parameter name='TARGETFIRMWARE' type='string'>0</Parameter>
38  <Parameter name='GENERALIZEDINDEX' type='int'>140211</Parameter>
39  <Data elements='128' encoding='hex'> .. </Data>
40  </CFGBrick>
41  </pre>
42  */
43 
44 class ConfigurationDBHandler : public DefaultHandler {
46 
47 public:
48  ConfigurationDBHandler(std::map<std::string, std::string>& parameters,
49  std::vector<std::string>& items,
50  std::string& encoding)
52  m_mode = md_Idle;
53  xc_Parameter = XMLString::transcode("Parameter");
54  xc_Data = XMLString::transcode("Data");
55  xc_name = XMLString::transcode("name");
56  xc_type = XMLString::transcode("type");
57  xc_elements = XMLString::transcode("elements");
58  xc_encoding = XMLString::transcode("encoding");
59  m_items.clear();
60  m_parameters.clear();
61  }
69  }
70  void startElement(const XMLCh* const uri,
71  const XMLCh* const localname,
72  const XMLCh* const qname,
73  const Attributes& attrs) override;
74  void endElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname) override;
75  void characters(const XMLCh* const chars, const XMLSize_t length) override;
76  void ignorableWhitespace(const XMLCh* const chars, const XMLSize_t length) override;
77 
78 private:
79  inline bool cvt2String(const XMLCh* val, std::string& ou) {
80  if (val == nullptr)
81  return false;
82  char* tool = XMLString::transcode(val);
83  ou = tool;
84  XMLString::release(&tool);
85  return true;
86  }
89  int n_elements;
91  std::vector<std::string>& m_items;
92  std::map<std::string, std::string>& m_parameters;
93  char m_workc[512];
94  XMLCh m_workx[256];
95 };
96 
97 void ConfigurationDBHandler::startElement(const XMLCh* const uri,
98  const XMLCh* const localname,
99  const XMLCh* const qname,
100  const Attributes& attrs) {
101  if (m_mode != md_Idle)
102  return;
103  if (!XMLString::compareIString(localname, xc_Parameter)) {
104  // parameter name
105  if (!cvt2String(attrs.getValue(xc_name), m_pname))
106  return;
107  // parameter type
108  if (!cvt2String(attrs.getValue(xc_type), m_ptype))
109  return;
110  // switch mode
112  m_text = "";
113  } else if (!XMLString::compareIString(localname, xc_Data)) {
114  // elements
115  std::string strElements;
116  if (!cvt2String(attrs.getValue(xc_elements), strElements))
117  return;
118  n_elements = atoi(strElements.c_str());
119  // encoding
120  m_dataEncoding = "";
121  cvt2String(attrs.getValue(xc_encoding), m_dataEncoding);
122  // switch mode
123  m_mode = md_Data;
124  m_text = "";
125  }
126 }
127 void ConfigurationDBHandler::endElement(const XMLCh* const uri,
128  const XMLCh* const localname,
129  const XMLCh* const qname) {
130  if (m_mode == md_Idle)
131  return;
132 
133  if (m_mode == md_Parameter) {
134  m_parameters[m_pname] = m_text; // ignore the type for now...
135  } else if (m_mode == md_Data) {
136  // parse the text
138  for (std::string::iterator q = m_text.begin(); q != m_text.end(); q++) {
139  if (isspace(*q)) {
140  if (entry.empty())
141  continue;
142  m_items.push_back(entry);
143  entry = "";
144  } else
145  entry += *q;
146  }
147  }
148 
149  m_mode = md_Idle;
150 }
151 void ConfigurationDBHandler::ignorableWhitespace(const XMLCh* chars, const XMLSize_t length) {
152  if (m_mode == md_Idle)
153  return;
154  m_text += ' ';
155 }
156 void ConfigurationDBHandler::characters(const XMLCh* chars, const XMLSize_t length) {
157  if (m_mode == md_Idle)
158  return;
159  unsigned int offset = 0;
160  while (offset < length) {
161  unsigned int i = 0;
162  for (i = 0; i < length - offset && i < 255; i++)
163  m_workx[i] = chars[i + offset];
164  m_workx[i] = 0; // terminate string
165  XMLString::transcode(m_workx, m_workc, 511);
166  m_text += m_workc;
167  offset += i;
168  }
169 }
170 
171 void HcalPatternXMLParser::parse(const std::string& xmlDocument,
172  std::map<std::string, std::string>& parameters,
173  std::vector<std::string>& items,
174  std::string& encoding) {
175  // uses XERCES SAX2 parser
176  ConfigurationDBHandler handler(parameters, items, encoding);
177 
178  try {
179  if (m_parser == nullptr) {
181  m_parser->parser = std::unique_ptr<xercesc::SAX2XMLReader>(xercesc::XMLReaderFactory::createXMLReader());
182  }
183 
184  MemBufInputSource src((const unsigned char*)xmlDocument.c_str(), xmlDocument.length(), "hcal::PatternReader");
185  m_parser->parser->setContentHandler(&handler);
186  m_parser->parser->parse(src);
187  } catch (std::exception& ex) {
188  throw cms::Exception("ParseError") << ex.what();
189  }
190 }
191 
192 void HcalPatternXMLParser::parse(const std::string& xmlDocument,
193  std::map<std::string, std::string>& parameters,
194  std::vector<uint32_t>& data) {
195  std::vector<std::string> items;
196  std::string encoding;
197 
198  this->parse(xmlDocument, parameters, items, encoding);
199  int formatting = 0;
200  if (encoding == "dec")
201  formatting = 10;
202  if (encoding == "hex")
203  formatting = 16;
204 
205  data.clear();
206  for (std::vector<std::string>::const_iterator i = items.begin(); i != items.end(); i++)
207  data.push_back(strtol(i->c_str(), nullptr, formatting));
208 }
BeamSpotPI::parameters
parameters
Definition: BeamSpotPayloadInspectorHelper.h:29
ConfigurationDBHandler::xc_Parameter
XMLCh * xc_Parameter
Definition: ConfigurationDatabaseStandardXMLParser.cc:81
mps_fire.i
i
Definition: mps_fire.py:428
ConfigurationDBHandler::endElement
void endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname) override
Definition: ConfigurationDatabaseStandardXMLParser.cc:148
ConfigurationDBHandler::~ConfigurationDBHandler
~ConfigurationDBHandler() override
Definition: HcalPatternXMLParser.cc:62
ConfigurationDBHandler::m_ptype
std::string m_ptype
Definition: ConfigurationDatabaseStandardXMLParser.cc:84
HcalPatternXMLParser::m_parser
HcalPatternXMLParserImpl * m_parser
Definition: HcalPatternXMLParser.h:25
mps_splice.entry
entry
Definition: mps_splice.py:68
ConfigurationDBHandler::md_Parameter
Definition: ConfigurationDatabaseStandardXMLParser.cc:38
ConfigurationDBHandler::md_Idle
Definition: ConfigurationDatabaseStandardXMLParser.cc:38
XERCES_CPP_NAMESPACE_USE
Definition: XMLConfigWriter.cc:40
ConfigurationDBHandler::xc_Data
XMLCh * xc_Data
Definition: ConfigurationDatabaseStandardXMLParser.cc:81
HcalPatternXMLParserImpl
Definition: HcalPatternXMLParser.cc:11
ConfigurationDBHandler::xc_type
XMLCh * xc_type
Definition: ConfigurationDatabaseStandardXMLParser.cc:81
ConfigurationDBHandler::characters
void characters(const XMLCh *const chars, const XMLSize_t length) override
Definition: ConfigurationDatabaseStandardXMLParser.cc:181
mps_monitormerge.items
list items
Definition: mps_monitormerge.py:29
HcalPatternXMLParserImpl::parser
std::unique_ptr< SAX2XMLReader > parser
Definition: HcalPatternXMLParser.cc:13
HcalPatternXMLParser.h
ConfigurationDBHandler::n_elements
int n_elements
Definition: ConfigurationDatabaseStandardXMLParser.cc:85
ConfigurationDBHandler::cvt2String
bool cvt2String(const XMLCh *val, std::string &ou)
Definition: HcalPatternXMLParser.cc:79
ConfigurationDBHandler::ConfigurationDBHandler
ConfigurationDBHandler(std::map< std::string, std::string > &parameters, std::vector< std::string > &items, std::string &encoding)
Definition: HcalPatternXMLParser.cc:48
ConfigurationDBHandler::m_pname
std::string m_pname
Definition: ConfigurationDatabaseStandardXMLParser.cc:84
HcalPatternXMLParser::HcalPatternXMLParser
HcalPatternXMLParser()
Definition: HcalPatternXMLParser.cc:16
HcalPatternXMLParser::parse
void parse(const std::string &xmlDocument, std::map< std::string, std::string > &parameters, std::vector< std::string > &items, std::string &encoding)
Definition: HcalPatternXMLParser.cc:171
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
ConfigurationDBHandler::xc_elements
XMLCh * xc_elements
Definition: ConfigurationDatabaseStandardXMLParser.cc:81
cppFunctionSkipper.exception
exception
Definition: cppFunctionSkipper.py:10
ConfigurationDBHandler::m_items
std::vector< std::string > & m_items
Definition: HcalPatternXMLParser.cc:91
TrackRefitter_38T_cff.src
src
Definition: TrackRefitter_38T_cff.py:24
ConfigurationDBHandler::m_items
std::list< ConfigurationDatabaseStandardXMLParser::Item > & m_items
Definition: ConfigurationDatabaseStandardXMLParser.cc:87
fetchall_from_DQM_v2.release
release
Definition: fetchall_from_DQM_v2.py:92
ConfigurationDBHandler::startElement
void startElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const Attributes &attrs) override
Definition: ConfigurationDatabaseStandardXMLParser.cc:101
ConfigurationDBHandler::m_parameters
std::map< std::string, std::string > & m_parameters
Definition: HcalPatternXMLParser.cc:92
submitPVResolutionJobs.q
q
Definition: submitPVResolutionJobs.py:84
ConfigurationDBHandler::m_dataEncoding
std::string & m_dataEncoding
Definition: HcalPatternXMLParser.cc:90
ConfigurationDBHandler
Definition: ConfigurationDatabaseStandardXMLParser.cc:37
ConfigurationDBHandler::ignorableWhitespace
void ignorableWhitespace(const XMLCh *const chars, const XMLSize_t length) override
Definition: ConfigurationDatabaseStandardXMLParser.cc:176
ConfigurationDBHandler::m_workc
char m_workc[512]
Definition: ConfigurationDatabaseStandardXMLParser.cc:91
ConfigurationDBHandler::m_workx
XMLCh m_workx[256]
Definition: ConfigurationDatabaseStandardXMLParser.cc:92
heppy_batch.val
val
Definition: heppy_batch.py:351
HcalPatternXMLParser::~HcalPatternXMLParser
~HcalPatternXMLParser()
Definition: HcalPatternXMLParser.cc:17
Exception
Definition: hltDiff.cc:246
ConfigurationDBHandler::xc_encoding
XMLCh * xc_encoding
Definition: ConfigurationDatabaseStandardXMLParser.cc:81
ConfigurationDBHandler::m_mode
enum ConfigurationDBHandler::@68 m_mode
ConfigurationDBHandler::m_text
std::string m_text
Definition: ConfigurationDatabaseStandardXMLParser.cc:84
Exception.h
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:82
ConfigurationDBHandler::md_Data
Definition: ConfigurationDatabaseStandardXMLParser.cc:38
ConfigurationDBHandler::xc_name
XMLCh * xc_name
Definition: ConfigurationDatabaseStandardXMLParser.cc:81