IBR-DTNSuite  0.10
XMLStreamWriter.cpp
Go to the documentation of this file.
1 /*
2  * XMLStreamWriter.cpp
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
23 #include "ibrcommon/Exceptions.h"
24 
25 namespace ibrcommon
26 {
27  int XMLStreamWriter::__write_callback(void * context, const char * buffer, int len)
28  {
29  XMLStreamWriter *writer = static_cast<XMLStreamWriter*>(context);
30  writer->_stream.write(buffer, len);
31  return len;
32  }
33 
35  {
36  //XMLStreamWriter *writer = static_cast<XMLStreamWriter*>(context);
37  return 0;
38  }
39 
40  XMLStreamWriter::XMLStreamWriter(std::ostream &stream)
41  : _stream(stream)
42  {
43  _out_buf = xmlOutputBufferCreateIO(__write_callback, __close_callback, this, NULL);
44  _writer = xmlNewTextWriter(_out_buf);
45  }
46 
48  {
49  xmlFreeTextWriter(_writer);
50  }
51 
52  void XMLStreamWriter::startDocument(const char *encoding)
53  {
54  if (xmlTextWriterStartDocument(_writer, NULL, encoding, NULL) < 0) {
55  throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterStartDocument\n");
56  }
57  }
58 
60  {
61  if (xmlTextWriterEndDocument(_writer) < 0) {
62  throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterEndDocument\n");
63  }
64  }
65 
66  void XMLStreamWriter::startElement(const std::string &name)
67  {
68  if (xmlTextWriterStartElement(_writer, BAD_CAST name.c_str()) < 0) {
69  throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterStartElement\n");
70  }
71  }
72 
73  void XMLStreamWriter::addAttribute(const std::string &name, const std::string &value)
74  {
75  if (xmlTextWriterWriteAttribute(_writer, BAD_CAST name.c_str(), BAD_CAST value.c_str()) < 0) {
76  throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterWriteAttribute\n");
77  }
78  }
79 
80  void XMLStreamWriter::addComment(const std::string &comment)
81  {
82  if (xmlTextWriterWriteComment(_writer, BAD_CAST comment.c_str()) < 0)
83  {
84  throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterWriteComment\n");
85  }
86  }
87 
89  {
90  if (xmlTextWriterEndElement(_writer) < 0)
91  {
92  throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterEndElement\n");
93  }
94  }
95 
96  void XMLStreamWriter::addData(const std::string &data)
97  {
98  if (xmlTextWriterWriteRaw(_writer, BAD_CAST data.c_str()) < 0)
99  {
100  throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterWriteRaw\n");
101  }
102  }
103 
104  void XMLStreamWriter::addData(const char *data, const int len)
105  {
106  if (xmlTextWriterWriteRawLen(_writer, BAD_CAST data, len) < 0)
107  {
108  throw ibrcommon::Exception("XMLStreamWriter: Error at xmlTextWriterWriteRaw\n");
109  }
110  }
111 }