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