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  SimpleSAXParser(const SimpleSAXParser &) = delete; // stop default
122  const SimpleSAXParser &operator=(const SimpleSAXParser &) = delete; // stop default
123 
124 private:
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_
std::istream & m_in
std::vector< Attribute > Attributes
virtual void endElement(const std::string &)
std::string parseEntity(const std::string &entity)
virtual void data(const std::string &)
std::string getToken(const char delim)
Attribute(const std::string &iKey, const std::string &iValue)
bool fgettoken(std::istream &in, char **buffer, size_t *maxSize, const char *separators, int *firstChar)
ParserError(const std::string &error)
bool operator<(const Attribute &attribute) const
bool skipChar(int c)
double f[11][100]
Definition: value.py:1
std::string getToken(const char *delim)
std::vector< std::string > m_elementTags
virtual ~SimpleSAXParser()
const SimpleSAXParser & operator=(const SimpleSAXParser &)=delete
#define get
Attribute(const Attribute &attr)
virtual void startElement(const std::string &, Attributes &)
Attributes m_attributes
SimpleSAXParser(std::istream &f)