if (!mDataSource || !mContainer) return NS_ERROR_NOT_INITIALIZED;
RDFContainerImpl::Renumber(PRInt32 aStartIndex, PRInt32 aIncrement){    // Renumber the elements in the container starting with    // aStartIndex, updating each element's index by aIncrement. For    // example,    //    //   (1:a 2:b 3:c)    //   Renumber(2, +1);    //   (1:a 3:b 4:c)    //   Renumber(3, -1);    //   (1:a 2:b 3:c)    //    nsresult rv;    if (! aIncrement)        return NS_OK;    PRInt32 count;    rv = GetCount(&count);    if (NS_FAILED(rv)) return rv;    if (aIncrement > 0) {        // Update the container's nextVal to reflect the        // renumbering. We do this now if aIncrement > 0 because we'll        // want to be able to acknowledge that new elements are in the        // container.        rv = SetNextValue(count + aIncrement + 1);        if (NS_FAILED(rv)) return rv;    }    PRInt32 i;    if (aIncrement < 0) {        i = aStartIndex;    }    else {        i = count; // we're one-indexed.    }    // Note: once we begin batch updates, don't exit this method until    // ending batch updates, otherwise viewers will get out of sync    nsCOMPtr<nsIRDFObserver>    dsObs;    if (mDataSource)    {        dsObs = do_QueryInterface(mDataSource);        if (dsObs)        {            dsObs->BeginUpdateBatch(mDataSource);        }    }    PRBool  err = PR_FALSE;    while ((err == PR_FALSE) && ((aIncrement < 0) ? (i <= count) : (i >= aStartIndex)))    {        nsCOMPtr<nsIRDFResource> oldOrdinal;        rv = gRDFContainerUtils->IndexToOrdinalResource(i, getter_AddRefs(oldOrdinal));        if (NS_FAILED(rv))        {            err = PR_TRUE;            continue;        }        nsCOMPtr<nsIRDFResource> newOrdinal;        rv = gRDFContainerUtils->IndexToOrdinalResource(i + aIncrement, getter_AddRefs(newOrdinal));        if (NS_FAILED(rv))        {            err = PR_TRUE;            continue;        }        // Because of aggregation, we need to be paranoid about the        // possibility that >1 element may be present per ordinal. If        // there _is_ in fact more than one element, they'll all get        // assigned to the same new ordinal; i.e., we don't make any        // attempt to "clean up" the duplicate numbering. (Doing so        // would require two passes.)        nsCOMPtr<nsISimpleEnumerator> targets;        rv = mDataSource->GetTargets(mContainer, oldOrdinal, PR_TRUE, getter_AddRefs(targets));        if (NS_FAILED(rv))        {            err = PR_TRUE;            continue;        }        while (1) {            PRBool hasMore;            rv = targets->HasMoreElements(&hasMore);            if (NS_FAILED(rv))            {                err = PR_TRUE;                break;            }            if (! hasMore)                break;            nsCOMPtr<nsISupports> isupports;            rv = targets->GetNext(getter_AddRefs(isupports));            if (NS_FAILED(rv))            {                err = PR_TRUE;                break;            }            nsCOMPtr<nsIRDFNode> element( do_QueryInterface(isupports) );            NS_ASSERTION(element != nsnull, "something funky in the enumerator");            if (! element)            {                err = PR_TRUE;                rv = NS_ERROR_UNEXPECTED;                break;            }            rv = mDataSource->Unassert(mContainer, oldOrdinal, element);            if (NS_FAILED(rv))            {                err = PR_TRUE;                break;            }            rv = mDataSource->Assert(mContainer, newOrdinal, element, PR_TRUE);            if (NS_FAILED(rv))            {                err = PR_TRUE;                break;            }        }        i -= aIncrement;    }    if ((err == PR_FALSE) && (aIncrement < 0))    {        // Update the container's nextVal to reflect the        // renumbering. We do this now if aIncrement < 0 because, up        // until this point, we'll want people to be able to find        // things that are still "at the end".        rv = SetNextValue(count + aIncrement + 1);        if (NS_FAILED(rv))        {            err = PR_TRUE;        }    }    // Note: MUST end update batching before exiting this method    // otherwise viewers will get out of sync    if (dsObs)  dsObs->EndUpdateBatch(mDataSource);    if (err == PR_TRUE) return(rv);    return NS_OK;}