IBR-DTNSuite  0.8
ibrcommon/ibrcommon/xml/XMLStreamReader.cpp
Go to the documentation of this file.
00001 /*
00002  * XMLStreamReader.cpp
00003  *
00004  *  Created on: 01.06.2011
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrcommon/xml/XMLStreamReader.h"
00009 #include "ibrcommon/Exceptions.h"
00010 #include <string.h>
00011 
00012 namespace ibrcommon
00013 {
00014         XMLStreamReader::XMLStreamReader(XMLStreamHandler &callback)
00015          : _callback(callback)
00016         { }
00017 
00018         XMLStreamReader::~XMLStreamReader()
00019         { }
00020 
00021         void XMLStreamReader::__startDocument(void *data)
00022         {
00023                 XMLStreamHandler *cb = static_cast<XMLStreamHandler*>(data);
00024                 cb->startDocument();
00025         }
00026 
00027         void XMLStreamReader::__endDocument(void *data)
00028         {
00029                 XMLStreamHandler *cb = static_cast<XMLStreamHandler*>(data);
00030                 cb->endDocument();
00031         }
00032 
00033         void XMLStreamReader::__startElement(void *data, const xmlChar *fullname, const xmlChar **ats)
00034         {
00035                 XMLStreamHandler *cb = static_cast<XMLStreamHandler*>(data);
00036                 std::map<std::string, std::string> attrs;
00037 
00038                 if (ats != NULL)
00039                 {
00040                         int i = 0;
00041                         while (ats[i] != NULL)
00042                         {
00043                                 attrs[std::string((const char*)ats[i])] = std::string((const char*)ats[i+1]);
00044                                 i += 2;
00045                         }
00046                 }
00047 
00048                 const std::string name((const char*)fullname);
00049                 cb->startElement(name, attrs);
00050         }
00051 
00052         void XMLStreamReader::__endElement(void *data, const xmlChar *fullname)
00053         {
00054                 XMLStreamHandler *cb = static_cast<XMLStreamHandler*>(data);
00055                 const std::string name((const char*)fullname);
00056                 cb->endElement(name);
00057         }
00058 
00059         void XMLStreamReader::__characters(void *data, const xmlChar *ch, int len)
00060         {
00061                 XMLStreamHandler *cb = static_cast<XMLStreamHandler*>(data);
00062                 cb->characters((const char*)ch, len);
00063         }
00064 
00065         void XMLStreamReader::parse(std::istream &stream)
00066         {
00067                 xmlSAXHandler my_handler;
00068                 ::memset(&my_handler, '\0', sizeof(xmlSAXHandler));
00069 
00070                 my_handler.startDocument = __startDocument;
00071                 my_handler.endDocument = __endDocument;
00072                 my_handler.startElement = __startElement;
00073                 my_handler.endElement = __endElement;
00074                 my_handler.characters = __characters;
00075 
00076                 int res = 0, size = 1024;
00077                 char chars[1024];
00078                 xmlParserCtxtPtr ctx = xmlCreatePushParserCtxt(&my_handler, &_callback, chars, res, NULL);
00079 
00080                 while (stream.good())
00081                 {
00082                         stream.read(chars, size);
00083 
00084                         if (stream.eof())
00085                         {
00086                                 xmlParseChunk(ctx, chars, stream.gcount(), 1);
00087                         }
00088                         else
00089                         {
00090                                 xmlParseChunk(ctx, chars, stream.gcount(), 0);
00091                         }
00092                 }
00093         }
00094 
00095 }