1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
24 * Chase Tingley <tingley@sundell.net>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
42 A simple test program that reads in RDF/XML into an in-memory data
43 source, then uses the RDF/XML serialization API to write an
44 equivalent (but not identical) RDF/XML file back to stdout.
46 The program takes a single parameter: the URL from which to read.
53 #include "nsIComponentManager.h"
54 #include "nsComponentManagerUtils.h"
55 #include "nsServiceManagerUtils.h"
56 #include "nsIIOService.h"
57 #include "nsIInputStream.h"
58 #include "nsIOutputStream.h"
59 #include "nsIRDFCompositeDataSource.h"
60 #include "nsIRDFNode.h"
61 #include "nsIRDFRemoteDataSource.h"
62 #include "nsIRDFService.h"
63 #include "nsIRDFXMLSource.h"
64 #include "nsIServiceManager.h"
65 #include "nsIStreamListener.h"
68 #include "nsThreadUtils.h"
73 ////////////////////////////////////////////////////////////////////////
77 static NS_DEFINE_CID(kRDFXMLDataSourceCID
, NS_RDFXMLDATASOURCE_CID
);
79 ////////////////////////////////////////////////////////////////////////
80 // Blatantly stolen from netwerk/test/
81 #define RETURN_IF_FAILED(rv, step) \
83 if (NS_FAILED(rv)) { \
84 printf(">>> %s failed: rv=%x\n", step, rv); \
89 ////////////////////////////////////////////////////////////////////////
91 class ConsoleOutputStreamImpl
: public nsIOutputStream
94 ConsoleOutputStreamImpl(void) {}
95 virtual ~ConsoleOutputStreamImpl(void) {}
97 // nsISupports interface
100 // nsIBaseStream interface
101 NS_IMETHOD
Close(void) {
105 // nsIOutputStream interface
106 NS_IMETHOD
Write(const char* aBuf
, PRUint32 aCount
, PRUint32
*aWriteCount
) {
107 PR_Write(PR_GetSpecialFD(PR_StandardOutput
), aBuf
, aCount
);
108 *aWriteCount
= aCount
;
113 WriteFrom(nsIInputStream
*inStr
, PRUint32 count
, PRUint32
*_retval
) {
114 NS_NOTREACHED("WriteFrom");
115 return NS_ERROR_NOT_IMPLEMENTED
;
119 WriteSegments(nsReadSegmentFun reader
, void * closure
, PRUint32 count
, PRUint32
*_retval
) {
120 NS_NOTREACHED("WriteSegments");
121 return NS_ERROR_NOT_IMPLEMENTED
;
125 IsNonBlocking(PRBool
*aNonBlocking
) {
126 NS_NOTREACHED("IsNonBlocking");
127 return NS_ERROR_NOT_IMPLEMENTED
;
130 NS_IMETHOD
Flush(void) {
131 PR_Sync(PR_GetSpecialFD(PR_StandardOutput
));
136 NS_IMPL_ISUPPORTS1(ConsoleOutputStreamImpl
, nsIOutputStream
)
138 ////////////////////////////////////////////////////////////////////////
141 main(int argc
, char** argv
)
146 fprintf(stderr
, "usage: %s <url>\n", argv
[0]);
150 NS_InitXPCOM2(nsnull
, nsnull
, nsnull
);
152 // Create a stream data source and initialize it on argv[1], which
153 // is hopefully a "file:" URL.
154 nsCOMPtr
<nsIRDFDataSource
> ds
= do_CreateInstance(kRDFXMLDataSourceCID
,
156 RETURN_IF_FAILED(rv
, "RDF/XML datasource creation");
158 nsCOMPtr
<nsIRDFRemoteDataSource
> remote
= do_QueryInterface(ds
, &rv
);
159 RETURN_IF_FAILED(rv
, "QI to nsIRDFRemoteDataSource");
161 rv
= remote
->Init(argv
[1]);
162 RETURN_IF_FAILED(rv
, "datasource initialization");
164 // Okay, this should load the XML file...
165 rv
= remote
->Refresh(PR_FALSE
);
166 RETURN_IF_FAILED(rv
, "datasource refresh");
168 // Pump events until the load is finished
169 nsCOMPtr
<nsIThread
> thread
= do_GetCurrentThread();
170 PRBool done
= PR_FALSE
;
172 NS_ENSURE_STATE(NS_ProcessNextEvent(thread
));
173 remote
->GetLoaded(&done
);
176 // And this should write it back out. The do_QI() on the pointer
177 // is a hack to make sure that the new object gets AddRef()-ed.
178 nsCOMPtr
<nsIOutputStream
> out
=
179 do_QueryInterface(new ConsoleOutputStreamImpl
, &rv
);
180 RETURN_IF_FAILED(rv
, "creation of console output stream");
182 nsCOMPtr
<nsIRDFXMLSource
> source
= do_QueryInterface(ds
);
183 RETURN_IF_FAILED(rv
, "QI to nsIRDFXMLSource");
185 rv
= source
->Serialize(out
);
186 RETURN_IF_FAILED(rv
, "datasoure serialization");