Back to index

lightning-sunbird  0.9+nobinonly
rdfcat.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *   Chase Tingley <tingley@sundell.net>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /*
00041 
00042   A simple test program that reads in RDF/XML into an in-memory data
00043   source, then uses the RDF/XML serialization API to write an
00044   equivalent (but not identical) RDF/XML file back to stdout.
00045 
00046   The program takes a single parameter: the URL from which to read.
00047 
00048  */
00049 
00050 #include <stdio.h>
00051 #include "nsCOMPtr.h"
00052 #include "nsIComponentManager.h"
00053 #include "nsIEventQueueService.h"
00054 #include "nsIIOService.h"
00055 #include "nsIInputStream.h"
00056 #include "nsIOutputStream.h"
00057 #include "nsIRDFCompositeDataSource.h"
00058 #include "nsIRDFNode.h"
00059 #include "nsIRDFRemoteDataSource.h"
00060 #include "nsIRDFService.h"
00061 #include "nsIRDFXMLSource.h"
00062 #include "nsIServiceManager.h"
00063 #include "nsIStreamListener.h"
00064 #include "nsIURL.h"
00065 #include "nsRDFCID.h"
00066 #include "plevent.h"
00067 #include "plstr.h"
00068 #include "prio.h"
00069 #include "prthread.h"
00070 
00072 // CIDs
00073 
00074 // rdf
00075 static NS_DEFINE_CID(kRDFXMLDataSourceCID,  NS_RDFXMLDATASOURCE_CID);
00076 
00077 // xpcom
00078 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00079 
00081 // Blatantly stolen from netwerk/test/
00082 #define RETURN_IF_FAILED(rv, step) \
00083     PR_BEGIN_MACRO \
00084     if (NS_FAILED(rv)) { \
00085         printf(">>> %s failed: rv=%x\n", step, rv); \
00086         return rv;\
00087     } \
00088     PR_END_MACRO
00089 
00091 
00092 class ConsoleOutputStreamImpl : public nsIOutputStream
00093 {
00094 public:
00095     ConsoleOutputStreamImpl(void) {}
00096     virtual ~ConsoleOutputStreamImpl(void) {}
00097 
00098     // nsISupports interface
00099     NS_DECL_ISUPPORTS
00100 
00101     // nsIBaseStream interface
00102     NS_IMETHOD Close(void) {
00103         return NS_OK;
00104     }
00105 
00106     // nsIOutputStream interface
00107     NS_IMETHOD Write(const char* aBuf, PRUint32 aCount, PRUint32 *aWriteCount) {
00108         PR_Write(PR_GetSpecialFD(PR_StandardOutput), aBuf, aCount);
00109         *aWriteCount = aCount;
00110         return NS_OK;
00111     }
00112 
00113     NS_IMETHOD
00114     WriteFrom(nsIInputStream *inStr, PRUint32 count, PRUint32 *_retval) {
00115         NS_NOTREACHED("WriteFrom");
00116         return NS_ERROR_NOT_IMPLEMENTED;
00117     }
00118 
00119     NS_IMETHOD
00120     WriteSegments(nsReadSegmentFun reader, void * closure, PRUint32 count, PRUint32 *_retval) {
00121         NS_NOTREACHED("WriteSegments");
00122         return NS_ERROR_NOT_IMPLEMENTED;
00123     }
00124 
00125     NS_IMETHOD
00126     IsNonBlocking(PRBool *aNonBlocking) {
00127         NS_NOTREACHED("IsNonBlocking");
00128         return NS_ERROR_NOT_IMPLEMENTED;
00129     }
00130 
00131     NS_IMETHOD Flush(void) {
00132         PR_Sync(PR_GetSpecialFD(PR_StandardOutput));
00133         return NS_OK;
00134     }
00135 };
00136 
00137 NS_IMPL_ISUPPORTS1(ConsoleOutputStreamImpl, nsIOutputStream)
00138 
00139 
00140 
00141 int
00142 main(int argc, char** argv)
00143 {
00144     nsresult rv;
00145 
00146     if (argc < 2) {
00147         fprintf(stderr, "usage: %s <url>\n", argv[0]);
00148         return 1;
00149     }
00150 
00151     NS_InitXPCOM2(nsnull, nsnull, nsnull);
00152 
00153     // Get netlib off the floor...
00154     nsCOMPtr<nsIEventQueueService> theEventQueueService = 
00155              do_GetService(kEventQueueServiceCID, &rv);
00156     RETURN_IF_FAILED(rv, "EventQueueService");
00157 
00158     nsIEventQueue* eq = nsnull;
00159     rv = theEventQueueService->GetThreadEventQueue(NS_CURRENT_THREAD, &eq);
00160     RETURN_IF_FAILED(rv, "GetThreadEventQueue");
00161 
00162     // Create a stream data source and initialize it on argv[1], which
00163     // is hopefully a "file:" URL.
00164     nsCOMPtr<nsIRDFDataSource> ds = do_CreateInstance(kRDFXMLDataSourceCID,
00165                                                       &rv);
00166     RETURN_IF_FAILED(rv, "RDF/XML datasource creation");
00167 
00168     nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(ds, &rv);
00169     RETURN_IF_FAILED(rv, "QI to nsIRDFRemoteDataSource");
00170 
00171     rv = remote->Init(argv[1]);
00172     RETURN_IF_FAILED(rv, "datasource initialization");
00173 
00174     // Okay, this should load the XML file...
00175     rv = remote->Refresh(PR_FALSE);
00176     RETURN_IF_FAILED(rv, "datasource refresh");
00177 
00178     // Pump events until the load is finished
00179     PRBool done = PR_FALSE;
00180     while (!done) {
00181         eq->ProcessPendingEvents();
00182         remote->GetLoaded(&done);
00183     }
00184 
00185     // And this should write it back out. The do_QI() on the pointer
00186     // is a hack to make sure that the new object gets AddRef()-ed.
00187     nsCOMPtr<nsIOutputStream> out =
00188         do_QueryInterface(new ConsoleOutputStreamImpl, &rv);
00189     RETURN_IF_FAILED(rv, "creation of console output stream");
00190 
00191     nsCOMPtr<nsIRDFXMLSource> source = do_QueryInterface(ds);
00192     RETURN_IF_FAILED(rv, "QI to nsIRDFXMLSource");
00193 
00194     rv = source->Serialize(out);
00195     RETURN_IF_FAILED(rv, "datasoure serialization");
00196 
00197     theEventQueueService->DestroyThreadEventQueue();
00198 
00199     return NS_OK;
00200 }