Back to index

lightning-sunbird  0.9+nobinonly
TestXMLExtras.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * International Business Machines Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2000
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   IBM Corporation.
00025  *   Heikki Toivonen <heikki@netscape.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include <nsCOMPtr.h>
00042 #include <nsString.h>
00043 #include <nsCRT.h>
00044 #include <nsIURI.h>
00045 #include <nsIChannel.h>
00046 #include <nsIInputStream.h>
00047 #include <nsIDOMDocument.h>
00048 #include <nsIDOMParser.h>
00049 #include <nsIXMLHttpRequest.h>
00050 #include <nsNetUtil.h>
00051 #include <nsIDOMElement.h>
00052 #include "nsIDocument.h"
00053 #include "nsIServiceManager.h"
00054 #include <nsIDOMNSDocument.h>
00055 #include <nsIDOMEventTarget.h>
00056 #include <nsIDOMEventListener.h>
00057 #include <nsIDOMLoadListener.h>
00058 #include "nsContentCID.h"
00059 #include "nsReadableUtils.h"
00060 static NS_DEFINE_CID( kXMLDocumentCID, NS_XMLDOCUMENT_CID );
00061 
00062 #if 0
00063 class nsMyListener : public nsIDOMLoadListener 
00064 {
00065 public:
00066   nsMyListener() {}
00067   ~nsMyListener() {}
00068 
00069   void Start(const char *a);
00070   
00071   NS_DECL_ISUPPORTS
00072   
00073   // nsIDOMEventListener
00074   virtual nsresult HandleEvent(nsIDOMEvent* aEvent) { return NS_OK;}
00075 
00076   // nsIDOMLoadListener
00077   virtual nsresult Load(nsIDOMEvent* aEvent)   {printf("Load\n"); return NS_OK;}
00078   virtual nsresult Unload(nsIDOMEvent* aEvent) {printf("Unload\n"); return NS_OK;}
00079   virtual nsresult Abort(nsIDOMEvent* aEvent)  {printf("Abort\n"); return NS_OK;}
00080   virtual nsresult Error(nsIDOMEvent* aEvent)  {printf("Error\n"); return NS_OK;}
00081 };
00082 
00083 NS_IMPL_ADDREF(nsMyListener)
00084 NS_IMPL_RELEASE(nsMyListener)
00085 
00086 NS_INTERFACE_MAP_BEGIN(nsMyListener)
00087    NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMLoadListener)
00088    NS_INTERFACE_MAP_ENTRY(nsIDOMLoadListener)
00089    NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
00090 NS_INTERFACE_MAP_END
00091 
00092 void nsMyListener::Start(const char *arg2)
00093 {
00094   nsresult rv;
00095   nsCOMPtr<nsIDOMDocument> doc(do_CreateInstance( kXMLDocumentCID, &rv ));
00096   if (NS_SUCCEEDED( rv )) {
00097     nsCOMPtr<nsIDOMEventTarget> pDOMEventTarget = do_QueryInterface( doc, &rv );
00098     if (NS_SUCCEEDED( rv )) {
00099       nsCOMPtr<nsIDOMEventListener> pDOMEventListener = do_QueryInterface( this, &rv );
00100       if (NS_SUCCEEDED( rv )) {
00101         nsAutoString sLoadCommand;
00102         sLoadCommand.Assign( NS_LITERAL_STRING("load") );
00103         rv = pDOMEventTarget->AddEventListener( sLoadCommand, pDOMEventListener, PR_FALSE );
00104         if (NS_SUCCEEDED( rv )) {
00105           nsCOMPtr<nsIDOMNSDocument> pDOMNSDocument = do_QueryInterface( doc, &rv );
00106           if (NS_SUCCEEDED( rv )) {
00107             rv = pDOMNSDocument->Load( NS_ConvertASCIItoUCS2(arg2) );
00108           }
00109         }
00110       }
00111     }
00112   }
00113 
00114 }
00115 #endif
00116 
00117 void usage( ) {
00118   printf( "\n" );
00119   printf( "Usage:\n" );
00120   printf( "  TestXMLExtras {parsestr | parse | syncread} xmlfile\n\n" );
00121   printf( "    parsestr = Invokes DOMParser against a string supplied as second argument\n" );
00122   printf( "    parse    = Invokes DOMParser against a local file\n" );
00123   printf( "      xmlfile  = A local XML URI (ie. file:///d:/file.xml)\n\n" );
00124   printf( "    syncread = Invokes XMLHttpRequest for a synchronous read\n" );
00125   printf( "      xmlfile  = An XML URI (ie. http://www.w3.org/TR/P3P/base\n\n" );
00126   printf( "    load       = Invokes document.load for asynchronous read\n" );
00127   printf( "      xmlfile  = An XML URI (ie. http://www.w3.org/TR/P3P/base\n\n" );
00128 
00129   return;
00130 }
00131 
00132 
00133 int main (int argc, char* argv[]) 
00134 {
00135   nsresult                     rv;
00136   nsCOMPtr<nsIURI>             pURI;
00137   nsCOMPtr<nsIChannel>         pChannel;
00138   nsCOMPtr<nsIInputStream>     pInputStream;
00139   PRUint32                     uiContentLength;
00140   nsCOMPtr<nsIDOMParser>       pDOMParser;
00141   nsCOMPtr<nsIDOMDocument>     pDOMDocument;
00142   nsCOMPtr<nsIXMLHttpRequest>  pXMLHttpRequest;
00143 
00144   nsIServiceManager *servMgr;
00145 
00146   rv = NS_InitXPCOM2(&servMgr, nsnull, nsnull);
00147   if (NS_FAILED(rv)) return rv;
00148 
00149   if (argc > 2) {
00150     if (nsCRT::strcasecmp( argv[1], "parsestr" ) == 0) {
00151       pDOMParser = do_CreateInstance( NS_DOMPARSER_CONTRACTID,
00152                                      &rv );
00153 
00154       if (NS_SUCCEEDED( rv )) {
00155         nsString str; str.AssignWithConversion(argv[2]);
00156         rv = pDOMParser->ParseFromString(str.get(), "application/xml",
00157                                           getter_AddRefs( pDOMDocument ) );
00158 
00159         if (NS_SUCCEEDED( rv )) {
00160           printf( "DOM parse string of\n\n%s\n\nsuccessful\n", argv[2] );
00161         }
00162         else {
00163           printf( "DOM parse of \n%s\n NOT successful\n", argv[2] );
00164         }
00165       }
00166       else {
00167         printf( "do_CreateInstance of DOMParser failed for %s - %08X\n", argv[2], rv );
00168       }
00169     } else if (nsCRT::strcasecmp( argv[1], "parse" ) == 0) {
00170       // DOM Parser
00171       rv = NS_NewURI( getter_AddRefs( pURI ),
00172                       argv[2] );
00173 
00174       if (NS_SUCCEEDED( rv )) {
00175         rv = NS_NewChannel( getter_AddRefs( pChannel ),
00176                          pURI,
00177                          nsnull,
00178                          nsnull );
00179 
00180         if (NS_SUCCEEDED( rv )) {
00181           rv = pChannel->Open( getter_AddRefs( pInputStream ) );
00182 
00183           if (NS_SUCCEEDED( rv )) {
00184             rv = pInputStream->Available(&uiContentLength );
00185 
00186             if (NS_SUCCEEDED( rv )) {
00187               pDOMParser = do_CreateInstance( NS_DOMPARSER_CONTRACTID,
00188                                              &rv );
00189 
00190               if (NS_SUCCEEDED( rv )) {
00191                 pDOMParser->SetBaseURI(pURI);
00192 
00193                 rv = pDOMParser->ParseFromStream( pInputStream,
00194                                                   "UTF-8",
00195                                                   uiContentLength,
00196                                                   "application/xml",
00197                                                   getter_AddRefs( pDOMDocument ) );
00198                 if (NS_SUCCEEDED( rv )) {
00199                   printf( "DOM parse of %s successful\n", argv[2] );
00200                 }
00201                 else {
00202                   printf( "DOM parse of %s NOT successful\n", argv[2] );
00203                 }
00204               }
00205               else {
00206                 printf( "do_CreateInstance of DOMParser failed for %s - %08X\n", argv[2], rv );
00207               }
00208             }
00209             else {
00210               printf( "pInputSteam->Available failed for %s - %08X\n", argv[2], rv );
00211             }
00212           }
00213           else {
00214             printf( "pChannel->OpenInputStream failed for %s - %08X\n", argv[2], rv );
00215           }
00216         }
00217         else {
00218           printf( "NS_NewChannel failed for %s - %08X\n", argv[2], rv );
00219         }
00220       }
00221       else {
00222         printf( "NS_NewURI failed for %s - %08X\n", argv[2], rv );
00223       }
00224     }
00225     else if (nsCRT::strcasecmp( argv[1], "syncread" ) == 0) {
00226       // Synchronous Read
00227       pXMLHttpRequest = do_CreateInstance( NS_XMLHTTPREQUEST_CONTRACTID,
00228                                           &rv );
00229 
00230       if (NS_SUCCEEDED( rv )) {
00231         const nsAString& emptyStr = EmptyString();
00232         rv = pXMLHttpRequest->OpenRequest( NS_LITERAL_CSTRING("GET"),
00233                                            nsDependentCString(argv[2]),
00234                                            PR_FALSE, emptyStr, emptyStr );
00235 
00236         if (NS_SUCCEEDED( rv )) {
00237           rv = pXMLHttpRequest->Send( nsnull );
00238 
00239           if (NS_SUCCEEDED( rv )) {
00240             rv = pXMLHttpRequest->GetResponseXML( getter_AddRefs( pDOMDocument ) );
00241 
00242             if (NS_SUCCEEDED( rv )) {
00243 
00244               if (pDOMDocument) {
00245                 printf( "Synchronous read of %s successful, DOMDocument created\n", argv[2] );
00246               }
00247               else {
00248                 printf( "Synchronous read of %s NOT successful, DOMDocument NOT created\n", argv[2] );
00249               }
00250             }
00251             else {
00252               printf( "pXMLHttpRequest->GetResponseXML failed for %s - %08X\n", argv[2], rv );
00253             }
00254           }
00255           else {
00256             printf( "pXMLHttpRequest->Send failed for %s - %08X\n", argv[2], rv );
00257           }
00258         }
00259         else {
00260           printf( "pXMLHttpRequest->OpenRequest failed for %s - %08X\n", argv[2], rv );
00261         }
00262       }
00263       else {
00264         printf( "do_CreateInstance of XMLHttpRequest failed for %s - %08X\n", argv[2], rv );
00265       }
00266     }
00267 #if 0
00268     else if (nsCRT::strcasecmp( argv[1], "load" ) == 0) {
00269       nsMyListener * listener = new nsMyListener();
00270       listener->Start(argv[2]);
00271     }
00272 #endif
00273     else {
00274       usage( );
00275     }
00276   }
00277   else {
00278     usage( );
00279   }
00280 
00281   if (pDOMDocument) {
00282     nsCOMPtr<nsIDOMElement> element;
00283     pDOMDocument->GetDocumentElement(getter_AddRefs(element));
00284     nsAutoString tagName;
00285     if (element) element->GetTagName(tagName);
00286     char *s = ToNewCString(tagName);
00287     printf("Document element=\"%s\"\n",s);
00288     nsCRT::free(s);
00289     nsCOMPtr<nsIDocument> doc = do_QueryInterface(pDOMDocument);
00290     if (doc) {
00291       nsCAutoString spec;
00292       doc->GetDocumentURI()->GetSpec(spec);
00293       printf("Document URI=\"%s\"\n",spec.get());
00294     }
00295   }
00296 
00297   pURI = nsnull;
00298   pChannel = nsnull;
00299   pInputStream = nsnull;
00300   pDOMParser = nsnull;
00301   pDOMDocument = nsnull;
00302   pXMLHttpRequest = nsnull;
00303 
00304   if (servMgr)
00305     rv = NS_ShutdownXPCOM(servMgr);
00306   
00307   return rv;
00308 }