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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
// --------------------------------------------------------------------------------
//
// Copyright (C) 2000
// Ralf Westram
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation. Ralf Westram makes no
// representations about the suitability of this software for any
// purpose. It is provided "as is" without express or implied warranty.
//
// This code is part of my library.
// You may find a more recent version at http://www.reallysoft.de/
//
// --------------------------------------------------------------------------------
#include "xml.hxx"
using namespace std;
XML_Document *the_XML_Document = 0;
static const char *entities =
" <!ENTITY nbsp \" \">\n"
" <!ENTITY acute \"´\">\n" // Acute accent (forward)
" <!ENTITY eacute \"é\">\n" // e Acute (forward)
" <!ENTITY apostr \"'\">\n" // single quote (vertical)
" <!ENTITY semi \";\">\n"
;
static string encodeEntities(const string& str, bool /*quotedText*/) {
// if quotedText is true the string is encoded for usage in quotes
// currently it makes no difference, but this might change
string neu;
neu.reserve(str.length()*4);
for (string::const_iterator s = str.begin(); s != str.end(); ++s) {
char replace = 0;
const char *entity = 0;
switch (*s) {
case '<': { entity = "lt"; break; }
case '>': { entity = "gt"; break; }
case '&': { entity = "amp"; break; }
case '\'': { entity = "apostr"; break; }
case char(0xb4): { entity = "acute"; break; } // acute (forward)
case '': { entity = "eacute"; break; }
default: { replace = *s; }
}
if (replace) {
neu.append(1, replace);
}
else {
xml_assert(entity);
neu.append(1, '&');
neu.append(entity);
neu.append(1, ';');
}
}
return neu;
}
// ----------------------
// XML_Attribute
XML_Attribute::XML_Attribute(const string& name_, const string& content_)
: name(name_)
, content(content_)
, next(0)
{}
XML_Attribute::~XML_Attribute() {
delete next;
}
XML_Attribute *XML_Attribute::append_to(XML_Attribute *queue) {
if (!queue) return this;
queue->next = append_to(queue->next);
return queue;
}
void XML_Attribute::print(FILE *out) const {
fprintf(out, " %s=\"%s\"", name.c_str(), encodeEntities(content, true).c_str());
if (next) next->print(out);
}
// -----------------
// XML_Node
XML_Node::XML_Node(bool is_tag) {
xml_assert(the_XML_Document);
father = the_XML_Document->LatestSon();
the_XML_Document->set_LatestSon(this);
indent = 0;
if (father) {
father->add_son(this, is_tag);
indent = father->Indent()+1;
}
opened = false;
}
XML_Node::~XML_Node() {
if (father) father->remove_son(this);
the_XML_Document->set_LatestSon(father);
}
// ----------------
// XML_Tag
XML_Tag::XML_Tag(const string &name_)
: XML_Node(true)
, name(name_)
, son(0)
, attribute(0)
, state(0)
, onExtraLine(true)
{}
XML_Tag::~XML_Tag() {
FILE *out = the_XML_Document->Out();
xml_assert(!son);
close(out);
delete attribute;
}
void XML_Tag::add_attribute(const string& name_, const string& content_) {
XML_Attribute *newAttr = new XML_Attribute(name_, content_);
attribute = newAttr->append_to(attribute);
}
void XML_Tag::add_attribute(const string& name_, int value) {
char buf[30];
sprintf(buf, "%i", value);
add_attribute(name_, buf);
}
void XML_Tag::add_son(XML_Node *son_, bool son_is_tag) {
if (son) throw string("Tried to add a second son! Destroy previous son first.");
son = son_;
int wanted_state = son_is_tag ? 2 : 1;
if (state<wanted_state) state = wanted_state;
}
void XML_Tag::remove_son(XML_Node *son_) {
if (son != son_) throw string("Tried to remove wrong son!");
son = 0;
}
inline void to_indent(FILE *out, int indent) { int i = indent*the_XML_Document->indentation_per_level; while (i--) fputc(' ', out); }
void XML_Tag::open(FILE *out) {
if (father && !father->Opened()) father->open(out);
if (onExtraLine) {
fputc('\n', out);
to_indent(out, Indent());
}
fputc('<', out); fputs(name.c_str(), out);
if (attribute) attribute->print(out);
fputc('>', out);
opened = true;
}
void XML_Tag::close(FILE *out) {
if (!opened) {
if (!the_XML_Document->skip_empty_tags || attribute || !father) {
if (father && !father->Opened()) father->open(out);
if (onExtraLine) {
fputc('\n', out);
to_indent(out, Indent());
}
fputc('<', out); fputs(name.c_str(), out);
if (attribute) attribute->print(out);
fputs("/>", out);
}
}
else {
if (state >= 2 && onExtraLine) { fputc('\n', out); to_indent(out, Indent()); }
fprintf(out, "</%s>", name.c_str());
}
}
// -----------------
// XML_Text
XML_Text::~XML_Text() {
FILE *out = the_XML_Document->Out();
close(out);
}
void XML_Text::add_son(XML_Node *, bool) {
throw string("Can't add son to XML_Text-Node");
}
void XML_Text::remove_son(XML_Node * /* son_ */) {
throw string("Can't remove son from XML_Text-Node");
}
void XML_Text::open(FILE *) {}
void XML_Text::close(FILE *out) {
if (father && !father->Opened()) father->open(out);
fputs(encodeEntities(content, false).c_str(), out);
}
// --------------------
// XML_Comment
XML_Comment::~XML_Comment() {
FILE *out = the_XML_Document->Out();
close(out);
}
void XML_Comment::add_son(XML_Node *, bool) {
throw string("Can't add son to XML_Comment-Node");
}
void XML_Comment::remove_son(XML_Node *) {
throw string("Can't remove son from XML_Comment-Node");
}
void XML_Comment::open(FILE *) {}
void XML_Comment::close(FILE *out) {
fputc('\n', out); to_indent(out, Indent());
fputs("<!--", out);
fputs(encodeEntities(content, false).c_str(), out);
fputs("-->", out);
}
// ---------------------
// XML_Document
XML_Document::XML_Document(const string& name_, const string& dtd_, FILE *out_)
: dtd(dtd_),
root(0),
out(out_),
skip_empty_tags(false),
indentation_per_level(1)
{
xml_assert(out);
if (the_XML_Document) GBK_terminate("You can only have one XML_Document at a time.");
the_XML_Document = this;
latest_son = 0;
root = new XML_Tag(name_);
xml_assert(latest_son == root);
fputs("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n", out);
fprintf(out, "<!DOCTYPE %s SYSTEM '%s' [\n%s]>\n", name_.c_str(), dtd.c_str(), entities);
}
XML_Document::~XML_Document() {
delete root;
xml_assert(the_XML_Document == this);
the_XML_Document = 0;
}
|