2001-09-28 20:14:13 +00:00
|
|
|
/* -*- 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2003-12-12 23:02:12 +00:00
|
|
|
#include <stdio.h>
|
2005-11-09 14:40:19 +00:00
|
|
|
#include "nsXPCOM.h"
|
1999-05-05 03:14:06 +00:00
|
|
|
#include "nsCOMPtr.h"
|
1999-09-15 00:16:04 +00:00
|
|
|
#include "nsIComponentManager.h"
|
2005-11-09 14:40:19 +00:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
1999-06-18 17:34:08 +00:00
|
|
|
#include "nsIIOService.h"
|
1999-09-15 00:16:04 +00:00
|
|
|
#include "nsIInputStream.h"
|
1999-04-02 18:33:34 +00:00
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsIRDFCompositeDataSource.h"
|
|
|
|
#include "nsIRDFNode.h"
|
1999-09-15 00:16:04 +00:00
|
|
|
#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"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsThreadUtils.h"
|
1999-04-02 18:33:34 +00:00
|
|
|
#include "plstr.h"
|
1999-09-15 00:16:04 +00:00
|
|
|
#include "prio.h"
|
|
|
|
#include "prthread.h"
|
1999-04-02 18:33:34 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// CIDs
|
|
|
|
|
|
|
|
// rdf
|
|
|
|
static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
|
|
|
|
|
2001-12-21 23:48:07 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Blatantly stolen from netwerk/test/
|
|
|
|
#define RETURN_IF_FAILED(rv, step) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (NS_FAILED(rv)) { \
|
2012-10-15 21:12:50 +00:00
|
|
|
printf(">>> %s failed: rv=%x\n", step, static_cast<uint32_t>(rv)); \
|
2012-08-07 17:17:27 +00:00
|
|
|
return 1;\
|
2001-12-21 23:48:07 +00:00
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
1999-04-02 18:33:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class ConsoleOutputStreamImpl : public nsIOutputStream
|
|
|
|
{
|
|
|
|
public:
|
2003-01-08 22:45:23 +00:00
|
|
|
ConsoleOutputStreamImpl(void) {}
|
1999-04-02 18:33:34 +00:00
|
|
|
virtual ~ConsoleOutputStreamImpl(void) {}
|
|
|
|
|
|
|
|
// nsISupports interface
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2011-08-09 16:28:00 +00:00
|
|
|
// nsIOutputStream interface
|
1999-04-02 18:33:34 +00:00
|
|
|
NS_IMETHOD Close(void) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
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;
|
|
|
|
}
|
1999-04-22 07:31:03 +00:00
|
|
|
|
2000-08-22 07:03:33 +00:00
|
|
|
NS_IMETHOD
|
2012-08-22 15:56:38 +00:00
|
|
|
WriteFrom(nsIInputStream *inStr, uint32_t count, uint32_t *_retval) {
|
2000-08-22 07:03:33 +00:00
|
|
|
NS_NOTREACHED("WriteFrom");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2012-08-22 15:56:38 +00:00
|
|
|
WriteSegments(nsReadSegmentFun reader, void * closure, uint32_t count, uint32_t *_retval) {
|
2000-08-22 07:03:33 +00:00
|
|
|
NS_NOTREACHED("WriteSegments");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2011-09-29 06:19:26 +00:00
|
|
|
IsNonBlocking(bool *aNonBlocking) {
|
2002-03-12 00:59:06 +00:00
|
|
|
NS_NOTREACHED("IsNonBlocking");
|
2000-08-22 07:03:33 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-04-22 07:31:03 +00:00
|
|
|
NS_IMETHOD Flush(void) {
|
|
|
|
PR_Sync(PR_GetSpecialFD(PR_StandardOutput));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-04-02 18:33:34 +00:00
|
|
|
};
|
|
|
|
|
2000-11-17 20:54:21 +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) {
|
2001-12-21 23:48:07 +00:00
|
|
|
fprintf(stderr, "usage: %s <url>\n", argv[0]);
|
1999-04-02 18:33:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
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
|
1999-05-24 03:53:02 +00:00
|
|
|
// is hopefully a "file:" URL.
|
2001-12-21 23:48:07 +00:00
|
|
|
nsCOMPtr<nsIRDFDataSource> ds = do_CreateInstance(kRDFXMLDataSourceCID,
|
|
|
|
&rv);
|
|
|
|
RETURN_IF_FAILED(rv, "RDF/XML datasource creation");
|
1999-06-24 00:22:58 +00:00
|
|
|
|
2001-12-21 23:48:07 +00:00
|
|
|
nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(ds, &rv);
|
|
|
|
RETURN_IF_FAILED(rv, "QI to nsIRDFRemoteDataSource");
|
1999-06-24 00:22:58 +00:00
|
|
|
|
|
|
|
rv = remote->Init(argv[1]);
|
2001-12-21 23:48:07 +00:00
|
|
|
RETURN_IF_FAILED(rv, "datasource initialization");
|
1999-04-02 18:33:34 +00:00
|
|
|
|
|
|
|
// Okay, this should load the XML file...
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = remote->Refresh(false);
|
2001-12-21 23:48:07 +00:00
|
|
|
RETURN_IF_FAILED(rv, "datasource refresh");
|
|
|
|
|
|
|
|
// Pump events until the load is finished
|
2006-05-10 17:30:15 +00:00
|
|
|
nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
|
2011-09-29 06:19:26 +00:00
|
|
|
bool done = false;
|
2001-12-21 23:48:07 +00:00
|
|
|
while (!done) {
|
2012-08-07 17:17:27 +00:00
|
|
|
NS_ENSURE_TRUE(NS_ProcessNextEvent(thread), 1);
|
2001-12-21 23:48:07 +00:00
|
|
|
remote->GetLoaded(&done);
|
1999-09-15 00:16:04 +00:00
|
|
|
}
|
1999-04-02 18:33:34 +00:00
|
|
|
|
1999-06-17 19:47:21 +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 =
|
2001-12-21 23:48:07 +00:00
|
|
|
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);
|
2001-12-21 23:48:07 +00:00
|
|
|
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);
|
2001-12-21 23:48:07 +00:00
|
|
|
RETURN_IF_FAILED(rv, "datasoure serialization");
|
|
|
|
|
2012-08-07 17:17:27 +00:00
|
|
|
return 0;
|
1999-04-02 18:33:34 +00:00
|
|
|
}
|