CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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>
7 XERCES_CPP_NAMESPACE_USE
9 #include <ostream>
10 
12 public:
13  std::auto_ptr<SAX2XMLReader> parser;
14 };
15 
17  m_parser=0;
18 }
20  if (m_parser!=0) delete m_parser;
21 }
22 
23  /*
24  Example
25  <pre>
26  <CFGBrick>
27  <Parameter name='IETA' type='int'>14</Parameter>
28  <Parameter name='IPHI' type='int'>2</Parameter>
29  <Parameter name='DEPTH' type='int'>1</Parameter>
30  <Parameter name='CRATE' type='int'>1</Parameter>
31  <Parameter name='SLOT' type='int'>8</Parameter>
32  <Parameter name='TOPBOTTOM' type='int'>0</Parameter>
33  <Parameter name='CHANNEL' type='int'>6</Parameter>
34  <Parameter name='LUT_TYPE' type='int'>1</Parameter>
35  <Parameter name='CREATIONTAG' type='string'>Identity</Parameter>
36  <Parameter name='CREATIONSTAMP' type='string'>2005-03-08 11:44:34</Parameter>
37  <Parameter name='FORMATREVISION' type='string'>1</Parameter>
38  <Parameter name='TARGETFIRMWARE' type='string'>0</Parameter>
39  <Parameter name='GENERALIZEDINDEX' type='int'>140211</Parameter>
40  <Data elements='128' encoding='hex'> .. </Data>
41  </CFGBrick>
42  </pre>
43  */
44 
45  class ConfigurationDBHandler : public DefaultHandler {
47  public:
48  ConfigurationDBHandler(std::map<std::string,std::string>& parameters, std::vector<std::string>& items, std::string& encoding) : m_dataEncoding(encoding), m_items(items), m_parameters(parameters) {
54  xc_elements=XMLString::transcode("elements");
56  m_items.clear();
57  m_parameters.clear();
58  }
66  }
67  virtual void startElement (const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const Attributes &attrs) override;
68  virtual void endElement (const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname) override;
69  virtual void characters(const XMLCh* const chars, const unsigned int length) override;
70  virtual void ignorableWhitespace(const XMLCh* chars, const unsigned int length) override;
71  private:
72  inline bool cvt2String(const XMLCh* val, std::string& ou) {
73  if (val==0) return false;
74  char* tool=XMLString::transcode(val);
75  ou=tool;
76  XMLString::release(&tool);
77  return true;
78  }
81  int n_elements;
83  std::vector<std::string>& m_items;
84  std::map<std::string,std::string>& m_parameters;
85  char m_workc[512];
86  XMLCh m_workx[256];
87  };
88 
89  void ConfigurationDBHandler::startElement (const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const Attributes &attrs) {
90  if (m_mode!=md_Idle) return;
91  if (!XMLString::compareIString(localname,xc_Parameter)) {
92  // parameter name
93  if (!cvt2String(attrs.getValue(xc_name),m_pname)) return;
94  // parameter type
95  if (!cvt2String(attrs.getValue(xc_type),m_ptype)) return;
96  // switch mode
98  m_text="";
99  } else if (!XMLString::compareIString(localname,xc_Data)) {
100  // elements
101  std::string strElements;
102  if (!cvt2String(attrs.getValue(xc_elements),strElements)) return;
103  n_elements=atoi(strElements.c_str());
104  // encoding
105  m_dataEncoding="";
106  cvt2String(attrs.getValue(xc_encoding),m_dataEncoding);
107  // switch mode
108  m_mode=md_Data;
109  m_text="";
110  }
111 
112  }
113  void ConfigurationDBHandler::endElement (const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname) {
114  if (m_mode==md_Idle) return;
115 
116  if (m_mode==md_Parameter) {
117  m_parameters[m_pname]=m_text; // ignore the type for now...
118  } else if (m_mode==md_Data) {
119  // parse the text
120  std::string entry;
121  for (std::string::iterator q=m_text.begin(); q!=m_text.end(); q++) {
122  if (isspace(*q)) {
123  if (entry.empty()) continue;
124  m_items.push_back(entry);
125  entry="";
126  } else entry+=*q;
127  }
128  }
129 
130  m_mode=md_Idle;
131  }
132  void ConfigurationDBHandler::ignorableWhitespace(const XMLCh* chars, const unsigned int length) {
133  if (m_mode==md_Idle) return;
134  m_text+=' ';
135  }
136  void ConfigurationDBHandler::characters(const XMLCh* chars, const unsigned int length) {
137  if (m_mode==md_Idle) return;
138  unsigned int offset=0;
139  while (offset<length) {
140  unsigned int i=0;
141  for (i=0; i<length-offset && i<255; i++) m_workx[i]=chars[i+offset];
142  m_workx[i]=0; // terminate string
144  m_text+=m_workc;
145  offset+=i;
146  }
147  }
148 
149 void HcalPatternXMLParser::parse(const std::string& xmlDocument, std::map<std::string,std::string>& parameters, std::vector<std::string>& items, std::string& encoding) {
150  // uses XERCES SAX2 parser
151  ConfigurationDBHandler handler(parameters,items,encoding);
152 
153  try {
154  if (m_parser==0) {
156  m_parser->parser=std::auto_ptr<xercesc::SAX2XMLReader>(xercesc::XMLReaderFactory::createXMLReader());
157  }
158 
159  MemBufInputSource src((const unsigned char*)xmlDocument.c_str(), xmlDocument.length(),"hcal::PatternReader");
160  m_parser->parser->setContentHandler(&handler);
161  m_parser->parser->parse(src);
162  } catch (std::exception& ex) {
163  throw cms::Exception("ParseError") << ex.what();
164  }
165  }
166 
167 void HcalPatternXMLParser::parse(const std::string& xmlDocument, std::map<std::string,std::string>& parameters, std::vector<uint32_t>& data) {
168  std::vector<std::string> items;
169  std::string encoding;
170 
171  this->parse(xmlDocument,parameters,items,encoding);
172  int formatting=0;
173  if (encoding=="dec") formatting=10;
174  if (encoding=="hex") formatting=16;
175 
176  data.clear();
177  for (std::vector<std::string>::const_iterator i=items.begin(); i!=items.end(); i++)
178  data.push_back(strtol(i->c_str(),0,formatting));
179 
180 }
int i
Definition: DBlmapReader.cc:9
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
std::auto_ptr< SAX2XMLReader > parser
ConfigurationDBHandler(std::map< std::string, std::string > &parameters, std::vector< std::string > &items, std::string &encoding)
virtual void ignorableWhitespace(const XMLCh *chars, const unsigned int length) override
enum ConfigurationDBHandler::@66 m_mode
XMLCh * transcode(const T &fInput)
std::map< std::string, std::string > & m_parameters
std::list< ConfigurationDatabaseStandardXMLParser::Item > & m_items
virtual 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:82
HcalPatternXMLParserImpl * m_parser
virtual void endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname) override
virtual void characters(const XMLCh *const chars, const unsigned int length) override