summaryrefslogtreecommitdiff
path: root/server/src/saxparser.cc
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/saxparser.cc')
-rw-r--r--server/src/saxparser.cc251
1 files changed, 125 insertions, 126 deletions
diff --git a/server/src/saxparser.cc b/server/src/saxparser.cc
index 9072ab6..f26c965 100644
--- a/server/src/saxparser.cc
+++ b/server/src/saxparser.cc
@@ -74,7 +74,7 @@ SAXParser::SAXParser()
{
p = XML_ParserCreate(NULL);
if(!p) {
- PRACRO_ERR_LOG(sax, "Couldn't allocate memory for parser\n");
+ PRACRO_ERR(sax, "Couldn't allocate memory for parser\n");
// throw Exception(...);
return;
}
@@ -103,7 +103,8 @@ int SAXParser::parse()
do {
len = readData(buf, sizeof(buf) - 1);
if (! XML_Parse(p, buf, len, len == 0)) {
- parseError(buf, len, XML_ErrorString(XML_GetErrorCode(p)), (int)XML_GetCurrentLineNumber(p));
+ parseError(buf, len, XML_ErrorString(XML_GetErrorCode(p)),
+ (int)XML_GetCurrentLineNumber(p));
return 1;
}
@@ -123,35 +124,43 @@ static bool iswhitespace(const char *buf, size_t size)
bool SAXParser::parse(const char *data, size_t size)
{
- PRACRO_DEBUG(sax, "parse %d bytes\n", size);
+ std::string xml;
+ xml.append(data, size);
+ PRACRO_DEBUG(sax, "parse %d bytes [%s]\n", size, xml.c_str());
bufferbytes = size;
totalbytes += bufferbytes;
if(! XML_Parse(p, data, size, false) ) {
if(XML_GetErrorCode(p) == XML_ERROR_JUNK_AFTER_DOC_ELEMENT) return true;
- if(XML_GetErrorCode(p) == XML_ERROR_FINISHED && iswhitespace(data, size)) return true;
+ if(XML_GetErrorCode(p) == XML_ERROR_FINISHED &&
+ iswhitespace(data, size)) return true;
if(done && XML_GetErrorCode(p) == XML_ERROR_UNCLOSED_TOKEN) return true;
- parseError(data, size, XML_ErrorString(XML_GetErrorCode(p)), (int)XML_GetCurrentLineNumber(p));
+ parseError(data, size, XML_ErrorString(XML_GetErrorCode(p)),
+ (int)XML_GetCurrentLineNumber(p));
return false;
}
if(done) {
if(! XML_Parse(p, data, 0, true) ) {
if(XML_GetErrorCode(p) == XML_ERROR_JUNK_AFTER_DOC_ELEMENT) return true;
- if(XML_GetErrorCode(p) == XML_ERROR_FINISHED && iswhitespace(data, size)) return true;
+ if(XML_GetErrorCode(p) == XML_ERROR_FINISHED &&
+ iswhitespace(data, size)) return true;
if(XML_GetErrorCode(p) == XML_ERROR_UNCLOSED_TOKEN) return true;
- parseError(data, 0, XML_ErrorString(XML_GetErrorCode(p)), (int)XML_GetCurrentLineNumber(p));
+ parseError(data, 0, XML_ErrorString(XML_GetErrorCode(p)),
+ (int)XML_GetCurrentLineNumber(p));
return false;
}
}
- if(done) PRACRO_DEBUG(sax, "Got END_OF_DOCUMENT [%s] at %ld\n", outertag.c_str(), XML_GetCurrentByteIndex(p));
+ if(done) PRACRO_DEBUG(sax, "Got END_OF_DOCUMENT [%s] at %ld\n",
+ outertag.c_str(), XML_GetCurrentByteIndex(p));
return done;
}
-void SAXParser::parseError(const char *buf, size_t len, std::string error, int lineno)
+void SAXParser::parseError(const char *buf, size_t len,
+ std::string error, int lineno)
{
fprintf(stderr, "SAXParser error at line %d: %s\n", lineno, error.c_str());
fprintf(stderr, "\tBuffer %u bytes: [", len);
@@ -167,6 +176,10 @@ unsigned int SAXParser::usedBytes()
#ifdef TEST_SAXPARSER
+//deps: log.cc debug.cc exception.cc
+//cflags: -I..
+//libs: -lexpat
+#include <test.h>
#define XMLFILE "/tmp/saxparsertest.xml"
@@ -253,139 +266,125 @@ public:
}
};
-int main(int argc, char *argv[])
-{
- FILE *fp = fopen(XMLFILE, "w");
- if(!fp) {
- printf("Could not write to %s\n", XMLFILE);
- return 1;
- }
- fprintf(fp, xml);
- fclose(fp);
+TEST_BEGIN;
- // Test callback parser
- {
- MyFileParser parser(XMLFILE);
- parser.parse();
- }
+FILE *fp = fopen(XMLFILE, "w");
+TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written.");
+if(!fp) TEST_FATAL("Could not write "XMLFILE);
+fprintf(fp, "%s", xml);
+fclose(fp);
- // Test buffer parser
- for(size_t sz = 1; sz < 1000; sz++) {
- bool test = false;
- MyBufferParser parser;
- std::string buf = xml;
- size_t pos = 0;
- while(pos < buf.length()) {
- std::string substr = buf.substr(pos, sz);
-
- try {
- test |= parser.parse((char*)substr.c_str(), substr.length());
- } catch(Exception &e) {
- printf("Buffer parser failed on size %d: %s [%s]\n", sz, e.what(), substr.c_str());
- }
- pos += sz;
- }
+TEST_MSG("Test callback parser.");
+{
+ MyFileParser parser(XMLFILE);
+ parser.parse();
+}
- if(!test) {
- printf("Buffer parser failed on size %d\n", sz);
- return 1;
+TEST_MSG("Test buffer parser.");
+for(size_t sz = 1; sz < 1000; sz++) {
+ bool test = false;
+ MyBufferParser parser;
+ std::string buf = xml;
+ size_t pos = 0;
+ while(pos < buf.length()) {
+ std::string substr = buf.substr(pos, sz);
+
+ try {
+ test |= parser.parse((char*)substr.c_str(), substr.length());
+ } catch(Exception &e) {
+ TEST_TRUE(true, "Buffer parser failed on size %d: %s [%s]",
+ sz, e.what(), substr.c_str());
}
+ pos += sz;
}
+
+ TEST_TRUE(test, "Test buffer parser on %d bytes", sz);
+ }
- fp = fopen(XMLFILE, "w");
- if(!fp) {
- printf("Could not write to %s\n", XMLFILE);
- return 1;
- }
- fprintf(fp, xml_notrailingwhitespace);
- fprintf(fp, xml_notrailingwhitespace);
- fclose(fp);
- // Test buffer parser with multiple documents in the same buffer
- {
- fp = fopen(XMLFILE, "r");
- if(!fp) {
- printf("Could not write to %s\n", XMLFILE);
- return 1;
- }
- for(size_t sz = 1; sz < 1000; sz++) {
- MyBufferParser *parser = NULL;
- rewind(fp);
- size_t numdocs = 0;
- char *buf = new char[sz + 1];
- memset(buf, 0, sz + 1);
- size_t size;
- while( (size = fread(buf, 1, sz, fp)) > 0) {
- while(size) {
- if(parser == NULL) {
- parser = new MyBufferParser();
- }
- if(parser->parse(buf, size)) {
-
- // Got one
- numdocs++;
-
- size = size - parser->usedBytes();
- strcpy(buf, buf + parser->usedBytes());
- delete parser; parser = NULL;
- } else {
- size = 0;
- memset(buf, 0, sz + 1);
- }
+fp = fopen(XMLFILE, "w");
+TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written.");
+if(!fp) TEST_FATAL("Could not write "XMLFILE);
+fprintf(fp, "%s", xml_notrailingwhitespace);
+fprintf(fp, "%s", xml_notrailingwhitespace);
+fclose(fp);
+
+TEST_MSG("Test buffer parser with multiple documents in the same buffer.");
+{
+ fp = fopen(XMLFILE, "r");
+ TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be read.");
+ if(!fp) TEST_FATAL("Could not read from "XMLFILE);
+
+ for(size_t sz = 1; sz < 1000; sz++) {
+ MyBufferParser *parser = NULL;
+ rewind(fp);
+ size_t numdocs = 0;
+ char *buf = new char[sz + 1];
+ memset(buf, 0, sz + 1);
+ size_t size;
+ while( (size = fread(buf, 1, sz, fp)) > 0) {
+ while(size) {
+ if(parser == NULL) {
+ parser = new MyBufferParser();
+ }
+ if(parser->parse(buf, size)) {
+
+ // Got one
+ numdocs++;
+
+ size = size - parser->usedBytes();
+ strcpy(buf, buf + parser->usedBytes());
+ delete parser; parser = NULL;
+ } else {
+ size = 0;
+ memset(buf, 0, sz + 1);
}
}
- if(numdocs != 2) {
- printf("Failed to parse two documents.\n");
- return 1;
- }
- if(parser) delete parser; parser = NULL;
- delete[] buf;
}
- fclose(fp);
- }
-
- fp = fopen(XMLFILE, "w");
- if(!fp) {
- printf("Could not write to %s\n", XMLFILE);
- return 1;
+ TEST_EQUAL(numdocs, 2, "Test if 2 documents were parsed on docsize %d.", sz);
+ if(parser) delete parser; parser = NULL;
+ delete[] buf;
}
- fprintf(fp, xml_fail);
fclose(fp);
+}
- // Test failure
- {
- MyFileParser parser(XMLFILE);
- try {
- parser.parse();
- } catch(Exception &e) {
- goto goon;
- }
- printf("This test should fail...\n");
- return 1;
- }
- goon:
+fp = fopen(XMLFILE, "w");
+TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written.");
+if(!fp) TEST_FATAL("Could not write "XMLFILE);
+fprintf(fp, "%s", xml_fail);
+fclose(fp);
- fp = fopen(XMLFILE, "w");
- if(!fp) {
- printf("Could not write to %s\n", XMLFILE);
- return 1;
+TEST_MSG("Test failure");
+{
+ MyFileParser parser(XMLFILE);
+ try {
+ parser.parse();
+ } catch(Exception &e) {
+ goto goon;
}
- fprintf(fp, xml_fail2);
- fclose(fp);
+ TEST_TRUE(false, "This test should fail...\n");
+}
+goon:
- // Test failure
- {
- MyFileParser parser(XMLFILE);
- try {
- parser.parse();
- } catch(Exception &e) {
- goto goonagain;
- }
- printf("This test should fail...\n");
- return 1;
- }
- goonagain:
+fp = fopen(XMLFILE, "w");
+TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written.");
+if(!fp) TEST_FATAL("Could not write "XMLFILE);
+fprintf(fp, "%s", xml_fail2);
+fclose(fp);
- unlink(XMLFILE);
+// Test failure
+{
+ MyFileParser parser(XMLFILE);
+ try {
+ parser.parse();
+ } catch(Exception &e) {
+ goto goonagain;
+ }
+ TEST_TRUE(false, "This test should fail...\n");
}
+goonagain:
+
+unlink(XMLFILE);
+
+TEST_END;
#endif/*TEST_SAXPARSER*/