mDocument->SetForm(htmlFormElement);
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;    rv = nsRDFContentUtils::GetElementResource(aElement, getter_AddRefs(resource));    // If it doesn't have a resource, then it's not a XUL element (maybe it was    // an anonymous template node generated by RDF). Regardless, we don't care.    if (NS_FAILED(rv))        return NS_OK;    // 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;    }    return rv;}