00001
00002 #ifndef XMLTRAVERSAL
00003 #define XMLTRAVERSAL
00004
00005 #include <QHostAddress>
00006 #include "trace.hpp"
00007 #include "base.hpp"
00008
00072 class UTILITYSHARED_EXPORT XMLTraversal {
00073 QStack<QDomNode> savePoints;
00074 public:
00075
00076 class UTILITYSHARED_EXPORT iterator {
00077 public:
00078 iterator() ;
00079 iterator(const QDomNode&) ;
00080 iterator(const iterator&) ;
00081 iterator operator ++() ;
00082 iterator operator ++(int) ;
00083 bool operator==(const iterator&) const ;
00084 bool operator!=(const iterator&) const ;
00085 QDomElement operator*() const ;
00086 QDomElement operator->() const ;
00087 private:
00088 iterator(const QDomNode&, const QDomNode&) ;
00089 QDomNode elt_ ;
00090 QDomNode cur_ ;
00091 } ;
00092
00093 XMLTraversal() ;
00094 XMLTraversal(QString ) ;
00095 XMLTraversal(QDomDocument) ;
00096 XMLTraversal(QByteArray );
00097 XMLTraversal(XMLTraversal &x) { setDocument(x.document());}
00098
00099
00100
00101
00106 bool isValid() const ;
00107
00112 bool exists(const QString& path) const ;
00113
00128 QString pwd() const ;
00129
00154 QDomNode currentNode(const QString& path=QString()) const ;
00155 QDomElement currentElement()
00156 {
00157 if(cur_path_node_.isElement()) return cur_path_node_.toElement();
00158 return QDomElement();
00159 }
00160
00168 QString attribute(const QString& name) const ;
00169
00177 bool hasAttribute(const QString& attrname) const ;
00178
00182 const QDomDocument& document() const;
00183
00187 QString dump() const ;
00188
00210 QStringList ls() const ;
00211
00229 iterator begin() ;
00230
00236 iterator end() ;
00237
00241 unsigned int size() const ;
00242
00243
00244
00245
00246
00252 virtual bool setDocument(QString doc) ;
00253 virtual bool setDocument(QByteArray doc) ;
00254
00260 virtual bool setDocument(QDomDocument doc) ;
00261
00280 bool cd(const QString& path = QString()) ;
00294 bool cd(const QDomElement&) ;
00295
00296 bool cdToRoot() { return cd(QString("/%1").arg(document().documentElement().tagName()));}
00315 QDomElement mkdir(const QString& path) ;
00316
00331 bool rmdir(const QString& path) ;
00332
00354 bool mv(const QString& oldpath, const QString& newpath) ;
00355
00370 bool cp(const QString& source, const QString& target) ;
00371
00379 bool setAttribute(const QString& path,
00380 const QString& attrname,
00381 const QString& attrvalue) ;
00382
00389 bool setAttribute(const QString& attrname, const QString& attrvalue) ;
00390 void push() { savePoints.push(cur_path_node_); }
00391 void pop() { if(savePoints.count() > 0) cur_path_node_ = savePoints.pop();}
00392 void clearStack() { savePoints.clear(); }
00393 void setText(QString s);
00394 QString text(QString path)
00395 {
00396 QString res("");
00397 push();
00398 if(cd(path)) res = currentElement().text();
00399 pop();
00400 return res;
00401 }
00402 QDomElement mkdirAndCd(QString s)
00403 {
00404 QDomElement e = mkdir(s);
00405 cd(s);
00406 return e;
00407 };
00408
00409 QDomElement mkdirSetText(QString s, QString text)
00410 {
00411 QDomElement e = mkdir(s);
00412 cd(e);
00413 setText(text);
00414 return e;
00415 };
00416
00417 bool loadFromFile(QString s)
00418 {
00419 QFile f(s);
00420 if(f.open(QIODevice::ReadOnly))
00421 {
00422 doc_.clear();
00423 markUpdated();
00424 return doc_.setContent(&f);
00425 };
00426 return false;
00427 }
00428
00429
00430
00431 void XmlSet ( QString s, QVariant v)
00432 {
00433 push();
00434 mkdir(s);
00435 cd(s);
00436 VariantToElement(v,currentElement());
00437 pop();
00438 };
00439
00440 void XmlGet ( QString s, QVariant &b)
00441 {
00442 push();
00443 if(cd(s))
00444 {
00445 b = (elementToVariant(currentElement()));
00446 }
00447 pop();
00448 };
00449
00450
00451 void XmlSet ( QString s, QHostAddress a)
00452 {
00453 push();
00454 mkdir(s);
00455 cd(s);
00456 VariantToElement(QVariant(a.toString()),currentElement());
00457 pop();
00458 };
00459
00460 void XmlGet ( QString s, QHostAddress &b)
00461 {
00462 push();
00463 if(cd(s))
00464 {
00465 QVariant v = (elementToVariant(currentElement()));
00466 b = QHostAddress(v.toString());
00467 }
00468 pop();
00469 };
00470
00471
00472
00473
00474 QVariant GetVar ( QString s)
00475 {
00476 QVariant b;
00477 XmlGet(s,b);
00478 return b;
00479 };
00480
00481
00482
00483
00484
00485 #ifdef QT_GUI_LIB
00486
00487 void XmlGet(QLineEdit *item){ item->setText(GetVar(item->objectName()).toString());}
00488 void XmlGet(QLabel *item){ item->setText(GetVar(item->objectName()).toString());}
00489 void XmlSet(QLineEdit *item) {XmlSet(item->objectName(),QVariant(item->text()));}
00490 void XmlSet(QLabel *item) {XmlSet(item->objectName(),QVariant(item->text()));}
00491 void XmlGet(QDoubleSpinBox *item){ item->setValue(GetVar(item->objectName()).toDouble());}
00492 void XmlSet(QDoubleSpinBox *item) {XmlSet(item->objectName(),QVariant(item->value()));}
00493 void XmlGet(QSpinBox *item){ item->setValue(GetVar(item->objectName()).toInt());}
00494 void XmlSet(QSpinBox *item) {XmlSet(item->objectName(),QVariant(item->value()));}
00495 void XmlGet(QComboBox *item){ item->setCurrentIndex(GetVar(item->objectName()).toInt());}
00496 void XmlSet(QComboBox *item) {XmlSet(item->objectName(),QVariant(item->currentIndex()));}
00497 void XmlGetText(QComboBox *item)
00498 {
00499 setComboText(item,GetVar(item->objectName()).toString());
00500 }
00501 void XmlSetText(QComboBox *item) {XmlSet(item->objectName(),QVariant(item->currentText()));}
00502
00503
00504
00505 void XmlGet(QAbstractButton *item){ item->setChecked(GetVar(item->objectName()).toBool());}
00506 void XmlSet(QAbstractButton *item) {XmlSet(item->objectName(),QVariant(item->isChecked()));}
00507
00508
00509 void XmlGet(QDateTimeEdit *item){ item->setDateTime(GetVar(item->objectName()).toDateTime());}
00510 void XmlSet(QDateTimeEdit *item) {XmlSet(item->objectName(),QVariant(item->dateTime()));}
00511
00512
00513 void XmlGet(QListWidget *item, bool fGetChecked = false)
00514 {
00515 QStringList l = GetVar(item->objectName()).toStringList();
00516 if(fGetChecked)
00517 {
00518
00519 setCheckList(item, l);
00520 }
00521 else
00522 {
00523 item->clear();
00524 item->addItems(l);
00525 }
00526 }
00527 void XmlSet(QListWidget *item, bool fSetChecked = false)
00528 {
00529 QStringList l;
00530 if(fSetChecked)
00531 {
00532 l = getCheckList(item);
00533 }
00534 else
00535 {
00536 for(int i = 0; i < item->count(); i++) l << item->item(i)->text();
00537 }
00538 XmlSet(item->objectName(),QVariant(l));
00539 }
00540 void XmlGet(QPlainTextEdit *item) { item->setPlainText(GetVar(item->objectName()).toString()); }
00541 void XmlSet(QPlainTextEdit *item) {XmlSet(item->objectName(),QVariant(item->toPlainText()));}
00542 #endif
00543
00544 void initialise(QString s = "Document")
00545 {
00546 doc_.clear();
00547 clearStack();
00548 cur_path_node_ = doc_;
00549 doc_.appendChild (doc_.createElement(s));
00550 markUpdated();
00551 }
00552
00553 QVariant elementToVariant(QDomElement e);
00554 void VariantToElement(QVariant v, QDomElement e);
00555
00556 QDateTime getUpdateTime() const { return updateTime;}
00557 void markUpdated() { updateTime = QDateTime::currentDateTime();}
00558 private:
00559 QDomNode cd_to_node(const QString& path) ;
00560 QDomDocument doc_ ;
00561 QDomNode cur_path_node_ ;
00562 QDateTime updateTime;
00563
00564 } ;
00565
00566
00567
00568 template <class T>
00569 XMLTraversal & XmlSet (XMLTraversal &x, QString s, T b)
00570 {
00571 QVariant v;
00572 v.setValue<T>(b);
00573 x.XmlSet(s,v);
00574 return x;
00575 };
00576 template <class T>
00577 XMLTraversal & XmlGet (XMLTraversal &x, QString s, T &b)
00578 {
00579 QVariant v;
00580 x.XmlGet(s,v);
00581 b = qVariantValue<T>(v);
00582 return x;
00583 };
00584
00585
00586
00587 #define XMLSET(x,v) { XmlSet(x,#v,v);}
00588
00589 #define XMLGET(x,v) { XmlGet(x,#v,v);}
00590
00591 inline XMLTraversal & XmlSet (XMLTraversal &x, QString s, QVariant v)
00592 {
00593 x.XmlSet(s,v);
00594 return x;
00595 };
00596
00597 inline XMLTraversal & XmlGet (XMLTraversal &x, QString s, QVariant &b)
00598 {
00599 x.XmlGet(s,b);
00600 return x;
00601 };
00602
00603
00604 #define XMLSETVAR(x,v) { XmlSetVar(x,#v,v);}
00605
00606 #define XMLGETVAR(x,v) { XmlGetVar(x,#v,v);}
00607
00608
00609
00610
00611 inline const QDomDocument & XMLTraversal::document() const {
00612 return doc_ ;
00613 }
00614
00615 inline XMLTraversal::iterator XMLTraversal::begin() {
00616 return iterator(currentNode()) ;
00617 }
00618
00619 inline XMLTraversal::iterator XMLTraversal::end() {
00620 return iterator() ;
00621 }
00622
00623 inline unsigned int XMLTraversal::size() const {
00624 int i = 0 ;
00625 QDomNode node = cur_path_node_.firstChild() ;
00626 while(!node.isNull()) {
00627 if (node.isElement())
00628 i++ ;
00629 node = node.nextSibling() ;
00630 }
00631 return i ;
00632 }
00633
00634 inline bool XMLTraversal::isValid() const {
00635 return (!doc_.isNull()) ;
00636 }
00637
00638 inline QString XMLTraversal::attribute(const QString& name) const {
00639 if (!cur_path_node_.isElement())
00640 return QString::null ;
00641 return (const_cast<QDomNode&>(cur_path_node_)).
00642 toElement().attribute(name) ;
00643 }
00644
00645 inline bool XMLTraversal::hasAttribute(const QString& attrname) const {
00646 if (!cur_path_node_.isElement())
00647 return false ;
00648 return (const_cast<QDomNode&>(cur_path_node_)).
00649 toElement().hasAttribute(attrname) ;
00650 }
00651
00652 inline bool XMLTraversal::exists(const QString& d) const {
00653 XMLTraversal* This = const_cast<XMLTraversal*>(this) ;
00654 QDomNode cur = cur_path_node_ ;
00655 QDomNode node = This->cd_to_node(d) ;
00656 This->cur_path_node_ = cur ;
00657 return !node.isNull() ;
00658 }
00659
00660 inline QString XMLTraversal::dump() const {
00661 return doc_.toString() ;
00662 }
00663
00664
00665
00666 inline bool XMLTraversal::iterator::operator!=(const iterator& i) const {
00667 return !(operator==(i)) ;
00668 }
00669
00670 inline QDomElement XMLTraversal::iterator::operator*() const {
00671 return const_cast<QDomNode&>(cur_).toElement() ;
00672 }
00673
00674 inline QDomElement XMLTraversal::iterator::operator->() const {
00675 return const_cast<QDomNode&>(cur_).toElement() ;
00676 }
00677
00678 inline bool XMLTraversal::iterator::operator==(const iterator& i) const {
00679 if (i.cur_.isNull() && cur_.isNull())
00680 return true ;
00681
00682 if (i.elt_ == elt_ && i.cur_ == cur_)
00683 return true ;
00684 return false ;
00685 }
00686
00687
00688
00689 #endif
00690