diff options
Diffstat (limited to 'server/src/templateparser.cc')
-rw-r--r-- | server/src/templateparser.cc | 142 |
1 files changed, 98 insertions, 44 deletions
diff --git a/server/src/templateparser.cc b/server/src/templateparser.cc index d01aa31..277a8bd 100644 --- a/server/src/templateparser.cc +++ b/server/src/templateparser.cc @@ -144,6 +144,12 @@ void TemplateParser::parseError(char *buf, size_t len, std::string error, int li if(fwrite(buf, len, 1, stderr) != len) {} fprintf(stderr, "]\n"); fflush(stderr); + + char *slineno; + if(asprintf(&slineno, " at line %d\n", lineno) != -1) { + throw Exception(error + slineno); + free(slineno); + } } Template *TemplateParser::getTemplate() @@ -153,56 +159,104 @@ Template *TemplateParser::getTemplate() #ifdef TEST_TEMPLATEPARSER -void print_attributes(std::string prefix, - std::map< std::string, std::string > &att) -{ - std::map< std::string, std::string >::iterator i = att.begin(); - while(i != att.end()) { - printf("%s %s = \"%s\"\n", prefix.c_str(), (*i).first.c_str(), (*i).second.c_str()); - i++; - } -} +#define XMLFILE "/tmp/test_templateparser.xml" + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <string.h> +#include <stdio.h> +#include <memory.h> + +static char xml[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<template name=\"testtemplate\" version=\"1.0\">\n" +" <macro name=\"mymacro\"/>\n" +" <header caption=\"mycaption\"/>\n" +"</template>" +; + +static char xml_nontemplate[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<dims name=\"testtemplate\" version=\"1.0\">\n" +" <sometag/>\n" +" <someothertag/>\n" +"</dims>" +; + +static char xml_fail[] = +"<?xml version='1.0' encoding='UTF-8'?>\n" +"<template name\"testtemplate\" version=\"1.0\">\n" +" <sometag/>\n" +" <someothertag/>\n" +"</template>" +; int main() { - Conf::xml_basedir = "../xml/"; - - try { - TemplateParser parser("../xml/templates/example.xml"); - parser.parse(); - - Template *t = parser.getTemplate(); - - printf("\t[Template]:\n"); - print_attributes("\t\t-", t->attributes); - - printf("\t\t[Macros]:\n"); - std::vector< Macro >::iterator i = t->macros.begin(); - - while(i != t->macros.end()) { - printf("\t\t\t[Macro]:\n"); - print_attributes("\t\t\t\t-", (*i).attributes); - - std::vector< Query >::iterator qi = (*i).queries.begin(); - while(qi != (*i).queries.end()) { - printf("\t\t\t\t[Query]:\n"); - print_attributes("\t\t\t\t\t-", (*qi).attributes); - qi++; - } - - std::vector< Map >::iterator mi = (*i).maps.begin(); - while(mi != (*i).maps.end()) { - printf("\t\t\t\t[Map]:\n"); - print_attributes("\t\t\t\t\t-", (*mi).attributes); - mi++; - } - - i++; + FILE *fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; + } + fprintf(fp, xml); + fclose(fp); + + { + // Test parsing of correct template xml data. + TemplateParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + return 1; + } + } + + fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; + } + fprintf(fp, xml_nontemplate); + fclose(fp); + + // Test parsing of correct xml data, but not template (should throw an exception). + { + TemplateParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + goto onandon; + } + return 1; + } + onandon: + + fp = fopen(XMLFILE, "w"); + if(!fp) { + printf("Could not write to %s\n", XMLFILE); + return 1; + } + fprintf(fp, xml_fail); + fclose(fp); + + // Test parsing of invalid xml data (should throw an exception). + { + TemplateParser parser(XMLFILE); + try { + parser.parse(); + } catch(Exception &e) { + printf("Failed to parse: %s\n", e.what()); + goto yetonandon; } - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); return 1; } + yetonandon: + + unlink(XMLFILE); + return 0; } |