XMLElement.h++
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __ccxx_XMLElement_hxx
00024 #define __ccxx_XMLElement_hxx
00025
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/String.h++>
00028 #include <commonc++/InvalidArgumentException.h++>
00029
00030 #include <vector>
00031
00032 namespace ccxx {
00033
00034 class XMLElement;
00035
00051 class COMMONCPPXML_API XMLElement
00052 {
00053 friend class XMLDocument;
00054 friend class XMLElementConstIterator;
00055 friend class XMLElementIterator;
00056
00057 public:
00058
00060 inline String getName() const
00061 { return(_name); }
00062
00070 void setAttribute(const String &name, const String &value)
00071 throw(InvalidArgumentException);
00072
00080 void setAttribute(const String &name, int value)
00081 throw(InvalidArgumentException);
00082
00090 void setAttribute(const String &name, const int64_t &value)
00091 throw(InvalidArgumentException);
00092
00100 void setAttribute(const String &name, const double &value)
00101 throw(InvalidArgumentException);
00102
00110 void setAttribute(const String &name, bool value)
00111 throw(InvalidArgumentException);
00112
00118 XMLElement & getParent() throw();
00119
00125 const XMLElement & getParent() const throw();
00126
00137 bool getAttribute(const String &name, String &value,
00138 const String &defaultValue = "") const
00139 throw(InvalidArgumentException);
00140
00151 bool getAttribute(const String &name, int &value, int defaultValue = 0)
00152 const throw(InvalidArgumentException);
00153
00164 bool getAttribute(const String &name, int64_t &value,
00165 const int64_t &defaultValue = 0) const
00166 throw(InvalidArgumentException);
00167
00178 bool getAttribute(const String &name, bool &value,
00179 bool defaultValue = false) const
00180 throw(InvalidArgumentException);
00181
00192 bool getAttribute(const String &name, double &value,
00193 const double &defaultValue = 0.0) const
00194 throw(InvalidArgumentException);
00195
00200 void getAttributeNames(StringVec &names) const;
00201
00203 void removeAttributes();
00204
00210 bool hasAttributes() const;
00211
00218 inline bool hasContent() const
00219 { return(_content.getLength() > 0); }
00220
00225 inline void setContent(const String &content)
00226 { _content = content; }
00227
00232 inline const String getContent() const
00233 { return(_content); }
00234
00240 bool hasChildren() const;
00241
00246 uint_t getChildCount() const;
00247
00254 XMLElement & addChild(const String &name) throw(InvalidArgumentException);
00255
00263 XMLElement & getChild(const String &name = String::null);
00264
00272 const XMLElement & getChild(const String &name = String::null) const;
00273
00280 XMLElement & getChild(uint_t index);
00281
00288 const XMLElement & getChild(uint_t index) const;
00289
00298 bool hasChild(const String &name) const throw(InvalidArgumentException);
00299
00307 XMLElement & operator[](const String &name) throw(InvalidArgumentException);
00308
00316 XMLElement & operator[](const char *name) throw(InvalidArgumentException);
00317
00325 inline const XMLElement & operator[](const String &name) const
00326 throw(InvalidArgumentException)
00327 { return(_getChild(name)); }
00328
00336 inline const XMLElement & operator[](const char *name) const
00337 throw(InvalidArgumentException)
00338 { return(_getChild(name)); }
00339
00347 XMLElement & operator[](int index) throw(OutOfBoundsException);
00348
00356 const XMLElement & operator[](int index) const throw(OutOfBoundsException);
00357
00359 void removeChildren();
00360
00366 void removeChildren(const String &name) throw(InvalidArgumentException);
00367
00371 XMLElement & operator=(bool val);
00372
00376 XMLElement & operator=(long val);
00377
00381 XMLElement & operator=(unsigned long val);
00382
00386 XMLElement & operator=(const int64_t &val);
00387
00391 XMLElement & operator=(const uint64_t &val);
00392
00396 XMLElement & operator=(float val);
00397
00401 XMLElement & operator=(const double &val);
00402
00404 XMLElement & operator=(const String &val);
00405
00407 bool toBool() const;
00408
00410 int toInt() const;
00411
00413 uint_t toUInt() const;
00414
00416 int64_t toInt64() const;
00417
00419 uint64_t toUInt64() const;
00420
00422 float toFloat() const;
00423
00425 double toDouble() const;
00426
00429 inline String toString() const
00430 { return(getContent()); }
00431
00433 inline bool isNull() const
00434 { return(_name.isNull()); }
00435
00437 inline operator const void *() const throw()
00438 { return(isNull() ? NULL : this); }
00439
00441 inline bool operator!() const
00442 { return(isNull()); }
00443
00445 void write(std::ostream &stream, int depth, uint_t tabWidth) const;
00446
00448 static void validateName(const String &name)
00449 throw(InvalidArgumentException);
00450
00452 static XMLElement null;
00453
00454 private:
00455
00456 const XMLElement & _getChild(const String &name = String::null) const;
00457 const XMLElement & _getChild(uint_t index) const;
00458
00459 String _name;
00460 String _content;
00461
00462 class XMLAttributeMap;
00463
00464 XMLAttributeMap *_attrs;
00465 XMLElement *_firstChild;
00466 XMLElement *_lastChild;
00467 XMLElement *_next;
00468 XMLElement *_parent;
00469
00470 XMLElement(const String &name, XMLElement *parent = NULL);
00471 ~XMLElement();
00472
00473 CCXX_COPY_DECLS(XMLElement);
00474 };
00475
00476 };
00477
00478 #endif // __ccxx_XMLElement_hxx
00479
00480