if (result) {  NS_ADDREF(result); *aResource = result;
if (PL_DHASH_ENTRY_IS_BUSY(hdr)) { ResourceHashEntry *entry = NS_STATIC_CAST(ResourceHashEntry *, hdr); NS_ADDREF(*aResource = entry->mResource);
RDFServiceImpl::GetResource(const char* aURI, nsIRDFResource** aResource){    // Sanity checks    NS_PRECONDITION(aURI != nsnull, "null ptr");    if (! aURI)        return NS_ERROR_NULL_POINTER;    NS_PRECONDITION(aResource != nsnull, "null ptr");    if (! aResource)        return NS_ERROR_NULL_POINTER;    PR_LOG(gLog, PR_LOG_DEBUG, ("rdfserv get-resource %s", aURI));    // First, check the cache to see if we've already created and    // registered this thing.    nsIRDFResource* result =        NS_STATIC_CAST(nsIRDFResource*, PL_HashTableLookup(mResources, aURI));    if (result) {        // Addref for the callee.        NS_ADDREF(result);        *aResource = result;        return NS_OK;    }    // Nope. So go to the repository to create it.    // Compute the scheme of the URI. Scan forward until we either:    //    // 1. Reach the end of the string    // 2. Encounter a non-alpha character    // 3. Encouter a colon.    //    // If we encounter a colon _before_ encountering a non-alpha    // character, then assume it's the scheme.    //    // XXX Although it's really not correct, we'll allow underscore    // characters ('_'), too.    const char* p = aURI;    while (IsLegalSchemeCharacter(*p))        ++p;    nsresult rv;    nsCOMPtr<nsIFactory> factory;    PRUint32 prefixlen = 0;    if (*p == ':') {        // There _was_ a scheme. First see if it's the same scheme        // that we just tried to use...        prefixlen = (p - aURI);        if ((mLastFactory) && ((PRInt32)prefixlen == mLastPrefixlen) &&            (aURI[0] == mLastURIPrefix[0]) &&            (0 == PL_strncmp(aURI, mLastURIPrefix, prefixlen))) {            factory = mLastFactory;        }        else {            // Try to find a factory using the component manager.            static const char kRDFResourceFactoryContractIDPrefix[]                = NS_RDF_RESOURCE_FACTORY_CONTRACTID_PREFIX;            PRInt32 pos = p - aURI;            PRInt32 len = pos + sizeof(kRDFResourceFactoryContractIDPrefix) - 1;            // Safely convert to a C-string for the XPCOM APIs            char buf[128];            char* contractID = buf;            if (len >= PRInt32(sizeof buf))                contractID = (char *)nsMemory::Alloc(len + 1);            if (contractID == nsnull)                return NS_ERROR_OUT_OF_MEMORY;            PL_strcpy(contractID, kRDFResourceFactoryContractIDPrefix);            PL_strncpy(contractID + sizeof(kRDFResourceFactoryContractIDPrefix) - 1, aURI, pos);            contractID[len] = '\0';            nsCID cid;            rv = nsComponentManager::ContractIDToClassID(contractID, &cid);            if (contractID != buf)                nsCRT::free(contractID);            if (NS_SUCCEEDED(rv)) {                rv = nsComponentManager::FindFactory(cid, getter_AddRefs(factory));                NS_ASSERTION(NS_SUCCEEDED(rv), "factory registered, but couldn't load");                if (NS_FAILED(rv)) return rv;                // Store the factory in our one-element cache.                if ((prefixlen > 0) && (prefixlen < sizeof(mLastURIPrefix))) {                    mLastFactory = factory;                    PL_strncpyz(mLastURIPrefix, aURI, prefixlen + 1);                    mLastPrefixlen = prefixlen;                }            }        }    }    if (! factory) {        // fall through to using the "default" resource factory if either:        //        // 1. The URI didn't have a scheme, or        // 2. There was no resource factory registered for the scheme.        factory = mDefaultResourceFactory;        // Store the factory in our one-element cache.        if ((prefixlen > 0) && (prefixlen < sizeof(mLastURIPrefix))) {            mLastFactory = factory;            PL_strncpyz(mLastURIPrefix, aURI, prefixlen + 1);            mLastPrefixlen = prefixlen;        }    }    rv = factory->CreateInstance(nsnull, NS_GET_IID(nsIRDFResource), (void**) &result);    if (NS_FAILED(rv)) return rv;    // Now initialize it with it's URI. At this point, the resource    // implementation should register itself with the RDF service.    rv = result->Init(aURI);    if (NS_FAILED(rv)) {        NS_ERROR("unable to initialize resource");        NS_RELEASE(result);        return rv;    }    *aResource = result; // already refcounted from repository    return rv;}