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