nsAutoString uri; if (NS_FAILED(rv = aElement->GetAttribute(kNameSpaceID_RDF, kIdAtom, uri))) { NS_ERROR("severe error retrieving attribute"); return rv; }  if (rv != NS_CONTENT_ATTR_HAS_VALUE) { NS_ERROR("tree element has no RDF:ID"); return NS_ERROR_UNEXPECTED; }
RDFTreeBuilderImpl::CreateContents(nsIContent* aElement){    nsresult rv;    // First, make sure that the element is in the right tree -- ours.    if (! IsElementInTree(aElement))        return NS_OK;    // Next, see if the treeitem is even "open". If not, then just    // pretend it doesn't have _any_ contents. We check this _before_    // checking the contents-generated attribute so that we don't    // eagerly set contents-generated on a closed node.    if (! IsOpen(aElement))        return NS_OK;    // Now 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,                                              kTreeContentsGeneratedAtom,                                              attrValue))) {        NS_ERROR("unable to test contents-generated attribute");        return rv;    }    if ((rv == NS_CONTENT_ATTR_HAS_VALUE) && (attrValue.EqualsIgnoreCase("true")))        return NS_OK;    // Get the treeitem's resource so that we can generate cell    // values. We could QI for the nsIRDFResource here, but doing this    // via the nsIContent interface allows us to support generic nodes    // that might get added in by DOM calls.    nsAutoString uri;    if (NS_FAILED(rv = aElement->GetAttribute(kNameSpaceID_RDF,                                              kIdAtom,                                              uri))) {        NS_ERROR("severe error retrieving attribute");        return rv;    }    if (rv != NS_CONTENT_ATTR_HAS_VALUE) {        NS_ERROR("tree element has no RDF:ID");        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,                                              kTreeContentsGeneratedAtom,                                              "true",                                              PR_FALSE))) {        NS_ERROR("unable to set contents-generated attribute");        return rv;    }    // XXX Eventually, we may want to factor this into one method that    // handles RDF containers (RDF:Bag, et al.) and another that    // handles multi-attributes. For performance...    // Create a cursor that'll enumerate all of the outbound arcs    nsCOMPtr<nsIRDFResource> resource;    if (NS_FAILED(rv = gRDFService->GetUnicodeResource(uri, getter_AddRefs(resource)))) {        NS_ERROR("unable to create resource");        return rv;    }    nsCOMPtr<nsIRDFArcsOutCursor> properties;    if (NS_FAILED(rv = mDB->ArcLabelsOut(resource, getter_AddRefs(properties))))        return rv;// rjc - sort	nsVoidArray	*tempArray;        if ((tempArray = new nsVoidArray()) == nsnull)	return (NS_ERROR_OUT_OF_MEMORY);    while (NS_SUCCEEDED(rv = properties->Advance())) {        nsCOMPtr<nsIRDFResource> property;        if (NS_FAILED(rv = properties->GetPredicate(getter_AddRefs(property))))            break;        // If it's not a tree property, then it doesn't specify an        // object that is member of the current container element;        // rather, it specifies a "simple" property of the container        // element. Skip it.        if (! IsTreeProperty(aElement, property))            continue;        // Create a second cursor that'll enumerate all of the values        // for all of the arcs.        nsCOMPtr<nsIRDFAssertionCursor> assertions;        if (NS_FAILED(rv = mDB->GetTargets(resource, property, PR_TRUE, getter_AddRefs(assertions)))) {            NS_ERROR("unable to get targets for property");            return rv;        }        while (NS_SUCCEEDED(rv = assertions->Advance())) {            nsCOMPtr<nsIRDFNode> value;            if (NS_FAILED(rv = assertions->GetValue(getter_AddRefs(value)))) {                NS_ERROR("unable to get cursor value");                // return rv;                break;            }            nsCOMPtr<nsIRDFResource> valueResource;            if (NS_SUCCEEDED(value->QueryInterface(kIRDFResourceIID, (void**) getter_AddRefs(valueResource)))                && IsTreeProperty(aElement, property)) {			/* XXX hack: always append value resource 1st!			       due to sort callback implementation */                	tempArray->AppendElement(valueResource);                	tempArray->AppendElement(property);/*                if (NS_FAILED(rv = AddTreeRow(aElement, property, valueResource))) {                    NS_ERROR("unable to create tree row");                    return rv;                }*/            }            else {                if (NS_FAILED(rv = SetCellValue(aElement, property, value))) {                    NS_ERROR("unable to set cell value");                    // return rv;                    break;                }            }        }    }        unsigned long numElements = tempArray->Count();        if (numElements > 0)        {        	nsIRDFResource ** flatArray = (nsIRDFResource **)malloc(numElements * sizeof(void *));        	if (flatArray)        	{			_sortStruct		sortInfo;			// get sorting info (property to sort on, direction to sort, etc)			// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX			// XXX Note: currently hardcoded; should get from DOM, or... ?			// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX			sortInfo.db = mDB;			sortInfo.descendingSort = PR_FALSE;			if (NS_FAILED(rv = gRDFService->GetResource("http://home.netscape.com/NC-rdf#Name", &sortInfo.sortProperty)))			{				NS_ERROR("unable to create gSortProperty resource");				return rv;			}			// flatten array of resources, sort them, then add as tree elements			unsigned long loop;        	        for (loop=0; loop<numElements; loop++)				flatArray[loop] = (nsIRDFResource *)tempArray->ElementAt(loop);        		rdf_qsort((void *)flatArray, numElements/2, 2 * sizeof(void *), rdfSortCallback, (void *)&sortInfo);        		for (loop=0; loop<numElements; loop+=2)        		{				if (NS_FAILED(rv = AddTreeRow(aElement,							(nsIRDFResource *)flatArray[loop+1],							(nsIRDFResource *)flatArray[loop])))				{					NS_ERROR("unable to create tree row");				}        		}        		free(flatArray);        	}        }        delete tempArray;    if (rv == NS_ERROR_RDF_CURSOR_EMPTY)        // This is a normal return code from nsIRDFCursor::Advance()        rv = NS_OK;    return rv;}