2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1999-02-17 23:42:09 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
1999-02-17 23:42:09 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-02-17 23:42:09 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:43:54 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Contributor(s):
|
2000-01-11 20:49:15 +00:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-02-17 23:42:09 +00:00
|
|
|
|
|
|
|
#include "nsRDFResource.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsIServiceManager.h"
|
1999-11-24 20:51:39 +00:00
|
|
|
#include "nsIRDFDelegateFactory.h"
|
1999-02-17 23:42:09 +00:00
|
|
|
#include "nsIRDFService.h"
|
|
|
|
#include "nsRDFCID.h"
|
1999-03-29 19:52:54 +00:00
|
|
|
#include "nsXPIDLString.h"
|
1999-03-09 09:44:27 +00:00
|
|
|
#include "prlog.h"
|
1999-02-17 23:42:09 +00:00
|
|
|
|
|
|
|
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
|
|
|
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
|
|
|
|
|
1999-09-21 20:02:50 +00:00
|
|
|
nsIRDFService* nsRDFResource::gRDFService = nsnull;
|
|
|
|
nsrefcnt nsRDFResource::gRDFServiceRefCnt = 0;
|
1999-03-02 01:44:01 +00:00
|
|
|
|
1999-02-17 23:42:09 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-02-26 10:17:14 +00:00
|
|
|
nsRDFResource::nsRDFResource(void)
|
1999-11-24 20:51:39 +00:00
|
|
|
: mURI(nsnull), mDelegates(nsnull)
|
1999-02-17 23:42:09 +00:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRDFResource::~nsRDFResource(void)
|
|
|
|
{
|
1999-11-24 20:51:39 +00:00
|
|
|
// Release all of the delegate objects
|
|
|
|
while (mDelegates) {
|
|
|
|
DelegateEntry* doomed = mDelegates;
|
|
|
|
mDelegates = mDelegates->mNext;
|
|
|
|
delete doomed;
|
|
|
|
}
|
|
|
|
|
1999-09-21 20:02:50 +00:00
|
|
|
gRDFService->UnregisterResource(this);
|
1999-02-17 23:42:09 +00:00
|
|
|
|
|
|
|
// N.B. that we need to free the URI *after* we un-cache the resource,
|
|
|
|
// due to the way that the resource manager is implemented.
|
1999-09-04 15:57:05 +00:00
|
|
|
nsCRT::free(mURI);
|
1999-09-21 20:02:50 +00:00
|
|
|
|
|
|
|
if (--gRDFServiceRefCnt == 0) {
|
|
|
|
nsServiceManager::ReleaseService(kRDFServiceCID, gRDFService);
|
|
|
|
gRDFService = nsnull;
|
|
|
|
}
|
1999-02-17 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
2000-03-05 21:26:01 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(nsRDFResource, nsIRDFResource, nsIRDFNode)
|
1999-02-17 23:42:09 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIRDFNode methods:
|
|
|
|
|
1999-02-26 10:17:14 +00:00
|
|
|
NS_IMETHODIMP
|
1999-06-25 20:51:13 +00:00
|
|
|
nsRDFResource::EqualsNode(nsIRDFNode* aNode, PRBool* aResult)
|
1999-02-17 23:42:09 +00:00
|
|
|
{
|
1999-06-25 20:51:13 +00:00
|
|
|
NS_PRECONDITION(aNode != nsnull, "null ptr");
|
|
|
|
if (! aNode)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-02-17 23:42:09 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsIRDFResource* resource;
|
2000-01-11 20:49:15 +00:00
|
|
|
rv = aNode->QueryInterface(NS_GET_IID(nsIRDFResource), (void**)&resource);
|
1999-06-25 20:51:13 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*aResult = (NS_STATIC_CAST(nsIRDFResource*, this) == resource);
|
1999-02-17 23:42:09 +00:00
|
|
|
NS_RELEASE(resource);
|
1999-06-25 20:51:13 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else if (rv == NS_NOINTERFACE) {
|
|
|
|
*aResult = PR_FALSE;
|
|
|
|
return NS_OK;
|
1999-02-17 23:42:09 +00:00
|
|
|
}
|
|
|
|
else {
|
1999-06-25 20:51:13 +00:00
|
|
|
return rv;
|
1999-02-17 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIRDFResource methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-03-29 19:52:54 +00:00
|
|
|
nsRDFResource::Init(const char* aURI)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aURI != nsnull, "null ptr");
|
|
|
|
if (! aURI)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2000-06-03 09:46:12 +00:00
|
|
|
if (! (mURI = (char *)nsMemory::Alloc(nsCRT::strlen(aURI) + 1)))
|
1999-03-29 19:52:54 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
PL_strcpy(mURI, aURI);
|
|
|
|
|
1999-09-03 16:49:26 +00:00
|
|
|
nsresult rv = NS_OK;
|
1999-09-21 20:02:50 +00:00
|
|
|
if (gRDFServiceRefCnt++ == 0) {
|
|
|
|
rv = nsServiceManager::GetService(kRDFServiceCID,
|
2000-01-11 20:49:15 +00:00
|
|
|
NS_GET_IID(nsIRDFService),
|
1999-09-21 20:02:50 +00:00
|
|
|
(nsISupports**)&gRDFService);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-03-31 19:22:10 +00:00
|
|
|
}
|
|
|
|
|
1999-03-29 19:52:54 +00:00
|
|
|
// don't replace an existing resource with the same URI automatically
|
1999-09-21 20:02:50 +00:00
|
|
|
return gRDFService->RegisterResource(this, PR_TRUE);
|
1999-03-29 19:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-06-25 20:51:13 +00:00
|
|
|
nsRDFResource::GetValue(char* *aURI)
|
1999-02-17 23:42:09 +00:00
|
|
|
{
|
1999-06-25 20:51:13 +00:00
|
|
|
if (!aURI)
|
1999-02-17 23:42:09 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-03-29 19:52:54 +00:00
|
|
|
|
2001-06-17 05:23:38 +00:00
|
|
|
if ((*aURI = nsCRT::strdup(mURI)) == nsnull)
|
1999-03-29 19:52:54 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
else
|
|
|
|
return NS_OK;
|
1999-02-17 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-06-25 20:51:13 +00:00
|
|
|
nsRDFResource::GetValueConst(const char** aURI)
|
1999-02-17 23:42:09 +00:00
|
|
|
{
|
1999-06-25 20:51:13 +00:00
|
|
|
*aURI = mURI;
|
1999-03-26 04:44:24 +00:00
|
|
|
return NS_OK;
|
1999-02-17 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-06-25 20:51:13 +00:00
|
|
|
nsRDFResource::EqualsString(const char* aURI, PRBool* aResult)
|
1999-02-17 23:42:09 +00:00
|
|
|
{
|
1999-06-25 20:51:13 +00:00
|
|
|
NS_PRECONDITION(aURI != nsnull, "null ptr");
|
|
|
|
if (! aURI)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-03-26 04:44:24 +00:00
|
|
|
|
1999-06-25 20:51:13 +00:00
|
|
|
NS_PRECONDITION(aResult != nsnull, "null ptr");
|
|
|
|
if (!aResult)
|
1999-02-17 23:42:09 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-03-26 04:44:24 +00:00
|
|
|
|
1999-06-25 20:51:13 +00:00
|
|
|
*aResult = (nsCRT::strcmp(aURI, mURI) == 0);
|
1999-02-17 23:42:09 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-24 20:51:39 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRDFResource::GetDelegate(const char* aKey, REFNSIID aIID, void** aResult)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aKey != nsnull, "null ptr");
|
|
|
|
if (! aKey)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
*aResult = nsnull;
|
|
|
|
|
|
|
|
DelegateEntry* entry = mDelegates;
|
|
|
|
while (entry) {
|
2000-09-03 22:45:50 +00:00
|
|
|
if (entry->mKey.Equals(aKey)) {
|
1999-11-24 20:51:39 +00:00
|
|
|
rv = entry->mDelegate->QueryInterface(aIID, aResult);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = entry->mNext;
|
|
|
|
}
|
|
|
|
|
2000-09-13 23:57:52 +00:00
|
|
|
// Construct a ContractID of the form "@mozilla.org/rdf/delegate/[key]/[scheme];1
|
|
|
|
nsCAutoString contractID(NS_RDF_DELEGATEFACTORY_CONTRACTID_PREFIX);
|
|
|
|
contractID.Append(aKey);
|
2000-09-15 06:43:40 +00:00
|
|
|
contractID.Append("&scheme=");
|
1999-11-24 20:51:39 +00:00
|
|
|
|
|
|
|
for (const char* p = mURI; *p && *p != ':'; ++p)
|
2000-09-13 23:57:52 +00:00
|
|
|
contractID.Append(*p);
|
1999-11-24 20:51:39 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFDelegateFactory> delegateFactory;
|
2000-09-13 23:57:52 +00:00
|
|
|
rv = nsComponentManager::CreateInstance(contractID,
|
1999-11-24 20:51:39 +00:00
|
|
|
nsnull,
|
|
|
|
NS_GET_IID(nsIRDFDelegateFactory),
|
|
|
|
getter_AddRefs(delegateFactory));
|
|
|
|
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(*NS_REINTERPRET_CAST(nsISupports**, aResult));
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->mKey = aKey;
|
2001-01-23 23:10:53 +00:00
|
|
|
entry->mDelegate = do_QueryInterface(*NS_REINTERPRET_CAST(nsISupports**, aResult), &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("nsRDFResource::GetDelegate(): can't QI to nsISupports!");
|
|
|
|
|
|
|
|
delete entry;
|
|
|
|
NS_RELEASE(*NS_REINTERPRET_CAST(nsISupports**, aResult));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-11-24 20:51:39 +00:00
|
|
|
entry->mNext = mDelegates;
|
|
|
|
|
|
|
|
mDelegates = entry;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRDFResource::ReleaseDelegate(const char* aKey)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aKey != nsnull, "null ptr");
|
|
|
|
if (! aKey)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
DelegateEntry* entry = mDelegates;
|
|
|
|
DelegateEntry** link = &mDelegates;
|
|
|
|
|
|
|
|
while (entry) {
|
2000-09-03 22:45:50 +00:00
|
|
|
if (entry->mKey.Equals(aKey)) {
|
1999-11-24 20:51:39 +00:00
|
|
|
*link = entry->mNext;
|
|
|
|
delete entry;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
link = &(entry->mNext);
|
|
|
|
entry = entry->mNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("nsRDFResource::ReleaseDelegate() no delegate found");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-02-17 23:42:09 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|