gecko-dev/rdf/util/nsRDFResource.cpp
Birunthan Mohanathas 2864f7996a Bug 1040774 - Flatten rdf/{base,datasource,util}/{idl,public,src}/ directories. r=pike
--HG--
rename : rdf/base/src/nsCompositeDataSource.cpp => rdf/base/nsCompositeDataSource.cpp
rename : rdf/base/src/nsContainerEnumerator.cpp => rdf/base/nsContainerEnumerator.cpp
rename : rdf/base/src/nsDefaultResourceFactory.cpp => rdf/base/nsDefaultResourceFactory.cpp
rename : rdf/base/idl/nsIRDFCompositeDataSource.idl => rdf/base/nsIRDFCompositeDataSource.idl
rename : rdf/base/idl/nsIRDFContainer.idl => rdf/base/nsIRDFContainer.idl
rename : rdf/base/idl/nsIRDFContainerUtils.idl => rdf/base/nsIRDFContainerUtils.idl
rename : rdf/base/public/nsIRDFContentSink.h => rdf/base/nsIRDFContentSink.h
rename : rdf/base/idl/nsIRDFDataSource.idl => rdf/base/nsIRDFDataSource.idl
rename : rdf/base/idl/nsIRDFDelegateFactory.idl => rdf/base/nsIRDFDelegateFactory.idl
rename : rdf/base/idl/nsIRDFInMemoryDataSource.idl => rdf/base/nsIRDFInMemoryDataSource.idl
rename : rdf/base/idl/nsIRDFInferDataSource.idl => rdf/base/nsIRDFInferDataSource.idl
rename : rdf/base/idl/nsIRDFLiteral.idl => rdf/base/nsIRDFLiteral.idl
rename : rdf/base/idl/nsIRDFNode.idl => rdf/base/nsIRDFNode.idl
rename : rdf/base/idl/nsIRDFObserver.idl => rdf/base/nsIRDFObserver.idl
rename : rdf/base/idl/nsIRDFPropagatableDataSource.idl => rdf/base/nsIRDFPropagatableDataSource.idl
rename : rdf/base/idl/nsIRDFPurgeableDataSource.idl => rdf/base/nsIRDFPurgeableDataSource.idl
rename : rdf/base/idl/nsIRDFRemoteDataSource.idl => rdf/base/nsIRDFRemoteDataSource.idl
rename : rdf/base/idl/nsIRDFResource.idl => rdf/base/nsIRDFResource.idl
rename : rdf/base/idl/nsIRDFService.idl => rdf/base/nsIRDFService.idl
rename : rdf/base/idl/nsIRDFXMLParser.idl => rdf/base/nsIRDFXMLParser.idl
rename : rdf/base/idl/nsIRDFXMLSerializer.idl => rdf/base/nsIRDFXMLSerializer.idl
rename : rdf/base/idl/nsIRDFXMLSink.idl => rdf/base/nsIRDFXMLSink.idl
rename : rdf/base/idl/nsIRDFXMLSource.idl => rdf/base/nsIRDFXMLSource.idl
rename : rdf/base/src/nsInMemoryDataSource.cpp => rdf/base/nsInMemoryDataSource.cpp
rename : rdf/base/src/nsNameSpaceMap.cpp => rdf/base/nsNameSpaceMap.cpp
rename : rdf/base/src/nsNameSpaceMap.h => rdf/base/nsNameSpaceMap.h
rename : rdf/base/src/nsRDFBaseDataSources.h => rdf/base/nsRDFBaseDataSources.h
rename : rdf/base/src/nsRDFContainer.cpp => rdf/base/nsRDFContainer.cpp
rename : rdf/base/src/nsRDFContainerUtils.cpp => rdf/base/nsRDFContainerUtils.cpp
rename : rdf/base/src/nsRDFContentSink.cpp => rdf/base/nsRDFContentSink.cpp
rename : rdf/base/src/nsRDFContentSinkAtomList.h => rdf/base/nsRDFContentSinkAtomList.h
rename : rdf/base/src/nsRDFService.cpp => rdf/base/nsRDFService.cpp
rename : rdf/base/src/nsRDFService.h => rdf/base/nsRDFService.h
rename : rdf/base/src/nsRDFXMLDataSource.cpp => rdf/base/nsRDFXMLDataSource.cpp
rename : rdf/base/src/nsRDFXMLParser.cpp => rdf/base/nsRDFXMLParser.cpp
rename : rdf/base/src/nsRDFXMLParser.h => rdf/base/nsRDFXMLParser.h
rename : rdf/base/src/nsRDFXMLSerializer.cpp => rdf/base/nsRDFXMLSerializer.cpp
rename : rdf/base/src/nsRDFXMLSerializer.h => rdf/base/nsRDFXMLSerializer.h
rename : rdf/base/public/rdf.h => rdf/base/rdf.h
rename : rdf/base/idl/rdfIDataSource.idl => rdf/base/rdfIDataSource.idl
rename : rdf/base/idl/rdfISerializer.idl => rdf/base/rdfISerializer.idl
rename : rdf/base/idl/rdfITripleVisitor.idl => rdf/base/rdfITripleVisitor.idl
rename : rdf/base/src/rdfTriplesSerializer.cpp => rdf/base/rdfTriplesSerializer.cpp
rename : rdf/base/src/rdfutil.cpp => rdf/base/rdfutil.cpp
rename : rdf/base/src/rdfutil.h => rdf/base/rdfutil.h
rename : rdf/datasource/src/nsFileSystemDataSource.cpp => rdf/datasource/nsFileSystemDataSource.cpp
rename : rdf/datasource/src/nsFileSystemDataSource.h => rdf/datasource/nsFileSystemDataSource.h
rename : rdf/datasource/public/nsILocalStore.h => rdf/datasource/nsILocalStore.h
rename : rdf/datasource/public/nsIRDFFTP.h => rdf/datasource/nsIRDFFTP.h
rename : rdf/datasource/src/nsLocalStore.cpp => rdf/datasource/nsLocalStore.cpp
rename : rdf/datasource/src/nsRDFBuiltInDataSources.h => rdf/datasource/nsRDFBuiltInDataSources.h
rename : rdf/util/src/Makefile.in => rdf/util/Makefile.in
rename : rdf/util/src/internal/moz.build => rdf/util/internal/moz.build
rename : rdf/util/src/nsRDFResource.cpp => rdf/util/nsRDFResource.cpp
rename : rdf/util/public/nsRDFResource.h => rdf/util/nsRDFResource.h
rename : rdf/util/src/objs.mozbuild => rdf/util/objs.mozbuild
2014-07-21 06:01:08 -07:00

222 lines
5.4 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsRDFResource.h"
#include "nsIServiceManager.h"
#include "nsIRDFDelegateFactory.h"
#include "nsIRDFService.h"
#include "nsRDFCID.h"
#include "prlog.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
nsIRDFService* nsRDFResource::gRDFService = nullptr;
nsrefcnt nsRDFResource::gRDFServiceRefCnt = 0;
////////////////////////////////////////////////////////////////////////////////
nsRDFResource::nsRDFResource(void)
: mDelegates(nullptr)
{
}
nsRDFResource::~nsRDFResource(void)
{
// Release all of the delegate objects
while (mDelegates) {
DelegateEntry* doomed = mDelegates;
mDelegates = mDelegates->mNext;
delete doomed;
}
if (!gRDFService)
return;
gRDFService->UnregisterResource(this);
if (--gRDFServiceRefCnt == 0)
NS_RELEASE(gRDFService);
}
NS_IMPL_ISUPPORTS(nsRDFResource, nsIRDFResource, nsIRDFNode)
////////////////////////////////////////////////////////////////////////////////
// nsIRDFNode methods:
NS_IMETHODIMP
nsRDFResource::EqualsNode(nsIRDFNode* aNode, bool* aResult)
{
NS_PRECONDITION(aNode != nullptr, "null ptr");
if (! aNode)
return NS_ERROR_NULL_POINTER;
nsresult rv;
nsIRDFResource* resource;
rv = aNode->QueryInterface(NS_GET_IID(nsIRDFResource), (void**)&resource);
if (NS_SUCCEEDED(rv)) {
*aResult = (static_cast<nsIRDFResource*>(this) == resource);
NS_RELEASE(resource);
return NS_OK;
}
else if (rv == NS_NOINTERFACE) {
*aResult = false;
return NS_OK;
}
else {
return rv;
}
}
////////////////////////////////////////////////////////////////////////////////
// nsIRDFResource methods:
NS_IMETHODIMP
nsRDFResource::Init(const char* aURI)
{
NS_PRECONDITION(aURI != nullptr, "null ptr");
if (! aURI)
return NS_ERROR_NULL_POINTER;
mURI = aURI;
if (gRDFServiceRefCnt++ == 0) {
nsresult rv = CallGetService(kRDFServiceCID, &gRDFService);
if (NS_FAILED(rv)) return rv;
}
// don't replace an existing resource with the same URI automatically
return gRDFService->RegisterResource(this, true);
}
NS_IMETHODIMP
nsRDFResource::GetValue(char* *aURI)
{
NS_ASSERTION(aURI, "Null out param.");
*aURI = ToNewCString(mURI);
if (!*aURI)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
NS_IMETHODIMP
nsRDFResource::GetValueUTF8(nsACString& aResult)
{
aResult = mURI;
return NS_OK;
}
NS_IMETHODIMP
nsRDFResource::GetValueConst(const char** aURI)
{
*aURI = mURI.get();
return NS_OK;
}
NS_IMETHODIMP
nsRDFResource::EqualsString(const char* aURI, bool* aResult)
{
NS_PRECONDITION(aURI != nullptr, "null ptr");
if (! aURI)
return NS_ERROR_NULL_POINTER;
NS_PRECONDITION(aResult, "null ptr");
*aResult = mURI.Equals(aURI);
return NS_OK;
}
NS_IMETHODIMP
nsRDFResource::GetDelegate(const char* aKey, REFNSIID aIID, void** aResult)
{
NS_PRECONDITION(aKey != nullptr, "null ptr");
if (! aKey)
return NS_ERROR_NULL_POINTER;
nsresult rv;
*aResult = nullptr;
DelegateEntry* entry = mDelegates;
while (entry) {
if (entry->mKey.Equals(aKey)) {
rv = entry->mDelegate->QueryInterface(aIID, aResult);
return rv;
}
entry = entry->mNext;
}
// Construct a ContractID of the form "@mozilla.org/rdf/delegate/[key]/[scheme];1
nsAutoCString contractID(NS_RDF_DELEGATEFACTORY_CONTRACTID_PREFIX);
contractID.Append(aKey);
contractID.AppendLiteral("&scheme=");
int32_t i = mURI.FindChar(':');
contractID += StringHead(mURI, i);
nsCOMPtr<nsIRDFDelegateFactory> delegateFactory =
do_CreateInstance(contractID.get(), &rv);
if (NS_FAILED(rv)) return rv;
rv = delegateFactory->CreateDelegate(this, aKey, aIID, aResult);
if (NS_FAILED(rv)) return rv;
// Okay, we've successfully created a delegate. Let's remember it.
entry = new DelegateEntry;
if (! entry) {
NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
return NS_ERROR_OUT_OF_MEMORY;
}
entry->mKey = aKey;
entry->mDelegate = do_QueryInterface(*reinterpret_cast<nsISupports**>(aResult), &rv);
if (NS_FAILED(rv)) {
NS_ERROR("nsRDFResource::GetDelegate(): can't QI to nsISupports!");
delete entry;
NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
return NS_ERROR_FAILURE;
}
entry->mNext = mDelegates;
mDelegates = entry;
return NS_OK;
}
NS_IMETHODIMP
nsRDFResource::ReleaseDelegate(const char* aKey)
{
NS_PRECONDITION(aKey != nullptr, "null ptr");
if (! aKey)
return NS_ERROR_NULL_POINTER;
DelegateEntry* entry = mDelegates;
DelegateEntry** link = &mDelegates;
while (entry) {
if (entry->mKey.Equals(aKey)) {
*link = entry->mNext;
delete entry;
return NS_OK;
}
link = &(entry->mNext);
entry = entry->mNext;
}
NS_WARNING("nsRDFResource::ReleaseDelegate() no delegate found");
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////