1998-05-14 05:08:06 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
#include "nsIRobotSink.h"
|
|
|
|
#include "nsIRobotSinkObserver.h"
|
|
|
|
#include "nsIParser.h"
|
1998-07-19 01:47:56 +04:00
|
|
|
#include "nsIWebShell.h"
|
1998-05-14 05:08:06 +04:00
|
|
|
#include "nsVoidArray.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIURL.h"
|
1998-06-04 05:19:25 +04:00
|
|
|
#include "nsIStreamListener.h"
|
1998-06-25 05:42:50 +04:00
|
|
|
#include "nsIDTDDebug.h"
|
1998-07-31 02:42:27 +04:00
|
|
|
#include "nsRepository.h"
|
|
|
|
#include "nsParserCIID.h"
|
1998-05-14 05:08:06 +04:00
|
|
|
|
|
|
|
static NS_DEFINE_IID(kIRobotSinkObserverIID, NS_IROBOTSINKOBSERVER_IID);
|
|
|
|
|
|
|
|
class RobotSinkObserver : public nsIRobotSinkObserver {
|
|
|
|
public:
|
|
|
|
RobotSinkObserver() {
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
~RobotSinkObserver() {
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD ProcessLink(const nsString& aURLSpec);
|
1998-05-19 23:06:59 +04:00
|
|
|
NS_IMETHOD VerifyDirectory (const char * verify_dir);
|
1998-05-15 01:47:33 +04:00
|
|
|
|
1998-05-14 05:08:06 +04:00
|
|
|
};
|
|
|
|
|
1998-05-15 01:47:33 +04:00
|
|
|
static nsVoidArray * g_workList;
|
|
|
|
static nsVoidArray * g_duplicateList;
|
|
|
|
static int g_iProcessed;
|
|
|
|
static int g_iMaxProcess = 5000;
|
|
|
|
static PRBool g_bHitTop;
|
1998-05-28 04:21:34 +04:00
|
|
|
static PRBool g_bReadyForNextUrl;
|
1998-05-15 01:47:33 +04:00
|
|
|
|
1998-05-14 05:08:06 +04:00
|
|
|
NS_IMPL_ISUPPORTS(RobotSinkObserver, kIRobotSinkObserverIID);
|
|
|
|
|
1998-05-19 23:06:59 +04:00
|
|
|
NS_IMETHODIMP RobotSinkObserver::VerifyDirectory(const char * verify_dir)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1998-05-14 05:08:06 +04:00
|
|
|
NS_IMETHODIMP RobotSinkObserver::ProcessLink(const nsString& aURLSpec)
|
|
|
|
{
|
1998-05-15 01:47:33 +04:00
|
|
|
if (!g_bHitTop) {
|
|
|
|
|
1998-06-04 05:19:25 +04:00
|
|
|
nsAutoString str;
|
1998-06-12 05:35:36 +04:00
|
|
|
// Geez this is ugly. temporary hack to only process html files
|
1998-06-04 05:19:25 +04:00
|
|
|
str.Truncate();
|
|
|
|
nsString(aURLSpec).Right(str,1);
|
|
|
|
if (!str.Equals("/"))
|
|
|
|
{
|
|
|
|
str.Truncate();
|
|
|
|
nsString(aURLSpec).Right(str,4);
|
|
|
|
if (!str.Equals("html"))
|
|
|
|
{
|
|
|
|
str.Truncate();
|
|
|
|
nsString(aURLSpec).Right(str,3);
|
|
|
|
if (!str.Equals("htm"))
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
1998-05-15 01:47:33 +04:00
|
|
|
PRInt32 nCount = g_duplicateList->Count();
|
|
|
|
if (nCount > 0)
|
|
|
|
{
|
|
|
|
for (PRInt32 n = 0; n < nCount; n++)
|
|
|
|
{
|
|
|
|
nsString * pstr = (nsString *)g_duplicateList->ElementAt(n);
|
|
|
|
if (pstr->Equals(aURLSpec)) {
|
1998-06-12 05:35:36 +04:00
|
|
|
fputs ("Robot: (duplicate '",stdout);
|
1998-05-15 01:47:33 +04:00
|
|
|
fputs (aURLSpec,stdout);
|
|
|
|
fputs ("')\n",stdout);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_duplicateList->AppendElement(new nsString(aURLSpec));
|
|
|
|
str.Truncate();
|
|
|
|
nsString(aURLSpec).Left(str,5);
|
|
|
|
if (str.Equals("http:")) {
|
|
|
|
g_iProcessed++;
|
1998-06-12 05:35:36 +04:00
|
|
|
if (g_iProcessed == (g_iMaxProcess > 0 ? g_iMaxProcess-1 : 0))
|
1998-05-15 01:47:33 +04:00
|
|
|
g_bHitTop = PR_TRUE;
|
|
|
|
g_workList->AppendElement(new nsString(aURLSpec));
|
|
|
|
}
|
|
|
|
else {
|
1998-06-12 05:35:36 +04:00
|
|
|
fputs ("Robot: (cannot process URL types '",stdout);
|
1998-05-15 01:47:33 +04:00
|
|
|
fputs (aURLSpec,stdout);
|
|
|
|
fputs ("')\n",stdout);
|
|
|
|
}
|
|
|
|
}
|
1998-05-14 05:08:06 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1998-05-19 23:06:59 +04:00
|
|
|
extern "C" NS_EXPORT void SetVerificationDirectory(char * verify_dir);
|
1998-05-14 05:08:06 +04:00
|
|
|
|
1998-07-01 15:11:44 +04:00
|
|
|
class CStreamListener: public nsIStreamObserver
|
1998-06-04 05:19:25 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CStreamListener() {
|
|
|
|
NS_INIT_REFCNT();
|
1998-07-01 15:11:44 +04:00
|
|
|
|
1998-06-04 05:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~CStreamListener() {
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
1998-12-16 08:40:20 +03:00
|
|
|
NS_IMETHOD OnProgress(nsIURL* aURL, PRUint32 Progress, PRUint32 ProgressMax) { return NS_OK; }
|
|
|
|
NS_IMETHOD OnStatus(nsIURL* aURL, const PRUnichar* aMsg) { return NS_OK; }
|
1998-07-16 02:38:01 +04:00
|
|
|
NS_IMETHOD OnStartBinding(nsIURL* aURL, const char *aContentType) { return NS_OK; }
|
1998-12-16 08:40:20 +03:00
|
|
|
NS_IMETHOD OnStopBinding(nsIURL* aURL, nsresult status, const PRUnichar* aMsg);
|
1998-06-04 05:19:25 +04:00
|
|
|
};
|
|
|
|
|
1998-12-16 08:40:20 +03:00
|
|
|
NS_IMETHODIMP CStreamListener::OnStopBinding(nsIURL* aURL, nsresult status, const PRUnichar* aMsg)
|
1998-06-04 05:19:25 +04:00
|
|
|
{
|
1998-06-12 05:35:36 +04:00
|
|
|
fputs("done.\n",stdout);
|
1998-06-04 05:19:25 +04:00
|
|
|
g_bReadyForNextUrl = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CStreamListener::QueryInterface(const nsIID& aIID, void** aInstancePtr)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(CStreamListener)
|
|
|
|
NS_IMPL_RELEASE(CStreamListener)
|
|
|
|
|
1998-06-12 05:35:36 +04:00
|
|
|
extern "C" NS_EXPORT void DumpVectorRecord(void);
|
1998-05-19 23:06:59 +04:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
extern "C" NS_EXPORT int DebugRobot(
|
|
|
|
nsVoidArray * workList,
|
1998-07-19 01:47:56 +04:00
|
|
|
nsIWebShell * ww,
|
1998-05-19 23:06:59 +04:00
|
|
|
int iMaxLoads,
|
|
|
|
char * verify_dir,
|
|
|
|
void (*yieldProc )(const char *)
|
|
|
|
)
|
1998-05-14 05:08:06 +04:00
|
|
|
{
|
1998-06-12 05:35:36 +04:00
|
|
|
int iCount = 1;
|
1998-06-04 05:19:25 +04:00
|
|
|
CStreamListener * pl = new CStreamListener;
|
|
|
|
NS_ADDREF(pl);
|
1998-07-01 15:11:44 +04:00
|
|
|
|
1998-05-15 01:47:33 +04:00
|
|
|
if (nsnull==workList)
|
|
|
|
return -1;
|
1998-05-19 23:06:59 +04:00
|
|
|
g_iMaxProcess = iMaxLoads;
|
1998-05-15 01:47:33 +04:00
|
|
|
g_iProcessed = 0;
|
|
|
|
g_bHitTop = PR_FALSE;
|
|
|
|
g_duplicateList = new nsVoidArray();
|
1998-05-14 05:08:06 +04:00
|
|
|
RobotSinkObserver* myObserver = new RobotSinkObserver();
|
|
|
|
NS_ADDREF(myObserver);
|
1998-05-15 01:47:33 +04:00
|
|
|
g_workList = workList;
|
1998-05-14 05:08:06 +04:00
|
|
|
|
1998-07-30 03:43:20 +04:00
|
|
|
/*
|
1998-06-25 05:42:50 +04:00
|
|
|
nsIDTDDebug * pIDTDDebug;
|
|
|
|
nsresult rval = NS_NewDTDDebug(&pIDTDDebug);
|
1998-06-19 03:26:55 +04:00
|
|
|
if (NS_OK != rval) {
|
|
|
|
fputs("Cannot create parser debugger.\n", stdout);
|
|
|
|
NS_RELEASE(myObserver);
|
|
|
|
return -1;
|
|
|
|
}
|
1998-06-25 05:42:50 +04:00
|
|
|
pIDTDDebug->SetVerificationDirectory(verify_dir);
|
1998-07-30 03:43:20 +04:00
|
|
|
*/
|
1998-06-19 03:26:55 +04:00
|
|
|
|
1998-05-14 05:08:06 +04:00
|
|
|
for (;;) {
|
1998-05-15 01:47:33 +04:00
|
|
|
PRInt32 n = g_workList->Count();
|
1998-05-14 05:08:06 +04:00
|
|
|
if (0 == n) {
|
|
|
|
break;
|
|
|
|
}
|
1998-05-15 01:47:33 +04:00
|
|
|
nsString* urlName = (nsString*) g_workList->ElementAt(n - 1);
|
|
|
|
g_workList->RemoveElementAt(n - 1);
|
1998-05-14 05:08:06 +04:00
|
|
|
|
|
|
|
// Create url
|
|
|
|
nsIURL* url;
|
|
|
|
nsresult rv = NS_NewURL(&url, *urlName);
|
|
|
|
if (NS_OK != rv) {
|
|
|
|
printf("invalid URL: '");
|
|
|
|
fputs(*urlName, stdout);
|
|
|
|
printf("'\n");
|
1998-06-19 03:26:55 +04:00
|
|
|
NS_RELEASE(myObserver);
|
1998-05-14 05:08:06 +04:00
|
|
|
return -1;
|
|
|
|
}
|
1998-06-12 05:35:36 +04:00
|
|
|
|
|
|
|
char str_num[25];
|
|
|
|
sprintf (str_num,"%d",iCount++);
|
|
|
|
fputs ("Robot: parsing(",stdout);
|
|
|
|
fputs (str_num,stdout);
|
|
|
|
fputs (") ",stdout);
|
|
|
|
fputs (*urlName,stdout);
|
|
|
|
fputs ("...",stdout);
|
|
|
|
|
1998-05-14 05:08:06 +04:00
|
|
|
delete urlName;
|
|
|
|
|
|
|
|
nsIParser* parser;
|
1998-07-31 02:42:27 +04:00
|
|
|
|
|
|
|
static NS_DEFINE_IID(kCParserIID, NS_IPARSER_IID);
|
|
|
|
static NS_DEFINE_IID(kCParserCID, NS_PARSER_IID);
|
|
|
|
|
1998-09-01 04:26:28 +04:00
|
|
|
rv = nsRepository::CreateInstance(kCParserCID,
|
1998-07-31 02:42:27 +04:00
|
|
|
nsnull,
|
|
|
|
kCParserIID,
|
|
|
|
(void **)&parser);
|
|
|
|
|
1998-05-14 05:08:06 +04:00
|
|
|
if (NS_OK != rv) {
|
|
|
|
printf("can't make parser\n");
|
1998-06-19 03:26:55 +04:00
|
|
|
NS_RELEASE(myObserver);
|
1998-05-14 05:08:06 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIRobotSink* sink;
|
|
|
|
rv = NS_NewRobotSink(&sink);
|
|
|
|
if (NS_OK != rv) {
|
|
|
|
printf("can't make parser\n");
|
1998-06-19 03:26:55 +04:00
|
|
|
NS_RELEASE(myObserver);
|
1998-05-14 05:08:06 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sink->Init(url);
|
|
|
|
sink->AddObserver(myObserver);
|
|
|
|
|
|
|
|
parser->SetContentSink(sink);
|
1998-11-26 05:55:59 +03:00
|
|
|
g_bReadyForNextUrl = PR_FALSE;
|
1998-06-25 05:42:50 +04:00
|
|
|
|
1998-07-30 03:43:20 +04:00
|
|
|
parser->Parse(url, pl,PR_TRUE);/* XXX hook up stream listener here! */
|
1998-05-28 04:21:34 +04:00
|
|
|
while (!g_bReadyForNextUrl) {
|
1998-12-16 08:40:20 +03:00
|
|
|
if (yieldProc != NULL) {
|
|
|
|
const char* spec;
|
|
|
|
(void)url->GetSpec(&spec);
|
|
|
|
(*yieldProc)(spec);
|
|
|
|
}
|
1998-05-28 04:21:34 +04:00
|
|
|
}
|
1998-06-04 05:19:25 +04:00
|
|
|
g_bReadyForNextUrl = PR_FALSE;
|
|
|
|
if (ww) {
|
1998-07-22 15:19:54 +04:00
|
|
|
ww->SetObserver(pl);
|
1998-12-16 08:40:20 +03:00
|
|
|
const char* spec;
|
|
|
|
(void)url->GetSpec(&spec);
|
|
|
|
ww->LoadURL(nsString(spec));/* XXX hook up stream listener here! */
|
1998-06-04 05:19:25 +04:00
|
|
|
while (!g_bReadyForNextUrl) {
|
1998-12-16 08:40:20 +03:00
|
|
|
if (yieldProc != NULL) {
|
|
|
|
const char* spec;
|
|
|
|
(void)url->GetSpec(&spec);
|
|
|
|
(*yieldProc)(spec);
|
|
|
|
}
|
1998-06-04 05:19:25 +04:00
|
|
|
}
|
|
|
|
}
|
1998-05-28 04:21:34 +04:00
|
|
|
|
1998-05-14 05:08:06 +04:00
|
|
|
NS_RELEASE(sink);
|
|
|
|
NS_RELEASE(parser);
|
|
|
|
NS_RELEASE(url);
|
|
|
|
}
|
|
|
|
|
1998-06-12 05:35:36 +04:00
|
|
|
fputs ("Robot completed.\n", stdout);
|
|
|
|
|
1998-06-04 05:19:25 +04:00
|
|
|
NS_RELEASE(pl);
|
1998-05-14 05:08:06 +04:00
|
|
|
NS_RELEASE(myObserver);
|
|
|
|
|
1998-07-30 03:43:20 +04:00
|
|
|
// pIDTDDebug->DumpVectorRecord();
|
|
|
|
//NS_RELEASE(pIDTDDebug);
|
1998-06-12 05:35:36 +04:00
|
|
|
|
1998-05-14 05:08:06 +04:00
|
|
|
return 0;
|
|
|
|
}
|