From 78807d92fd9690d9e199b60b7532dc8c1a7bd1cd Mon Sep 17 00:00:00 2001 From: "heikki%netscape.com" Date: Fri, 20 Oct 2000 23:26:10 +0000 Subject: [PATCH] Not part of Netscape 6.0 package. Checking in patches from toml@us.ibm.com. These fix bug 55508 (DOMParser does not work without script environment) and bug 55599 (add GetChannel method to nsIXMLHTTPRequest), as well as add a test program. r=heikki, a=vidur. --- allmakefiles.sh | 1 + extensions/xmlextras/Makefile.in | 4 + .../xmlextras/base/public/nsIDOMParser.idl | 9 + .../base/public/nsIXMLHttpRequest.idl | 9 + extensions/xmlextras/base/src/nsDOMParser.cpp | 32 ++- extensions/xmlextras/base/src/nsDOMParser.h | 13 +- .../xmlextras/base/src/nsXMLHttpRequest.cpp | 10 + extensions/xmlextras/makefile.win | 9 +- extensions/xmlextras/tests/Makefile.in | 46 ++++ extensions/xmlextras/tests/TestXMLExtras.cpp | 208 ++++++++++++++++++ extensions/xmlextras/tests/makefile.win | 58 +++++ 11 files changed, 389 insertions(+), 10 deletions(-) create mode 100644 extensions/xmlextras/tests/Makefile.in create mode 100644 extensions/xmlextras/tests/TestXMLExtras.cpp create mode 100755 extensions/xmlextras/tests/makefile.win diff --git a/allmakefiles.sh b/allmakefiles.sh index 5a83c7a26859..d1e1df065be2 100755 --- a/allmakefiles.sh +++ b/allmakefiles.sh @@ -902,6 +902,7 @@ for extension in $MOZ_EXTENSIONS; do extensions/xmlextras/build/Makefile extensions/xmlextras/build/src/Makefile extensions/xmlextras/soap/public/Makefile + extensions/xmlextras/tests/Makefile " ;; xmlterm ) MAKEFILES_extensions="$MAKEFILES_extensions extensions/xmlterm/Makefile diff --git a/extensions/xmlextras/Makefile.in b/extensions/xmlextras/Makefile.in index 1a232fb09efd..c46e5c38cf6d 100644 --- a/extensions/xmlextras/Makefile.in +++ b/extensions/xmlextras/Makefile.in @@ -28,4 +28,8 @@ include $(DEPTH)/config/autoconf.mk DIRS = base soap build +ifdef ENABLE_TESTS +DIRS += tests +endif + include $(topsrcdir)/config/rules.mk diff --git a/extensions/xmlextras/base/public/nsIDOMParser.idl b/extensions/xmlextras/base/public/nsIDOMParser.idl index b448d5cc2f4a..03e88887274e 100644 --- a/extensions/xmlextras/base/public/nsIDOMParser.idl +++ b/extensions/xmlextras/base/public/nsIDOMParser.idl @@ -24,6 +24,7 @@ interface nsIInputStream; interface nsIDOMDocument; +interface nsIURI; /** * The nsIDOMParser interface is a non-SAX interface that can be used @@ -63,6 +64,14 @@ interface nsIDOMParser : nsISupports { in string charset, in long contentLength, in string contentType); + + /** + * Set/Get the baseURI. You will probably not need this if you + * have a script environment. This is mostly intended for cases + * without a script environment, for example calling from native + * code. + */ + attribute nsIURI baseURI; }; %{ C++ diff --git a/extensions/xmlextras/base/public/nsIXMLHttpRequest.idl b/extensions/xmlextras/base/public/nsIXMLHttpRequest.idl index 7c198521cb49..0bf376c0b850 100644 --- a/extensions/xmlextras/base/public/nsIXMLHttpRequest.idl +++ b/extensions/xmlextras/base/public/nsIXMLHttpRequest.idl @@ -24,6 +24,7 @@ interface nsIDOMDocument; interface nsIDOMEventListener; +interface nsIHTTPChannel; [scriptable, uuid(b7215e70-4157-11d4-9a42-000064657374)] interface nsIXMLHttpRequest : nsISupports { @@ -65,6 +66,14 @@ interface nsIXMLHttpRequest : nsISupports { */ attribute nsISupports onerror; + /** + * The HTTP request uses an HTTP channel in order to perform the + * request. This attribute represents the HTTP channel used + * for the request. NULL if the HTTP channel has not yet been + * created. + */ + readonly attribute nsIHTTPChannel channel; + /** * The response to the HTTP request is parsed as if it were a * text/xml stream. This attributes represents the response as diff --git a/extensions/xmlextras/base/src/nsDOMParser.cpp b/extensions/xmlextras/base/src/nsDOMParser.cpp index 383a99666021..055c59ecf7b0 100644 --- a/extensions/xmlextras/base/src/nsDOMParser.cpp +++ b/extensions/xmlextras/base/src/nsDOMParser.cpp @@ -416,6 +416,7 @@ nsDOMParser::ParseFromString(const PRUnichar *str, return ParseFromStream(stream, "UTF-8", contentLength, contentType, _retval); } + /* nsIDOMDocument parseFromStream (in nsIInputStream stream, in string charset, in string contentType); */ NS_IMETHODIMP nsDOMParser::ParseFromStream(nsIInputStream *stream, @@ -449,7 +450,7 @@ nsDOMParser::ParseFromStream(nsIInputStream *stream, JSContext* cx; rv = cc->GetJSContext(&cx); if (NS_FAILED(rv)) return NS_ERROR_FAILURE; - + NS_WITH_SERVICE(nsIScriptSecurityManager, secMan, NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv); if (NS_SUCCEEDED(rv)) { @@ -463,6 +464,19 @@ nsDOMParser::ParseFromStream(nsIInputStream *stream, } } + if (!baseURI) { + // No URI from script environment (we are running from command line, for example). + // Create a dummy one. + // XXX Is this safe? Could we get the URI from stream or something? + if (!mBaseURI) { + rv = NS_NewURI(getter_AddRefs(baseURI), + "about:blank" ); + if (NS_FAILED(rv)) return rv; + } else { + baseURI = mBaseURI; + } + } + // Get and initialize a DOMImplementation nsCOMPtr implementation = do_CreateInstance(kIDOMDOMImplementationCID, &rv); if (NS_FAILED(rv)) return NS_ERROR_FAILURE; @@ -525,6 +539,22 @@ nsDOMParser::ParseFromStream(nsIInputStream *stream, return NS_OK; } +NS_IMETHODIMP +nsDOMParser::GetBaseURI(nsIURI **aBaseURI) +{ + NS_ENSURE_ARG_POINTER(aBaseURI); + *aBaseURI = mBaseURI; + NS_IF_ADDREF(*aBaseURI); + return NS_OK; +} + +NS_IMETHODIMP +nsDOMParser::SetBaseURI(nsIURI *aBaseURI) +{ + mBaseURI = aBaseURI; + return NS_OK; +} + static const char* kAllAccess = "AllAccess"; /* string canCreateWrapper (in nsIIDPtr iid); */ diff --git a/extensions/xmlextras/base/src/nsDOMParser.h b/extensions/xmlextras/base/src/nsDOMParser.h index 08bb00541b61..569f3fc088db 100644 --- a/extensions/xmlextras/base/src/nsDOMParser.h +++ b/extensions/xmlextras/base/src/nsDOMParser.h @@ -27,6 +27,7 @@ #include "nsISecurityCheckedComponent.h" #include "nsISupportsUtils.h" #include "nsCOMPtr.h" +#include "nsIURI.h" class nsDOMParser : public nsIDOMParser, public nsISecurityCheckedComponent @@ -38,16 +39,12 @@ public: NS_DECL_ISUPPORTS // nsIDOMParser - NS_IMETHOD ParseFromString(const PRUnichar *str, - const char *contentType, - nsIDOMDocument **_retval); - NS_IMETHOD ParseFromStream(nsIInputStream *stream, - const char *charset, - PRInt32 contentLength, - const char *contentType, - nsIDOMDocument **_retval); + NS_DECL_NSIDOMPARSER NS_DECL_NSISECURITYCHECKEDCOMPONENT + +private: + nsCOMPtr mBaseURI; }; #endif diff --git a/extensions/xmlextras/base/src/nsXMLHttpRequest.cpp b/extensions/xmlextras/base/src/nsXMLHttpRequest.cpp index fe98b5f1d48f..ddaef35113b3 100644 --- a/extensions/xmlextras/base/src/nsXMLHttpRequest.cpp +++ b/extensions/xmlextras/base/src/nsXMLHttpRequest.cpp @@ -552,6 +552,16 @@ nsXMLHttpRequest::SetOnerror(nsISupports * aOnerror) return AddEventListener(kErrorStr, listener); } +/* readonly attribute nsIHTTPChannel channel; */ +NS_IMETHODIMP nsXMLHttpRequest::GetChannel(nsIHTTPChannel **aChannel) +{ + NS_ENSURE_ARG_POINTER(aChannel); + *aChannel = mChannel; + NS_IF_ADDREF(*aChannel); + + return NS_OK; +} + /* readonly attribute nsIDOMDocument responseXML; */ NS_IMETHODIMP nsXMLHttpRequest::GetResponseXML(nsIDOMDocument **aResponseXML) { diff --git a/extensions/xmlextras/makefile.win b/extensions/xmlextras/makefile.win index 3c15e48c6d4b..8fb97867ebfe 100644 --- a/extensions/xmlextras/makefile.win +++ b/extensions/xmlextras/makefile.win @@ -21,6 +21,13 @@ DEPTH=..\.. -DIRS=base soap build +DIRS= \ + base \ + soap \ + build \ +!if !defined(DISABLE_TESTS) + tests \ +!endif + $(NULL) include <$(DEPTH)\config\rules.mak> diff --git a/extensions/xmlextras/tests/Makefile.in b/extensions/xmlextras/tests/Makefile.in new file mode 100644 index 000000000000..7c477924712d --- /dev/null +++ b/extensions/xmlextras/tests/Makefile.in @@ -0,0 +1,46 @@ +# +# The contents of this file are subject to the Netscape Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/NPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# + +DEPTH = ../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(topsrcdir)/config/autoconf.mk + +CPPSRCS = \ + TestXMLExtras.cpp \ + $(NULL) + +SIMPLE_PROGRAMS = $(CPPSRCS:.cpp=$(BIN_SUFFIX)) + +include $(topsrcdir)/config/config.mk + +LIBS = \ + $(MOZ_JS_LIBS) \ + $(XPCOM_LIBS) \ + $(LOST_SYM_LIBS) \ + $(NSPR_LIBS) \ + $(NULL) + +include $(topsrcdir)/config/rules.mk + +DEFINES += -DUSE_NSREG diff --git a/extensions/xmlextras/tests/TestXMLExtras.cpp b/extensions/xmlextras/tests/TestXMLExtras.cpp new file mode 100644 index 000000000000..f51f58f7cf5f --- /dev/null +++ b/extensions/xmlextras/tests/TestXMLExtras.cpp @@ -0,0 +1,208 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and imitations under the License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is International + * Business Machines Corporation. Portions created by IBM + * Corporation are Copyright (C) 2000 International Business + * Machines Corporation. All Rights Reserved. + * + * Contributor(s): IBM Corporation. + * Heikki Toivonen + * + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "nsIDocument.h" + +void usage( ) { + + 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" ); + + return; +} + + +int main (int argc, char* argv[]) +{ + nsresult rv; + nsCOMPtr pURI; + nsCOMPtr pChannel; + nsCOMPtr pInputStream; + PRUint32 uiContentLength; + nsCOMPtr pDOMParser; + nsCOMPtr pDOMDocument; + nsCOMPtr pXMLHttpRequest; + + 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.GetUnicode(),"text/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_OpenURI( getter_AddRefs( pChannel ), + pURI, + nsnull, + nsnull ); + + if (NS_SUCCEEDED( rv )) { + rv = pChannel->OpenInputStream( 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, + "text/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_OpenURI 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 )) { + rv = pXMLHttpRequest->OpenRequest( "GET", + argv[2], + PR_FALSE, + nsnull, nsnull ); + + 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 ); + } + } + else { + usage( ); + } + } + else { + usage( ); + } + + if (pDOMDocument) { + nsCOMPtr element; + pDOMDocument->GetDocumentElement(getter_AddRefs(element)); + nsAutoString tagName; + if (element) element->GetTagName(tagName); + char *s = tagName.ToNewCString(); + printf("Document element=\"%s\"\n",s); + nsCRT::free(s); + nsCOMPtr doc = do_QueryInterface(pDOMDocument); + if (doc) { + nsCOMPtr uri = dont_AddRef(doc->GetDocumentURL()); + nsXPIDLCString spec; + uri->GetSpec(getter_Copies(spec)); + printf("Document URI=\"%s\"\n",spec.get()); + } + } + + return 0; +} diff --git a/extensions/xmlextras/tests/makefile.win b/extensions/xmlextras/tests/makefile.win new file mode 100755 index 000000000000..990a3580a006 --- /dev/null +++ b/extensions/xmlextras/tests/makefile.win @@ -0,0 +1,58 @@ +#!nmake +# +# The contents of this file are subject to the Mozilla Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and imitations under the License. +# +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is International +# Business Machines Corporation. Portions created by IBM +# Corporation are Copyright (C) 2000 International Business +# Machines Corporation. All Rights Reserved. +# +# Contributor(s): IBM Corporation. +# Heikki Toivonen +# + +DEPTH = ..\..\.. + +include <$(DEPTH)/config/config.mak> + +MAKE_OBJ_TYPE = EXE +PROG1 = .\$(OBJDIR)\TestXMLExtras.exe + +PROGRAMS = $(PROG1) \ + $(NULL) + +LCFLAGS = -DUSE_NSREG -GX + +REQUIRES = libreg + +LINCS = \ + -Iservices \ + -I$(PUBLIC)\xpcom + +LLIBS = \ + $(DIST)\lib\xpcom.lib \ + $(LIBNSPR) + +!if "$(MOZ_BITS)"=="32" && defined(MOZ_DEBUG) && defined(GLOWCODE) +LLIBS = $(LLIBS) $(GLOWDIR)\glowcode.lib +!endif + +include <$(DEPTH)\config\rules.mak> + +install:: $(PROGRAMS) + -for %p in ($(PROGRAMS)) do $(MAKE_INSTALL) %p $(DIST)\bin + +clobber:: + -for %p in ($(PROGRAMS)) do $(RM) %p $(DIST)\bin\%p + +$(PROG1): $(OBJDIR) TestXMLExtras.cpp