NS_ASSERTION(rv != NS_RDF_NO_VALUE, "null value in cursor"); if (rv == NS_RDF_NO_VALUE) continue;
rdf_ContainerInsertElementAt(nsIRDFDataSource* aDataSource,                             nsIRDFResource* aContainer,                             nsIRDFNode* aElement,                             PRInt32 aIndex){    NS_PRECONDITION(aDataSource != nsnull, "null ptr");    if (! aDataSource)        return NS_ERROR_NULL_POINTER;    NS_PRECONDITION(aContainer != nsnull, "null ptr");    if (! aContainer)        return NS_ERROR_NULL_POINTER;    NS_PRECONDITION(aElement != nsnull, "null ptr");    if (! aElement)        return NS_ERROR_NULL_POINTER;    NS_PRECONDITION(aIndex >= 1, "illegal value");    if (aIndex < 1)        return NS_ERROR_ILLEGAL_VALUE;    nsresult rv;    nsCOMPtr<nsIRDFAssertionCursor> elements;    if (NS_FAILED(rv = NS_NewContainerCursor(aDataSource,                                             aContainer,                                             getter_AddRefs(elements)))) {        NS_ERROR("unable to create container cursor");        return rv;    }    nsCOMPtr<nsIRDFResource> ordinal;    PRInt32 index = 1;    // Advance the cursor to the aIndex'th element.    while (1) {        rv = elements->Advance();        if (NS_FAILED(rv))            return rv;        if (rv == NS_RDF_CURSOR_EMPTY)            break;        if (NS_FAILED(rv = elements->GetLabel(getter_AddRefs(ordinal)))) {            NS_ERROR("unable to get element's ordinal index");            return rv;        }        if (NS_FAILED(rv = rdf_OrdinalResourceToIndex(ordinal, &index))) {            NS_ERROR("unable to convert ordinal resource to index");            return rv;        }        // XXX Use >= in case any "holes" exist...        if (index >= aIndex)            break;    }    // Remember if we've exhausted the cursor: if so, this degenerates    // into a simple "append" operation.    PRBool cursorExhausted = (rv == NS_RDF_CURSOR_EMPTY);    // XXX Be paranoid: there may have been a "hole"    if (index > aIndex)        index = aIndex;    // If we ran all the way to the end of the cursor, then "index"    // will contain the ordinal value of the last element in the    // container. Increment it by one to get the position at which    // we'll want to insert it into the container.    if (cursorExhausted && aIndex > index) {        if (index != aIndex) {            // Sanity check only: aIndex was _way_ too large...            NS_WARNING("out of bounds");        }        ++index;    }    // At this point "index" contains the index at which we want to    // insert the new element.    if (NS_FAILED(rv = rdf_IndexToOrdinalResource(index, getter_AddRefs(ordinal)))) {        NS_ERROR("unable to convert index to ordinal resource");        return rv;    }    // Insert it!    if (NS_FAILED(rv = aDataSource->Assert(aContainer, ordinal, aElement, PR_TRUE))) {        NS_ERROR("unable to add element to container");        return rv;    }    // Now slide the rest of the container "up" by one...    if (! cursorExhausted) {        do {            if (NS_FAILED(rv = elements->GetLabel(getter_AddRefs(ordinal)))) {                NS_ERROR("unable to get element's ordinal index");                return rv;            }            nsCOMPtr<nsIRDFNode> element;            if (NS_FAILED(rv = elements->GetTarget(getter_AddRefs(element)))) {                NS_ERROR("unable to get element from cursor");                return rv;            }            if (NS_FAILED(rv = aDataSource->Unassert(aContainer, ordinal, element))) {                NS_ERROR("unable to remove element from container");                return rv;            }            if (NS_FAILED(rv = rdf_IndexToOrdinalResource(++index, getter_AddRefs(ordinal)))) {                NS_ERROR("unable to convert index to ordinal resource");                return rv;            }            if (NS_FAILED(rv = aDataSource->Assert(aContainer, ordinal, element, PR_TRUE))) {                NS_ERROR("unable to add element to container");                return rv;            }            rv = elements->Advance();            if (NS_FAILED(rv))                return rv;            if (rv == NS_RDF_CURSOR_EMPTY)                break;        }        while (1);    }    // Now update the container's nextVal    if (NS_FAILED(rv = rdf_ContainerSetNextValue(aDataSource, aContainer, ++index))) {        NS_ERROR("unable to update container's nextVal");        return rv;    }    return NS_OK;}