2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
1999-04-26 22:46:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Implementation for the RDF container utils.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIRDFContainer.h"
|
|
|
|
#include "nsIRDFContainerUtils.h"
|
|
|
|
#include "nsIRDFService.h"
|
|
|
|
#include "nsRDFCID.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "plstr.h"
|
|
|
|
#include "rdf.h"
|
|
|
|
#include "rdfutil.h"
|
|
|
|
|
|
|
|
class RDFContainerUtilsImpl : public nsIRDFContainerUtils
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// nsISupports interface
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIRDFContainerUtils interface
|
1999-08-24 13:11:48 +04:00
|
|
|
NS_DECL_NSIRDFCONTAINERUTILS
|
1999-04-26 22:46:36 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend nsresult NS_NewRDFContainerUtils(nsIRDFContainerUtils** aResult);
|
|
|
|
|
|
|
|
RDFContainerUtilsImpl();
|
|
|
|
virtual ~RDFContainerUtilsImpl();
|
|
|
|
|
|
|
|
nsresult MakeContainer(nsIRDFDataSource* aDataSource,
|
|
|
|
nsIRDFResource* aResource,
|
|
|
|
nsIRDFResource* aType,
|
|
|
|
nsIRDFContainer** aResult);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsA(nsIRDFDataSource* aDataSource, nsIRDFResource* aResource, nsIRDFResource* aType);
|
1999-09-01 04:50:01 +04:00
|
|
|
|
1999-04-26 22:46:36 +04:00
|
|
|
// pseudo constants
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t gRefCnt;
|
1999-04-26 22:46:36 +04:00
|
|
|
static nsIRDFService* gRDFService;
|
|
|
|
static nsIRDFResource* kRDF_instanceOf;
|
|
|
|
static nsIRDFResource* kRDF_nextVal;
|
|
|
|
static nsIRDFResource* kRDF_Bag;
|
|
|
|
static nsIRDFResource* kRDF_Seq;
|
|
|
|
static nsIRDFResource* kRDF_Alt;
|
2000-07-19 07:58:25 +04:00
|
|
|
static nsIRDFLiteral* kOne;
|
2013-12-01 09:42:59 +04:00
|
|
|
static const char kRDFNameSpaceURI[];
|
1999-04-26 22:46:36 +04:00
|
|
|
};
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t RDFContainerUtilsImpl::gRefCnt = 0;
|
1999-04-26 22:46:36 +04:00
|
|
|
nsIRDFService* RDFContainerUtilsImpl::gRDFService;
|
|
|
|
nsIRDFResource* RDFContainerUtilsImpl::kRDF_instanceOf;
|
|
|
|
nsIRDFResource* RDFContainerUtilsImpl::kRDF_nextVal;
|
|
|
|
nsIRDFResource* RDFContainerUtilsImpl::kRDF_Bag;
|
|
|
|
nsIRDFResource* RDFContainerUtilsImpl::kRDF_Seq;
|
|
|
|
nsIRDFResource* RDFContainerUtilsImpl::kRDF_Alt;
|
2000-07-19 07:58:25 +04:00
|
|
|
nsIRDFLiteral* RDFContainerUtilsImpl::kOne;
|
2013-12-01 09:42:59 +04:00
|
|
|
const char RDFContainerUtilsImpl::kRDFNameSpaceURI[] = RDF_NAMESPACE_URI;
|
1999-04-26 22:46:36 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsISupports interface
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(RDFContainerUtilsImpl, nsIRDFContainerUtils)
|
1999-04-26 22:46:36 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIRDFContainerUtils interface
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
RDFContainerUtilsImpl::IsOrdinalProperty(nsIRDFResource *aProperty, bool *_retval)
|
1999-04-26 22:46:36 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aProperty != nullptr, "null ptr");
|
1999-06-11 07:32:02 +04:00
|
|
|
if (! aProperty)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-04-26 22:46:36 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
1999-07-31 03:40:30 +04:00
|
|
|
const char *propertyStr;
|
|
|
|
rv = aProperty->GetValueConst( &propertyStr );
|
1999-04-26 22:46:36 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (PL_strncmp(propertyStr, kRDFNameSpaceURI, sizeof(kRDFNameSpaceURI) - 1) != 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_retval = false;
|
1999-04-26 22:46:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* s = propertyStr;
|
|
|
|
s += sizeof(kRDFNameSpaceURI) - 1;
|
|
|
|
if (*s != '_') {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_retval = false;
|
1999-04-26 22:46:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
++s;
|
|
|
|
while (*s) {
|
|
|
|
if (*s < '0' || *s > '9') {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_retval = false;
|
1999-04-26 22:46:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*_retval = true;
|
1999-04-26 22:46:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
RDFContainerUtilsImpl::IndexToOrdinalResource(int32_t aIndex, nsIRDFResource **aOrdinal)
|
1999-04-26 22:46:36 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aIndex > 0, "illegal value");
|
|
|
|
if (aIndex <= 0)
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString uri(kRDFNameSpaceURI);
|
2003-02-25 22:02:00 +03:00
|
|
|
uri.Append('_');
|
|
|
|
uri.AppendInt(aIndex);
|
1999-04-26 22:46:36 +04:00
|
|
|
|
2003-02-25 22:02:00 +03:00
|
|
|
nsresult rv = gRDFService->GetResource(uri, aOrdinal);
|
1999-04-26 22:46:36 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get ordinal resource");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
RDFContainerUtilsImpl::OrdinalResourceToIndex(nsIRDFResource *aOrdinal, int32_t *aIndex)
|
1999-04-26 22:46:36 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aOrdinal != nullptr, "null ptr");
|
1999-06-11 07:32:02 +04:00
|
|
|
if (! aOrdinal)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-07-31 03:40:30 +04:00
|
|
|
const char *ordinalStr;
|
|
|
|
if (NS_FAILED(aOrdinal->GetValueConst( &ordinalStr )))
|
2001-03-29 02:42:49 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-04-26 22:46:36 +04:00
|
|
|
|
|
|
|
const char* s = ordinalStr;
|
|
|
|
if (PL_strncmp(s, kRDFNameSpaceURI, sizeof(kRDFNameSpaceURI) - 1) != 0) {
|
|
|
|
NS_ERROR("not an ordinal");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
s += sizeof(kRDFNameSpaceURI) - 1;
|
|
|
|
if (*s != '_') {
|
|
|
|
NS_ERROR("not an ordinal");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t idx = 0;
|
1999-04-26 22:46:36 +04:00
|
|
|
|
|
|
|
++s;
|
|
|
|
while (*s) {
|
|
|
|
if (*s < '0' || *s > '9') {
|
|
|
|
NS_ERROR("not an ordinal");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
1999-08-24 13:11:48 +04:00
|
|
|
idx *= 10;
|
|
|
|
idx += (*s - '0');
|
1999-04-26 22:46:36 +04:00
|
|
|
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
|
1999-08-24 13:11:48 +04:00
|
|
|
*aIndex = idx;
|
1999-04-26 22:46:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
RDFContainerUtilsImpl::IsContainer(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, bool *_retval)
|
1999-04-26 22:46:36 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aDataSource != nullptr, "null ptr");
|
1999-06-11 07:32:02 +04:00
|
|
|
if (! aDataSource)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aResource != nullptr, "null ptr");
|
1999-06-11 07:32:02 +04:00
|
|
|
if (! aResource)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(_retval != nullptr, "null ptr");
|
1999-04-26 22:46:36 +04:00
|
|
|
if (! _retval)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2002-02-12 03:49:56 +03:00
|
|
|
if (IsA(aDataSource, aResource, kRDF_Seq) ||
|
|
|
|
IsA(aDataSource, aResource, kRDF_Bag) ||
|
1999-09-01 04:50:01 +04:00
|
|
|
IsA(aDataSource, aResource, kRDF_Alt)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_retval = true;
|
1999-04-26 22:46:36 +04:00
|
|
|
}
|
|
|
|
else {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_retval = false;
|
1999-04-26 22:46:36 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-19 07:58:25 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
RDFContainerUtilsImpl::IsEmpty(nsIRDFDataSource* aDataSource, nsIRDFResource* aResource, bool* _retval)
|
2000-07-19 07:58:25 +04:00
|
|
|
{
|
|
|
|
if (! aDataSource)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// By default, say that we're an empty container. Even if we're not
|
|
|
|
// really even a container.
|
2011-10-17 18:59:28 +04:00
|
|
|
*_retval = true;
|
2000-07-19 07:58:25 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFNode> nextValNode;
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = aDataSource->GetTarget(aResource, kRDF_nextVal, true, getter_AddRefs(nextValNode));
|
2000-07-19 07:58:25 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (rv == NS_RDF_NO_VALUE)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFLiteral> nextValLiteral;
|
|
|
|
rv = nextValNode->QueryInterface(NS_GET_IID(nsIRDFLiteral), getter_AddRefs(nextValLiteral));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (nextValLiteral.get() != kOne)
|
2011-10-17 18:59:28 +04:00
|
|
|
*_retval = false;
|
2000-07-19 07:58:25 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-26 22:46:36 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
RDFContainerUtilsImpl::IsBag(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, bool *_retval)
|
1999-04-26 22:46:36 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aDataSource != nullptr, "null ptr");
|
1999-06-11 07:32:02 +04:00
|
|
|
if (! aDataSource)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aResource != nullptr, "null ptr");
|
1999-06-11 07:32:02 +04:00
|
|
|
if (! aResource)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(_retval != nullptr, "null ptr");
|
1999-04-26 22:46:36 +04:00
|
|
|
if (! _retval)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-09-01 04:50:01 +04:00
|
|
|
*_retval = IsA(aDataSource, aResource, kRDF_Bag);
|
1999-04-26 22:46:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
RDFContainerUtilsImpl::IsSeq(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, bool *_retval)
|
1999-04-26 22:46:36 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aDataSource != nullptr, "null ptr");
|
1999-06-11 07:32:02 +04:00
|
|
|
if (! aDataSource)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aResource != nullptr, "null ptr");
|
1999-06-11 07:32:02 +04:00
|
|
|
if (! aResource)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(_retval != nullptr, "null ptr");
|
1999-04-26 22:46:36 +04:00
|
|
|
if (! _retval)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-09-01 04:50:01 +04:00
|
|
|
*_retval = IsA(aDataSource, aResource, kRDF_Seq);
|
1999-04-26 22:46:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
RDFContainerUtilsImpl::IsAlt(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, bool *_retval)
|
1999-04-26 22:46:36 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aDataSource != nullptr, "null ptr");
|
1999-06-11 07:32:02 +04:00
|
|
|
if (! aDataSource)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aResource != nullptr, "null ptr");
|
1999-06-11 07:32:02 +04:00
|
|
|
if (! aResource)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(_retval != nullptr, "null ptr");
|
1999-04-26 22:46:36 +04:00
|
|
|
if (! _retval)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-09-01 04:50:01 +04:00
|
|
|
*_retval = IsA(aDataSource, aResource, kRDF_Alt);
|
1999-04-26 22:46:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
RDFContainerUtilsImpl::MakeBag(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, nsIRDFContainer **_retval)
|
|
|
|
{
|
|
|
|
return MakeContainer(aDataSource, aResource, kRDF_Bag, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
RDFContainerUtilsImpl::MakeSeq(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, nsIRDFContainer **_retval)
|
|
|
|
{
|
|
|
|
return MakeContainer(aDataSource, aResource, kRDF_Seq, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
RDFContainerUtilsImpl::MakeAlt(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, nsIRDFContainer **_retval)
|
|
|
|
{
|
|
|
|
return MakeContainer(aDataSource, aResource, kRDF_Alt, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-10-07 05:51:30 +04:00
|
|
|
|
1999-04-26 22:46:36 +04:00
|
|
|
RDFContainerUtilsImpl::RDFContainerUtilsImpl()
|
|
|
|
{
|
|
|
|
if (gRefCnt++ == 0) {
|
|
|
|
nsresult rv;
|
|
|
|
|
2013-12-11 00:25:55 +04:00
|
|
|
NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
|
|
|
|
rv = CallGetService(kRDFServiceCID, &gRDFService);
|
1999-04-26 22:46:36 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get RDF service");
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2003-02-25 22:02:00 +03:00
|
|
|
gRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "instanceOf"),
|
|
|
|
&kRDF_instanceOf);
|
|
|
|
gRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "nextVal"),
|
|
|
|
&kRDF_nextVal);
|
|
|
|
gRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "Bag"),
|
|
|
|
&kRDF_Bag);
|
|
|
|
gRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "Seq"),
|
|
|
|
&kRDF_Seq);
|
|
|
|
gRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "Alt"),
|
|
|
|
&kRDF_Alt);
|
2016-07-21 08:03:25 +03:00
|
|
|
gRDFService->GetLiteral(u"1", &kOne);
|
1999-04-26 22:46:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RDFContainerUtilsImpl::~RDFContainerUtilsImpl()
|
|
|
|
{
|
1999-10-07 05:51:30 +04:00
|
|
|
#ifdef DEBUG_REFS
|
|
|
|
--gInstanceCount;
|
2000-10-29 02:17:53 +04:00
|
|
|
fprintf(stdout, "%d - RDF: RDFContainerUtilsImpl\n", gInstanceCount);
|
1999-10-07 05:51:30 +04:00
|
|
|
#endif
|
|
|
|
|
1999-04-26 22:46:36 +04:00
|
|
|
if (--gRefCnt == 0) {
|
2004-11-08 02:59:35 +03:00
|
|
|
NS_IF_RELEASE(gRDFService);
|
1999-04-26 22:46:36 +04:00
|
|
|
NS_IF_RELEASE(kRDF_instanceOf);
|
|
|
|
NS_IF_RELEASE(kRDF_nextVal);
|
|
|
|
NS_IF_RELEASE(kRDF_Bag);
|
|
|
|
NS_IF_RELEASE(kRDF_Seq);
|
|
|
|
NS_IF_RELEASE(kRDF_Alt);
|
2000-07-19 07:58:25 +04:00
|
|
|
NS_IF_RELEASE(kOne);
|
1999-04-26 22:46:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewRDFContainerUtils(nsIRDFContainerUtils** aResult)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aResult != nullptr, "null ptr");
|
1999-04-26 22:46:36 +04:00
|
|
|
if (! aResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
RDFContainerUtilsImpl* result =
|
|
|
|
new RDFContainerUtilsImpl();
|
|
|
|
|
|
|
|
if (! result)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(result);
|
|
|
|
*aResult = result;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RDFContainerUtilsImpl::MakeContainer(nsIRDFDataSource* aDataSource, nsIRDFResource* aResource, nsIRDFResource* aType, nsIRDFContainer** aResult)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aDataSource != nullptr, "null ptr");
|
2000-05-07 12:38:45 +04:00
|
|
|
if (! aDataSource) return NS_ERROR_NULL_POINTER;
|
1999-06-11 07:32:02 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aResource != nullptr, "null ptr");
|
2000-05-07 12:38:45 +04:00
|
|
|
if (! aResource) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_PRECONDITION(aType != nullptr, "null ptr");
|
2000-05-07 12:38:45 +04:00
|
|
|
if (! aType) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (aResult) *aResult = nullptr;
|
1999-06-11 07:32:02 +04:00
|
|
|
|
1999-04-26 22:46:36 +04:00
|
|
|
nsresult rv;
|
1999-07-17 06:24:13 +04:00
|
|
|
|
|
|
|
// Check to see if somebody has already turned it into a container; if so
|
|
|
|
// don't try to do it again.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isContainer;
|
1999-07-17 06:24:13 +04:00
|
|
|
rv = IsContainer(aDataSource, aResource, &isContainer);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2010-02-07 18:52:43 +03:00
|
|
|
if (!isContainer)
|
2000-05-07 12:38:45 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = aDataSource->Assert(aResource, kRDF_instanceOf, aType, true);
|
2000-05-07 12:38:45 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-07-17 06:24:13 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = aDataSource->Assert(aResource, kRDF_nextVal, kOne, true);
|
2000-05-07 12:38:45 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
1999-04-26 22:46:36 +04:00
|
|
|
|
|
|
|
if (aResult) {
|
|
|
|
rv = NS_NewRDFContainer(aResult);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = (*aResult)->Init(aDataSource, aResource);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
1999-09-01 04:50:01 +04:00
|
|
|
RDFContainerUtilsImpl::IsA(nsIRDFDataSource* aDataSource, nsIRDFResource* aResource, nsIRDFResource* aType)
|
|
|
|
{
|
Bug 671185 - Incorrect return of NS_ERROR_* codes in functions returning PRBool, r=mak,ehsan,taras,biesi,pike,khuey,dholbert,josh,bjacob,bsmith
2011-07-26 08:57:58 +04:00
|
|
|
if (!aDataSource || !aResource || !aType) {
|
|
|
|
NS_WARNING("Unexpected null argument");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
Bug 671185 - Incorrect return of NS_ERROR_* codes in functions returning PRBool, r=mak,ehsan,taras,biesi,pike,khuey,dholbert,josh,bjacob,bsmith
2011-07-26 08:57:58 +04:00
|
|
|
}
|
2002-06-16 04:44:06 +04:00
|
|
|
|
1999-09-01 04:50:01 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool result;
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = aDataSource->HasAssertion(aResource, kRDF_instanceOf, aType, true, &result);
|
Bug 671185 - Incorrect return of NS_ERROR_* codes in functions returning PRBool, r=mak,ehsan,taras,biesi,pike,khuey,dholbert,josh,bjacob,bsmith
2011-07-26 08:57:58 +04:00
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
1999-09-01 04:50:01 +04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2001-03-29 02:42:49 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
RDFContainerUtilsImpl::IndexOf(nsIRDFDataSource* aDataSource, nsIRDFResource* aContainer, nsIRDFNode* aElement, int32_t* aIndex)
|
2001-03-29 02:42:49 +04:00
|
|
|
{
|
2002-06-16 04:44:06 +04:00
|
|
|
if (!aDataSource || !aContainer)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2001-03-29 02:42:49 +04:00
|
|
|
// Assume we can't find it.
|
|
|
|
*aIndex = -1;
|
|
|
|
|
2002-07-09 23:37:24 +04:00
|
|
|
// If the resource is null, bail quietly
|
|
|
|
if (! aElement)
|
|
|
|
return NS_OK;
|
|
|
|
|
2001-03-29 02:42:49 +04:00
|
|
|
// We'll assume that fan-out is much higher than fan-in, so grovel
|
|
|
|
// through the inbound arcs, look for an ordinal resource, and
|
|
|
|
// decode it.
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> arcsIn;
|
|
|
|
aDataSource->ArcLabelsIn(aElement, getter_AddRefs(arcsIn));
|
|
|
|
if (! arcsIn)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
while (1) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasMoreArcs = false;
|
2001-03-29 02:42:49 +04:00
|
|
|
arcsIn->HasMoreElements(&hasMoreArcs);
|
|
|
|
if (! hasMoreArcs)
|
|
|
|
break;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> isupports;
|
|
|
|
arcsIn->GetNext(getter_AddRefs(isupports));
|
|
|
|
if (! isupports)
|
|
|
|
break;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFResource> property =
|
|
|
|
do_QueryInterface(isupports);
|
|
|
|
|
|
|
|
if (! property)
|
|
|
|
continue;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isOrdinal;
|
2001-03-29 02:42:49 +04:00
|
|
|
IsOrdinalProperty(property, &isOrdinal);
|
|
|
|
if (! isOrdinal)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> sources;
|
2011-10-17 18:59:28 +04:00
|
|
|
aDataSource->GetSources(property, aElement, true, getter_AddRefs(sources));
|
2001-03-29 02:42:49 +04:00
|
|
|
if (! sources)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while (1) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasMoreSources = false;
|
2001-03-29 02:42:49 +04:00
|
|
|
sources->HasMoreElements(&hasMoreSources);
|
|
|
|
if (! hasMoreSources)
|
2001-10-17 01:52:01 +04:00
|
|
|
break;
|
2001-03-29 02:42:49 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> isupports2;
|
|
|
|
sources->GetNext(getter_AddRefs(isupports2));
|
|
|
|
if (! isupports2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFResource> source =
|
|
|
|
do_QueryInterface(isupports2);
|
|
|
|
|
|
|
|
if (source == aContainer)
|
|
|
|
// Found it.
|
|
|
|
return OrdinalResourceToIndex(property, aIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|