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 }
void parse(const std::string &xmlDocument, std::map< std::string, std::string > &parameters, std::vector< std::string > &items, std::string &encoding)
bool cvt2String(const XMLCh *val, std::string &ou)
std::vector< std::string > & m_items
void characters(const XMLCh *const chars, const XMLSize_t length) override
std::unique_ptr< SAX2XMLReader > parser
ROOT::VecOps::RVec< UChar_t > chars
Definition: Resolutions.cc:6
ConfigurationDBHandler(std::map< std::string, std::string > &parameters, std::vector< std::string > &items, std::string &encoding)
void ignorableWhitespace(const XMLCh *const chars, const XMLSize_t length) override
std::list< ConfigurationDatabaseStandardXMLParser::Item > & m_items
std::map< std::string, std::string > & m_parameters
void startElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const Attributes &attrs) override
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
enum ConfigurationDBHandler::@71 m_mode
HcalPatternXMLParserImpl * m_parser
void endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname) override