rv = element->SetAttribute(kNameSpaceID_None, kIdAtom, id, PR_FALSE); if (NS_FAILED(rv)) return rv;
nsAutoString id; rv = nsRDFContentUtils::MakeElementID(document, nsAutoString(uri), id); if (NS_FAILED(rv)) return rv;
RDFXULBuilderImpl::CreateXULElement(nsINameSpace* aContainingNameSpace,                                    nsIRDFResource* aResource,                                    PRInt32 aNameSpaceID,                                    nsIAtom* aTagName,                                    nsIContent** aResult){    nsresult rv;    // Create a new XUL element.    nsCOMPtr<nsIContent> element;    rv = NS_NewRDFElement(aNameSpaceID, aTagName, getter_AddRefs(element));    NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create new content element");    if (NS_FAILED(rv)) return rv;    nsCOMPtr<nsIDocument> document = do_QueryInterface(mDocument);    if (! document)        return NS_ERROR_UNEXPECTED;    // Set the document for this element.    element->SetDocument(document, PR_FALSE);    // Initialize the element's containing namespace to that of its    // parent.    nsCOMPtr<nsIXMLContent> xml( do_QueryInterface(element) );    NS_ASSERTION(xml != nsnull, "not an XML element");    if (! xml)        return NS_ERROR_UNEXPECTED;    rv = xml->SetContainingNameSpace(aContainingNameSpace);    if (NS_FAILED(rv)) return rv;    if (aResource) {        // Set the element's ID. The XUL document will be listening        // for 'id' and 'ref' attribute changes, so we're sure that        // this will get properly hashed into the document's        // resource-to-element map.        nsXPIDLCString uri;        rv = aResource->GetValue( getter_Copies(uri) );        if (NS_FAILED(rv)) return rv;        nsAutoString id;        rv = nsRDFContentUtils::MakeElementID(document, nsAutoString(uri), id);        if (NS_FAILED(rv)) return rv;        rv = element->SetAttribute(kNameSpaceID_None, kIdAtom, id, PR_FALSE);        if (NS_FAILED(rv)) return rv;        // The XUL element's implementation has a hacked        // SetAttribute() method that'll be smart enough to add the        // element to the document's element-to-resource map, so no        // need to do it ourselves. N.B. that this is _different_ from        // an HTML element...        // Now iterate through all the properties and add them as        // attributes on the element.  First, create a cursor that'll        // iterate through all the properties that lead out of this        // resource.        nsCOMPtr<nsISimpleEnumerator> properties;        rv = mDB->ArcLabelsOut(aResource, getter_AddRefs(properties));        NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create arcs-out cursor");        if (NS_FAILED(rv)) return rv;        // Advance that cursor 'til it runs outta steam        while (1) {            PRBool hasMore;            rv = properties->HasMoreElements(&hasMore);            if (NS_FAILED(rv)) return rv;            if (! hasMore)                break;            nsCOMPtr<nsISupports> isupports;            rv = properties->GetNext(getter_AddRefs(isupports));            NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get property from cursor");            if (NS_FAILED(rv)) return rv;            // Skip any properties that aren't meant to be added to            // the content model.            nsCOMPtr<nsIRDFResource> property = do_QueryInterface(isupports);            if (! IsAttributeProperty(property))                continue;            // For each property, set its value.            nsCOMPtr<nsIRDFNode> value;            rv = mDB->GetTarget(aResource, property, PR_TRUE, getter_AddRefs(value));            NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get value for property");            if (NS_FAILED(rv)) return rv;            // Add the attribute to the newly constructed element            rv = AddAttribute(element, property, value);            NS_ASSERTION(NS_SUCCEEDED(rv), "unable to add attribute to element");            if (NS_FAILED(rv)) return rv;        }        // Set the XML tag info: namespace prefix and ID. We do this        // _after_ processing all the attributes so that we can extract an        // appropriate prefix based on whatever namespace decls are        // active.        rv = xml->SetNameSpaceID(aNameSpaceID);        if (NS_FAILED(rv)) return rv;        if (aNameSpaceID != kNameSpaceID_None) {            nsCOMPtr<nsINameSpace> nameSpace;            rv = xml->GetContainingNameSpace(*getter_AddRefs(nameSpace));            if (NS_FAILED(rv)) return rv;            nsCOMPtr<nsIAtom> prefix;            rv = nameSpace->FindNameSpacePrefix(aNameSpaceID, *getter_AddRefs(prefix));            if (NS_FAILED(rv)) return rv;            rv = xml->SetNameSpacePrefix(prefix);            if (NS_FAILED(rv)) return rv;        }        // Make it a lazy so that its contents get recursively        // generated on-demand.        rv = element->SetAttribute(kNameSpaceID_None, kLazyContentAtom, nsAutoString("true"), PR_FALSE);        NS_ASSERTION(NS_SUCCEEDED(rv), "unable to make element lazy");        if (NS_FAILED(rv)) return rv;        // Check for a 'datasources' tag, in which case we'll create a        // template builder.        {            nsAutoString dataSources;            if (NS_CONTENT_ATTR_HAS_VALUE ==                element->GetAttribute(kNameSpaceID_None,                                      kDataSourcesAtom,                                      dataSources)) {                nsCOMPtr<nsIRDFContentModelBuilder> builder;                rv = CreateTemplateBuilder(element, dataSources, getter_AddRefs(builder));                NS_ASSERTION(NS_SUCCEEDED(rv), "unable to add datasources");            }        }            // We also need to pay special attention to the keyset tag to set up a listener        if(aTagName == kKeysetAtom) {            // Create our nsXULKeyListener and hook it up.            nsCOMPtr<nsIXULKeyListener> keyListener;            rv = nsComponentManager::CreateInstance(kXULKeyListenerCID,                                                    nsnull,                                                    kIXULKeyListenerIID,                                                    getter_AddRefs(keyListener));            NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create a key listener");            if (NS_FAILED(rv)) return rv;                    nsCOMPtr<nsIDOMEventListener> domEventListener = do_QueryInterface(keyListener);            if (domEventListener) {                // Take the key listener and add it as an event listener for keypress events.                nsCOMPtr<nsIDOMDocument> domDocument = do_QueryInterface(mDocument);                // Init the listener with the keyset node                nsCOMPtr<nsIDOMElement> domElement = do_QueryInterface(element);                keyListener->Init(domElement, domDocument);                            nsCOMPtr<nsIDOMEventReceiver> receiver = do_QueryInterface(document);                if(receiver) {                    receiver->AddEventListener("keypress", domEventListener, PR_TRUE);                     receiver->AddEventListener("keydown",  domEventListener, PR_TRUE);                      receiver->AddEventListener("keyup",    domEventListener, PR_TRUE);                   }            }        }    }    // Finally, assign the newly constructed element to the result    // pointer and addref it for the trip home.    *aResult = element;    NS_ADDREF(*aResult);    return NS_OK;}