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 | |
michael@0 | 8 | A simple test program that reads in RDF/XML into an in-memory data |
michael@0 | 9 | source, then uses the RDF/XML serialization API to write an |
michael@0 | 10 | equivalent (but not identical) RDF/XML file back to stdout. |
michael@0 | 11 | |
michael@0 | 12 | The program takes a single parameter: the URL from which to read. |
michael@0 | 13 | |
michael@0 | 14 | */ |
michael@0 | 15 | |
michael@0 | 16 | #include <stdio.h> |
michael@0 | 17 | #include "nsXPCOM.h" |
michael@0 | 18 | #include "nsCOMPtr.h" |
michael@0 | 19 | #include "nsIComponentManager.h" |
michael@0 | 20 | #include "nsComponentManagerUtils.h" |
michael@0 | 21 | #include "nsServiceManagerUtils.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 "nsThreadUtils.h" |
michael@0 | 35 | #include "plstr.h" |
michael@0 | 36 | #include "prio.h" |
michael@0 | 37 | #include "prthread.h" |
michael@0 | 38 | |
michael@0 | 39 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 40 | // CIDs |
michael@0 | 41 | |
michael@0 | 42 | // rdf |
michael@0 | 43 | static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID); |
michael@0 | 44 | |
michael@0 | 45 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 46 | // Blatantly stolen from netwerk/test/ |
michael@0 | 47 | #define RETURN_IF_FAILED(rv, step) \ |
michael@0 | 48 | PR_BEGIN_MACRO \ |
michael@0 | 49 | if (NS_FAILED(rv)) { \ |
michael@0 | 50 | printf(">>> %s failed: rv=%x\n", step, static_cast<uint32_t>(rv)); \ |
michael@0 | 51 | return 1;\ |
michael@0 | 52 | } \ |
michael@0 | 53 | PR_END_MACRO |
michael@0 | 54 | |
michael@0 | 55 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 56 | |
michael@0 | 57 | class ConsoleOutputStreamImpl : public nsIOutputStream |
michael@0 | 58 | { |
michael@0 | 59 | public: |
michael@0 | 60 | ConsoleOutputStreamImpl(void) {} |
michael@0 | 61 | virtual ~ConsoleOutputStreamImpl(void) {} |
michael@0 | 62 | |
michael@0 | 63 | // nsISupports interface |
michael@0 | 64 | NS_DECL_ISUPPORTS |
michael@0 | 65 | |
michael@0 | 66 | // nsIOutputStream interface |
michael@0 | 67 | NS_IMETHOD Close(void) { |
michael@0 | 68 | return NS_OK; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | NS_IMETHOD Write(const char* aBuf, uint32_t aCount, uint32_t *aWriteCount) { |
michael@0 | 72 | PR_Write(PR_GetSpecialFD(PR_StandardOutput), aBuf, aCount); |
michael@0 | 73 | *aWriteCount = aCount; |
michael@0 | 74 | return NS_OK; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | NS_IMETHOD |
michael@0 | 78 | WriteFrom(nsIInputStream *inStr, uint32_t count, uint32_t *_retval) { |
michael@0 | 79 | NS_NOTREACHED("WriteFrom"); |
michael@0 | 80 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | NS_IMETHOD |
michael@0 | 84 | WriteSegments(nsReadSegmentFun reader, void * closure, uint32_t count, uint32_t *_retval) { |
michael@0 | 85 | NS_NOTREACHED("WriteSegments"); |
michael@0 | 86 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | NS_IMETHOD |
michael@0 | 90 | IsNonBlocking(bool *aNonBlocking) { |
michael@0 | 91 | NS_NOTREACHED("IsNonBlocking"); |
michael@0 | 92 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | NS_IMETHOD Flush(void) { |
michael@0 | 96 | PR_Sync(PR_GetSpecialFD(PR_StandardOutput)); |
michael@0 | 97 | return NS_OK; |
michael@0 | 98 | } |
michael@0 | 99 | }; |
michael@0 | 100 | |
michael@0 | 101 | NS_IMPL_ISUPPORTS(ConsoleOutputStreamImpl, nsIOutputStream) |
michael@0 | 102 | |
michael@0 | 103 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 104 | |
michael@0 | 105 | int |
michael@0 | 106 | main(int argc, char** argv) |
michael@0 | 107 | { |
michael@0 | 108 | nsresult rv; |
michael@0 | 109 | |
michael@0 | 110 | if (argc < 2) { |
michael@0 | 111 | fprintf(stderr, "usage: %s <url>\n", argv[0]); |
michael@0 | 112 | return 1; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | NS_InitXPCOM2(nullptr, nullptr, nullptr); |
michael@0 | 116 | |
michael@0 | 117 | // Create a stream data source and initialize it on argv[1], which |
michael@0 | 118 | // is hopefully a "file:" URL. |
michael@0 | 119 | nsCOMPtr<nsIRDFDataSource> ds = do_CreateInstance(kRDFXMLDataSourceCID, |
michael@0 | 120 | &rv); |
michael@0 | 121 | RETURN_IF_FAILED(rv, "RDF/XML datasource creation"); |
michael@0 | 122 | |
michael@0 | 123 | nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(ds, &rv); |
michael@0 | 124 | RETURN_IF_FAILED(rv, "QI to nsIRDFRemoteDataSource"); |
michael@0 | 125 | |
michael@0 | 126 | rv = remote->Init(argv[1]); |
michael@0 | 127 | RETURN_IF_FAILED(rv, "datasource initialization"); |
michael@0 | 128 | |
michael@0 | 129 | // Okay, this should load the XML file... |
michael@0 | 130 | rv = remote->Refresh(false); |
michael@0 | 131 | RETURN_IF_FAILED(rv, "datasource refresh"); |
michael@0 | 132 | |
michael@0 | 133 | // Pump events until the load is finished |
michael@0 | 134 | nsCOMPtr<nsIThread> thread = do_GetCurrentThread(); |
michael@0 | 135 | bool done = false; |
michael@0 | 136 | while (!done) { |
michael@0 | 137 | NS_ENSURE_TRUE(NS_ProcessNextEvent(thread), 1); |
michael@0 | 138 | remote->GetLoaded(&done); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | // And this should write it back out. The do_QI() on the pointer |
michael@0 | 142 | // is a hack to make sure that the new object gets AddRef()-ed. |
michael@0 | 143 | nsCOMPtr<nsIOutputStream> out = |
michael@0 | 144 | do_QueryInterface(new ConsoleOutputStreamImpl, &rv); |
michael@0 | 145 | RETURN_IF_FAILED(rv, "creation of console output stream"); |
michael@0 | 146 | |
michael@0 | 147 | nsCOMPtr<nsIRDFXMLSource> source = do_QueryInterface(ds); |
michael@0 | 148 | RETURN_IF_FAILED(rv, "QI to nsIRDFXMLSource"); |
michael@0 | 149 | |
michael@0 | 150 | rv = source->Serialize(out); |
michael@0 | 151 | RETURN_IF_FAILED(rv, "datasoure serialization"); |
michael@0 | 152 | |
michael@0 | 153 | return 0; |
michael@0 | 154 | } |