CMS 3D CMS Logo

List of all members | Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes
SimpleSAXParser Class Reference

#include <SimpleSAXParser.h>

Inheritance diagram for SimpleSAXParser:
FWXMLConfigParser

Classes

struct  Attribute
 
class  ParserError
 

Public Types

typedef std::vector< AttributeAttributes
 
enum  PARSER_STATES {
  IN_DOCUMENT, IN_BEGIN_TAG, IN_DONE, IN_BEGIN_ELEMENT,
  IN_ELEMENT_WHITESPACE, IN_END_ELEMENT, IN_ATTRIBUTE_KEY, IN_END_TAG,
  IN_DATA, IN_BEGIN_ATTRIBUTE_VALUE, IN_STRING, IN_END_ATTRIBUTE_VALUE,
  IN_STRING_ENTITY, IN_DATA_ENTITY
}
 

Public Member Functions

virtual void data (const std::string &)
 
virtual void endElement (const std::string &)
 
const SimpleSAXParseroperator= (const SimpleSAXParser &)=delete
 
void parse (void)
 
 SimpleSAXParser (std::istream &f)
 
 SimpleSAXParser (const SimpleSAXParser &)=delete
 
virtual void startElement (const std::string &, Attributes &)
 
virtual ~SimpleSAXParser ()
 

Private Member Functions

std::string getToken (const char *delim)
 
std::string getToken (const char delim)
 
int nextChar (void)
 
std::string parseEntity (const std::string &entity)
 
bool skipChar (int c)
 

Private Attributes

Attributes m_attributes
 
char * m_buffer
 
size_t m_bufferSize
 
std::vector< std::string > m_elementTags
 
std::istream & m_in
 
int m_nextChar
 

Detailed Description

A simple SAX parser which is able to parse the configuration.

State machine for the parser can be drawn by cut and pasting the following to graphviz:

digraph { IN_DOCUMENT->IN_BEGIN_TAG [label="nextChar == '<'"]; IN_DOCUMENT->IN_DATA [label="nextChar != '<'"];

IN_BEGIN_TAG->IN_BEGIN_ELEMENT [label="nextChar >= 'a' && nextChar < 'Z'"]; IN_BEGIN_TAG->IN_END_ELEMENT [label= "nextChar == '/'"];

IN_BEGIN_ELEMENT->IN_END_ELEMENT [label="nextChar == '/'"]; IN_BEGIN_ELEMENT->IN_ELEMENT_WHITESPACE [label="nextChar == ' '"]; IN_BEGIN_ELEMENT->IN_END_TAG [label="nextChar == '>'"];

IN_ELEMENT_WHITESPACE->IN_ELEMENT_WHITESPACE [ label = "nextChar == \"\ \t\n""] IN_ELEMENT_WHITESPACE->IN_ATTRIBUTE_KEY [ label = "nextChar >= 'a' && nextChar < 'Z'"] IN_ELEMENT_WHITESPACE->IN_END_ELEMENT [label="nextChar == '/'"]

IN_END_ELEMENT->IN_END_TAG [label = "nextChar == '>'"];

IN_END_TAG->IN_BEGIN_TAG [label="nextChar == '<'"]; IN_END_TAG->IN_DATA [label="nextChar != '<'"]

IN_DATA->IN_BEGIN_TAG [label="nextChar == '<'"]; IN_DATA->IN_DATA_ENTITY [label="nextChar == '&'"]; IN_DATA->IN_DONE [label = "nextChar == EOF"];

IN_DATA_ENTITY->IN_DATA [label="nextChar == ';'"];

IN_ATTRIBUTE_KEY->IN_BEGIN_ATTRIBUTE_VALUE [label = "nextChar == '='"]

IN_BEGIN_ATTRIBUTE_VALUE->IN_STRING [label = "nextChar == '\"' || nextChar == '\'' "]

IN_STRING->IN_END_ATTRIBUTE_VALUE [label = "nextChar == quote"] IN_STRING->IN_STRING_ENTITY [label = "nextChar == '&'"]

IN_END_ATTRIBUTE_VALUE->IN_ELEMENT_WHITESPACE [label = "nextChar == ' '"] IN_END_ATTRIBUTE_VALUE->IN_END_ELEMENT [label = "nextChar == '/'"] IN_END_ATTRIBUTE_VALUE->IN_END_TAG [label = "nextChar == '>'"]

IN_STRING_ENTITY->IN_STRING [label = "nextChar == ';'"] }

Definition at line 69 of file SimpleSAXParser.h.

Member Typedef Documentation

◆ Attributes

typedef std::vector<Attribute> SimpleSAXParser::Attributes

Definition at line 80 of file SimpleSAXParser.h.

Member Enumeration Documentation

◆ PARSER_STATES

Enumerator
IN_DOCUMENT 
IN_BEGIN_TAG 
IN_DONE 
IN_BEGIN_ELEMENT 
IN_ELEMENT_WHITESPACE 
IN_END_ELEMENT 
IN_ATTRIBUTE_KEY 
IN_END_TAG 
IN_DATA 
IN_BEGIN_ATTRIBUTE_VALUE 
IN_STRING 
IN_END_ATTRIBUTE_VALUE 
IN_STRING_ENTITY 
IN_DATA_ENTITY 

Definition at line 91 of file SimpleSAXParser.h.

Constructor & Destructor Documentation

◆ SimpleSAXParser() [1/2]

SimpleSAXParser::SimpleSAXParser ( std::istream &  f)
inline

Definition at line 108 of file SimpleSAXParser.h.

109  : m_in(f), m_bufferSize(1024), m_buffer(new char[m_bufferSize]), m_nextChar(m_in.get()) {}
std::istream & m_in
double f[11][100]

◆ ~SimpleSAXParser()

SimpleSAXParser::~SimpleSAXParser ( )
virtual

Definition at line 197 of file SimpleSAXParser.cc.

References m_buffer.

197 { delete[] m_buffer; }

◆ SimpleSAXParser() [2/2]

SimpleSAXParser::SimpleSAXParser ( const SimpleSAXParser )
delete

Member Function Documentation

◆ data()

virtual void SimpleSAXParser::data ( const std::string &  )
inlinevirtual

Reimplemented in FWXMLConfigParser.

Definition at line 117 of file SimpleSAXParser.h.

Referenced by parse().

117 {}

◆ endElement()

virtual void SimpleSAXParser::endElement ( const std::string &  )
inlinevirtual

Reimplemented in FWXMLConfigParser.

Definition at line 116 of file SimpleSAXParser.h.

Referenced by parse().

116 {}

◆ getToken() [1/2]

std::string SimpleSAXParser::getToken ( const char *  delim)
inlineprivate

Definition at line 124 of file SimpleSAXParser.h.

References fgettoken(), m_buffer, m_bufferSize, m_in, and m_nextChar.

Referenced by parse().

124  {
126  return m_buffer;
127  }
std::istream & m_in
bool fgettoken(std::istream &in, char **buffer, size_t *maxSize, const char *separators, int *firstChar)

◆ getToken() [2/2]

std::string SimpleSAXParser::getToken ( const char  delim)
inlineprivate

Definition at line 129 of file SimpleSAXParser.h.

References visDQMUpload::buf, fgettoken(), m_buffer, m_bufferSize, m_in, and m_nextChar.

129  {
130  char buf[2] = {delim, 0};
132  m_nextChar = m_in.get();
133  return m_buffer;
134  }
std::istream & m_in
bool fgettoken(std::istream &in, char **buffer, size_t *maxSize, const char *separators, int *firstChar)

◆ nextChar()

int SimpleSAXParser::nextChar ( void  )
inlineprivate

Definition at line 143 of file SimpleSAXParser.h.

References m_nextChar.

Referenced by parse().

143 { return m_nextChar; }

◆ operator=()

const SimpleSAXParser& SimpleSAXParser::operator= ( const SimpleSAXParser )
delete

◆ parse()

void SimpleSAXParser::parse ( void  )

Runs the state machine of the parser, invoking startElement(), setAttribute(), endElement(), data() virtual methods as approppriate. In order have the parser doing something usefull you need to derive from it and specialize the above mentioned virtual methods.

Default implementation is in any case useful to check syntax.

Definition at line 46 of file SimpleSAXParser.cc.

References data(), debug_state_machine(), endElement(), getToken(), mps_fire::i, IN_ATTRIBUTE_KEY, IN_BEGIN_ATTRIBUTE_VALUE, IN_BEGIN_ELEMENT, IN_BEGIN_TAG, IN_DATA, IN_DATA_ENTITY, IN_DOCUMENT, IN_DONE, IN_ELEMENT_WHITESPACE, IN_END_ATTRIBUTE_VALUE, IN_END_ELEMENT, IN_END_TAG, IN_STRING, IN_STRING_ENTITY, SimpleSAXParser::Attribute::key, pfDeepBoostedJetPreprocessParams_cfi::lower_bound, m_attributes, m_elementTags, nextChar(), parseEntity(), skipChar(), startElement(), AlCaHLTBitMon_QueryRunRegistry::string, and createJobs::tmp.

Referenced by FWConfigurationManager::readFromFile().

46  {
48  // Current delimiters for strings in attributes.
49  char stringDelims[] = "\"&";
50  std::string attributeName;
51  std::string attributeValue;
53  std::string currentData;
54 
55  while (state != IN_DONE) {
57 
58  switch (state) {
59  // FIXME: IN_DOCUMENT should check the dtd...
60  case IN_DOCUMENT:
61  state = IN_DATA;
62  if (skipChar('<'))
64  break;
65 
66  case IN_BEGIN_TAG:
67  if (nextChar() >= 'A' && nextChar() <= 'z')
69  else if (skipChar('/'))
71  else
72  throw ParserError("Bad tag");
73  break;
74 
75  case IN_BEGIN_ELEMENT:
76  m_attributes.clear();
77  m_elementTags.push_back(getToken(" />"));
78  if (nextChar() == ' ')
80  else if (skipChar('/'))
82  else if (skipChar('>')) {
84  state = IN_END_TAG;
85  } else
86  throw ParserError("Bad element.");
87  break;
88 
90  while (skipChar(' ') || skipChar('\n') || skipChar('\t')) {
91  }
92 
93  if (nextChar() >= 'A' && nextChar() <= 'z')
95  else if (nextChar() == '/')
97  else
98  throw ParserError("Syntax error in element" + m_elementTags.back());
99  break;
100 
101  case IN_ATTRIBUTE_KEY:
102  attributeName = getToken('=');
104  break;
105 
107  if (skipChar('"')) {
108  state = IN_STRING;
109  attributeValue.clear();
110  stringDelims[0] = '\"';
111  } else if (skipChar('\'')) {
112  state = IN_STRING;
113  attributeValue.clear();
114  stringDelims[0] = '\'';
115  } else
116  throw ParserError("Expecting quotes.");
117  break;
118 
119  case IN_STRING:
120  attributeValue += getToken(stringDelims);
121  if (skipChar(stringDelims[0])) {
122  // Save the attributes in order, replacing those that are
123  // specified more than once.
124  Attribute attr(attributeName, attributeValue);
125  Attributes::iterator i = std::lower_bound(m_attributes.begin(), m_attributes.end(), attr);
126  if (i != m_attributes.end() && i->key == attr.key)
127  throw ParserError("Attribute " + i->key + " defined more than once");
128  m_attributes.insert(i, attr);
130  } else if (skipChar(stringDelims[1]))
132  else
133  throw ParserError("Unexpected end of input at " + attributeValue);
134  break;
135 
137  getToken(" />");
138  if (nextChar() == ' ')
140  else if (skipChar('/'))
142  else if (skipChar('>')) {
144  state = IN_END_TAG;
145  }
146  break;
147 
148  case IN_END_ELEMENT:
149  tmp = getToken('>');
150  if (!tmp.empty() && tmp != m_elementTags.back())
151  throw ParserError("Non-matching closing element " + tmp + " for " + attributeValue);
152  endElement(tmp);
153  m_elementTags.pop_back();
154  state = IN_END_TAG;
155  break;
156 
157  case IN_END_TAG:
158  if (nextChar() == EOF)
159  return;
160  else if (skipChar('<'))
162  else
163  state = IN_DATA;
164  break;
165 
166  case IN_DATA:
167  currentData += getToken("<&");
168  if (skipChar('&'))
170  else if (skipChar('<')) {
171  data(currentData);
172  currentData.clear();
174  } else if (nextChar() == EOF) {
175  data(currentData);
176  return;
177  } else
178  throw ParserError("Unexpected end of input in element " + m_elementTags.back() + currentData);
179  break;
180 
181  case IN_DATA_ENTITY:
182  currentData += parseEntity(getToken(';'));
183  state = IN_DATA;
184  break;
185 
186  case IN_STRING_ENTITY:
187  attributeValue += parseEntity(getToken(';'));
188  state = IN_STRING;
189  break;
190 
191  case IN_DONE:
192  return;
193  }
194  }
195 }
virtual void endElement(const std::string &)
std::string parseEntity(const std::string &entity)
virtual void data(const std::string &)
bool skipChar(int c)
std::string getToken(const char *delim)
std::vector< std::string > m_elementTags
void debug_state_machine(enum SimpleSAXParser::PARSER_STATES state)
tmp
align.sh
Definition: createJobs.py:716
virtual void startElement(const std::string &, Attributes &)
Attributes m_attributes

◆ parseEntity()

std::string SimpleSAXParser::parseEntity ( const std::string &  entity)
private

Helper function to handle entities, i.e. characters specified with the "&label;" syntax.

Definition at line 6 of file SimpleSAXParser.cc.

Referenced by parse().

6  {
7  if (entity == "quot")
8  return "\"";
9  else if (entity == "amp")
10  return "&";
11  else if (entity == "lt")
12  return "<";
13  else if (entity == "gt")
14  return ">";
15  throw ParserError("Unknown entity " + entity);
16 }

◆ skipChar()

bool SimpleSAXParser::skipChar ( int  c)
inlineprivate

Definition at line 136 of file SimpleSAXParser.h.

References HltBtagPostValidation_cff::c, m_in, and m_nextChar.

Referenced by parse().

136  {
137  if (m_nextChar != c)
138  return false;
139  m_nextChar = m_in.get();
140  return true;
141  }
std::istream & m_in

◆ startElement()

virtual void SimpleSAXParser::startElement ( const std::string &  ,
Attributes  
)
inlinevirtual

Reimplemented in FWXMLConfigParser.

Definition at line 115 of file SimpleSAXParser.h.

Referenced by parse().

115 {}

Member Data Documentation

◆ m_attributes

Attributes SimpleSAXParser::m_attributes
private

Definition at line 150 of file SimpleSAXParser.h.

Referenced by parse().

◆ m_buffer

char* SimpleSAXParser::m_buffer
private

Definition at line 147 of file SimpleSAXParser.h.

Referenced by getToken(), and ~SimpleSAXParser().

◆ m_bufferSize

size_t SimpleSAXParser::m_bufferSize
private

Definition at line 146 of file SimpleSAXParser.h.

Referenced by getToken().

◆ m_elementTags

std::vector<std::string> SimpleSAXParser::m_elementTags
private

Definition at line 149 of file SimpleSAXParser.h.

Referenced by parse().

◆ m_in

std::istream& SimpleSAXParser::m_in
private

Definition at line 145 of file SimpleSAXParser.h.

Referenced by getToken(), and skipChar().

◆ m_nextChar

int SimpleSAXParser::m_nextChar
private

Definition at line 148 of file SimpleSAXParser.h.

Referenced by getToken(), nextChar(), and skipChar().