00001 00002 // 00003 // Copyright (C) 2000 00004 // Ralf Westram 00005 // Time-stamp: <Thu May/02/2002 09:33 MET Coder@ReallySoft.de> 00006 // 00007 // Permission to use, copy, modify, distribute and sell this software 00008 // and its documentation for any purpose is hereby granted without fee, 00009 // provided that the above copyright notice appear in all copies and 00010 // that both that copyright notice and this permission notice appear 00011 // in supporting documentation. Ralf Westram makes no 00012 // representations about the suitability of this software for any 00013 // purpose. It is provided "as is" without express or implied warranty. 00014 // 00015 // This code is part of my library. 00016 // You may find a more recent version at http://www.reallysoft.de/ 00017 // 00019 00020 #include "xml.h" 00021 00022 using namespace std; 00023 using namespace rs; 00024 using namespace rs::xml; 00025 00026 static XML_Document *theDocument = 0; 00027 00028 // ******************************************************************************** 00029 00030 // ---------------------------------------------------------------------------------- 00031 // XML_Attribute::XML_Attribute(const string& name_, const string& content_) 00032 // ---------------------------------------------------------------------------------- 00033 XML_Attribute::XML_Attribute(const string& name_, const string& content_) 00034 : name(name_), content(content_), next(0) 00035 {} 00036 00037 // ---------------------------------------- 00038 // XML_Attribute::~XML_Attribute() 00039 // ---------------------------------------- 00040 XML_Attribute::~XML_Attribute() { 00041 delete next; 00042 } 00043 // ---------------------------------------------------------------------- 00044 // XML_Attribute *XML_Attribute::append_to(XML_Attribute *queue) 00045 // ---------------------------------------------------------------------- 00046 XML_Attribute *XML_Attribute::append_to(XML_Attribute *queue) { 00047 if (!queue) return this; 00048 queue->next = append_to(queue->next); 00049 return queue; 00050 } 00051 // ------------------------------------------------ 00052 // void XML_Attribute::print(ostream& out) 00053 // ------------------------------------------------ 00054 void XML_Attribute::print(ostream& out) const { 00055 out << " " << name << "=\"" << content << "\""; 00056 if (next) next->print(out); 00057 } 00058 00059 // ******************************************************************************** 00060 00061 // ----------------------------- 00062 // XML_Node::XML_Node() 00063 // ----------------------------- 00064 XML_Node::XML_Node() { 00065 assert(theDocument); 00066 00067 father = theDocument->LatestSon(); 00068 theDocument->set_LatestSon(this); 00069 indent = 0; 00070 00071 if (father) { 00072 father->add_son(this); 00073 indent = father->Indent()+1; 00074 } 00075 00076 opened = false; 00077 } 00078 00079 // ------------------------------ 00080 // XML_Node::~XML_Node() 00081 // ------------------------------ 00082 XML_Node::~XML_Node() { 00083 if (father) father->remove_son(this); 00084 theDocument->set_LatestSon(father); 00085 } 00086 00087 // ******************************************************************************** 00088 00089 inline void to_indent(ostream& out, int indent) { while (indent--) out << ' '; } 00090 00091 // --------------------------------------------- 00092 // XML_Tag::XML_Tag(const string &name_) 00093 // --------------------------------------------- 00094 XML_Tag::XML_Tag(const string &name_) 00095 : name(name_), son(0), attribute(0) 00096 { 00097 } 00098 // ---------------------------- 00099 // XML_Tag::~XML_Tag() 00100 // ---------------------------- 00101 XML_Tag::~XML_Tag() { 00102 ostream& out = theDocument->Out(); 00103 if (son) throw string("XML_Tag has son in destructor"); 00104 close(out); 00105 } 00106 00107 // --------------------------------------------------------------------------------- 00108 // void XML_Tag::add_attribute(const string& name_, const string& content_) 00109 // --------------------------------------------------------------------------------- 00110 void XML_Tag::add_attribute(const string& name_, const string& content_) { 00111 XML_Attribute *newAttr = new XML_Attribute(name_, content_); 00112 attribute = newAttr->append_to(attribute); 00113 } 00114 // ---------------------------------------------- 00115 // void XML_Tag::add_son(XML_Node *son_) 00116 // ---------------------------------------------- 00117 void XML_Tag::add_son(XML_Node *son_) { 00118 if (son) throw string("Tried to add a second son! Destroy previous son first."); 00119 son = son_; 00120 } 00121 // ------------------------------------------------- 00122 // void XML_Tag::remove_son(XML_Node *son_) 00123 // ------------------------------------------------- 00124 void XML_Tag::remove_son(XML_Node *son_) { 00125 if (son != son_) throw string("Tried to remove wrong son!"); 00126 son = 0; 00127 } 00128 00129 // ----------------------------------------- 00130 // void XML_Tag::open(ostream& out) 00131 // ----------------------------------------- 00132 void XML_Tag::open(ostream& out) { 00133 if (father && !father->Opened()) father->open(out); 00134 out << "\n"; to_indent(out, Indent()); 00135 out << "<" << name; 00136 if (attribute) attribute->print(out); 00137 out << ">"; 00138 opened = true; 00139 } 00140 // ------------------------------------------- 00141 // void XML_Tag::close(ostream& out) 00142 // ------------------------------------------- 00143 void XML_Tag::close(ostream& out) { 00144 if (!opened) { 00145 if (father && !father->Opened()) father->open(out); 00146 out << "\n"; to_indent(out, Indent()); 00147 out << "<" << name; 00148 if (attribute) attribute->print(out); 00149 out << "/>"; 00150 } 00151 else { 00152 out << "\n"; to_indent(out, Indent()); 00153 out << "</" << name << ">"; 00154 } 00155 } 00156 00157 // ******************************************************************************** 00158 00159 // ------------------------------ 00160 // XML_Text::~XML_Text() 00161 // ------------------------------ 00162 XML_Text::~XML_Text() { 00163 ostream& out = theDocument->Out(); 00164 close(out); 00165 } 00166 // --------------------------------------------------- 00167 // void XML_Text::add_son(XML_Node */*son_*/) 00168 // --------------------------------------------------- 00169 void XML_Text::add_son(XML_Node */*son_*/) { 00170 throw string("Can't add son to XML_Text-Node"); 00171 } 00172 // ------------------------------------------------------ 00173 // void XML_Text::remove_son(XML_Node */*son_*/) 00174 // ------------------------------------------------------ 00175 void XML_Text::remove_son(XML_Node */*son_*/) { 00176 throw string("Can't remove son from XML_Text-Node"); 00177 } 00178 // ---------------------------------------------- 00179 // void XML_Text::open(ostream& /*out*/) 00180 // ---------------------------------------------- 00181 void XML_Text::open(ostream& /*out*/) { 00182 } 00183 // ------------------------------------------- 00184 // void XML_Text::close(ostream& out) 00185 // ------------------------------------------- 00186 void XML_Text::close(ostream& out) { 00187 if (father && !father->Opened()) father->open(out); 00188 out << "\n"; to_indent(out, Indent()); 00189 out << content; 00190 } 00191 00192 // ******************************************************************************** 00193 00194 // ------------------------------------------------------------------------------------------- 00195 // XML_Document::XML_Document(const string& name_, const string& dtd_, ostream& out_) 00196 // ------------------------------------------------------------------------------------------- 00197 XML_Document::XML_Document(const string& name_, const string& dtd_, ostream& out_) 00198 : dtd(dtd_), root(0), out(out_) 00199 { 00200 assert(!theDocument); // there may be only one at a time 00201 theDocument = this; 00202 latest_son = 0; 00203 root = new XML_Tag(name_); 00204 assert(latest_son == root); 00205 00206 out << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"; 00207 out << "<!DOCTYPE " << name_ << " SYSTEM '" << dtd << "'>\n"; 00208 } 00209 00210 // -------------------------------------- 00211 // XML_Document::~XML_Document() 00212 // -------------------------------------- 00213 XML_Document::~XML_Document() { 00214 delete root; 00215 assert(theDocument == this); 00216 theDocument = 0; 00217 } 00218
Contact me in case of errors or questions. This documentation is powered by ![]() |
(C) 2000-2002 ![]() |