XmlElement.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 $id$
00004 
00005 */
00006 
00007 #include "Error.h"
00008 
00009 #include "XmlElement.h"
00010 
00011 // Public methods.
00012 
00013 XmlElement::XmlElement(const String &tag,
00014                        XmlAttribute *attr, const String &innerData,
00015                        XmlElement* nextChild, XmlElement *nextSibling) : Support(),
00016   _tag(tag),
00017   _document(NULL),
00018   _parent(NULL),
00019   _nextSibling(nextSibling),
00020   _innerData(innerData),
00021   _attributes(),
00022   _children()
00023 {
00024   CHECK_PARENT;
00025 
00026   if (attr) {
00027     XmlAttribute *a = attr;
00028 
00029     while (a) {
00030       _attributes[a->name()] = a->value();
00031       a = a->next();
00032     }
00033 
00034     delete attr;
00035   }
00036 
00037   int res;
00038 
00039   if (nextChild)
00040     if (FAILED(res = appendChild(nextChild))) {
00041       failure(ERROR_BACKTRACE(res));
00042       return;
00043     }
00044 }
00045 
00046 XmlElement::~XmlElement()
00047 {
00048 }
00049 
00050 String XmlElement::tag() const
00051 {
00052   return _tag;
00053 }
00054 
00055 String XmlElement::id() const
00056 {
00057   XmlAttributeCollection::const_iterator f = _attributes.find(XML_ATTR_ID);
00058   if (f == _attributes.end()) {
00059     WARNING(MSG_XML_ELEMENT_ATTR_NOT_FOUND, XML_ATTR_ID);
00060     return "";
00061   }
00062   return f->second;
00063 }
00064 
00065 int XmlElement::setId(const String &id)
00066 {
00067   _attributes[XML_ATTR_ID] = id;
00068   return OK;
00069 }
00070 
00071 String XmlElement::name() const
00072 {
00073   XmlAttributeCollection::const_iterator f = _attributes.find(XML_ATTR_NAME);
00074   if (f == _attributes.end()) {
00075     WARNING(MSG_XML_ELEMENT_ATTR_NOT_FOUND, XML_ATTR_NAME);
00076     return "";
00077   }
00078   return f->second;
00079 }
00080 
00081 int XmlElement::setName(const String &name)
00082 {
00083   _attributes[XML_ATTR_NAME] = name;
00084   return OK;
00085 }
00086 
00087 int XmlElement::attribute(const String &name, String &value) const
00088 {
00089   XmlAttributeCollection::const_iterator f = _attributes.find(name);
00090 
00091   if (f == _attributes.end())
00092     return WARNING(MSG_XML_ELEMENT_ATTR_NOT_FOUND, CSTRING(name));
00093 
00094   value = f->second;
00095 
00096   return OK;
00097 }
00098 
00099 XmlAttributeCollection XmlElement::atributes() const
00100 {
00101   return _attributes;
00102 }
00103 
00104 int XmlElement::setAttribute(const String &name, const String &value)
00105 {
00106   _attributes[name] = value;
00107   return OK;
00108 }
00109 
00110 bool XmlElement::hasChildren() const
00111 {
00112   return _children.size() > 0;
00113 }
00114 
00115 XmlElementCollection *XmlElement::children() const
00116 {
00117   return (XmlElementCollection *)(&_children);
00118 }
00119 
00120 int XmlElement::appendChild(XmlElement *element)
00121 {
00122   if (!element)
00123     return ERROR(MSG_NULL_POINTER);
00124 
00125   int res;
00126 
00127   _children.push_back(element);
00128 
00129   if (FAILED(res = element->setParent(this)))
00130     return ERROR_BACKTRACE(res);
00131 
00132   return OK;
00133 }
00134 
00135 int XmlElement::detachChild(XmlElement *element)
00136 {
00137   if (!element)
00138     return ERROR(MSG_NULL_POINTER);
00139 
00140   XmlElementCollection::iterator i = _children.begin();
00141   XmlElementCollection::iterator e = _children.end();
00142   int res;
00143 
00144   while (i != e) {
00145     if (*i == element) {
00146       _children.erase(i);
00147       break;
00148     }
00149     ++i;
00150   }
00151   if (i == e)
00152     return WARNING(MSG_XML_ELEMENT_NOT_FOUND);
00153 
00154   if (FAILED(res = element->setParent(NULL)))
00155     return ERROR_BACKTRACE(res);
00156 
00157   return OK;
00158 }
00159 
00160 int XmlElement::removeChild(XmlElement *element)
00161 {
00162   int res;
00163 
00164   if (FAILED(res = detachChild(element)))
00165     return ERROR_BACKTRACE(res);
00166   delete element;
00167   return OK;
00168 }
00169 
00170 int XmlElement::removeChildren()
00171 {
00172   XmlElementCollection::iterator i = _children.begin();
00173   XmlElementCollection::iterator e = _children.end();
00174 
00175   while (i != e) {
00176     delete *i;
00177     ++i;
00178   }
00179   _children.clear();
00180   return OK;
00181 }
00182 
00183 XmlDocument *XmlElement::document() const
00184 {
00185   return _document;
00186 }
00187 
00188 XmlElement *XmlElement::parent() const
00189 {
00190   return _parent;
00191 }
00192 
00193 String XmlElement::innerData() const
00194 {
00195   return _innerData;
00196 }
00197 
00198 int XmlElement::setInnerData(const String &data)
00199 {
00200   _innerData = data;
00201   _innerData.trim();
00202   return OK;
00203 }
00204 
00205 int XmlElement::appendInnerData(const String &data)
00206 {
00207   _innerData += data;
00208   _innerData.trim();
00209   return OK;
00210 }
00211 
00212 int XmlElement::toString(String &str) const
00213 {
00214   unsigned int indent = 0;
00215   int res;
00216 
00217   str.clear();
00218   if (FAILED(res = _toString(str, indent))) {
00219     str.clear();
00220     return ERROR_BACKTRACE(res);
00221   }
00222   return OK;
00223 }
00224 
00225 // Protected methods.
00226 
00227 int XmlElement::close()
00228 {
00229   return OK;
00230 }
00231 
00232 // Private methods.
00233 
00234 int XmlElement::setDocument(const XmlDocument *doc)
00235 {
00236   _document = (XmlDocument *)doc;
00237   return OK;
00238 }
00239 
00240 int XmlElement::setParent(const XmlElement *parent)
00241 {
00242   int res;
00243 
00244   if (parent && !_parent && _nextSibling) {
00245     if (FAILED(res = _nextSibling->setParent(parent)))
00246       return ERROR_BACKTRACE(res);
00247     _nextSibling = NULL;
00248   }
00249   _parent = (XmlElement *)parent;
00250   return OK;
00251 }
00252 
00253 int XmlElement::_toString(String &str, unsigned int &indent) const
00254 {
00255   String indentation(indent, ' ');
00256 
00257   str += indentation + "<" + _tag;
00258 
00259   if (!_attributes.empty()) {
00260     XmlAttributeCollection::const_iterator i = _attributes.begin();
00261     XmlAttributeCollection::const_iterator e = _attributes.end();
00262 
00263     while (i != e) {
00264       str += " " + i->first + "=\"" + i->second + "\"";
00265       ++i;
00266     }
00267     str += " ";
00268   }
00269   str += ">";
00270   str += String::eol();
00271 
00272   if (hasChildren()) {
00273     XmlElementCollection::const_iterator i = _children.begin();
00274     XmlElementCollection::const_iterator e = _children.end();
00275     int res;
00276 
00277     indent += XML_INDENT;
00278     while (i != e) {
00279       if (FAILED(res = (*i)->_toString(str, indent)))
00280         return ERROR_BACKTRACE(res);
00281       ++i;
00282     }
00283     indent -= XML_INDENT;
00284   } else
00285       str += indentation + String(XML_INDENT, ' ') + _innerData + String::eol();
00286 
00287   str += indentation + "</" + _tag + ">" + String::eol();
00288 
00289   return OK;
00290 }

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1