00001 /*************************************************************************** 00002 * Copyright (C) 2004 by ljp * 00003 * lpotter@trolltech.com * 00004 * * 00005 * This program may be distributed under the terms of the Q Public * 00006 * License as defined by Trolltech AS of Norway and appearing in the * 00007 * file LICENSE.QPL included in the packaging of this file. * 00008 * * 00009 * This program is distributed in the hope that it will be useful, * 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 00012 ***************************************************************************/ 00013 #include "rssparser.h" 00014 00015 #include <qstring.h> 00016 #include <qmessagebox.h> 00017 #include <qlist.h> 00018 00019 RssParser::RssParser() 00020 { 00021 isItem = false; 00022 } 00023 00024 RssParser::~RssParser() 00025 { 00026 int size = channel->rssItems.size(); 00027 for (int i = 0; i < size; i ++) { 00028 delete channel->rssItems.at(i); 00029 } 00030 delete channel; 00031 } 00032 00033 bool RssParser::startElement( const QString &, const QString & /*localName*/,const QString & qName, const QXmlAttributes &atts) 00034 { 00035 if( qName == "rss") { 00036 channel = new rssChannel(); 00037 channel->rssItems.resize( 0); 00038 return true; 00039 } 00040 // qWarning(qName + " %d",atts.length()); 00041 if(qName == "item") { 00042 isItem = true; 00043 tag = NewItemTag; 00044 Item = new rssItem(); 00045 } 00046 00047 if(qName == "image") { 00048 tag = NewItemTag; 00049 image = new rssImage(); 00050 } 00051 00052 if(qName == "title") { 00053 tag = TitleTag; 00054 } 00055 00056 if (qName == "description") { 00057 tag = DescriptionTag; 00058 } 00059 00060 if( qName == "link") { 00061 tag = LinkTag; 00062 } 00063 00064 if( qName == "pubDate") { 00065 tag = pubDateTag; 00066 } 00067 00068 // if( qName == "enclosure") { 00069 // tag = EnclosureTag; 00070 // } 00071 00072 if(atts.length() > 0/* && tag == EnclosureTag*/) { 00073 // qWarning(qName +" attributes %d", atts.length()); 00074 // Item->attributes << qName; 00075 // for(int i=0; i < atts.length(); i++) { 00076 // Item->attributes << atts.qName(i) << atts.value(atts.qName(i)); 00077 // } 00078 00079 QStringList *sList; 00080 sList = new QStringList(); 00081 sList->append(qName); 00082 for(int i=0; i < atts.length(); i++) { 00083 sList->append( atts.qName(i)); 00084 sList->append( atts.value(atts.qName(i))); 00085 } 00086 if(isItem) 00087 Item->attributes.append( sList); 00088 else 00089 channel->attributes.append( sList); 00090 } 00091 00092 return true; 00093 } 00094 00095 bool RssParser::endElement( const QString &, const QString &, const QString & qName ) 00096 { 00097 tag = NoneTag; 00098 if(qName == "item") { 00099 isItem = false; 00100 int size = channel->rssItems.size(); 00101 channel->rssItems.resize( size + 1); 00102 channel->rssItems.insert( channel->rssItems.size() - 1, Item); 00103 } 00104 if(qName == "channel") { 00105 // isItem = false; 00106 // int size = channel->rssItems.size(); 00107 // channel->rssItems.resize( size + 1); 00108 // channel->rssItems.insert( channel->rssItems.size() - 1, Item); 00109 } 00110 return true; 00111 } 00112 00113 bool RssParser::characters( const QString & ch ) 00114 { 00115 if(!ch.isEmpty()) { 00116 if(isItem) { 00117 // qWarning("ch "+ch); 00118 switch(tag) { 00119 case NewItemTag: 00120 break; 00121 case TitleTag: 00122 Item->title = ch; 00123 break; 00124 case DescriptionTag: 00125 Item->description = ch; 00126 break; 00127 case LinkTag: 00128 Item->link = ch; 00129 break; 00130 case pubDateTag: 00131 Item->pubdate = ch; 00132 break; 00133 case NoneTag: 00134 break; 00135 }; 00136 } else { //channel 00137 switch(tag) { 00138 case TitleTag: 00139 channel->title = ch; 00140 break; 00141 case DescriptionTag: 00142 channel->description = ch; 00143 break; 00144 case LinkTag: 00145 channel->link = ch; 00146 break; 00147 case pubDateTag: 00148 channel->pubdate = ch; 00149 break; 00150 case NoneTag: 00151 case NewItemTag: 00152 break; 00153 }; 00154 } 00155 } 00156 return true; 00157 } 00158 00159 bool RssParser::warning(const QXmlParseException &e) 00160 { 00161 errorMessage = e.message(); 00162 // QMessageBox::message("Warning",tr("<p>Sorry, could not find the requested document.</p>")); 00163 qWarning("Warning " + errorMessage); 00164 return true; 00165 } 00166 00167 bool RssParser::error(const QXmlParseException &e) 00168 { 00169 errorMessage = e.message(); 00170 // QMessageBox::message("Error", "<p>" + errorMessage + "</p>"); 00171 qWarning("Error: " + errorMessage); 00172 return true; 00173 } 00174 00175 bool RssParser::fatalError(const QXmlParseException &e) 00176 { 00177 errorMessage = e.message(); 00178 // errorMessage += " line: " + e.lineNumber(); 00179 // errorMessage += " col: " + e.columnNumber(); 00180 qWarning("Fatal Error: "+ errorMessage); 00181 qWarning("line %d, col %d\n", e.lineNumber(), e.columnNumber()); 00182 // QMessageBox::message("Fatal Error", errorMessage ); 00183 return false; 00184 } 00185 00186 QVector<rssItem> &RssParser::getItems() 00187 { 00188 return channel->rssItems; 00189 } 00190 00191 int RssParser::getSize() 00192 { 00193 return channel->rssItems.size(); 00194 } 00195 00196 QStringList RssParser::getChannelInfo() 00197 { 00198 QStringList ch; 00199 ch << channel->title << channel->description << channel->link << channel->pubdate << channel->copyright << channel->language; 00200 return ch; 00201 }
1.4.2