Back to index

lightning-sunbird  0.9+nobinonly
triplescat.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  *   Axel Hecht <axel@pike.org>
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 
00048 #include <stdio.h>
00049 #include "nsXPCOM.h"
00050 #include "nsCOMPtr.h"
00051 //#include "nsString.h"
00052 #include "nsIComponentManager.h"
00053 #include "nsComponentManagerUtils.h"
00054 #include "nsServiceManagerUtils.h"
00055 #include "nsIEventQueueService.h"
00056 #include "nsIIOService.h"
00057 #include "nsIInputStream.h"
00058 #include "nsIOutputStream.h"
00059 #include "nsIRDFCompositeDataSource.h"
00060 #include "nsIRDFNode.h"
00061 #include "nsIRDFRemoteDataSource.h"
00062 #include "nsIRDFService.h"
00063 #include "nsIRDFXMLSource.h"
00064 #include "nsIServiceManager.h"
00065 #include "nsIStreamListener.h"
00066 #include "nsIURL.h"
00067 #include "nsRDFCID.h"
00068 #include "plevent.h"
00069 #include "plstr.h"
00070 #include "prio.h"
00071 #include "prthread.h"
00072 
00073 #include "rdf.h"
00074 #include "rdfIDataSource.h"
00075 #include "rdfITripleVisitor.h"
00076 #include "rdfISerializer.h"
00077 
00079 // Blatantly stolen from netwerk/test/
00080 #define RETURN_IF_FAILED(rv, step) \
00081     PR_BEGIN_MACRO \
00082     if (NS_FAILED(rv)) { \
00083         printf(">>> %s failed: rv=%x\n", step, rv); \
00084         return rv;\
00085     } \
00086     PR_END_MACRO
00087 
00089 
00090 class ConsoleOutputStreamImpl : public nsIOutputStream
00091 {
00092 public:
00093     ConsoleOutputStreamImpl(void) {}
00094     virtual ~ConsoleOutputStreamImpl(void) {}
00095 
00096     // nsISupports interface
00097     NS_DECL_ISUPPORTS
00098 
00099     // nsIBaseStream interface
00100     NS_IMETHOD Close(void) {
00101         return NS_OK;
00102     }
00103 
00104     // nsIOutputStream interface
00105     NS_IMETHOD Write(const char* aBuf, PRUint32 aCount, PRUint32 *aWriteCount) {
00106         PR_Write(PR_GetSpecialFD(PR_StandardOutput), aBuf, aCount);
00107         *aWriteCount = aCount;
00108         return NS_OK;
00109     }
00110 
00111     NS_IMETHOD
00112     WriteFrom(nsIInputStream *inStr, PRUint32 count, PRUint32 *_retval) {
00113         NS_NOTREACHED("WriteFrom");
00114         return NS_ERROR_NOT_IMPLEMENTED;
00115     }
00116 
00117     NS_IMETHOD
00118     WriteSegments(nsReadSegmentFun reader, void * closure, PRUint32 count, PRUint32 *_retval) {
00119         NS_NOTREACHED("WriteSegments");
00120         return NS_ERROR_NOT_IMPLEMENTED;
00121     }
00122 
00123     NS_IMETHOD
00124     IsNonBlocking(PRBool *aNonBlocking) {
00125         NS_NOTREACHED("IsNonBlocking");
00126         return NS_ERROR_NOT_IMPLEMENTED;
00127     }
00128 
00129     NS_IMETHOD Flush(void) {
00130         PR_Sync(PR_GetSpecialFD(PR_StandardOutput));
00131         return NS_OK;
00132     }
00133 };
00134 
00135 NS_IMPL_ISUPPORTS1(ConsoleOutputStreamImpl, nsIOutputStream)
00136 
00137 
00138 
00139 int
00140 main(int argc, char** argv)
00141 {
00142     nsresult rv;
00143 
00144     if (argc < 2) {
00145         fprintf(stderr, "usage: %s <url>\n", argv[0]);
00146         return 1;
00147     }
00148 
00149     NS_InitXPCOM2(nsnull, nsnull, nsnull);
00150 
00151     // Get netlib off the floor...
00152     nsCOMPtr<nsIEventQueueService> theEventQueueService = 
00153              do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv);
00154     RETURN_IF_FAILED(rv, "EventQueueService");
00155 
00156     nsIEventQueue* eq = nsnull;
00157     rv = theEventQueueService->GetThreadEventQueue(NS_CURRENT_THREAD, &eq);
00158     RETURN_IF_FAILED(rv, "GetThreadEventQueue");
00159 
00160     // Create a stream data source and initialize it on argv[1], which
00161     // is hopefully a "file:" URL.
00162     nsCOMPtr<nsIRDFDataSource> ds =
00163         do_CreateInstance(NS_RDF_DATASOURCE_CONTRACTID_PREFIX "xml-datasource",
00164                           &rv);
00165     RETURN_IF_FAILED(rv, "RDF/XML datasource creation");
00166 
00167     nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(ds, &rv);
00168     RETURN_IF_FAILED(rv, "QI to nsIRDFRemoteDataSource");
00169 
00170     rv = remote->Init(argv[1]);
00171     RETURN_IF_FAILED(rv, "datasource initialization");
00172 
00173     // Okay, this should load the XML file...
00174     rv = remote->Refresh(PR_FALSE);
00175     RETURN_IF_FAILED(rv, "datasource refresh");
00176 
00177     // Pump events until the load is finished
00178     PRBool done = PR_FALSE;
00179     while (!done) {
00180         eq->ProcessPendingEvents();
00181         remote->GetLoaded(&done);
00182     }
00183 
00184     nsCOMPtr<rdfIDataSource> rdfds = do_QueryInterface(ds, &rv);
00185     RETURN_IF_FAILED(rv, "QI to rdIDataSource");
00186     {
00187         nsCOMPtr<nsIOutputStream> out = new ConsoleOutputStreamImpl();
00188         nsCOMPtr<rdfISerializer> ser =
00189             do_CreateInstance(NS_RDF_SERIALIZER "ntriples", &rv);
00190         RETURN_IF_FAILED(rv, "Creation of NTriples Serializer");
00191         rv = ser->Serialize(rdfds, out);
00192         RETURN_IF_FAILED(rv, "Serialization to NTriples");
00193         out->Close();
00194     }
00195 
00196     theEventQueueService->DestroyThreadEventQueue();
00197 
00198     return NS_OK;
00199 }