This commit is contained in:
rhp%netscape.com 1999-06-13 22:08:55 +00:00
Родитель 9959454518
Коммит 44c85e64df
1 изменённых файлов: 93 добавлений и 133 удалений

Просмотреть файл

@ -15,77 +15,47 @@
* Copyright (C) 1998 Netscape Communications Corporation. All Rights * Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved. * Reserved.
*/ */
/* /*
This program drives libmime to parse an RFC822 message and output the This program drives libmime to parse an RFC822 message and output the
HTML representation of it. HTML representation of it.
The program takes a single parameter: the disk file from which to read. The program takes a single parameter: the disk file from which to read.
*/ */
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsIURL.h"
#include "nsIEventQueueService.h" #include "nsIEventQueueService.h"
#include "nsINetService.h"
#include "nsIInputStream.h" #include "nsIInputStream.h"
#include "nsINetService.h"
#include "nsINetService.h"
#include "nsIOutputStream.h" #include "nsIOutputStream.h"
#include "nsIGenericFactory.h" #include "nsIGenericFactory.h"
#include "nsIPostToServer.h"
#include "nsIRDFCompositeDataSource.h"
#include "nsIRDFXMLDataSource.h"
#include "nsIRDFDocument.h"
#include "nsIRDFNode.h"
#include "nsIRDFService.h"
#include "nsIRDFXMLSource.h"
#include "nsIServiceManager.h" #include "nsIServiceManager.h"
#include "nsIStreamListener.h" #include "nsIStreamListener.h"
#include "nsIURL.h"
#include "nsDOMCID.h" // for NS_SCRIPT_NAMESET_REGISTRY_CID
#include "nsLayoutCID.h" // for NS_NAMESPACEMANAGER_CID
#include "nsRDFCID.h"
#include "nsRDFCID.h"
#include "nsIComponentManager.h"
#include "prthread.h"
#include "plevent.h"
#include "plstr.h"
#include "nsParserCIID.h"
#include "nsIStreamConverter.h" #include "nsIStreamConverter.h"
#include "nsFileStream.h" #include "nsFileStream.h"
#include "nsFileSpec.h" #include "nsFileSpec.h"
#include "nsMimeTypes.h" #include "nsMimeTypes.h"
#include "nsIPref.h" #include "nsIPref.h"
#include "nsICharsetConverterManager.h" #include "nsICharsetConverterManager.h"
#include "prprf.h"
#include "nsIAllocator.h" // for the CID
#ifdef XP_PC ////////////////////////////////////////////////////////////////////////////////////
#include "windows.h" // THIS IS THE STUFF TO GET THE TEST HARNESS OFF THE GROUND
#endif ////////////////////////////////////////////////////////////////////////////////////
#if defined(XP_PC) #if defined(XP_PC)
#define DOM_DLL "jsdom.dll"
#define LAYOUT_DLL "raptorhtml.dll"
#define NETLIB_DLL "netlib.dll" #define NETLIB_DLL "netlib.dll"
#define PARSER_DLL "raptorhtmlpars.dll"
#define RDF_DLL "rdf.dll"
#define XPCOM_DLL "xpcom32.dll" #define XPCOM_DLL "xpcom32.dll"
#define MIME_DLL "mime.dll" #define MIME_DLL "mime.dll"
#define PREF_DLL "xppref32.dll" #define PREF_DLL "xppref32.dll"
#define UNICHAR_DLL "uconv.dll" #define UNICHAR_DLL "uconv.dll"
#elif defined(XP_UNIX) #elif defined(XP_UNIX)
#define DOM_DLL "libjsdom"MOZ_DLL_SUFFIX
#define LAYOUT_DLL "libraptorhtml"MOZ_DLL_SUFFIX
#define NETLIB_DLL "libnetlib"MOZ_DLL_SUFFIX #define NETLIB_DLL "libnetlib"MOZ_DLL_SUFFIX
#define PARSER_DLL "libraptorhtmlpars"MOZ_DLL_SUFFIX
#define RDF_DLL "librdf"MOZ_DLL_SUFFIX
#define XPCOM_DLL "libxpcom"MOZ_DLL_SUFFIX #define XPCOM_DLL "libxpcom"MOZ_DLL_SUFFIX
#define MIME_DLL "libmime.dll"MOZ_DLL_SUFFIX #define MIME_DLL "libmime.dll"MOZ_DLL_SUFFIX
#define PREF_DLL "libxppref32"MOZ_DLL_SUFFIX #define PREF_DLL "libxppref32"MOZ_DLL_SUFFIX
#define UNICHAR_DLL "libunicharutil"MOZ_DLL_SUFFIX #define UNICHAR_DLL "libunicharutil"MOZ_DLL_SUFFIX
#elif defined(XP_MAC) #elif defined(XP_MAC)
#define DOM_DLL "DOM_DLL"
#define LAYOUT_DLL "LAYOUT_DLL"
#define NETLIB_DLL "NETLIB_DLL" #define NETLIB_DLL "NETLIB_DLL"
#define PARSER_DLL "PARSER_DLL"
#define RDF_DLL "RDF_DLL"
#define XPCOM_DLL "XPCOM_DLL" #define XPCOM_DLL "XPCOM_DLL"
#define MIME_DLL "MIME_DLL" #define MIME_DLL "MIME_DLL"
#define PREF_DLL "XPPREF32_DLL" #define PREF_DLL "XPPREF32_DLL"
@ -93,54 +63,29 @@
#endif #endif
// {588595CB-2012-11d3-8EF0-00A024A7D144} // {588595CB-2012-11d3-8EF0-00A024A7D144}
#define CONV_CID \ #define CONV_CID { 0x1e3f79f1, 0x6b6b, 0x11d2, { 0x8a, 0x86, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 } };
{ 0x1e3f79f1, 0x6b6b, 0x11d2, \ #define CONV_IID { 0x1e3f79f0, 0x6b6b, 0x11d2, { 0x8a, 0x86, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 } }
{ 0x8a, 0x86, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 } };
#define CONV_IID \
{ 0x1e3f79f0, 0x6b6b, 0x11d2, \
{ 0x8a, 0x86, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 } }
////////////////////////////////////////////////////////////////////////
// CIDs // CIDs
// I18N
static NS_DEFINE_CID(charsetCID, CONV_CID); static NS_DEFINE_CID(charsetCID, CONV_CID);
NS_DEFINE_IID(kConvMeIID, CONV_IID);
// prefs // prefs
static NS_DEFINE_CID(kPrefCID, NS_PREF_CID); static NS_DEFINE_CID(kPrefCID, NS_PREF_CID);
// rdf
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
// xpcom // xpcom
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
static NS_DEFINE_CID(kGenericFactoryCID, NS_GENERICFACTORY_CID); static NS_DEFINE_CID(kGenericFactoryCID, NS_GENERICFACTORY_CID);
static NS_DEFINE_CID(kStreamConverterCID, NS_STREAM_CONVERTER_CID); // netlib definitions....
/*
* nsStreamConverter definitions....
*/
static NS_DEFINE_CID(kNetServiceCID, NS_NETSERVICE_CID); static NS_DEFINE_CID(kNetServiceCID, NS_NETSERVICE_CID);
//////////////////////////////////////////////////////////////////////// // Stream converter
// IIDs static NS_DEFINE_CID(kStreamConverterCID, NS_STREAM_CONVERTER_CID);
NS_DEFINE_IID(kIEventQueueServiceIID, NS_IEVENTQUEUESERVICE_IID); nsICharsetConverterManager *ccMan = nsnull;
NS_DEFINE_IID(kIOutputStreamIID, NS_IOUTPUTSTREAM_IID);
NS_DEFINE_IID(kIInputStreamIID, NS_IINPUTSTREAM_IID);
NS_DEFINE_IID(kIRDFXMLDataSourceIID, NS_IRDFXMLDATASOURCE_IID);
NS_DEFINE_IID(kIRDFServiceIID, NS_IRDFSERVICE_IID);
NS_DEFINE_IID(kIRDFXMLSourceIID, NS_IRDFXMLSOURCE_IID);
NS_DEFINE_IID(kConvMeIID, CONV_IID);
#include "nsIAllocator.h" // for the CID
nsICharsetConverterManager *ccMan;
static nsresult static nsresult
SetupRegistry(void) SetupRegistry(void)
@ -160,22 +105,29 @@ SetupRegistry(void)
// xpcom // xpcom
static NS_DEFINE_CID(kAllocatorCID, NS_ALLOCATOR_CID); static NS_DEFINE_CID(kAllocatorCID, NS_ALLOCATOR_CID);
static NS_DEFINE_CID(kEventQueueCID, NS_EVENTQUEUE_CID); static NS_DEFINE_CID(kEventQueueCID, NS_EVENTQUEUE_CID);
nsComponentManager::RegisterComponent(kEventQueueServiceCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE); nsComponentManager::RegisterComponent(kEventQueueServiceCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);
nsComponentManager::RegisterComponent(kEventQueueCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE); nsComponentManager::RegisterComponent(kEventQueueCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);
nsComponentManager::RegisterComponent(kGenericFactoryCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE); nsComponentManager::RegisterComponent(kGenericFactoryCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);
nsComponentManager::RegisterComponent(kAllocatorCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE); nsComponentManager::RegisterComponent(kAllocatorCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);
// prefs // prefs
nsComponentManager::RegisterComponent(kPrefCID, NULL, NULL, PREF_DLL, PR_FALSE, PR_FALSE); nsComponentManager::RegisterComponent(kPrefCID, NULL, NULL, PREF_DLL, PR_FALSE, PR_FALSE);
// mime // mime
nsComponentManager::RegisterComponent(kStreamConverterCID, NULL, NULL, MIME_DLL, PR_FALSE, PR_FALSE); nsComponentManager::RegisterComponent(kStreamConverterCID, NULL, NULL, MIME_DLL, PR_FALSE, PR_FALSE);
return NS_OK; return NS_OK;
} }
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
// END OF STUFF TO GET THE TEST HARNESS OFF THE GROUND
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// THIS IS THE CLASS THAT WOULD BE IMPLEMENTED BY THE CONSUMER OF THE HTML OUPUT
// FROM LIBMIME. THIS EXAMPLE SIMPLY WRITES THE OUTPUT TO STDOUT()
////////////////////////////////////////////////////////////////////////////////////
class ConsoleOutputStreamImpl : public nsIOutputStream class ConsoleOutputStreamImpl : public nsIOutputStream
{ {
public: public:
@ -186,8 +138,9 @@ public:
NS_DECL_ISUPPORTS NS_DECL_ISUPPORTS
// nsIBaseStream interface // nsIBaseStream interface
NS_IMETHOD Close(void) { NS_IMETHOD Close(void)
return NS_OK; {
return NS_OK;
} }
// nsIOutputStream interface // nsIOutputStream interface
@ -202,9 +155,16 @@ public:
return NS_OK; return NS_OK;
} }
}; };
NS_IMPL_ISUPPORTS(ConsoleOutputStreamImpl, nsIOutputStream::GetIID());
////////////////////////////////////////////////////////////////////////////////////
// END OF CONSUMER STREAM
////////////////////////////////////////////////////////////////////////////////////
NS_IMPL_ISUPPORTS(ConsoleOutputStreamImpl, kIOutputStreamIID); ////////////////////////////////////////////////////////////////////////////////////
// THIS IS A TEMPORARY CLASS THAT MAKES A DISK FILE LOOK LIKE A nsIInputStream
// INTERFACE...this may already exist, but I didn't find it. Eventually, you would
// just plugin a Necko stream when they are all rewritten to be new style streams
////////////////////////////////////////////////////////////////////////////////////
class FileInputStreamImpl : public nsIInputStream class FileInputStreamImpl : public nsIInputStream
{ {
public: public:
@ -277,10 +237,16 @@ FileInputStreamImpl::PumpFileStream()
return NS_BASE_STREAM_EOF; return NS_BASE_STREAM_EOF;
} }
NS_IMPL_ISUPPORTS(FileInputStreamImpl, kIInputStreamIID); NS_IMPL_ISUPPORTS(FileInputStreamImpl, nsIInputStream::GetIID());
////////////////////////////////////////////////////////////////////////////////////
// End of FileInputStreamImpl()
////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Utility to make back slashes forward slashes...
void void
FixURL(char *url) FixURL(char *url)
{ {
@ -293,6 +259,7 @@ FixURL(char *url)
} }
} }
// Utility to create a nsIURL object...
nsresult nsresult
NewURL(nsIURL** aInstancePtrResult, const nsString& aSpec) NewURL(nsIURL** aInstancePtrResult, const nsString& aSpec)
{ {
@ -310,54 +277,53 @@ NewURL(nsIURL** aInstancePtrResult, const nsString& aSpec)
return rv; return rv;
} }
#include "nsSpecialSystemDirectory.h"
nsresult
AutoregisterComponents()
{
// Autoregister components to populate registry
// All this logic is in nsSpecialFileSpec in apprunner.
// But hey this is viewer a different app.
nsresult rv = NS_ERROR_FAILURE;
nsSpecialSystemDirectory sysdir(nsSpecialSystemDirectory::OS_CurrentProcessDirectory);
#ifdef XP_MAC
sysdir += "Components";
#else
sysdir += "components";
#endif /* XP_MAC */
nsNSPRPath componentsDir(sysdir);
const char *componentsDirPath = (const char *) componentsDir;
if (componentsDirPath != NULL)
{
rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, componentsDirPath);
}
return rv;
}
int int
main(int argc, char** argv) main(int argc, char** argv)
{ {
nsresult DoRFC822toHTMLConversion(char *filename);
nsresult rv; nsresult rv;
// Do some sanity checking...
if (argc < 2) if (argc < 2)
{ {
fprintf(stderr, "usage: %s <rfc822_disk_file>\n", argv[0]); fprintf(stderr, "usage: %s <rfc822_disk_file>\n", argv[0]);
return 1; return 1;
} }
// Setup the registry...
SetupRegistry(); SetupRegistry();
// AutoregisterComponents();
// Get an event queue started... // Get an event queue started...
NS_WITH_SERVICE(nsIEventQueueService, theEventQueueService, kEventQueueServiceCID, &rv); NS_WITH_SERVICE(nsIEventQueueService, theEventQueueService, kEventQueueServiceCID, &rv);
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
rv = theEventQueueService->CreateThreadEventQueue(); rv = theEventQueueService->CreateThreadEventQueue();
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create thread event queue"); NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create thread event queue");
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
DoRFC822toHTMLConversion(argv[1]);
// Cleanup stuff necessary...
NS_RELEASE(ccMan);
return NS_OK;
}
nsresult
DoRFC822toHTMLConversion(char *filename)
{
nsFilePath inFilePath(filename, PR_TRUE); // relative path.
nsFileSpec mySpec(inFilePath);
nsresult rv;
char newURL[1024] = ""; // URL for filename
nsIURL *aURL = nsnull;
if (!mySpec.Exists())
{
printf("Unable to open input file %s\n", filename);
return NS_ERROR_FAILURE;
}
// Create a mime parser (nsIStreamConverter)!
nsCOMPtr<nsIStreamConverter> mimeParser; nsCOMPtr<nsIStreamConverter> mimeParser;
rv = nsComponentManager::CreateInstance(kStreamConverterCID, rv = nsComponentManager::CreateInstance(kStreamConverterCID,
NULL, nsIStreamConverter::GetIID(), NULL, nsIStreamConverter::GetIID(),
@ -368,51 +334,48 @@ main(int argc, char** argv)
return rv; return rv;
} }
// And this should receive all the HTML from libmime // Create the consumer output stream.. this will receive all the HTML from libmime
nsCOMPtr<nsIOutputStream> out = do_QueryInterface(new ConsoleOutputStreamImpl()); nsCOMPtr<nsIOutputStream> out = do_QueryInterface(new ConsoleOutputStreamImpl());
if (! out) if (!out)
{ {
printf("Failed to create nsIOutputStream\n"); printf("Failed to create nsIOutputStream\n");
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_FAILURE;
} }
nsFilePath inFilePath(argv[1], PR_TRUE); // relative path.
const char *pathAsString = (const char*)argv[1];
nsFileSpec mySpec(inFilePath);
// And this should receive all the HTML from libmime // This is the producer stream that will deliver data from the disk file...
nsCOMPtr<FileInputStreamImpl> in = do_QueryInterface(new FileInputStreamImpl()); nsCOMPtr<FileInputStreamImpl> in = do_QueryInterface(new FileInputStreamImpl());
if (! in ) if (!in )
{ {
printf("Failed to create nsIInputStream\n"); printf("Failed to create nsIInputStream\n");
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_FAILURE;
} }
if (NS_FAILED(in->OpenDiskFile(mySpec))) if (NS_FAILED(in->OpenDiskFile(mySpec)))
{ {
printf("Unable to open input file %s\n", argv[1]); printf("Unable to open input file %s\n", filename);
return -1; return NS_ERROR_FAILURE;
} }
// Create an nsIURL object needed for stream IO... // Create an nsIURL object needed for stream IO...
char newURL[512]; PR_snprintf(newURL, sizeof(newURL), "file://%s", filename);
PR_snprintf(newURL, sizeof(newURL), "file://%s", argv[1]);
FixURL(newURL); FixURL(newURL);
nsIURL *aURL = nsnull;
if (NS_FAILED(NewURL(&aURL, nsString(newURL)))) if (NS_FAILED(NewURL(&aURL, nsString(newURL))))
{ {
printf("Unable to open input file\n"); printf("Unable to open input file\n");
return -1; return NS_ERROR_FAILURE;
} }
// Set us as the output stream for HTML data from libmime... // Set us as the output stream for HTML data from libmime...
if (NS_FAILED(mimeParser->SetOutputStream(out, newURL))) if (NS_FAILED(mimeParser->SetOutputStream(out, newURL)))
{ {
printf("Unable to set the output stream for the mime parser...\ncould be failure to create internal data\n"); printf("Unable to set the output stream for the mime parser...\ncould be failure to create internal libmime data\n");
return -1; return NS_ERROR_FAILURE;
} }
// Assuming this is an RFC822 message...
mimeParser->OnStartBinding(aURL, MESSAGE_RFC822); mimeParser->OnStartBinding(aURL, MESSAGE_RFC822);
// Just pump all of the data from the file into libmime...
while (NS_SUCCEEDED(in->PumpFileStream())) while (NS_SUCCEEDED(in->PumpFileStream()))
{ {
PRUint32 len; PRUint32 len;
@ -421,9 +384,6 @@ main(int argc, char** argv)
} }
mimeParser->OnStopBinding(aURL, NS_OK, nsnull); mimeParser->OnStopBinding(aURL, NS_OK, nsnull);
NS_RELEASE(aURL);
return NS_OK; return NS_OK;
} }
// SetOutputListener(nsIStreamListener *outListner);