1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/rdf/tests/triplescat/triplescat.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,153 @@ 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 + * A simple test program that reads in RDF/XML into an in-memory data 1.11 + * source, then serializes NTriples format back to stdout (or none). 1.12 + * 1.13 + * The program takes a single parameter: the URL from which to read, 1.14 + * plus an optional parameter -q 1.15 + */ 1.16 + 1.17 +#include <stdio.h> 1.18 +#include "nsXPCOM.h" 1.19 +#include "nsCOMPtr.h" 1.20 +//#include "nsString.h" 1.21 +#include "nsIComponentManager.h" 1.22 +#include "nsComponentManagerUtils.h" 1.23 +#include "nsServiceManagerUtils.h" 1.24 +#include "nsThreadUtils.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 "plstr.h" 1.38 +#include "prio.h" 1.39 +#include "prthread.h" 1.40 + 1.41 +#include "rdf.h" 1.42 +#include "rdfIDataSource.h" 1.43 +#include "rdfITripleVisitor.h" 1.44 +#include "rdfISerializer.h" 1.45 + 1.46 +//////////////////////////////////////////////////////////////////////// 1.47 +// Blatantly stolen from netwerk/test/ 1.48 +#define RETURN_IF_FAILED(rv, step) \ 1.49 + PR_BEGIN_MACRO \ 1.50 + if (NS_FAILED(rv)) { \ 1.51 + printf(">>> %s failed: rv=%x\n", step, static_cast<uint32_t>(rv)); \ 1.52 + return 1;\ 1.53 + } \ 1.54 + PR_END_MACRO 1.55 + 1.56 +//////////////////////////////////////////////////////////////////////// 1.57 + 1.58 +class ConsoleOutputStreamImpl : public nsIOutputStream 1.59 +{ 1.60 +public: 1.61 + ConsoleOutputStreamImpl(void) {} 1.62 + virtual ~ConsoleOutputStreamImpl(void) {} 1.63 + 1.64 + // nsISupports interface 1.65 + NS_DECL_ISUPPORTS 1.66 + 1.67 + // nsIOutputStream interface 1.68 + NS_IMETHOD Close(void) { 1.69 + return NS_OK; 1.70 + } 1.71 + 1.72 + NS_IMETHOD Write(const char* aBuf, uint32_t aCount, uint32_t *aWriteCount) { 1.73 + PR_Write(PR_GetSpecialFD(PR_StandardOutput), aBuf, aCount); 1.74 + *aWriteCount = aCount; 1.75 + return NS_OK; 1.76 + } 1.77 + 1.78 + NS_IMETHOD 1.79 + WriteFrom(nsIInputStream *inStr, uint32_t count, uint32_t *_retval) { 1.80 + NS_NOTREACHED("WriteFrom"); 1.81 + return NS_ERROR_NOT_IMPLEMENTED; 1.82 + } 1.83 + 1.84 + NS_IMETHOD 1.85 + WriteSegments(nsReadSegmentFun reader, void * closure, uint32_t count, uint32_t *_retval) { 1.86 + NS_NOTREACHED("WriteSegments"); 1.87 + return NS_ERROR_NOT_IMPLEMENTED; 1.88 + } 1.89 + 1.90 + NS_IMETHOD 1.91 + IsNonBlocking(bool *aNonBlocking) { 1.92 + NS_NOTREACHED("IsNonBlocking"); 1.93 + return NS_ERROR_NOT_IMPLEMENTED; 1.94 + } 1.95 + 1.96 + NS_IMETHOD Flush(void) { 1.97 + PR_Sync(PR_GetSpecialFD(PR_StandardOutput)); 1.98 + return NS_OK; 1.99 + } 1.100 +}; 1.101 + 1.102 +NS_IMPL_ISUPPORTS(ConsoleOutputStreamImpl, nsIOutputStream) 1.103 + 1.104 +//////////////////////////////////////////////////////////////////////// 1.105 + 1.106 +int 1.107 +main(int argc, char** argv) 1.108 +{ 1.109 + nsresult rv; 1.110 + 1.111 + if (argc < 2) { 1.112 + fprintf(stderr, "usage: %s <url>\n", argv[0]); 1.113 + return 1; 1.114 + } 1.115 + 1.116 + NS_InitXPCOM2(nullptr, nullptr, nullptr); 1.117 + 1.118 + // Create a stream data source and initialize it on argv[1], which 1.119 + // is hopefully a "file:" URL. 1.120 + nsCOMPtr<nsIRDFDataSource> ds = 1.121 + do_CreateInstance(NS_RDF_DATASOURCE_CONTRACTID_PREFIX "xml-datasource", 1.122 + &rv); 1.123 + RETURN_IF_FAILED(rv, "RDF/XML datasource creation"); 1.124 + 1.125 + nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(ds, &rv); 1.126 + RETURN_IF_FAILED(rv, "QI to nsIRDFRemoteDataSource"); 1.127 + 1.128 + rv = remote->Init(argv[1]); 1.129 + RETURN_IF_FAILED(rv, "datasource initialization"); 1.130 + 1.131 + // Okay, this should load the XML file... 1.132 + rv = remote->Refresh(false); 1.133 + RETURN_IF_FAILED(rv, "datasource refresh"); 1.134 + 1.135 + // Pump events until the load is finished 1.136 + nsCOMPtr<nsIThread> thread = do_GetCurrentThread(); 1.137 + bool done = false; 1.138 + while (!done) { 1.139 + NS_ENSURE_TRUE(NS_ProcessNextEvent(thread), 1); 1.140 + remote->GetLoaded(&done); 1.141 + } 1.142 + 1.143 + nsCOMPtr<rdfIDataSource> rdfds = do_QueryInterface(ds, &rv); 1.144 + RETURN_IF_FAILED(rv, "QI to rdIDataSource"); 1.145 + { 1.146 + nsCOMPtr<nsIOutputStream> out = new ConsoleOutputStreamImpl(); 1.147 + nsCOMPtr<rdfISerializer> ser = 1.148 + do_CreateInstance(NS_RDF_SERIALIZER "ntriples", &rv); 1.149 + RETURN_IF_FAILED(rv, "Creation of NTriples Serializer"); 1.150 + rv = ser->Serialize(rdfds, out); 1.151 + RETURN_IF_FAILED(rv, "Serialization to NTriples"); 1.152 + out->Close(); 1.153 + } 1.154 + 1.155 + return 0; 1.156 +}