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
23 fgettoken(std::istream &in, char **buffer, size_t *maxSize, const char *separators,
24  int *firstChar);
25 
72 {
73 public:
74  struct Attribute
75  {
78 
79  Attribute(const std::string &iKey, const std::string &iValue)
80  :key(iKey), value(iValue)
81  {}
82 
83  Attribute(const Attribute &attr)
84  :key(attr.key), value(attr.value)
85  {}
86 
87  bool operator<(const Attribute &attribute) const
88  {
89  return this->key < attribute.key;
90  }
91  };
92 
93  typedef std::vector<Attribute> Attributes;
95  {
96  public:
98  :m_error(error)
99  {}
100 
101  const char *error() { return m_error.c_str(); }
102  private:
104  };
105 
121  };
122 
123  SimpleSAXParser(std::istream &f)
124  : m_in(f),
125  m_bufferSize(1024),
126  m_buffer(new char[m_bufferSize]),
127  m_nextChar(m_in.get())
128  {}
129 
130  virtual ~SimpleSAXParser();
131 
132  void parse(void);
133 
134  virtual void startElement(const std::string &/*name*/,
135  Attributes &/*attributes*/) {}
136  virtual void endElement(const std::string &/*name*/) {}
137  virtual void data(const std::string &/*data*/) {}
138 
139 private:
140  SimpleSAXParser(const SimpleSAXParser&) = delete; // stop default
141  const SimpleSAXParser& operator=(const SimpleSAXParser&) = delete; // stop default
142 
143  std::string parseEntity(const std::string &entity);
145  {
147  return m_buffer;
148  }
149 
151  {
152  char buf[2] = {delim, 0};
154  m_nextChar = m_in.get();
155  return m_buffer;
156  }
157 
158  bool skipChar(int c)
159  {
160  if (m_nextChar != c)
161  return false;
162  m_nextChar = m_in.get();
163  return true;
164  }
165 
166  int nextChar(void) { return m_nextChar; }
167 
168  std::istream &m_in;
169  size_t m_bufferSize;
170  char *m_buffer;
172  std::vector<std::string> m_elementTags;
173  Attributes m_attributes;
174 };
175 
176 // NOTE: put in a .cc if this file is used in more than one place.
177 #endif // __SIMPLE_SAX_PARSER_H_
std::istream & m_in
std::vector< Attribute > Attributes
bool operator<(const Attribute &attribute) const
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 skipChar(int c)
double f[11][100]
std::string getToken(const char *delim)
std::vector< std::string > m_elementTags
virtual ~SimpleSAXParser()
const SimpleSAXParser & operator=(const SimpleSAXParser &)=delete
Attribute(const Attribute &attr)
virtual void startElement(const std::string &, Attributes &)
Attributes m_attributes
T get(const Candidate &c)
Definition: component.h:55
SimpleSAXParser(std::istream &f)