CMS 3D CMS Logo

FWXMLConfigParser.h
Go to the documentation of this file.
1 #ifndef Fireworks_Core_FWXMLConfigParser
2 #define Fireworks_Core_FWXMLConfigParser
3 #include <istream>
4 #include <iostream>
6 
8 
34 
35 public:
37 
39  void pushConfig(Attributes &attributes) {
41  int version = 0;
42  for (size_t i = 0, e = attributes.size(); i != e; ++i) {
43  Attribute &attr = attributes[i];
44  if (attr.key == "name")
45  name = attr.value;
46  else if (attr.key == "version") {
47  char *endptr;
48  version = strtol(attr.value.c_str(), &endptr, 10);
49  if (endptr == attr.value.c_str())
50  throw ParserError("Version must be an integer.");
51  } else
52  throw ParserError("Unexpected attribute " + attr.key);
53  }
54  m_configs.push_back(std::make_pair(name, new FWConfiguration(version)));
55  }
56 
60  void startElement(const std::string &tag, Attributes &attributes) override {
62  if (m_state == IN_BEGIN_DOCUMENT) {
63  if (tag != "config")
64  throw ParserError("Expecting toplevel <config> tag");
65  pushConfig(attributes);
66  m_first.reset(m_configs.back().second);
68  } else if (m_state == IN_PUSHED_CONFIG) {
69  if (tag == "config")
70  pushConfig(attributes);
71  else if (tag == "string")
73  else
74  throw ParserError("Unexpected element " + tag);
75  } else if (m_state == IN_POPPED_CONFIG) {
76  if (tag != "config")
77  throw ParserError("Unexpected element " + tag);
78  pushConfig(attributes);
80  } else
81  throw ParserError("Wrong opening tag found " + tag);
82  }
83 
92  void endElement(const std::string &tag) override {
95  if (tag != "config")
96  throw ParserError("Wrong closing tag found " + tag);
97 
98  FWConfiguration *current = m_configs.back().second;
99  std::string key = m_configs.back().first;
100  m_configs.pop_back();
101  if (!m_configs.empty())
102  m_configs.back().second->addKeyValue(key, *current);
104  } else if (m_state == IN_BEGIN_STRING && tag == "string") {
105  m_configs.back().second->addValue("");
107  } else if (m_state == IN_STORED_STRING && tag == "string")
109  else
110  throw ParserError("Wrong closing tag found " + tag);
111  }
112 
118  void data(const std::string &data) override {
120  // We ignore whitespace but complain about any text which is not
121  // in the <string> tag.
122  if (m_state == IN_BEGIN_STRING) {
123  m_configs.back().second->addValue(data);
125  } else if (strspn(data.c_str(), " \t\n") != data.size())
126  throw ParserError("Unexpected text " + data);
127  }
128 
132  FWConfiguration *config(void) { return m_first.get(); }
133 
134  void debug_config_state_machine(const char *where, const std::string &tag, int state) {
135 #ifdef FW_CONFIG_PARSER_DEBUG
136  static char *debug_states[] = {
137  "IN_BEGIN_DOCUMENT", "IN_PUSHED_CONFIG", "IN_POPPED_CONFIG", "IN_BEGIN_STRING", "IN_STORED_STRING"};
138 
139  std::cerr << " " << where << " tag/data " << tag << "in state " << debug_states[state] << std::endl;
140 #endif
141  }
142 
143 private:
144  std::vector<std::pair<std::string, FWConfiguration *> > m_configs;
146  std::unique_ptr<FWConfiguration> m_first;
147  // unsigned int m_currentConfigVersion;
149 };
150 #endif
FWXMLConfigParser(std::istream &f)
std::string m_currentConfigName
std::vector< Attribute > Attributes
std::vector< std::pair< std::string, FWConfiguration * > > m_configs
FWConfiguration * config(void)
void pushConfig(Attributes &attributes)
void debug_config_state_machine(const char *where, const std::string &tag, int state)
double f[11][100]
std::unique_ptr< FWConfiguration > m_first
void startElement(const std::string &tag, Attributes &attributes) override
void data(const std::string &data) override
void endElement(const std::string &tag) override