Back to index

lightning-sunbird  0.9+nobinonly
Functions
TestXMLExtras.cpp File Reference
#include <nsCOMPtr.h>
#include <nsString.h>
#include <nsCRT.h>
#include <nsIURI.h>
#include <nsIChannel.h>
#include <nsIInputStream.h>
#include <nsIDOMDocument.h>
#include <nsIDOMParser.h>
#include <nsIXMLHttpRequest.h>
#include <nsNetUtil.h>
#include <nsIDOMElement.h>
#include "nsIDocument.h"
#include "nsIServiceManager.h"
#include <nsIDOMNSDocument.h>
#include <nsIDOMEventTarget.h>
#include <nsIDOMEventListener.h>
#include <nsIDOMLoadListener.h>
#include "nsContentCID.h"
#include "nsReadableUtils.h"

Go to the source code of this file.

Functions

static NS_DEFINE_CID (kXMLDocumentCID, NS_XMLDOCUMENT_CID)
void usage ()
int main (int argc, char *argv[])

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 133 of file TestXMLExtras.cpp.

{
  nsresult                     rv;
  nsCOMPtr<nsIURI>             pURI;
  nsCOMPtr<nsIChannel>         pChannel;
  nsCOMPtr<nsIInputStream>     pInputStream;
  PRUint32                     uiContentLength;
  nsCOMPtr<nsIDOMParser>       pDOMParser;
  nsCOMPtr<nsIDOMDocument>     pDOMDocument;
  nsCOMPtr<nsIXMLHttpRequest>  pXMLHttpRequest;

  nsIServiceManager *servMgr;

  rv = NS_InitXPCOM2(&servMgr, nsnull, nsnull);
  if (NS_FAILED(rv)) return rv;

  if (argc > 2) {
    if (nsCRT::strcasecmp( argv[1], "parsestr" ) == 0) {
      pDOMParser = do_CreateInstance( NS_DOMPARSER_CONTRACTID,
                                     &rv );

      if (NS_SUCCEEDED( rv )) {
        nsString str; str.AssignWithConversion(argv[2]);
        rv = pDOMParser->ParseFromString(str.get(), "application/xml",
                                          getter_AddRefs( pDOMDocument ) );

        if (NS_SUCCEEDED( rv )) {
          printf( "DOM parse string of\n\n%s\n\nsuccessful\n", argv[2] );
        }
        else {
          printf( "DOM parse of \n%s\n NOT successful\n", argv[2] );
        }
      }
      else {
        printf( "do_CreateInstance of DOMParser failed for %s - %08X\n", argv[2], rv );
      }
    } else if (nsCRT::strcasecmp( argv[1], "parse" ) == 0) {
      // DOM Parser
      rv = NS_NewURI( getter_AddRefs( pURI ),
                      argv[2] );

      if (NS_SUCCEEDED( rv )) {
        rv = NS_NewChannel( getter_AddRefs( pChannel ),
                         pURI,
                         nsnull,
                         nsnull );

        if (NS_SUCCEEDED( rv )) {
          rv = pChannel->Open( getter_AddRefs( pInputStream ) );

          if (NS_SUCCEEDED( rv )) {
            rv = pInputStream->Available(&uiContentLength );

            if (NS_SUCCEEDED( rv )) {
              pDOMParser = do_CreateInstance( NS_DOMPARSER_CONTRACTID,
                                             &rv );

              if (NS_SUCCEEDED( rv )) {
                pDOMParser->SetBaseURI(pURI);

                rv = pDOMParser->ParseFromStream( pInputStream,
                                                  "UTF-8",
                                                  uiContentLength,
                                                  "application/xml",
                                                  getter_AddRefs( pDOMDocument ) );
                if (NS_SUCCEEDED( rv )) {
                  printf( "DOM parse of %s successful\n", argv[2] );
                }
                else {
                  printf( "DOM parse of %s NOT successful\n", argv[2] );
                }
              }
              else {
                printf( "do_CreateInstance of DOMParser failed for %s - %08X\n", argv[2], rv );
              }
            }
            else {
              printf( "pInputSteam->Available failed for %s - %08X\n", argv[2], rv );
            }
          }
          else {
            printf( "pChannel->OpenInputStream failed for %s - %08X\n", argv[2], rv );
          }
        }
        else {
          printf( "NS_NewChannel failed for %s - %08X\n", argv[2], rv );
        }
      }
      else {
        printf( "NS_NewURI failed for %s - %08X\n", argv[2], rv );
      }
    }
    else if (nsCRT::strcasecmp( argv[1], "syncread" ) == 0) {
      // Synchronous Read
      pXMLHttpRequest = do_CreateInstance( NS_XMLHTTPREQUEST_CONTRACTID,
                                          &rv );

      if (NS_SUCCEEDED( rv )) {
        const nsAString& emptyStr = EmptyString();
        rv = pXMLHttpRequest->OpenRequest( NS_LITERAL_CSTRING("GET"),
                                           nsDependentCString(argv[2]),
                                           PR_FALSE, emptyStr, emptyStr );

        if (NS_SUCCEEDED( rv )) {
          rv = pXMLHttpRequest->Send( nsnull );

          if (NS_SUCCEEDED( rv )) {
            rv = pXMLHttpRequest->GetResponseXML( getter_AddRefs( pDOMDocument ) );

            if (NS_SUCCEEDED( rv )) {

              if (pDOMDocument) {
                printf( "Synchronous read of %s successful, DOMDocument created\n", argv[2] );
              }
              else {
                printf( "Synchronous read of %s NOT successful, DOMDocument NOT created\n", argv[2] );
              }
            }
            else {
              printf( "pXMLHttpRequest->GetResponseXML failed for %s - %08X\n", argv[2], rv );
            }
          }
          else {
            printf( "pXMLHttpRequest->Send failed for %s - %08X\n", argv[2], rv );
          }
        }
        else {
          printf( "pXMLHttpRequest->OpenRequest failed for %s - %08X\n", argv[2], rv );
        }
      }
      else {
        printf( "do_CreateInstance of XMLHttpRequest failed for %s - %08X\n", argv[2], rv );
      }
    }
#if 0
    else if (nsCRT::strcasecmp( argv[1], "load" ) == 0) {
      nsMyListener * listener = new nsMyListener();
      listener->Start(argv[2]);
    }
#endif
    else {
      usage( );
    }
  }
  else {
    usage( );
  }

  if (pDOMDocument) {
    nsCOMPtr<nsIDOMElement> element;
    pDOMDocument->GetDocumentElement(getter_AddRefs(element));
    nsAutoString tagName;
    if (element) element->GetTagName(tagName);
    char *s = ToNewCString(tagName);
    printf("Document element=\"%s\"\n",s);
    nsCRT::free(s);
    nsCOMPtr<nsIDocument> doc = do_QueryInterface(pDOMDocument);
    if (doc) {
      nsCAutoString spec;
      doc->GetDocumentURI()->GetSpec(spec);
      printf("Document URI=\"%s\"\n",spec.get());
    }
  }

  pURI = nsnull;
  pChannel = nsnull;
  pInputStream = nsnull;
  pDOMParser = nsnull;
  pDOMDocument = nsnull;
  pXMLHttpRequest = nsnull;

  if (servMgr)
    rv = NS_ShutdownXPCOM(servMgr);
  
  return rv;
}

Here is the call graph for this function:

static NS_DEFINE_CID ( kXMLDocumentCID  ,
NS_XMLDOCUMENT_CID   
) [static]
void usage ( void  )

Definition at line 117 of file TestXMLExtras.cpp.

              {
  printf( "\n" );
  printf( "Usage:\n" );
  printf( "  TestXMLExtras {parsestr | parse | syncread} xmlfile\n\n" );
  printf( "    parsestr = Invokes DOMParser against a string supplied as second argument\n" );
  printf( "    parse    = Invokes DOMParser against a local file\n" );
  printf( "      xmlfile  = A local XML URI (ie. file:///d:/file.xml)\n\n" );
  printf( "    syncread = Invokes XMLHttpRequest for a synchronous read\n" );
  printf( "      xmlfile  = An XML URI (ie. http://www.w3.org/TR/P3P/base\n\n" );
  printf( "    load       = Invokes document.load for asynchronous read\n" );
  printf( "      xmlfile  = An XML URI (ie. http://www.w3.org/TR/P3P/base\n\n" );

  return;
}