nsCID cid; rv = nsComponentManager::ContractIDToClassID(contractID.get(), &cid);  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;
factory = do_GetClassObject(contractID.get()); if (factory) {
RDFServiceImpl::GetResource(const nsACString& aURI, nsIRDFResource** aResource){    // Sanity checks    NS_PRECONDITION(aResource != nsnull, "null ptr");    NS_PRECONDITION(!aURI.IsEmpty(), "URI is empty");    if (! aResource)        return NS_ERROR_NULL_POINTER;    if (aURI.IsEmpty())        return NS_ERROR_INVALID_ARG;    const nsAFlatCString& flatURI = PromiseFlatCString(aURI);    PR_LOG(gLog, PR_LOG_DEBUG, ("rdfserv get-resource %s", flatURI.get()));    // First, check the cache to see if we've already created and    // registered this thing.    PLDHashEntryHdr *hdr =        PL_DHashTableOperate(&mResources, flatURI.get(), PL_DHASH_LOOKUP);    if (PL_DHASH_ENTRY_IS_BUSY(hdr)) {        ResourceHashEntry *entry = NS_STATIC_CAST(ResourceHashEntry *, hdr);        NS_ADDREF(*aResource = entry->mResource);        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.    nsACString::const_iterator p, end;    aURI.BeginReading(p);    aURI.EndReading(end);    while (p != end && IsLegalSchemeCharacter(*p))        ++p;    nsresult rv;    nsCOMPtr<nsIFactory> factory;    nsACString::const_iterator begin;    aURI.BeginReading(begin);    if (*p == ':') {        // There _was_ a scheme. First see if it's the same scheme        // that we just tried to use...        if (mLastFactory && mLastURIPrefix.Equals(Substring(begin, p)))            factory = mLastFactory;        else {            // Try to find a factory using the component manager.            nsACString::const_iterator begin;            aURI.BeginReading(begin);            nsCAutoString contractID;            contractID = NS_LITERAL_CSTRING(NS_RDF_RESOURCE_FACTORY_CONTRACTID_PREFIX) +                         Substring(begin, p);            nsCID cid;            rv = nsComponentManager::ContractIDToClassID(contractID.get(), &cid);            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 (p != begin) {                    mLastFactory = factory;                    mLastURIPrefix = Substring(begin, p);                }            }        }    }    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 (p != begin) {            mLastFactory = factory;            mLastURIPrefix = Substring(begin, p);        }    }    nsIRDFResource *result;    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(flatURI.get());    if (NS_FAILED(rv)) {        NS_ERROR("unable to initialize resource");        NS_RELEASE(result);        return rv;    }    *aResource = result; // already refcounted from repository    return rv;}