summaryrefslogtreecommitdiffstats
path: root/kbugbuster/backend/htmlparser.cpp
blob: 055b8d1157e0d30b88cbac3c41578dbed5a4d079 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
    This file is part of KBugBuster.

    Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

    As a special exception, permission is given to link this program
    with any edition of Qt, and distribute the resulting executable,
    without including the source code for Qt in the source distribution.
*/

#include "htmlparser.h"
#include "bugimpl.h"
#include "packageimpl.h"

#include <kdebug.h>

#include <tqbuffer.h>
#include <tqregexp.h>
#include <tqtextstream.h>

KBB::Error HtmlParser::parseBugList( const TQByteArray &data, Bug::List &bugs )
{
  TQBuffer buffer( data );
  if ( !buffer.open( IO_ReadOnly ) ) {
    return KBB::Error( "Can't open buffer" );
  }

  TQTextStream ts( &buffer );

  mState = Idle;

  TQString line;
  while ( !( line = ts.readLine() ).isNull() ) {
    KBB::Error err = parseLine( line, bugs );
    if ( err ) return err;
  }

  return KBB::Error();
}

KBB::Error HtmlParser::parsePackageList( const TQByteArray &data,
                                         Package::List &packages )
{
  init();

  TQBuffer buffer( data );
  if ( !buffer.open( IO_ReadOnly ) ) {
    return KBB::Error( "Can't open buffer" );
  }

  TQTextStream ts( &buffer );

  TQString line;
  while ( !( line = ts.readLine() ).isNull() ) {
    KBB::Error err = parseLine( line, packages );
    if ( err ) return err;
  }

  processResult( packages );

  return KBB::Error();
}

void HtmlParser::init()
{
}

void HtmlParser::setPackageListQuery( KURL &url )
{
  url.setFileName( "query.cgi" );
}

KBB::Error HtmlParser::parseLine( const TQString &, Bug::List & )
{
  return KBB::Error();
}

KBB::Error HtmlParser::parseLine( const TQString &, Package::List & )
{
  return KBB::Error();
}

void HtmlParser::processResult( Package::List & )
{
}

TQString HtmlParser::getAttribute( const TQString &line, const TQString &name )
{
  int pos1 = line.find( name + "=\"" );
  if ( pos1 < 1 ) return TQString::null;
  pos1 += name.length() + 2;
  int pos2 = line.find( "\"", pos1 );
  if ( pos2 < 1 ) return TQString::null;
  return line.mid( pos1, pos2 - pos1 );
}

bool HtmlParser::getCpts( const TQString &line, TQString &key,
                          TQStringList &values )
{
  if ( !line.contains( TQRegExp( "\\s*cpts" ) ) ) return false;

//  kdDebug() << "LINE: " << line << endl;
  int pos1 = line.find( "[" );
  if ( pos1 < 0 ) return false;
  int pos2 = line.find( "]", ++pos1 );
  if ( pos2 < 0 ) return false;

  key = line.mid( pos1, pos2 - pos1 );
  int pos3 = key.find( "'" );
  if ( pos3 >= 0 ) {
    int pos4 = key.find( "'", ++pos3 );
    if ( pos4 >= 0 ) key = key.mid( pos3, pos4 - pos3 );
  }
//  kdDebug() << " KEY: " << key << endl;

  pos1 = line.find( "'", ++pos2 );
  if ( pos1 >= 0 ) pos2 = line.find( "'", ++pos1 );

  while ( pos1 >= 0 && pos2 >= 0 ) {
    TQString value = line.mid( pos1, pos2 - pos1 );
//      kdDebug() << " VALUE: " << value << endl;

    values.append( value );

    pos1 = line.find( "'", ++pos2 );
    if ( pos1 >= 0 ) pos2 = line.find( "'", ++pos1 );
  }

  return true;
}

KBB::Error HtmlParser_2_10::parseLine( const TQString &line, Bug::List &bugs )
{
  if ( line.startsWith( "<TR VALIGN" ) ) {
//  kdDebug() << "LINE: " << line << endl;
    TQRegExp re( "show_bug\\.cgi\\?id=(\\d+)" );
    re.search( line );
    TQString number = re.cap( 1 );
//  kdDebug() << " NUMBER: " << number << endl;

    TQString summary;
    int pos = line.findRev( "summary>" );
    if ( pos >= 0 ) summary = line.mid( pos + 8 );

    Bug bug( new BugImpl( summary, Person(), number, 0xFFFFFFFF, Bug::SeverityUndefined,
                          Person(), Bug::StatusUndefined,
                          Bug::BugMergeList() ) );

    if ( !bug.isNull() ) {
      bugs.append( bug );
    }
  }

  return KBB::Error();
}

KBB::Error HtmlParser_2_10::parseLine( const TQString &line,
                                       Package::List &packages )
{
  TQString package;
  TQStringList components;

  if ( getCpts( line, package, components ) ) {
    packages.append( Package( new PackageImpl( package, "", 0, Person(),
                                               components ) ) );
  }

  return KBB::Error();
}


void HtmlParser_2_14_2::init()
{
  mComponentsMap.clear();
  
  mState = Idle;
}

KBB::Error HtmlParser_2_14_2::parseLine( const TQString &line,
                                         Package::List & )
{
  switch ( mState ) {
    case Idle:
      if ( line.startsWith( "tms[" ) ) mState = Components;
      break;
    case Components: {
      if ( line.startsWith( "function" ) ) mState = Finished;
      TQString key;
      TQStringList values;
      if ( getCpts( line, key, values ) ) {
//        kdDebug() << "KEY: " << key << "  VALUES: " << values.join(",") << endl;
        if ( values.count() == 2 ) {
          mComponentsMap[ values.last() ].append( key );
        }
      }
    }
    default:
      break;
  }

  return KBB::Error();
}

void HtmlParser_2_14_2::processResult( Package::List &packages )
{
  TQMap<TQString,TQStringList>::ConstIterator it;
  for ( it = mComponentsMap.begin(); it != mComponentsMap.end(); ++it ) {
    packages.append( Package( new PackageImpl( it.key(), "", 0, Person(),
                                               it.data() ) ) );    
  }
}


void HtmlParser_2_17_1::init()
{
  mProducts.clear();
  mComponents.clear();
  
  mState = Idle;
}

KBB::Error HtmlParser_2_17_1::parseBugList( const TQByteArray &data, Bug::List &bugs )
{
  return RdfProcessor::parseBugList( data, bugs );
}

KBB::Error HtmlParser_2_17_1::parseLine( const TQString & /*line*/, Bug::List &/*bugs*/ )
{
  return KBB::Error( "Not implemented" );
}

KBB::Error HtmlParser_2_17_1::parseLine( const TQString &line, Package::List & )
{
  switch ( mState ) {
    case Idle:
    case SearchComponents:
      if ( line.contains( "var cpts" ) ) mState = Components;
      break;
    case SearchProducts:
      if ( line.contains( "onchange=\"selectProduct" ) ) mState = Products;
      break;
    case Components: {
      if ( line.contains( TQRegExp( "\\s*function" ) ) ) {
        mState = SearchProducts;
      }
      TQString key;
      TQStringList components;
      if ( getCpts( line, key, components ) ) {
        mComponents.append( components );
      }
    }
    case Products: {
      if ( line.contains( "</select>" ) ) mState = Finished;
      TQString product = getAttribute( line, "value" );
      if ( !product.isEmpty() ) {
        kdDebug() << "PRODUCT: " << product << endl;
        mProducts.append( product );
      }
      break;
    }
    case Finished:
    default:
      break;
  }

  return KBB::Error();
}

void HtmlParser_2_17_1::processResult( Package::List &packages )
{
  TQStringList::ConstIterator itProduct = mProducts.begin();
  TQValueList<TQStringList>::ConstIterator itComponents = mComponents.begin();
  
  while( itProduct != mProducts.end() && itComponents != mComponents.end() ) {
    packages.append( Package( new PackageImpl( *itProduct, "", 0, Person(),
                                               *itComponents ) ) );
    ++itProduct;
    ++itComponents;
  }
}