This repository was archived by the owner on Feb 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathXMLBuilder.cpp
More file actions
111 lines (93 loc) · 3.4 KB
/
XMLBuilder.cpp
File metadata and controls
111 lines (93 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "XMLBuilder.h"
using std::string;
XMLMemoryWriter::XMLMemoryWriter() {
buffer_ = xmlBufferCreate();
writer_ = xmlNewTextWriterMemory(buffer_, 0);
}
XMLMemoryWriter::~XMLMemoryWriter() {
xmlFreeTextWriter(writer_);
}
xmlTextWriterPtr XMLMemoryWriter::getWriter() {
return writer_;
}
string XMLMemoryWriter::getContent() {
return string((const char*)buffer_->content);
}
/*************************************************************
* Begin of XMLDocument's implementation *
* *
************************************************************/
XMLDocument::XMLDocument(xmlTextWriterPtr writer) :
writer_(writer),
init_(false) {
}
XMLDocument::XMLDocument(xmlTextWriterPtr writer, const DTDInfo &info) :
writer_(writer),
dtdInfo_(info),
init_(false) {
}
XMLDocument::~XMLDocument() {
LOG_DEBUG("XMLDocument::~");
if ( init_ ) {
end();
}
}
void XMLDocument::init() {
LOG_DEBUG("XMLDocument::init");
init_ = true;
if ( xmlTextWriterStartDocument(writer_, "1.0", "UTF-8", NULL) < 0 )
throw XMLError("Error starting the document.");
if (xmlTextWriterSetIndent(writer_, 2) < 0 )
throw XMLError("Error set document indentation");
if ( dtdInfo_.name != "" ) {
if (xmlTextWriterStartDTD(writer_,
BAD_CAST dtdInfo_.name.c_str(),
BAD_CAST dtdInfo_.id.c_str(),
BAD_CAST dtdInfo_.url.c_str()) < 0)
throw XMLError("Error setting DTD information");
if (xmlTextWriterEndDTD(writer_) < 0 )
throw XMLError("Error setting DTD information");
}
}
void XMLDocument::end() {
if ( xmlTextWriterEndDocument(writer_) < 0 )
throw XMLError("Error trying to close the document");
}
void XMLDocument::writeNode(XMLNode& node) {
LOG_DEBUG("XMLDocument::writeNode - node="+node.name);
if (xmlTextWriterStartElement(writer_, BAD_CAST node.name.c_str()) < 0)
throw XMLError("Error starting element " + node.name);
if ( node.attributes.size() > 0 ) {
writeAttributes(node.attributes);
}
if ( node.children.size() > 0 ) {
writeChildren(node.children);
} else if ( node.text.length() > 0 ) {
LOG_DEBUG("Writing node's text " + node.text);
if (xmlTextWriterWriteString(writer_, BAD_CAST node.text.c_str()) < 0)
throw XMLError("Error seting node's value "+node.text);
}
if (xmlTextWriterEndElement(writer_) < 0)
throw XMLError("Error closing element "+node.name);
}
void XMLDocument::writeChildren(XMLNode::MapType &children) {
LOG_DEBUG("XMLDocument::writeChildren");
for ( XMLNode::MapType::iterator it = children.begin();
it != children.end(); it++ ) {
XMLNode::ListType &list = it->second;
for ( XMLNode::ListType::iterator it1 = list.begin();
it1 != list.end(); it1++ ) {
writeNode(*it1);
}
}
}
void XMLDocument::writeAttributes(map<string, string> &attr) {
LOG_DEBUG("XMLDocument::writeAttributes");
for ( map<string, string>::iterator it = attr.begin();
it != attr.end(); it++ ) {
if ( xmlTextWriterWriteAttribute(writer_,
BAD_CAST it->first.c_str(),
BAD_CAST it->second.c_str()) < 0)
throw XMLError("Error adding attribute: " + it->first);
}
}