22template <
typename ostream_t>
32 ostream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
40 void put(
char c)
override {
67: name(name), writer(&writer), hasChildren(
false) {
72: name(
std::move(name)), writer(&writer), hasChildren(
false) {
77: name(name), writer(parent.writer), hasChildren(
false) {
78 parent.ensureIsCurrent();
83: name(
std::move(name)), writer(parent.writer), hasChildren(
false) {
84 parent.ensureIsCurrent();
89 this->operator =(std::move(
to_move));
101 name = std::move(
to_move.name);
104 attributesStillAlowed =
to_move.attributesStillAlowed;
105 hasChildren =
to_move.hasChildren;
107 this->writer->current =
this;
118 for (
Element* i = this->parent; i != 0; i = i->parent) ++
result;
123 if (!attributesStillAlowed)
124 throw XMLWriterException(format(
"can't append attribute \"{1}\" to \"{0}\" XML element because this element has already non-empty content.", name, attr_name));
125 writer->out->put(
' ');
126 writer->appendStr(attr_name);
127 writer->out->puts(
"=\"");
129 writer->out->put(
'"');
135 disallowAttributes();
136 writer->appendStrQuoted(
str);
142 disallowAttributes();
143 writer->out->puts(
"<![CDATA[");
144 writer->appendStr(
str);
145 writer->out->puts(
"]]>");
150 if (disallowAttributes()) writer->out->newline();
151 std::size_t l = (getLevel() + 1) * writer->indentation;
152 while (l > 0) { writer->out->put(
' '); --l; }
158 Element* current = writer->current;
160 return current ? *current : *
this;
163void XMLWriter::Element::writeOpening() {
164 attributesStillAlowed =
true;
165 parent = writer->current;
166 if (writer->current) {
167 writer->current->hasChildren =
true;
168 if (writer->current->disallowAttributes())
169 writer->out->newline();
171 writer->current =
this;
172 std::size_t l = getLevel() * writer->indentation;
173 while (l > 0) { writer->out->put(
' '); --l; }
174 writer->out->put(
'<');
175 writer->appendStr(name);
178void XMLWriter::Element::writeClosing()
180 if (attributesStillAlowed) {
181 writer->out->puts(
"/>");
184 std::size_t l = getLevel() * writer->indentation;
185 while (l > 0) { writer->out->put(
' '); --l; }
187 writer->out->puts(
"</");
188 writer->appendStr(name);
189 writer->out->put(
'>');
191 writer->out->newline();
192 writer->current = this->parent;
195bool XMLWriter::Element::disallowAttributes() {
196 if (attributesStillAlowed) {
197 writer->out->put(
'>');
199 writer->current->attributesStillAlowed =
false;
205void XMLWriter::Element::ensureIsCurrent() {
206 if (
this != writer->current)
207 throw XMLWriterException(
"operation is not permitted as the XML element \""+ name +
"\" is not the last one in the stack");
211 : out(
new OStreamRef(out)), current(0), indentation(indentation) {}
222 : out(out), current(0), indentation(indentation)
225void XMLWriter::appendStrQuoted(
const char *s) {
228 case '"': out->
puts(
""");
break;
229 case '<': out->
puts(
"<");
break;
230 case '>': out->
puts(
">");
break;
231 case '&': out->
puts(
"&");
break;
232 case '\'': out->
puts(
"'");
break;
233 default: out->
put(*s);
break;