1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/rdf/tests/rdfcat/rdfcat.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,154 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + 1.11 + A simple test program that reads in RDF/XML into an in-memory data 1.12 + source, then uses the RDF/XML serialization API to write an 1.13 + equivalent (but not identical) RDF/XML file back to stdout. 1.14 + 1.15 + The program takes a single parameter: the URL from which to read. 1.16 + 1.17 + */ 1.18 + 1.19 +#include <stdio.h> 1.20 +#include "nsXPCOM.h" 1.21 +#include "nsCOMPtr.h" 1.22 +#include "nsIComponentManager.h" 1.23 +#include "nsComponentManagerUtils.h" 1.24 +#include "nsServiceManagerUtils.h" 1.25 +#include "nsIIOService.h" 1.26 +#include "nsIInputStream.h" 1.27 +#include "nsIOutputStream.h" 1.28 +#include "nsIRDFCompositeDataSource.h" 1.29 +#include "nsIRDFNode.h" 1.30 +#include "nsIRDFRemoteDataSource.h" 1.31 +#include "nsIRDFService.h" 1.32 +#include "nsIRDFXMLSource.h" 1.33 +#include "nsIServiceManager.h" 1.34 +#include "nsIStreamListener.h" 1.35 +#include "nsIURL.h" 1.36 +#include "nsRDFCID.h" 1.37 +#include "nsThreadUtils.h" 1.38 +#include "plstr.h" 1.39 +#include "prio.h" 1.40 +#include "prthread.h" 1.41 + 1.42 +//////////////////////////////////////////////////////////////////////// 1.43 +// CIDs 1.44 + 1.45 +// rdf 1.46 +static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID); 1.47 + 1.48 +//////////////////////////////////////////////////////////////////////// 1.49 +// Blatantly stolen from netwerk/test/ 1.50 +#define RETURN_IF_FAILED(rv, step) \ 1.51 + PR_BEGIN_MACRO \ 1.52 + if (NS_FAILED(rv)) { \ 1.53 + printf(">>> %s failed: rv=%x\n", step, static_cast<uint32_t>(rv)); \ 1.54 + return 1;\ 1.55 + } \ 1.56 + PR_END_MACRO 1.57 + 1.58 +//////////////////////////////////////////////////////////////////////// 1.59 + 1.60 +class ConsoleOutputStreamImpl : public nsIOutputStream 1.61 +{ 1.62 +public: 1.63 + ConsoleOutputStreamImpl(void) {} 1.64 + virtual ~ConsoleOutputStreamImpl(void) {} 1.65 + 1.66 + // nsISupports interface 1.67 + NS_DECL_ISUPPORTS 1.68 + 1.69 + // nsIOutputStream interface 1.70 + NS_IMETHOD Close(void) { 1.71 + return NS_OK; 1.72 + } 1.73 + 1.74 + NS_IMETHOD Write(const char* aBuf, uint32_t aCount, uint32_t *aWriteCount) { 1.75 + PR_Write(PR_GetSpecialFD(PR_StandardOutput), aBuf, aCount); 1.76 + *aWriteCount = aCount; 1.77 + return NS_OK; 1.78 + } 1.79 + 1.80 + NS_IMETHOD 1.81 + WriteFrom(nsIInputStream *inStr, uint32_t count, uint32_t *_retval) { 1.82 + NS_NOTREACHED("WriteFrom"); 1.83 + return NS_ERROR_NOT_IMPLEMENTED; 1.84 + } 1.85 + 1.86 + NS_IMETHOD 1.87 + WriteSegments(nsReadSegmentFun reader, void * closure, uint32_t count, uint32_t *_retval) { 1.88 + NS_NOTREACHED("WriteSegments"); 1.89 + return NS_ERROR_NOT_IMPLEMENTED; 1.90 + } 1.91 + 1.92 + NS_IMETHOD 1.93 + IsNonBlocking(bool *aNonBlocking) { 1.94 + NS_NOTREACHED("IsNonBlocking"); 1.95 + return NS_ERROR_NOT_IMPLEMENTED; 1.96 + } 1.97 + 1.98 + NS_IMETHOD Flush(void) { 1.99 + PR_Sync(PR_GetSpecialFD(PR_StandardOutput)); 1.100 + return NS_OK; 1.101 + } 1.102 +}; 1.103 + 1.104 +NS_IMPL_ISUPPORTS(ConsoleOutputStreamImpl, nsIOutputStream) 1.105 + 1.106 +//////////////////////////////////////////////////////////////////////// 1.107 + 1.108 +int 1.109 +main(int argc, char** argv) 1.110 +{ 1.111 + nsresult rv; 1.112 + 1.113 + if (argc < 2) { 1.114 + fprintf(stderr, "usage: %s <url>\n", argv[0]); 1.115 + return 1; 1.116 + } 1.117 + 1.118 + NS_InitXPCOM2(nullptr, nullptr, nullptr); 1.119 + 1.120 + // Create a stream data source and initialize it on argv[1], which 1.121 + // is hopefully a "file:" URL. 1.122 + nsCOMPtr<nsIRDFDataSource> ds = do_CreateInstance(kRDFXMLDataSourceCID, 1.123 + &rv); 1.124 + RETURN_IF_FAILED(rv, "RDF/XML datasource creation"); 1.125 + 1.126 + nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(ds, &rv); 1.127 + RETURN_IF_FAILED(rv, "QI to nsIRDFRemoteDataSource"); 1.128 + 1.129 + rv = remote->Init(argv[1]); 1.130 + RETURN_IF_FAILED(rv, "datasource initialization"); 1.131 + 1.132 + // Okay, this should load the XML file... 1.133 + rv = remote->Refresh(false); 1.134 + RETURN_IF_FAILED(rv, "datasource refresh"); 1.135 + 1.136 + // Pump events until the load is finished 1.137 + nsCOMPtr<nsIThread> thread = do_GetCurrentThread(); 1.138 + bool done = false; 1.139 + while (!done) { 1.140 + NS_ENSURE_TRUE(NS_ProcessNextEvent(thread), 1); 1.141 + remote->GetLoaded(&done); 1.142 + } 1.143 + 1.144 + // And this should write it back out. The do_QI() on the pointer 1.145 + // is a hack to make sure that the new object gets AddRef()-ed. 1.146 + nsCOMPtr<nsIOutputStream> out = 1.147 + do_QueryInterface(new ConsoleOutputStreamImpl, &rv); 1.148 + RETURN_IF_FAILED(rv, "creation of console output stream"); 1.149 + 1.150 + nsCOMPtr<nsIRDFXMLSource> source = do_QueryInterface(ds); 1.151 + RETURN_IF_FAILED(rv, "QI to nsIRDFXMLSource"); 1.152 + 1.153 + rv = source->Serialize(out); 1.154 + RETURN_IF_FAILED(rv, "datasoure serialization"); 1.155 + 1.156 + return 0; 1.157 +}