OSDN Git Service

FavoriteThreads::readFromXML (use DOM)
authorikemo <ikemo@56b19765-1e22-0410-a548-a0f45d66c51a>
Mon, 23 Feb 2004 15:34:18 +0000 (15:34 +0000)
committerikemo <ikemo@56b19765-1e22-0410-a548-a0f45d66c51a>
Mon, 23 Feb 2004 15:34:18 +0000 (15:34 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/kita/kita/trunk@882 56b19765-1e22-0410-a548-a0f45d66c51a

kita/src/libkita/favoritethreads.cpp
kita/src/libkita/favoritethreads.h
kita/src/libkita/tests/favoritethreadstest.cpp

index 9f79560..2d40eaf 100644 (file)
 #include <kdebug.h>
 
 #include <qstylesheet.h>
+#include <qdom.h>
 
 #include "threadinfo.h"
+#include "board.h"
 
 FavoriteThreads* FavoriteThreads::instance = 0;
 
@@ -53,6 +55,76 @@ bool FavoriteThreads::contains( const QString& datURL ) const
     }
 }
 
+bool FavoriteThreads::readFromXML( const QString& xml )
+{
+    FavoriteThreads * instance = FavoriteThreads::getInstance();
+    instance->m_threadDict.clear();
+
+    QDomDocument document;
+    if ( ! document.setContent( xml, true ) ) {
+        return false;
+    }
+
+    QDomElement root = document.documentElement();
+
+    QDomNode node = root.firstChild();
+    while ( !node.isNull() ) {
+        if ( node.isElement() &&
+                node.nodeName() == QString( "thread" ) &&
+                node.namespaceURI() == QString( "http://kita.sourceforge.jp/ns/thread" ) ) {
+            processThreadNode( node );
+        }
+        node = node.nextSibling();
+    }
+    return true;
+}
+
+void FavoriteThreads::processThreadNode( QDomNode& node )
+{
+    QDomNode datURLNode = node.namedItem( "daturl" );
+    QDomNode nameNode = node.namedItem( "name" );
+    QDomNode resNumNode = node.namedItem( "resnum" );
+    QDomNode boardNode = node.namedItem( "board" );
+
+    if ( !datURLNode.isElement() || !nameNode.isElement() ||
+            !resNumNode.isElement() || !boardNode.isElement() ) {
+        return ;
+    }
+    if ( boardNode.namespaceURI() != QString( "http://kita.sourceforge.jp/ns/board" ) ) {
+        return ;
+    }
+    if ( !processBoardNode( boardNode ) ) {
+        return ;
+    }
+    QString datURLText = datURLNode.toElement().text();
+    QString nameText = nameNode.toElement().text();
+    int resNum = resNumNode.toElement().text().toInt();
+
+    Kita::Thread::setName( datURLText, nameText );
+    KitaThreadInfo::setResNum( datURLText, resNum );
+
+    FavoriteThreads::getInstance() ->insert( Kita::Thread::getByURL( datURLText ) );
+}
+
+bool FavoriteThreads::processBoardNode( QDomNode& node )
+{
+    QDomNode urlNode = node.namedItem( "url" );
+    QDomNode nameNode = node.namedItem( "name" );
+    if ( ! urlNode.isElement() || ! nameNode.isElement() ) {
+        return false;
+    }
+
+    QString urlText = urlNode.toElement().text();
+    QString nameText = nameNode.toElement().text();
+
+    KURL url = KURL( urlText );
+    if ( url.isValid() ) {
+        Kita::Board::setName( urlText, nameText );
+        return true;
+    }
+    return false;
+}
+
 FavoriteThreads* FavoriteThreads::fromXml( const QString& xml )
 {
     FavoriteThreads * instance = FavoriteThreads::getInstance();
index c74410a..c462049 100644 (file)
@@ -20,6 +20,8 @@
 
 #include "thread.h"
 
+class QDomNode;
+
 /**
 @author Hideki Ikemoto
 */
@@ -37,8 +39,9 @@ public:
   const QDict<Kita::Thread>& threads() const { return m_threadDict; };
   static FavoriteThreads* fromXml( const QString& xml );
   const QString toXml() const;
-
-  static bool test();
+  static bool readFromXML( const QString& xml );
+  static void processThreadNode( QDomNode& node );
+  static bool processBoardNode( QDomNode& node );
 };
 
 class FavoritesXmlParser : public QXmlDefaultHandler
index 179d07b..e432fc1 100644 (file)
@@ -37,7 +37,7 @@ void FavoriteThreadsTest::setUp()
             "</thread>\n"
             "</favorites>\n";
 
-    FavoriteThreads::fromXml( m_xml );
+    FavoriteThreads::readFromXML( m_xml );
 }
 
 void FavoriteThreadsTest::tearDown()