do {
if (mCurrent) {
ContainerEnumeratorImpl::HasMoreElements(PRBool* aResult){    NS_PRECONDITION(aResult != nsnull, "null ptr");    if (! aResult)        return NS_ERROR_NULL_POINTER;    nsresult rv;    // If we've already queued up a next value, then we know there are more elements.    if (mResult) {        *aResult = PR_TRUE;        return NS_OK;    }    // Otherwise, we need to grovel    // Figure out the upper bound so we'll know when we're done. Since it's    // possible that we're targeting a composite datasource, we'll need to    // "GetTargets()" and take the maximum value of "nextVal" to know the    // upper bound.    PRInt32 count = 0;    nsCOMPtr<nsISimpleEnumerator> targets;    rv = mDataSource->GetTargets(mContainer, kRDF_nextVal, PR_TRUE, getter_AddRefs(targets));    if (NS_FAILED(rv)) return rv;    while (1) {        PRBool hasmore;        targets->HasMoreElements(&hasmore);        if (! hasmore)            break;        nsCOMPtr<nsISupports> isupports;        targets->GetNext(getter_AddRefs(isupports));        nsCOMPtr<nsIRDFLiteral> nextValLiteral = do_QueryInterface(isupports);        if (! nextValLiteral)             continue;         nsXPIDLString nextValStr;         nextValLiteral->GetValue(getter_Copies(nextValStr));         PRInt32 err;         PRInt32 nextVal = nsAutoString(nextValStr).ToInteger(&err);         if (nextVal > count)             count = nextVal;    }    // Now iterate through each index.    while (mCurrent || mNextIndex < count) {        if (! mCurrent) {            NS_WITH_SERVICE(nsIRDFContainerUtils, rdfc, kRDFContainerUtilsCID, &rv);            if (NS_FAILED(rv)) return rv;            rv = rdfc->IndexToOrdinalResource(mNextIndex, getter_AddRefs(mOrdinalProperty));            if (NS_FAILED(rv)) return rv;            rv = mDataSource->GetTargets(mContainer, mOrdinalProperty, PR_TRUE, &mCurrent);            if (NS_FAILED(rv)) return rv;            ++mNextIndex;        }        do {            PRBool hasMore;            rv = mCurrent->HasMoreElements(&hasMore);            if (NS_FAILED(rv)) return rv;            // Is the current enumerator depleted?            if (! hasMore) {                NS_RELEASE(mCurrent);                break;            }            // "Peek" ahead and pull out the next target.            nsCOMPtr<nsISupports> result;            rv = mCurrent->GetNext(getter_AddRefs(result));            if (NS_FAILED(rv)) return rv;            rv = result->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) &mResult);            if (NS_FAILED(rv)) return rv;            *aResult = PR_TRUE;            return NS_OK;        } while (1);    }    // If we get here, we ran out of elements. The cursor is empty.    *aResult = PR_FALSE;    return NS_OK;}