00001
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 "htmlparser.h"
00026 #include "bugimpl.h"
00027 #include "packageimpl.h"
00028
00029 #include <kdebug.h>
00030
00031 #include <QtCore/QBuffer>
00032 #include <QtCore/QRegExp>
00033 #include <QtCore/QTextStream>
00034 #include <QtCore/QList>
00035 #include <QtCore/QLatin1String>
00036
00037 KBB::Error HtmlParser::parseBugList( const QByteArray &data, Bug::List &bugs )
00038 {
00039 QByteArray tmpData(data);
00040 QBuffer buffer( &tmpData );
00041 if ( !buffer.open( QIODevice::ReadOnly ) ) {
00042 return KBB::Error( "Cannot open buffer" );
00043 }
00044
00045 QTextStream ts( &buffer );
00046
00047 mState = Idle;
00048
00049 QString line;
00050 while ( !( line = ts.readLine() ).isNull() ) {
00051 KBB::Error err = parseLine( line, bugs );
00052 if ( err ) return err;
00053 }
00054
00055 return KBB::Error();
00056 }
00057
00058 KBB::Error HtmlParser::parsePackageList( const QByteArray &data,
00059 Package::List &packages )
00060 {
00061 init();
00062
00063 QByteArray tmpData( data );
00064 QBuffer buffer( &tmpData );
00065 if ( !buffer.open( QIODevice::ReadOnly ) ) {
00066 return KBB::Error( "Cannot open buffer" );
00067 }
00068
00069 QTextStream ts( &buffer );
00070
00071 QString line;
00072 while ( !( line = ts.readLine() ).isNull() ) {
00073 KBB::Error err = parseLine( line, packages );
00074 if ( err ) return err;
00075 }
00076
00077 processResult( packages );
00078
00079 return KBB::Error();
00080 }
00081
00082 void HtmlParser::init()
00083 {
00084 }
00085
00086 void HtmlParser::setPackageListQuery( KUrl &url )
00087 {
00088 url.setFileName( "query.cgi" );
00089 }
00090
00091 KBB::Error HtmlParser::parseLine( const QString &, Bug::List & )
00092 {
00093 return KBB::Error();
00094 }
00095
00096 KBB::Error HtmlParser::parseLine( const QString &, Package::List & )
00097 {
00098 return KBB::Error();
00099 }
00100
00101 void HtmlParser::processResult( Package::List & )
00102 {
00103 }
00104
00105 QString HtmlParser::getAttribute( const QString &line, const QString &name )
00106 {
00107 int pos1 = line.indexOf( name + "=\"" );
00108 if ( pos1 < 1 ) return QString();
00109 pos1 += name.length() + 2;
00110 int pos2 = line.indexOf( "\"", pos1 );
00111 if ( pos2 < 1 ) return QString();
00112 return line.mid( pos1, pos2 - pos1 );
00113 }
00114
00115 bool HtmlParser::getCpts( const QString &line, QString &key,
00116 QStringList &values )
00117 {
00118 if ( !line.contains( QRegExp( "\\s*cpts" ) ) ) return false;
00119
00120
00121 int pos1 = line.indexOf( '[' );
00122 if ( pos1 < 0 ) return false;
00123 int pos2 = line.indexOf( ']', ++pos1 );
00124 if ( pos2 < 0 ) return false;
00125
00126 key = line.mid( pos1, pos2 - pos1 );
00127 int pos3 = key.indexOf( '\'' );
00128 if ( pos3 >= 0 ) {
00129 int pos4 = key.indexOf( '\'', ++pos3 );
00130 if ( pos4 >= 0 ) key = key.mid( pos3, pos4 - pos3 );
00131 }
00132
00133
00134 pos1 = line.indexOf( '\'', ++pos2 );
00135 if ( pos1 >= 0 ) pos2 = line.indexOf( '\'', ++pos1 );
00136
00137 while ( pos1 >= 0 && pos2 >= 0 ) {
00138 QString value = line.mid( pos1, pos2 - pos1 );
00139
00140
00141 values.append( value );
00142
00143 pos1 = line.indexOf( '\'', ++pos2 );
00144 if ( pos1 >= 0 ) pos2 = line.indexOf( '\'', ++pos1 );
00145 }
00146
00147 return true;
00148 }
00149
00150 KBB::Error HtmlParser_2_10::parseLine( const QString &line, Bug::List &bugs )
00151 {
00152 if ( line.startsWith( QLatin1String( "<TR VALIGN" ) ) ) {
00153
00154 QRegExp re( "show_bug\\.cgi\\?id=(\\d+)" );
00155 re.indexIn( line );
00156 QString number = re.cap( 1 );
00157
00158
00159 QString summary;
00160 int pos = line.lastIndexOf( "summary>" );
00161 if ( pos >= 0 ) summary = line.mid( pos + 8 );
00162
00163 Bug bug( new BugImpl( summary, Person(), number, 0xFFFFFFFF, Bug::SeverityUndefined,
00164 Person(), Bug::StatusUndefined,
00165 Bug::BugMergeList() ) );
00166
00167 if ( !bug.isNull() ) {
00168 bugs.append( bug );
00169 }
00170 }
00171
00172 return KBB::Error();
00173 }
00174
00175 KBB::Error HtmlParser_2_10::parseLine( const QString &line,
00176 Package::List &packages )
00177 {
00178 QString package;
00179 QStringList components;
00180
00181 if ( getCpts( line, package, components ) ) {
00182 packages.append( Package( new PackageImpl( package, "", 0, Person(),
00183 components ) ) );
00184 }
00185
00186 return KBB::Error();
00187 }
00188
00189
00190 void HtmlParser_2_14_2::init()
00191 {
00192 mComponentsMap.clear();
00193
00194 mState = Idle;
00195 }
00196
00197 KBB::Error HtmlParser_2_14_2::parseLine( const QString &line,
00198 Package::List & )
00199 {
00200 switch ( mState ) {
00201 case Idle:
00202 if ( line.startsWith( QLatin1String( "tms[" ) ) ) mState = Components;
00203 break;
00204 case Components: {
00205 if ( line.startsWith( QLatin1String( "function" ) ) ) mState = Finished;
00206 QString key;
00207 QStringList values;
00208 if ( getCpts( line, key, values ) ) {
00209
00210 if ( values.count() == 2 ) {
00211 mComponentsMap[ values.last() ].append( key );
00212 }
00213 }
00214 }
00215 default:
00216 break;
00217 }
00218
00219 return KBB::Error();
00220 }
00221
00222 void HtmlParser_2_14_2::processResult( Package::List &packages )
00223 {
00224 QMap<QString,QStringList>::ConstIterator it;
00225 for ( it = mComponentsMap.constBegin(); it != mComponentsMap.constEnd(); ++it ) {
00226 packages.append( Package( new PackageImpl( it.key(), "", 0, Person(),
00227 it.value() ) ) );
00228 }
00229 }
00230
00231
00232 void HtmlParser_2_17_1::init()
00233 {
00234 mProducts.clear();
00235 mComponents.clear();
00236
00237 mState = Idle;
00238 }
00239
00240 KBB::Error HtmlParser_2_17_1::parseBugList( const QByteArray &data, Bug::List &bugs )
00241 {
00242 return RdfProcessor::parseBugList( data, bugs );
00243 }
00244
00245 KBB::Error HtmlParser_2_17_1::parseLine( const QString & , Bug::List & )
00246 {
00247 return KBB::Error( "Not implemented" );
00248 }
00249
00250 KBB::Error HtmlParser_2_17_1::parseLine( const QString &line, Package::List & )
00251 {
00252 switch ( mState ) {
00253 case Idle:
00254 case SearchComponents:
00255 if ( line.contains( "var cpts" ) ) mState = Components;
00256 break;
00257 case SearchProducts:
00258 if ( line.contains( "onchange=\"selectProduct" ) ) mState = Products;
00259 break;
00260 case Components: {
00261 if ( line.contains( QRegExp( "\\s*function" ) ) ) {
00262 mState = SearchProducts;
00263 }
00264 QString key;
00265 QStringList components;
00266 if ( getCpts( line, key, components ) ) {
00267 mComponents.append( components );
00268 }
00269 }
00270 case Products: {
00271 if ( line.contains( "</select>" ) ) mState = Finished;
00272 QString product = getAttribute( line, "value" );
00273 if ( !product.isEmpty() ) {
00274 kDebug() << "PRODUCT: " << product;
00275 mProducts.append( product );
00276 }
00277 break;
00278 }
00279 case Finished:
00280 default:
00281 break;
00282 }
00283
00284 return KBB::Error();
00285 }
00286
00287 void HtmlParser_2_17_1::processResult( Package::List &packages )
00288 {
00289 QStringList::ConstIterator itProduct = mProducts.constBegin();
00290 QList<QStringList>::ConstIterator itComponents = mComponents.constBegin();
00291
00292 while( itProduct != mProducts.constEnd() && itComponents != mComponents.constEnd() ) {
00293 packages.append( Package( new PackageImpl( *itProduct, "", 0, Person(),
00294 *itComponents ) ) );
00295 ++itProduct;
00296 ++itComponents;
00297 }
00298 }