Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /** |
michael@0 | 7 | * A simple test program that reads in RDF/XML into an in-memory data |
michael@0 | 8 | * source, then serializes NTriples format back to stdout (or none). |
michael@0 | 9 | * |
michael@0 | 10 | * The program takes a single parameter: the URL from which to read, |
michael@0 | 11 | * plus an optional parameter -q |
michael@0 | 12 | */ |
michael@0 | 13 | |
michael@0 | 14 | #include <stdio.h> |
michael@0 | 15 | #include "nsXPCOM.h" |
michael@0 | 16 | #include "nsCOMPtr.h" |
michael@0 | 17 | //#include "nsString.h" |
michael@0 | 18 | #include "nsIComponentManager.h" |
michael@0 | 19 | #include "nsComponentManagerUtils.h" |
michael@0 | 20 | #include "nsServiceManagerUtils.h" |
michael@0 | 21 | #include "nsThreadUtils.h" |
michael@0 | 22 | #include "nsIIOService.h" |
michael@0 | 23 | #include "nsIInputStream.h" |
michael@0 | 24 | #include "nsIOutputStream.h" |
michael@0 | 25 | #include "nsIRDFCompositeDataSource.h" |
michael@0 | 26 | #include "nsIRDFNode.h" |
michael@0 | 27 | #include "nsIRDFRemoteDataSource.h" |
michael@0 | 28 | #include "nsIRDFService.h" |
michael@0 | 29 | #include "nsIRDFXMLSource.h" |
michael@0 | 30 | #include "nsIServiceManager.h" |
michael@0 | 31 | #include "nsIStreamListener.h" |
michael@0 | 32 | #include "nsIURL.h" |
michael@0 | 33 | #include "nsRDFCID.h" |
michael@0 | 34 | #include "plstr.h" |
michael@0 | 35 | #include "prio.h" |
michael@0 | 36 | #include "prthread.h" |
michael@0 | 37 | |
michael@0 | 38 | #include "rdf.h" |
michael@0 | 39 | #include "rdfIDataSource.h" |
michael@0 | 40 | #include "rdfITripleVisitor.h" |
michael@0 | 41 | #include "rdfISerializer.h" |
michael@0 | 42 | |
michael@0 | 43 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 44 | // Blatantly stolen from netwerk/test/ |
michael@0 | 45 | #define RETURN_IF_FAILED(rv, step) \ |
michael@0 | 46 | PR_BEGIN_MACRO \ |
michael@0 | 47 | if (NS_FAILED(rv)) { \ |
michael@0 | 48 | printf(">>> %s failed: rv=%x\n", step, static_cast<uint32_t>(rv)); \ |
michael@0 | 49 | return 1;\ |
michael@0 | 50 | } \ |
michael@0 | 51 | PR_END_MACRO |
michael@0 | 52 | |
michael@0 | 53 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 54 | |
michael@0 | 55 | class ConsoleOutputStreamImpl : public nsIOutputStream |
michael@0 | 56 | { |
michael@0 | 57 | public: |
michael@0 | 58 | ConsoleOutputStreamImpl(void) {} |
michael@0 | 59 | virtual ~ConsoleOutputStreamImpl(void) {} |
michael@0 | 60 | |
michael@0 | 61 | // nsISupports interface |
michael@0 | 62 | NS_DECL_ISUPPORTS |
michael@0 | 63 | |
michael@0 | 64 | // nsIOutputStream interface |
michael@0 | 65 | NS_IMETHOD Close(void) { |
michael@0 | 66 | return NS_OK; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | NS_IMETHOD Write(const char* aBuf, uint32_t aCount, uint32_t *aWriteCount) { |
michael@0 | 70 | PR_Write(PR_GetSpecialFD(PR_StandardOutput), aBuf, aCount); |
michael@0 | 71 | *aWriteCount = aCount; |
michael@0 | 72 | return NS_OK; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | NS_IMETHOD |
michael@0 | 76 | WriteFrom(nsIInputStream *inStr, uint32_t count, uint32_t *_retval) { |
michael@0 | 77 | NS_NOTREACHED("WriteFrom"); |
michael@0 | 78 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | NS_IMETHOD |
michael@0 | 82 | WriteSegments(nsReadSegmentFun reader, void * closure, uint32_t count, uint32_t *_retval) { |
michael@0 | 83 | NS_NOTREACHED("WriteSegments"); |
michael@0 | 84 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | NS_IMETHOD |
michael@0 | 88 | IsNonBlocking(bool *aNonBlocking) { |
michael@0 | 89 | NS_NOTREACHED("IsNonBlocking"); |
michael@0 | 90 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | NS_IMETHOD Flush(void) { |
michael@0 | 94 | PR_Sync(PR_GetSpecialFD(PR_StandardOutput)); |
michael@0 | 95 | return NS_OK; |
michael@0 | 96 | } |
michael@0 | 97 | }; |
michael@0 | 98 | |
michael@0 | 99 | NS_IMPL_ISUPPORTS(ConsoleOutputStreamImpl, nsIOutputStream) |
michael@0 | 100 | |
michael@0 | 101 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 102 | |
michael@0 | 103 | int |
michael@0 | 104 | main(int argc, char** argv) |
michael@0 | 105 | { |
michael@0 | 106 | nsresult rv; |
michael@0 | 107 | |
michael@0 | 108 | if (argc < 2) { |
michael@0 | 109 | fprintf(stderr, "usage: %s <url>\n", argv[0]); |
michael@0 | 110 | return 1; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | NS_InitXPCOM2(nullptr, nullptr, nullptr); |
michael@0 | 114 | |
michael@0 | 115 | // Create a stream data source and initialize it on argv[1], which |
michael@0 | 116 | // is hopefully a "file:" URL. |
michael@0 | 117 | nsCOMPtr<nsIRDFDataSource> ds = |
michael@0 | 118 | do_CreateInstance(NS_RDF_DATASOURCE_CONTRACTID_PREFIX "xml-datasource", |
michael@0 | 119 | &rv); |
michael@0 | 120 | RETURN_IF_FAILED(rv, "RDF/XML datasource creation"); |
michael@0 | 121 | |
michael@0 | 122 | nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(ds, &rv); |
michael@0 | 123 | RETURN_IF_FAILED(rv, "QI to nsIRDFRemoteDataSource"); |
michael@0 | 124 | |
michael@0 | 125 | rv = remote->Init(argv[1]); |
michael@0 | 126 | RETURN_IF_FAILED(rv, "datasource initialization"); |
michael@0 | 127 | |
michael@0 | 128 | // Okay, this should load the XML file... |
michael@0 | 129 | rv = remote->Refresh(false); |
michael@0 | 130 | RETURN_IF_FAILED(rv, "datasource refresh"); |
michael@0 | 131 | |
michael@0 | 132 | // Pump events until the load is finished |
michael@0 | 133 | nsCOMPtr<nsIThread> thread = do_GetCurrentThread(); |
michael@0 | 134 | bool done = false; |
michael@0 | 135 | while (!done) { |
michael@0 | 136 | NS_ENSURE_TRUE(NS_ProcessNextEvent(thread), 1); |
michael@0 | 137 | remote->GetLoaded(&done); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | nsCOMPtr<rdfIDataSource> rdfds = do_QueryInterface(ds, &rv); |
michael@0 | 141 | RETURN_IF_FAILED(rv, "QI to rdIDataSource"); |
michael@0 | 142 | { |
michael@0 | 143 | nsCOMPtr<nsIOutputStream> out = new ConsoleOutputStreamImpl(); |
michael@0 | 144 | nsCOMPtr<rdfISerializer> ser = |
michael@0 | 145 | do_CreateInstance(NS_RDF_SERIALIZER "ntriples", &rv); |
michael@0 | 146 | RETURN_IF_FAILED(rv, "Creation of NTriples Serializer"); |
michael@0 | 147 | rv = ser->Serialize(rdfds, out); |
michael@0 | 148 | RETURN_IF_FAILED(rv, "Serialization to NTriples"); |
michael@0 | 149 | out->Close(); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | return 0; |
michael@0 | 153 | } |