pjs/rdf/tests/rdfcat/rdfcat.cpp

231 строка
7.4 KiB
C++
Исходник Обычный вид История

1999-04-02 22:33:34 +04:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* 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.
*/
/*
A simple test program that reads in RDF/XML into an in-memory data
source, then uses the RDF/XML serialization API to write an
equivalent (but not identical) RDF/XML file back to stdout.
The program takes a single parameter: the URL from which to read.
*/
1999-05-05 07:14:06 +04:00
#include "nsCOMPtr.h"
1999-04-02 22:33:34 +04:00
#include "nsIEventQueueService.h"
#include "nsIInputStream.h"
#include "nsINetService.h"
#include "nsINetService.h"
#include "nsIOutputStream.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 "nsIStreamListener.h"
#include "nsIURL.h"
#include "nsDOMCID.h" // for NS_SCRIPT_NAMESET_REGISTRY_CID
#include "nsLayoutCID.h" // for NS_NAMESPACEMANAGER_CID
#include "nsParserCIID.h"
#include "nsRDFCID.h"
#include "nsRDFCID.h"
#include "nsIComponentManager.h"
#include "nsXPComCIID.h"
1999-05-05 07:14:06 +04:00
#include "prthread.h"
1999-04-02 22:33:34 +04:00
#include "plevent.h"
#include "plstr.h"
#if defined(XP_PC)
#define DOM_DLL "jsdom.dll"
#define LAYOUT_DLL "raptorhtml.dll"
#define NETLIB_DLL "netlib.dll"
#define PARSER_DLL "raptorhtmlpars.dll"
#define RDF_DLL "rdf.dll"
#define XPCOM_DLL "xpcom32.dll"
#elif defined(XP_UNIX)
#define DOM_DLL "libjsdom.so"
#define LAYOUT_DLL "libraptorhtml.so"
#define NETLIB_DLL "libnetlib.so"
#define PARSER_DLL "libraptorhtmlpars.so"
#define RDF_DLL "librdf.so"
#define XPCOM_DLL "libxpcom.so"
#elif defined(XP_MAC)
#define DOM_DLL "DOM_DLL"
#define LAYOUT_DLL "LAYOUT_DLL"
#define NETLIB_DLL "NETLIB_DLL"
#define PARSER_DLL "PARSER_DLL"
#define RDF_DLL "RDF_DLL"
#define XPCOM_DLL "XPCOM_DLL"
#endif
////////////////////////////////////////////////////////////////////////
// CIDs
// rdf
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
// xpcom
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
static NS_DEFINE_CID(kGenericFactoryCID, NS_GENERICFACTORY_CID);
////////////////////////////////////////////////////////////////////////
// IIDs
NS_DEFINE_IID(kIEventQueueServiceIID, NS_IEVENTQUEUESERVICE_IID);
NS_DEFINE_IID(kIOutputStreamIID, NS_IOUTPUTSTREAM_IID);
NS_DEFINE_IID(kIRDFXMLDataSourceIID, NS_IRDFXMLDATASOURCE_IID);
NS_DEFINE_IID(kIRDFServiceIID, NS_IRDFSERVICE_IID);
NS_DEFINE_IID(kIRDFXMLSourceIID, NS_IRDFXMLSOURCE_IID);
1999-05-05 07:14:06 +04:00
#include "nsIAllocator.h" // for the CID
1999-04-02 22:33:34 +04:00
static nsresult
SetupRegistry(void)
{
// netlib
static NS_DEFINE_CID(kNetServiceCID, NS_NETSERVICE_CID);
nsComponentManager::RegisterComponent(kNetServiceCID, NULL, NULL, NETLIB_DLL, PR_FALSE, PR_FALSE);
// parser
static NS_DEFINE_CID(kParserCID, NS_PARSER_IID);
static NS_DEFINE_CID(kWellFormedDTDCID, NS_WELLFORMEDDTD_CID);
nsComponentManager::RegisterComponent(kParserCID, NULL, NULL, PARSER_DLL, PR_FALSE, PR_FALSE);
nsComponentManager::RegisterComponent(kWellFormedDTDCID, NULL, NULL, PARSER_DLL, PR_FALSE, PR_FALSE);
// layout
static NS_DEFINE_CID(kNameSpaceManagerCID, NS_NAMESPACEMANAGER_CID);
nsComponentManager::RegisterComponent(kNameSpaceManagerCID, NULL, NULL, LAYOUT_DLL, PR_FALSE, PR_FALSE);
// xpcom
1999-05-05 07:14:06 +04:00
static NS_DEFINE_CID(kAllocatorCID, NS_ALLOCATOR_CID);
1999-04-02 22:33:34 +04:00
nsComponentManager::RegisterComponent(kEventQueueServiceCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);
nsComponentManager::RegisterComponent(kGenericFactoryCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);
1999-05-05 07:14:06 +04:00
nsComponentManager::RegisterComponent(kAllocatorCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);
1999-04-02 22:33:34 +04:00
return NS_OK;
}
////////////////////////////////////////////////////////////////////////
class ConsoleOutputStreamImpl : public nsIOutputStream
{
public:
ConsoleOutputStreamImpl(void) { NS_INIT_REFCNT(); }
virtual ~ConsoleOutputStreamImpl(void) {}
// nsISupports interface
NS_DECL_ISUPPORTS
// nsIBaseStream interface
NS_IMETHOD Close(void) {
return NS_OK;
}
// nsIOutputStream interface
NS_IMETHOD Write(const char* aBuf, PRUint32 aCount, PRUint32 *aWriteCount) {
PR_Write(PR_GetSpecialFD(PR_StandardOutput), aBuf, aCount);
*aWriteCount = aCount;
return NS_OK;
}
NS_IMETHOD Write(nsIInputStream* fromStream, PRUint32 *aWriteCount) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHOD Flush(void) {
PR_Sync(PR_GetSpecialFD(PR_StandardOutput));
return NS_OK;
}
1999-04-02 22:33:34 +04:00
};
NS_IMPL_ISUPPORTS(ConsoleOutputStreamImpl, kIOutputStreamIID);
////////////////////////////////////////////////////////////////////////
int
main(int argc, char** argv)
{
nsresult rv;
if (argc < 2) {
1999-05-05 07:14:06 +04:00
fprintf(stderr, "usage: %s <url> [<poll-interval>]\n", argv[0]);
1999-04-02 22:33:34 +04:00
return 1;
}
SetupRegistry();
// Get netlib off the floor...
1999-05-05 07:14:06 +04:00
NS_WITH_SERVICE(nsIEventQueueService, theEventQueueService, kEventQueueServiceCID, &rv);
if (NS_FAILED(rv)) return rv;
1999-04-02 22:33:34 +04:00
1999-05-05 07:14:06 +04:00
rv = theEventQueueService->CreateThreadEventQueue();
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create thread event queue");
if (NS_FAILED(rv)) return rv;
1999-04-02 22:33:34 +04:00
// Create a stream data source and initialize it on argv[1], which
// is hopefully a "file:" URL. (Actually, we can do _any_ kind of
// URL, but only a "file:" URL will be written back to disk.)
1999-05-05 07:14:06 +04:00
nsCOMPtr<nsIRDFXMLDataSource> ds;
rv = nsComponentManager::CreateInstance(kRDFXMLDataSourceCID,
nsnull,
kIRDFXMLDataSourceIID,
getter_AddRefs(ds));
1999-04-02 22:33:34 +04:00
1999-05-05 07:14:06 +04:00
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create RDF/XML data source");
if (NS_FAILED(rv)) return rv;
rv = ds->Init(argv[1]);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to initialize data source");
if (NS_FAILED(rv)) return rv;
1999-04-02 22:33:34 +04:00
// Okay, this should load the XML file...
1999-05-05 07:14:06 +04:00
rv = ds->Open(PR_TRUE);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to open datasource");
if (NS_FAILED(rv)) return rv;
1999-04-02 22:33:34 +04:00
1999-05-05 07:14:06 +04:00
// And this should write it back out.
nsCOMPtr<nsIOutputStream> out =
do_QueryInterface(new ConsoleOutputStreamImpl());
1999-04-02 22:33:34 +04:00
1999-05-05 07:14:06 +04:00
NS_ASSERTION(out, "unable to create console output stream");
if (! out)
return NS_ERROR_OUT_OF_MEMORY;
1999-04-02 22:33:34 +04:00
1999-05-05 07:14:06 +04:00
nsCOMPtr<nsIRDFXMLSource> source = do_QueryInterface(ds);
NS_ASSERTION(source, "unable to RDF/XML interface");
if (! source)
return NS_ERROR_UNEXPECTED;
1999-04-02 22:33:34 +04:00
1999-05-05 07:14:06 +04:00
rv = source->Serialize(out);
NS_ASSERTION(NS_SUCCEEDED(rv), "error serializing");
if (NS_FAILED(rv)) return rv;
1999-04-02 22:33:34 +04:00
1999-05-05 07:14:06 +04:00
return NS_OK;
1999-04-02 22:33:34 +04:00
}