gecko-dev/rdf/tests/rdfcat/rdfcat.cpp

155 lines
4.5 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-05-21 11:12:37 +00:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
1999-04-02 18:33:34 +00:00
/*
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.
*/
#include <stdio.h>
#include "nsXPCOM.h"
1999-05-05 03:14:06 +00:00
#include "nsCOMPtr.h"
#include "nsIComponentManager.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsIIOService.h"
#include "nsIInputStream.h"
1999-04-02 18:33:34 +00:00
#include "nsIOutputStream.h"
#include "nsIRDFCompositeDataSource.h"
#include "nsIRDFNode.h"
#include "nsIRDFRemoteDataSource.h"
1999-04-02 18:33:34 +00:00
#include "nsIRDFService.h"
#include "nsIRDFXMLSource.h"
#include "nsIServiceManager.h"
#include "nsIStreamListener.h"
#include "nsIURL.h"
#include "nsRDFCID.h"
#include "nsThreadUtils.h"
1999-04-02 18:33:34 +00:00
#include "plstr.h"
#include "prio.h"
#include "prthread.h"
1999-04-02 18:33:34 +00:00
////////////////////////////////////////////////////////////////////////
// CIDs
// rdf
static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
////////////////////////////////////////////////////////////////////////
// Blatantly stolen from netwerk/test/
#define RETURN_IF_FAILED(rv, step) \
PR_BEGIN_MACRO \
if (NS_FAILED(rv)) { \
printf(">>> %s failed: rv=%x\n", step, static_cast<uint32_t>(rv)); \
return 1;\
} \
PR_END_MACRO
1999-04-02 18:33:34 +00:00
////////////////////////////////////////////////////////////////////////
class ConsoleOutputStreamImpl : public nsIOutputStream
{
public:
ConsoleOutputStreamImpl(void) {}
1999-04-02 18:33:34 +00:00
virtual ~ConsoleOutputStreamImpl(void) {}
// nsISupports interface
NS_DECL_ISUPPORTS
// nsIOutputStream interface
1999-04-02 18:33:34 +00:00
NS_IMETHOD Close(void) {
return NS_OK;
}
NS_IMETHOD Write(const char* aBuf, uint32_t aCount, uint32_t *aWriteCount) {
1999-04-02 18:33:34 +00:00
PR_Write(PR_GetSpecialFD(PR_StandardOutput), aBuf, aCount);
*aWriteCount = aCount;
return NS_OK;
}
NS_IMETHOD
WriteFrom(nsIInputStream *inStr, uint32_t count, uint32_t *_retval) {
NS_NOTREACHED("WriteFrom");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHOD
WriteSegments(nsReadSegmentFun reader, void * closure, uint32_t count, uint32_t *_retval) {
NS_NOTREACHED("WriteSegments");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHOD
IsNonBlocking(bool *aNonBlocking) {
NS_NOTREACHED("IsNonBlocking");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHOD Flush(void) {
PR_Sync(PR_GetSpecialFD(PR_StandardOutput));
return NS_OK;
}
1999-04-02 18:33:34 +00:00
};
NS_IMPL_ISUPPORTS1(ConsoleOutputStreamImpl, nsIOutputStream)
1999-04-02 18:33:34 +00:00
////////////////////////////////////////////////////////////////////////
int
main(int argc, char** argv)
{
nsresult rv;
if (argc < 2) {
fprintf(stderr, "usage: %s <url>\n", argv[0]);
1999-04-02 18:33:34 +00:00
return 1;
}
NS_InitXPCOM2(nullptr, nullptr, nullptr);
1999-04-02 18:33:34 +00:00
// Create a stream data source and initialize it on argv[1], which
// is hopefully a "file:" URL.
nsCOMPtr<nsIRDFDataSource> ds = do_CreateInstance(kRDFXMLDataSourceCID,
&rv);
RETURN_IF_FAILED(rv, "RDF/XML datasource creation");
nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(ds, &rv);
RETURN_IF_FAILED(rv, "QI to nsIRDFRemoteDataSource");
rv = remote->Init(argv[1]);
RETURN_IF_FAILED(rv, "datasource initialization");
1999-04-02 18:33:34 +00:00
// Okay, this should load the XML file...
rv = remote->Refresh(false);
RETURN_IF_FAILED(rv, "datasource refresh");
// Pump events until the load is finished
nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
bool done = false;
while (!done) {
NS_ENSURE_TRUE(NS_ProcessNextEvent(thread), 1);
remote->GetLoaded(&done);
}
1999-04-02 18:33:34 +00:00
// And this should write it back out. The do_QI() on the pointer
// is a hack to make sure that the new object gets AddRef()-ed.
1999-05-05 03:14:06 +00:00
nsCOMPtr<nsIOutputStream> out =
do_QueryInterface(new ConsoleOutputStreamImpl, &rv);
RETURN_IF_FAILED(rv, "creation of console output stream");
1999-04-02 18:33:34 +00:00
1999-05-05 03:14:06 +00:00
nsCOMPtr<nsIRDFXMLSource> source = do_QueryInterface(ds);
RETURN_IF_FAILED(rv, "QI to nsIRDFXMLSource");
1999-04-02 18:33:34 +00:00
1999-05-05 03:14:06 +00:00
rv = source->Serialize(out);
RETURN_IF_FAILED(rv, "datasoure serialization");
return 0;
1999-04-02 18:33:34 +00:00
}