CMS 3D CMS Logo

SimpleSAXParser.h
Go to the documentation of this file.
1 #ifndef __SIMPLE_SAX_PARSER_H_
2 #define __SIMPLE_SAX_PARSER_H_
3 /* A simple SAX-like parser.
4 
5  And yes, I know the S in SAX stands for Simple.
6 
7  Licensed under GPLv3 license.
8 
9  TODO: incomplete support for entities.
10  TODO: no support for DTD nor <?xml> preamble.
11  */
12 
13 #include <string>
14 #include <cstdio>
15 #include <cstdlib>
16 #include <cassert>
17 #include <cstring>
18 #include <iostream>
19 #include <algorithm>
20 #include <vector>
21 
22 bool fgettoken(std::istream &in, char **buffer, size_t *maxSize, const char *separators, int *firstChar);
23 
70 public:
71  struct Attribute {
74 
75  Attribute(const std::string &iKey, const std::string &iValue) : key(iKey), value(iValue) {}
76 
77  Attribute(const Attribute &attr) : key(attr.key), value(attr.value) {}
78 
79  bool operator<(const Attribute &attribute) const { return this->key < attribute.key; }
80  };
81 
82  typedef std::vector<Attribute> Attributes;
83  class ParserError {
84  public:
86 
87  const char *error() { return m_error.c_str(); }
88 
89  private:
91  };
92 
108  };
109 
110  SimpleSAXParser(std::istream &f)
111  : m_in(f), m_bufferSize(1024), m_buffer(new char[m_bufferSize]), m_nextChar(m_in.get()) {}
112 
113  virtual ~SimpleSAXParser();
114 
115  void parse(void);
116 
117  virtual void startElement(const std::string & /*name*/, Attributes & /*attributes*/) {}
118  virtual void endElement(const std::string & /*name*/) {}
119  virtual void data(const std::string & /*data*/) {}
120 
121 private:
122  SimpleSAXParser(const SimpleSAXParser &) = delete; // stop default
123  const SimpleSAXParser &operator=(const SimpleSAXParser &) = delete; // stop default
124 
125  std::string parseEntity(const std::string &entity);
126  std::string getToken(const char *delim) {
128  return m_buffer;
129  }
130 
131  std::string getToken(const char delim) {
132  char buf[2] = {delim, 0};
134  m_nextChar = m_in.get();
135  return m_buffer;
136  }
137 
138  bool skipChar(int c) {
139  if (m_nextChar != c)
140  return false;
141  m_nextChar = m_in.get();
142  return true;
143  }
144 
145  int nextChar(void) { return m_nextChar; }
146 
147  std::istream &m_in;
148  size_t m_bufferSize;
149  char *m_buffer;
151  std::vector<std::string> m_elementTags;
153 };
154 
155 // NOTE: put in a .cc if this file is used in more than one place.
156 #endif // __SIMPLE_SAX_PARSER_H_
SimpleSAXParser::getToken
std::string getToken(const char *delim)
Definition: SimpleSAXParser.h:126
SimpleSAXParser::startElement
virtual void startElement(const std::string &, Attributes &)
Definition: SimpleSAXParser.h:117
SimpleSAXParser::IN_DATA_ENTITY
Definition: SimpleSAXParser.h:107
SimpleSAXParser::ParserError::m_error
std::string m_error
Definition: SimpleSAXParser.h:90
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
SimpleSAXParser::m_attributes
Attributes m_attributes
Definition: SimpleSAXParser.h:152
SimpleSAXParser::PARSER_STATES
PARSER_STATES
Definition: SimpleSAXParser.h:93
SimpleSAXParser::IN_END_ELEMENT
Definition: SimpleSAXParser.h:99
SimpleSAXParser::IN_BEGIN_ATTRIBUTE_VALUE
Definition: SimpleSAXParser.h:103
SimpleSAXParser::data
virtual void data(const std::string &)
Definition: SimpleSAXParser.h:119
SimpleSAXParser::endElement
virtual void endElement(const std::string &)
Definition: SimpleSAXParser.h:118
SimpleSAXParser::skipChar
bool skipChar(int c)
Definition: SimpleSAXParser.h:138
SimpleSAXParser::IN_END_TAG
Definition: SimpleSAXParser.h:101
SimpleSAXParser::IN_ATTRIBUTE_KEY
Definition: SimpleSAXParser.h:100
SimpleSAXParser::parse
void parse(void)
Definition: SimpleSAXParser.cc:46
fgettoken
bool fgettoken(std::istream &in, char **buffer, size_t *maxSize, const char *separators, int *firstChar)
Definition: SimpleSAXParser.cc:224
SimpleSAXParser::SimpleSAXParser
SimpleSAXParser(std::istream &f)
Definition: SimpleSAXParser.h:110
SimpleSAXParser::Attribute
Definition: SimpleSAXParser.h:71
SimpleSAXParser::nextChar
int nextChar(void)
Definition: SimpleSAXParser.h:145
SimpleSAXParser::m_nextChar
int m_nextChar
Definition: SimpleSAXParser.h:150
SimpleSAXParser::m_in
std::istream & m_in
Definition: SimpleSAXParser.h:147
edmScanValgrind.buffer
buffer
Definition: edmScanValgrind.py:171
SimpleSAXParser::m_elementTags
std::vector< std::string > m_elementTags
Definition: SimpleSAXParser.h:151
SimpleSAXParser::ParserError::error
const char * error()
Definition: SimpleSAXParser.h:87
SimpleSAXParser::parseEntity
std::string parseEntity(const std::string &entity)
Definition: SimpleSAXParser.cc:6
SimpleSAXParser::getToken
std::string getToken(const char delim)
Definition: SimpleSAXParser.h:131
SimpleSAXParser::Attribute::Attribute
Attribute(const std::string &iKey, const std::string &iValue)
Definition: SimpleSAXParser.h:75
SimpleSAXParser::Attribute::operator<
bool operator<(const Attribute &attribute) const
Definition: SimpleSAXParser.h:79
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
SimpleSAXParser::IN_END_ATTRIBUTE_VALUE
Definition: SimpleSAXParser.h:105
SimpleSAXParser::IN_DATA
Definition: SimpleSAXParser.h:102
SimpleSAXParser::m_buffer
char * m_buffer
Definition: SimpleSAXParser.h:149
SimpleSAXParser::Attribute::key
std::string key
Definition: SimpleSAXParser.h:72
recoMuon::in
Definition: RecoMuonEnumerators.h:6
SimpleSAXParser::IN_DOCUMENT
Definition: SimpleSAXParser.h:94
SimpleSAXParser::IN_BEGIN_TAG
Definition: SimpleSAXParser.h:95
value
Definition: value.py:1
SimpleSAXParser::IN_BEGIN_ELEMENT
Definition: SimpleSAXParser.h:97
SimpleSAXParser::IN_DONE
Definition: SimpleSAXParser.h:96
get
#define get
visDQMUpload.buf
buf
Definition: visDQMUpload.py:154
SimpleSAXParser::Attribute::value
std::string value
Definition: SimpleSAXParser.h:73
SimpleSAXParser
Definition: SimpleSAXParser.h:69
SimpleSAXParser::m_bufferSize
size_t m_bufferSize
Definition: SimpleSAXParser.h:148
reco_skim_cfg_mod.maxSize
maxSize
Definition: reco_skim_cfg_mod.py:154
SimpleSAXParser::IN_STRING
Definition: SimpleSAXParser.h:104
SimpleSAXParser::~SimpleSAXParser
virtual ~SimpleSAXParser()
Definition: SimpleSAXParser.cc:197
SimpleSAXParser::IN_STRING_ENTITY
Definition: SimpleSAXParser.h:106
SimpleSAXParser::IN_ELEMENT_WHITESPACE
Definition: SimpleSAXParser.h:98
SimpleSAXParser::Attribute::Attribute
Attribute(const Attribute &attr)
Definition: SimpleSAXParser.h:77
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:46
SimpleSAXParser::Attributes
std::vector< Attribute > Attributes
Definition: SimpleSAXParser.h:82
SimpleSAXParser::ParserError
Definition: SimpleSAXParser.h:83
SimpleSAXParser::ParserError::ParserError
ParserError(const std::string &error)
Definition: SimpleSAXParser.h:85
SimpleSAXParser::operator=
const SimpleSAXParser & operator=(const SimpleSAXParser &)=delete