if (NS_FAILED(rv = mParser->QueryInterface(kIStreamListenerIID, (void**) aDocListener))) {
if (NS_FAILED(rv = parser->QueryInterface(kIStreamListenerIID, (void**) aDocListener))) {
XULDocumentImpl::StartDocumentLoad(nsIURL *aURL,                                    nsIContentViewerContainer* aContainer,                                   nsIStreamListener **aDocListener,                                   const char* aCommand){    NS_ASSERTION(aURL != nsnull, "null ptr");    if (! aURL)        return NS_ERROR_NULL_POINTER;    if (aContainer && aContainer != mContentViewerContainer)    {      // AddRef and hold the container      NS_ADDREF(aContainer);      mContentViewerContainer = aContainer;    }    nsresult rv;    mDocumentTitle.Truncate();    NS_IF_RELEASE(mDocumentURL);    mDocumentURL = aURL;    NS_ADDREF(aURL);    NS_IF_RELEASE(mDocumentURLGroup);    (void)aURL->GetURLGroup(&mDocumentURLGroup);    // Delete references to style sheets - this should be done in superclass...    PRInt32 index = mStyleSheets.Count();    while (--index >= 0) {        nsIStyleSheet* sheet = (nsIStyleSheet*) mStyleSheets.ElementAt(index);        sheet->SetOwningDocument(nsnull);        NS_RELEASE(sheet);    }    mStyleSheets.Clear();    // Create an HTML style sheet for the HTML content.    nsIHTMLStyleSheet* sheet;    if (NS_SUCCEEDED(rv = nsComponentManager::CreateInstance(kHTMLStyleSheetCID,                                                       nsnull,                                                       kIHTMLStyleSheetIID,                                                       (void**) &sheet))) {        if (NS_SUCCEEDED(rv = sheet->Init(aURL, this))) {            mAttrStyleSheet = sheet;            NS_ADDREF(mAttrStyleSheet);            AddStyleSheet(mAttrStyleSheet);        }        NS_RELEASE(sheet);    }    if (NS_FAILED(rv)) {        NS_ERROR("unable to add HTML style sheet");        return rv;    }    // Create an inline style sheet for inline content that contains a style     // attribute.    nsIHTMLCSSStyleSheet* inlineSheet;    if (NS_SUCCEEDED(rv = nsComponentManager::CreateInstance(kHTMLCSSStyleSheetCID,                                                       nsnull,                                                       kIHTMLCSSStyleSheetIID,                                                       (void**) &inlineSheet))) {        if (NS_SUCCEEDED(rv = inlineSheet->Init(aURL, this))) {            mInlineStyleSheet = inlineSheet;            NS_ADDREF(mInlineStyleSheet);            AddStyleSheet(mInlineStyleSheet);        }        NS_RELEASE(inlineSheet);    }    if (NS_FAILED(rv)) {        NS_ERROR("unable to add inline style sheet");        return rv;    }          // Create the composite data source and builder, but only do this if we're    // not a XUL fragment.    if (mFragmentRoot == nsnull) {        nsIRDFCompositeDataSource* db;        if (NS_FAILED(rv = nsComponentManager::CreateInstance(kRDFCompositeDataSourceCID,                                                        nsnull,                                                        kIRDFCompositeDataSourceIID,                                                        (void**) &db))) {            NS_ERROR("couldn't create composite datasource");            return rv;        }        // Create a XUL content model builder        NS_IF_RELEASE(mXULBuilder);        if (NS_FAILED(rv = nsComponentManager::CreateInstance(kRDFXULBuilderCID,                                                        nsnull,                                                                             kIRDFContentModelBuilderIID,                                                        (void**) &mXULBuilder))) {            NS_ERROR("couldn't create XUL builder");            return rv;        }        if (NS_FAILED(rv = mXULBuilder->SetDataBase(db))) {            NS_ERROR("couldn't set builder's db");            return rv;        }        NS_IF_RELEASE(mBuilders);        if (NS_FAILED(rv = AddContentModelBuilder(mXULBuilder))) {            NS_ERROR("could't add XUL builder");            return rv;        }        // Create a "scratch" in-memory data store to associate with the        // document to be a catch-all for any doc-specific info that we        // need to store (e.g., current sort order, etc.)        //        // XXX This needs to be cloned across windows, and the final        // instance needs to be flushed to disk. It may be that this is        // really an RDFXML data source...        if (NS_FAILED(rv = nsComponentManager::CreateInstance(kRDFInMemoryDataSourceCID,                                                        nsnull,                                                        kIRDFDataSourceIID,                                                        (void**) &mLocalDataSource))) {            NS_ERROR("couldn't create local data source");            return rv;        }        if (NS_FAILED(rv = db->AddDataSource(mLocalDataSource))) {            NS_ERROR("couldn't add local data source to db");            return rv;        }        // Now load the actual RDF/XML document data source. First, we'll        // see if the data source has been loaded and is registered with        // the RDF service. If so, do some monkey business to "pretend" to        // load it: really, we'll just walk its graph to generate the        // content model.        const char* uri;        if (NS_FAILED(rv = aURL->GetSpec(&uri)))            return rv;        // We need to construct a new stream and load it. The stream will        // automagically register itself as a named data source, so if        // subsequent docs ask for it, they'll get the real deal. In the        // meantime, add us as an nsIRDFXMLDataSourceObserver so that        // we'll be notified when we need to load style sheets, etc.        NS_IF_RELEASE(mDocumentDataSource);        if (NS_FAILED(rv = nsComponentManager::CreateInstance(kRDFInMemoryDataSourceCID,                                                        nsnull,                                                        kIRDFDataSourceIID,                                                        (void**) &mDocumentDataSource))) {            NS_ERROR("unable to create XUL datasource");            return rv;        }        if (NS_FAILED(rv = db->AddDataSource(mDocumentDataSource))) {            NS_ERROR("unable to add XUL datasource to db");            return rv;        }        if (NS_FAILED(rv = mDocumentDataSource->Init(uri))) {            NS_ERROR("unable to initialize XUL data source");            return rv;        }    }    nsCOMPtr<nsIXULContentSink> sink;    if (NS_FAILED(rv = nsComponentManager::CreateInstance(kXULContentSinkCID,                                                    nsnull,                                                    kIXULContentSinkIID,                                                    (void**) getter_AddRefs(sink)))) {        NS_ERROR("unable to create XUL content sink");        return rv;    }    {        if (NS_FAILED(rv = sink->Init(this, mDocumentDataSource))) {            NS_ERROR("Unable to initialize XUL content sink");            return rv;        }    }    if (NS_FAILED(rv = nsComponentManager::CreateInstance(kParserCID,                                                    nsnull,                                                    kIParserIID,                                                    (void**) &mParser))) {        NS_ERROR("unable to create parser");        return rv;    }    if (NS_FAILED(rv = mParser->QueryInterface(kIStreamListenerIID, (void**) aDocListener))) {        NS_ERROR("parser doesn't support nsIStreamListener");        return rv;    }    nsCOMPtr<nsIDTD> dtd;    if (NS_FAILED(rv = nsComponentManager::CreateInstance(kWellFormedDTDCID,                                                    nsnull,                                                    kIDTDIID,                                                    (void**) getter_AddRefs(dtd)))) {        NS_ERROR("unable to construct DTD");        return rv;    }    mCommand = aCommand;    mParser->RegisterDTD(dtd);    mParser->SetCommand(aCommand);    mParser->SetContentSink(sink);    mParser->Parse(aURL);       return NS_OK;}