if (NS_FAILED(rv = nsRDFContentUtils::GetElementResource(aElement, getter_AddRefs(resource)))) { NS_ERROR("unable to get element resource"); return rv; }
rv = nsRDFContentUtils::GetElementResource(aElement, getter_AddRefs(resource));    if (NS_FAILED(rv)) return NS_OK;
RDFXULBuilderImpl::CreateContents(nsIContent* aElement){    nsresult rv;    // See if someone has marked the element's contents as being    // generated: this prevents a re-entrant call from triggering    // another generation.    nsAutoString attrValue;    if (NS_FAILED(rv = aElement->GetAttribute(kNameSpaceID_None,                                              kXULContentsGeneratedAtom,                                              attrValue))) {        NS_ERROR("unable to test contents-generated attribute");        return rv;    }    if ((rv == NS_CONTENT_ATTR_HAS_VALUE) && (attrValue.EqualsIgnoreCase("true")))        return NS_OK;#ifdef PR_LOGGING    if (PR_LOG_TEST(gLog, PR_LOG_DEBUG)) {        nsAutoString elementStr;        rv = nsRDFContentUtils::GetElementLogString(aElement, elementStr);        NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get element string");        char* elementCStr = elementStr.ToNewCString();        PR_LOG(gLog, PR_LOG_DEBUG, ("xulbuilder create-contents"));        PR_LOG(gLog, PR_LOG_DEBUG, ("  %s", elementCStr));        delete[] elementCStr;    }#endif    // Get the XUL element's resource so that we can generate    // children. We _don't_ QI for the nsIRDFResource here: doing this    // via the nsIContent interface allows us to support generic nodes    // that might get added in by DOM calls.    nsCOMPtr<nsIRDFResource> resource;    if (NS_FAILED(rv = nsRDFContentUtils::GetElementResource(aElement, getter_AddRefs(resource)))) {        NS_ERROR("unable to get element resource");        return rv;    }    // Ignore any elements that aren't XUL elements: we can't    // construct content for them anyway.    PRBool isXULElement;    rv = mDB->HasAssertion(resource, kRDF_instanceOf, kXUL_element, PR_TRUE, &isXULElement);    NS_ASSERTION(NS_SUCCEEDED(rv), "unable to determine if element is a XUL element");    if (NS_FAILED(rv)) return rv;    if (! isXULElement)        return NS_OK;    // If it's a XUL element, it'd better be an RDF Sequence...    PRBool isContainer;    rv = gRDFContainerUtils->IsContainer(mDB, resource, &isContainer);    if (NS_FAILED(rv)) return rv;    NS_ASSERTION(isContainer, "element is a XUL:element, but not an RDF:Seq");    if (! isContainer)        return NS_ERROR_UNEXPECTED;    // Now mark the element's contents as being generated so that    // any re-entrant calls don't trigger an infinite recursion.    if (NS_FAILED(rv = aElement->SetAttribute(kNameSpaceID_None,                                              kXULContentsGeneratedAtom,                                              "true",                                              PR_FALSE))) {        NS_ERROR("unable to set contents-generated attribute");        return rv;    }    nsCOMPtr<nsINameSpace> containingNameSpace;    rv = GetContainingNameSpace(aElement, getter_AddRefs(containingNameSpace));    if (NS_FAILED(rv)) return rv;    // If we're the root content, then prepend a special hidden form    // element.    if (aElement == mRoot.get()) {      // Always insert a hidden form as the very first child of the window.      // Create a new form element.      nsCOMPtr<nsIHTMLContent> newElement;      if (!mHTMLElementFactory) {        rv = nsComponentManager::CreateInstance(kHTMLElementFactoryCID,                                                nsnull,                                                nsIHTMLElementFactory::GetIID(),                                                (void**) &mHTMLElementFactory);        if (NS_FAILED(rv)) {            return rv;        }      }      rv = mHTMLElementFactory->CreateInstanceByTag(nsAutoString("form"),                                                getter_AddRefs(newElement));      NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create HTML element");      if (NS_FAILED(rv)) return rv;      nsCOMPtr<nsIDOMHTMLFormElement> htmlFormElement = do_QueryInterface(newElement);      if (htmlFormElement) {        mDocument->SetForm(htmlFormElement);            nsCOMPtr<nsIContent> content = do_QueryInterface(htmlFormElement);        if (content) {          // Add this node as a child          mRoot->InsertChildAt(content, 0, PR_FALSE);        }      }    }    // Iterate through all of the element's children, and construct    // appropriate children for each arc.    nsCOMPtr<nsISimpleEnumerator> children;    rv = NS_NewContainerEnumerator(mDB, resource, getter_AddRefs(children));    NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create cursor for children");    if (NS_FAILED(rv)) return rv;    while (1) {        PRBool hasMore;        rv = children->HasMoreElements(&hasMore);        if (NS_FAILED(rv)) return rv;        if (! hasMore)            break;        nsCOMPtr<nsISupports> isupports;        rv = children->GetNext(getter_AddRefs(isupports));        NS_ASSERTION(NS_SUCCEEDED(rv), "error reading cursor");        if (NS_FAILED(rv)) return rv;        nsCOMPtr<nsIRDFNode> child = do_QueryInterface(isupports);        rv = AppendChild(containingNameSpace, aElement, child);        NS_ASSERTION(NS_SUCCEEDED(rv), "problem appending child to content model");        if (NS_FAILED(rv)) return rv;    }    // Now that we've built the children, check to see if the includesrc attribute    // exists on the node.    nsAutoString includeSrc;    if (NS_FAILED(rv = aElement->GetAttribute(kNameSpaceID_None,                                              kXULIncludeSrcAtom,                                              includeSrc))) {        NS_ERROR("unable to retrieve includeSrc attribute");        return rv;    }    if (rv == NS_CONTENT_ATTR_HAS_VALUE) {        // Build a URL object from the attribute's value.                nsCOMPtr<nsIDocument> parentDoc;        aElement->GetDocument(*getter_AddRefs(parentDoc));        if (parentDoc == nsnull) {            NS_ERROR("Unable to retrieve parent document for a subdocument.");            return rv;        }            nsCOMPtr<nsIContentViewerContainer> container;        nsCOMPtr<nsIXULParentDocument> xulParentDocument;        xulParentDocument = do_QueryInterface(parentDoc);        if (xulParentDocument == nsnull) {            NS_ERROR("Unable to turn document into a XUL parent document.");            return rv;        }        if (NS_FAILED(rv = xulParentDocument->GetContentViewerContainer(getter_AddRefs(container)))) {            NS_ERROR("Unable to retrieve content viewer container from parent document.");            return rv;        }        nsAutoString command;        if (NS_FAILED(rv = xulParentDocument->GetCommand(command))) {            NS_ERROR("Unable to retrieve the command from parent document.");            return rv;        }                nsCOMPtr<nsIDOMXULElement> xulElement;        xulElement = do_QueryInterface(aElement);        if (!xulElement) {            NS_ERROR("The fragment root is not a XUL element.");            return rv;        }        nsCOMPtr<nsIRDFResource> rdfResource;        xulElement->GetResource(getter_AddRefs(rdfResource));        if (!rdfResource) {            NS_ERROR("The fragment root doesn't have an RDF resource behind it.");            return rv;        }                nsCOMPtr<nsIXULDocumentInfo> docInfo;        if (NS_FAILED(rv = nsComponentManager::CreateInstance(kXULDocumentInfoCID,                                                              nsnull,                                                              kIXULDocumentInfoIID,                                                              (void**) getter_AddRefs(docInfo)))) {            NS_ERROR("unable to create document info object");            return rv;        }                if (NS_FAILED(rv = docInfo->Init(parentDoc, rdfResource))) {            NS_ERROR("unable to initialize doc info object.");            return rv;        }                // Turn the content viewer into a webshell        nsCOMPtr<nsIWebShell> webshell;        webshell = do_QueryInterface(container);        if (webshell == nsnull) {            NS_ERROR("this isn't a webshell. we're in trouble.");            return rv;        }        nsCOMPtr<nsIDocumentLoader> docLoader;        if (NS_FAILED(rv = webshell->GetDocumentLoader(*getter_AddRefs(docLoader)))) {            NS_ERROR("unable to obtain the document loader to kick off the load.");            return rv;        }        docLoader->LoadSubDocument(includeSrc,                                   docInfo.get());    }    return rv;}