From dad77becc53e2f2c3b0880ee4fddd97d69099f94 Mon Sep 17 00:00:00 2001 From: deva Date: Wed, 4 Jun 2008 11:41:46 +0000 Subject: Modulized the template/course/macro system. --- server/src/templateparser.cc | 163 ++----------------------------------------- 1 file changed, 6 insertions(+), 157 deletions(-) (limited to 'server/src/templateparser.cc') diff --git a/server/src/templateparser.cc b/server/src/templateparser.cc index c67ba63..969b4bd 100644 --- a/server/src/templateparser.cc +++ b/server/src/templateparser.cc @@ -66,14 +66,12 @@ void TemplateParser::error(const char* fmt, ...) TemplateParser::TemplateParser(std::string course) { state = UNDEFINED; - t = NULL; + t = new Template(); current_macro = NULL; current_map = NULL; current_luaprogram = NULL; - file = XML"/"; - file.append(course); - file.append(".xml"); + file = XML"/templates/" + course + ".xml"; printf("Using template file: %s\n", file.c_str()); @@ -84,7 +82,7 @@ TemplateParser::TemplateParser(std::string course) TemplateParser::~TemplateParser() { if(fd != -1) close(fd); - if(t) delete t; + delete t; } void TemplateParser::characterData(std::string &data) @@ -102,22 +100,9 @@ void TemplateParser::characterData(std::string &data) void TemplateParser::startTag(std::string name, std::map< std::string, std::string> attributes) { - // Create template and enable parsing of courses - if(name == "template") { - if(state != UNDEFINED) error("template found not in outer level."); - state = TEMPLATE; - - assert(!t); // A Template has already been allocated! - - t = new Template(); - t->attributes = attributes; - - return; - } - // Enable macro parsing if(name == "course") { - if(state != TEMPLATE) error("course found outside template."); + if(state != UNDEFINED) error("course found not a root node."); state = COURSE; assert(t); // A Template has not yet been allocated, cannot create course! @@ -142,152 +127,16 @@ void TemplateParser::startTag(std::string name, std::map< std::string, std::stri return; } - // Enable Query parsing - if(name == "queries") { - if(state != MACRO) error("queries found outside macro."); - state = QUERIES; - - assert(current_macro); // No macro is currently available, cannot create queries! - - return; - } - - // Create Query - if(name == "query") { - if(state != QUERIES) error("query found outside queries."); - state = QUERY; - - assert(current_macro); // No macro is currently available, cannot create query! - - Query q; - q.attributes = attributes; - current_macro->queries.push_back(q); - - return; - } - - // Enable Map parsing - if(name == "maps") { - if(state != MACRO) error("maps found outside macro."); - state = MAPS; - - assert(current_macro); // No macro is currently available, cannot create maps! - - return; - } - - // Create Query - if(name == "map") { - if(state != MAPS) error("map found outside maps."); - state = MAP; - - assert(current_macro); // No macro is currently available, cannot create map! - - Map m; - m.attributes = attributes; - current_macro->maps.push_back(m); - current_map = &(current_macro->maps.back()); - - return; - } - - // Enable LUA Program parsing - if(name == "luaprograms") { - if(state != MACRO) error("luaprograms found outside macro."); - state = LUAPROGRAMS; - - assert(current_macro); // No macro is currently available, cannot create maps! - - return; - } - - // Create Query - if(name == "luaprogram") { - if(state != LUAPROGRAMS) error("lua program found outside maps."); - state = LUAPROGRAM; - - assert(current_macro); // No macro is currently available, cannot create map! - - LUAProgram l; - l.attributes = attributes; - current_macro->luaprograms.push_back(l); - current_luaprogram = &(current_macro->luaprograms.back()); - - return; - } - - // Enable widget parsing - if(name == "window") { - - if(state != MACRO) error("window found outside macro."); - state = WINDOW; - - assert(current_macro); // No macro is currently available, cannot create window! - - current_macro->window.attributes = attributes; - current_macro->window.attributes["type"] = name; - - Widget *current = &(current_macro->window); - widgetstack.push_back(current); - - return; - } - - // TODO: We need to parse some (maybe even all) widgets in order to - // make db lookup of the previous values. - if(state == WINDOW) { - - assert(widgetstack.size()); // Widget stack is empty, cannot create! - - Widget w; - w.attributes = attributes; - w.attributes["type"] = name; - - Widget *parent = widgetstack.back(); - parent->widgets.push_back(w); - - Widget *current = &(parent->widgets.back()); - widgetstack.push_back(current); - - return; - } - - - // Handle include - if(name == "include") { - return; - } - error("Unknown/illegal tag: %s", name.c_str()); } void TemplateParser::endTag(std::string name) { - if(name == "template") state = UNDEFINED; - if(name == "course") state = TEMPLATE; + if(name == "course") state = UNDEFINED; if(name == "macro") { current_macro = NULL; state = COURSE; } - if(name == "queries") state = MACRO; - if(name == "query") state = QUERIES; - if(name == "maps") state = MACRO; - if(name == "map") { - current_map = NULL; - state = MAPS; - } - if(name == "luaprograms") state = MACRO; - if(name == "luaprogram") { - current_luaprogram = NULL; - state = LUAPROGRAMS; - } - if(name == "window") state = MACRO; - - if(state == WINDOW) { - assert(widgetstack.size()); // Widget stack is empty, cannot pop! - widgetstack.pop_back(); - if(widgetstack.size() == 0) state = MACRO; - } } int TemplateParser::readData(char *data, size_t size) @@ -332,7 +181,7 @@ void print_attributes(std::string prefix, int main() { - TemplateParser parser("example2"); + TemplateParser parser("example"); parser.parse(); Template *t = parser.getTemplate(); -- cgit v1.2.3