IBR-DTNSuite
0.8
|
00001 /* 00002 * XMLStreamWriter.cpp 00003 * 00004 * Created on: 01.06.2011 00005 * Author: morgenro 00006 */ 00007 00008 #include "ibrcommon/xml/XMLStreamWriter.h" 00009 #include "ibrcommon/Exceptions.h" 00010 00011 namespace ibrcommon 00012 { 00013 int XMLStreamWriter::__write_callback(void * context, const char * buffer, int len) 00014 { 00015 XMLStreamWriter *writer = static_cast<XMLStreamWriter*>(context); 00016 writer->_stream.write(buffer, len); 00017 return len; 00018 }; 00019 00020 int XMLStreamWriter::__close_callback(void * context) 00021 { 00022 //XMLStreamWriter *writer = static_cast<XMLStreamWriter*>(context); 00023 return 0; 00024 }; 00025 00026 XMLStreamWriter::XMLStreamWriter(std::ostream &stream) 00027 : _stream(stream) 00028 { 00029 _out_buf = xmlOutputBufferCreateIO(__write_callback, __close_callback, this, NULL); 00030 _writer = xmlNewTextWriter(_out_buf); 00031 } 00032 00033 XMLStreamWriter::~XMLStreamWriter() 00034 { 00035 xmlFreeTextWriter(_writer); 00036 } 00037 00038 void XMLStreamWriter::startDocument(const char *encoding) 00039 { 00040 if (xmlTextWriterStartDocument(_writer, NULL, encoding, NULL) < 0) { 00041 throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterStartDocument\n"); 00042 } 00043 } 00044 00045 void XMLStreamWriter::endDocument() 00046 { 00047 if (xmlTextWriterEndDocument(_writer) < 0) { 00048 throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterEndDocument\n"); 00049 } 00050 } 00051 00052 void XMLStreamWriter::startElement(const std::string &name) 00053 { 00054 if (xmlTextWriterStartElement(_writer, BAD_CAST name.c_str()) < 0) { 00055 throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterStartElement\n"); 00056 } 00057 } 00058 00059 void XMLStreamWriter::addAttribute(const std::string &name, const std::string &value) 00060 { 00061 if (xmlTextWriterWriteAttribute(_writer, BAD_CAST name.c_str(), BAD_CAST value.c_str()) < 0) { 00062 throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterWriteAttribute\n"); 00063 } 00064 } 00065 00066 void XMLStreamWriter::addComment(const std::string &comment) 00067 { 00068 if (xmlTextWriterWriteComment(_writer, BAD_CAST comment.c_str()) < 0) 00069 { 00070 throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterWriteComment\n"); 00071 } 00072 } 00073 00074 void XMLStreamWriter::endElement() 00075 { 00076 if (xmlTextWriterEndElement(_writer) < 0) 00077 { 00078 throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterEndElement\n"); 00079 } 00080 } 00081 00082 void XMLStreamWriter::addData(const std::string &data) 00083 { 00084 if (xmlTextWriterWriteRaw(_writer, BAD_CAST data.c_str()) < 0) 00085 { 00086 throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterWriteRaw\n"); 00087 } 00088 } 00089 00090 void XMLStreamWriter::addData(const char *data, const size_t len) 00091 { 00092 if (xmlTextWriterWriteRawLen(_writer, BAD_CAST data, len) < 0) 00093 { 00094 throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterWriteRaw\n"); 00095 } 00096 } 00097 }