akregator/src/librss
feeddetector.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <qregexp.h>
00026 #include <qstring.h>
00027 #include <qstringlist.h>
00028 #include <qvaluelist.h>
00029 #include <kcharsets.h>
00030 #include <kurl.h>
00031
00032 #include "feeddetector.h"
00033
00034
00035 using namespace RSS;
00036
00037 FeedDetectorEntryList FeedDetector::extractFromLinkTags(const QString& s)
00038 {
00039
00040 QString str = s.simplifyWhiteSpace();
00041
00042
00043 QRegExp reLinkTag("<[\\s]?LINK[^>]*REL[\\s]?=[\\s]?\\\"[\\s]?(ALTERNATE|SERVICE\\.FEED)[\\s]?\\\"[^>]*>", false);
00044
00045
00046 QRegExp reHref("HREF[\\s]?=[\\s]?\\\"([^\\\"]*)\\\"", false);
00047
00048 QRegExp reType("TYPE[\\s]?=[\\s]?\\\"([^\\\"]*)\\\"", false);
00049
00050 QRegExp reTitle("TITLE[\\s]?=[\\s]?\\\"([^\\\"]*)\\\"", false);
00051
00052 int pos = 0;
00053 int matchpos = 0;
00054
00055
00056 QStringList linkTags;
00057
00058 while ( matchpos != -1 )
00059 {
00060 matchpos = reLinkTag.search(str, pos);
00061 if (matchpos != -1)
00062 {
00063 linkTags.append( str.mid(matchpos, reLinkTag.matchedLength()) );
00064 pos = matchpos + reLinkTag.matchedLength();
00065 }
00066 }
00067
00068 FeedDetectorEntryList list;
00069
00070 for ( QStringList::Iterator it = linkTags.begin(); it != linkTags.end(); ++it )
00071 {
00072 QString type;
00073 int pos = reType.search(*it, 0);
00074 if (pos != -1)
00075 type = reType.cap(1).lower();
00076
00077
00078 if ( type != "application/rss+xml" && type != "application/rdf+xml"
00079 && type != "application/atom+xml" && type != "text/xml" )
00080 continue;
00081
00082 QString title;
00083 pos = reTitle.search(*it, 0);
00084 if (pos != -1)
00085 title = reTitle.cap(1);
00086
00087 title = KCharsets::resolveEntities(title);
00088
00089 QString url;
00090 pos = reHref.search(*it, 0);
00091 if (pos != -1)
00092 url = reHref.cap(1);
00093
00094 url = KCharsets::resolveEntities(url);
00095
00096
00097 if ( title.isEmpty() )
00098 title = url;
00099
00100 if ( !url.isEmpty() )
00101 list.append(FeedDetectorEntry(url, title) );
00102 }
00103
00104
00105 return list;
00106 }
00107
00108 QStringList FeedDetector::extractBruteForce(const QString& s)
00109 {
00110 QString str = s.simplifyWhiteSpace();
00111
00112 QRegExp reAhrefTag("<[\\s]?A[^>]?HREF=[\\s]?\\\"[^\\\"]*\\\"[^>]*>", false);
00113
00114
00115 QRegExp reHref("HREF[\\s]?=[\\s]?\\\"([^\\\"]*)\\\"", false);
00116
00117 QRegExp rssrdfxml(".*(RSS|RDF|XML)", false);
00118
00119 int pos = 0;
00120 int matchpos = 0;
00121
00122
00123 QStringList list;
00124
00125 while ( matchpos != -1 )
00126 {
00127 matchpos = reAhrefTag.search(str, pos);
00128 if ( matchpos != -1 )
00129 {
00130 QString ahref = str.mid(matchpos, reAhrefTag.matchedLength());
00131 int hrefpos = reHref.search(ahref, 0);
00132 if ( hrefpos != -1 )
00133 {
00134 QString url = reHref.cap(1);
00135
00136 url = KCharsets::resolveEntities(url);
00137
00138 if ( rssrdfxml.exactMatch(url) )
00139 list.append(url);
00140 }
00141
00142 pos = matchpos + reAhrefTag.matchedLength();
00143 }
00144 }
00145
00146 return list;
00147 }
00148
00149 QString FeedDetector::fixRelativeURL(const QString &s, const KURL &baseurl)
00150 {
00151 QString s2=s;
00152 KURL u;
00153 if (KURL::isRelativeURL(s2))
00154 {
00155 if (s2.startsWith("//"))
00156 {
00157 s2=s2.prepend(baseurl.protocol()+":");
00158 u=s2;
00159 }
00160 else if (s2.startsWith("/"))
00161 {
00162 KURL b2(baseurl);
00163 b2.setPath(QString());
00164 b2.setQuery(QString());
00165 u = KURL(b2, s2.remove(0,1));
00166 }
00167 else
00168 {
00169 u = KURL(baseurl, s2);
00170 }
00171 }
00172 else
00173 u=s2;
00174
00175 u.cleanPath();
00176
00177
00178 return u.url();
00179 }
|