Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | |
michael@0 | 8 | Implementation for the RDF container utils. |
michael@0 | 9 | |
michael@0 | 10 | */ |
michael@0 | 11 | |
michael@0 | 12 | |
michael@0 | 13 | #include "nsCOMPtr.h" |
michael@0 | 14 | #include "nsIServiceManager.h" |
michael@0 | 15 | #include "nsIRDFContainer.h" |
michael@0 | 16 | #include "nsIRDFContainerUtils.h" |
michael@0 | 17 | #include "nsIRDFService.h" |
michael@0 | 18 | #include "nsRDFCID.h" |
michael@0 | 19 | #include "nsString.h" |
michael@0 | 20 | #include "nsXPIDLString.h" |
michael@0 | 21 | #include "plstr.h" |
michael@0 | 22 | #include "prprf.h" |
michael@0 | 23 | #include "rdf.h" |
michael@0 | 24 | #include "rdfutil.h" |
michael@0 | 25 | |
michael@0 | 26 | class RDFContainerUtilsImpl : public nsIRDFContainerUtils |
michael@0 | 27 | { |
michael@0 | 28 | public: |
michael@0 | 29 | // nsISupports interface |
michael@0 | 30 | NS_DECL_ISUPPORTS |
michael@0 | 31 | |
michael@0 | 32 | // nsIRDFContainerUtils interface |
michael@0 | 33 | NS_DECL_NSIRDFCONTAINERUTILS |
michael@0 | 34 | |
michael@0 | 35 | private: |
michael@0 | 36 | friend nsresult NS_NewRDFContainerUtils(nsIRDFContainerUtils** aResult); |
michael@0 | 37 | |
michael@0 | 38 | RDFContainerUtilsImpl(); |
michael@0 | 39 | virtual ~RDFContainerUtilsImpl(); |
michael@0 | 40 | |
michael@0 | 41 | nsresult MakeContainer(nsIRDFDataSource* aDataSource, |
michael@0 | 42 | nsIRDFResource* aResource, |
michael@0 | 43 | nsIRDFResource* aType, |
michael@0 | 44 | nsIRDFContainer** aResult); |
michael@0 | 45 | |
michael@0 | 46 | bool IsA(nsIRDFDataSource* aDataSource, nsIRDFResource* aResource, nsIRDFResource* aType); |
michael@0 | 47 | |
michael@0 | 48 | // pseudo constants |
michael@0 | 49 | static int32_t gRefCnt; |
michael@0 | 50 | static nsIRDFService* gRDFService; |
michael@0 | 51 | static nsIRDFResource* kRDF_instanceOf; |
michael@0 | 52 | static nsIRDFResource* kRDF_nextVal; |
michael@0 | 53 | static nsIRDFResource* kRDF_Bag; |
michael@0 | 54 | static nsIRDFResource* kRDF_Seq; |
michael@0 | 55 | static nsIRDFResource* kRDF_Alt; |
michael@0 | 56 | static nsIRDFLiteral* kOne; |
michael@0 | 57 | static const char kRDFNameSpaceURI[]; |
michael@0 | 58 | }; |
michael@0 | 59 | |
michael@0 | 60 | int32_t RDFContainerUtilsImpl::gRefCnt = 0; |
michael@0 | 61 | nsIRDFService* RDFContainerUtilsImpl::gRDFService; |
michael@0 | 62 | nsIRDFResource* RDFContainerUtilsImpl::kRDF_instanceOf; |
michael@0 | 63 | nsIRDFResource* RDFContainerUtilsImpl::kRDF_nextVal; |
michael@0 | 64 | nsIRDFResource* RDFContainerUtilsImpl::kRDF_Bag; |
michael@0 | 65 | nsIRDFResource* RDFContainerUtilsImpl::kRDF_Seq; |
michael@0 | 66 | nsIRDFResource* RDFContainerUtilsImpl::kRDF_Alt; |
michael@0 | 67 | nsIRDFLiteral* RDFContainerUtilsImpl::kOne; |
michael@0 | 68 | const char RDFContainerUtilsImpl::kRDFNameSpaceURI[] = RDF_NAMESPACE_URI; |
michael@0 | 69 | |
michael@0 | 70 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 71 | // nsISupports interface |
michael@0 | 72 | |
michael@0 | 73 | NS_IMPL_ISUPPORTS(RDFContainerUtilsImpl, nsIRDFContainerUtils) |
michael@0 | 74 | |
michael@0 | 75 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 76 | // nsIRDFContainerUtils interface |
michael@0 | 77 | |
michael@0 | 78 | NS_IMETHODIMP |
michael@0 | 79 | RDFContainerUtilsImpl::IsOrdinalProperty(nsIRDFResource *aProperty, bool *_retval) |
michael@0 | 80 | { |
michael@0 | 81 | NS_PRECONDITION(aProperty != nullptr, "null ptr"); |
michael@0 | 82 | if (! aProperty) |
michael@0 | 83 | return NS_ERROR_NULL_POINTER; |
michael@0 | 84 | |
michael@0 | 85 | nsresult rv; |
michael@0 | 86 | |
michael@0 | 87 | const char *propertyStr; |
michael@0 | 88 | rv = aProperty->GetValueConst( &propertyStr ); |
michael@0 | 89 | if (NS_FAILED(rv)) return rv; |
michael@0 | 90 | |
michael@0 | 91 | if (PL_strncmp(propertyStr, kRDFNameSpaceURI, sizeof(kRDFNameSpaceURI) - 1) != 0) { |
michael@0 | 92 | *_retval = false; |
michael@0 | 93 | return NS_OK; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | const char* s = propertyStr; |
michael@0 | 97 | s += sizeof(kRDFNameSpaceURI) - 1; |
michael@0 | 98 | if (*s != '_') { |
michael@0 | 99 | *_retval = false; |
michael@0 | 100 | return NS_OK; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | ++s; |
michael@0 | 104 | while (*s) { |
michael@0 | 105 | if (*s < '0' || *s > '9') { |
michael@0 | 106 | *_retval = false; |
michael@0 | 107 | return NS_OK; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | ++s; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | *_retval = true; |
michael@0 | 114 | return NS_OK; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | |
michael@0 | 118 | NS_IMETHODIMP |
michael@0 | 119 | RDFContainerUtilsImpl::IndexToOrdinalResource(int32_t aIndex, nsIRDFResource **aOrdinal) |
michael@0 | 120 | { |
michael@0 | 121 | NS_PRECONDITION(aIndex > 0, "illegal value"); |
michael@0 | 122 | if (aIndex <= 0) |
michael@0 | 123 | return NS_ERROR_ILLEGAL_VALUE; |
michael@0 | 124 | |
michael@0 | 125 | nsAutoCString uri(kRDFNameSpaceURI); |
michael@0 | 126 | uri.Append('_'); |
michael@0 | 127 | uri.AppendInt(aIndex); |
michael@0 | 128 | |
michael@0 | 129 | nsresult rv = gRDFService->GetResource(uri, aOrdinal); |
michael@0 | 130 | NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get ordinal resource"); |
michael@0 | 131 | if (NS_FAILED(rv)) return rv; |
michael@0 | 132 | |
michael@0 | 133 | return NS_OK; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | |
michael@0 | 137 | NS_IMETHODIMP |
michael@0 | 138 | RDFContainerUtilsImpl::OrdinalResourceToIndex(nsIRDFResource *aOrdinal, int32_t *aIndex) |
michael@0 | 139 | { |
michael@0 | 140 | NS_PRECONDITION(aOrdinal != nullptr, "null ptr"); |
michael@0 | 141 | if (! aOrdinal) |
michael@0 | 142 | return NS_ERROR_NULL_POINTER; |
michael@0 | 143 | |
michael@0 | 144 | const char *ordinalStr; |
michael@0 | 145 | if (NS_FAILED(aOrdinal->GetValueConst( &ordinalStr ))) |
michael@0 | 146 | return NS_ERROR_FAILURE; |
michael@0 | 147 | |
michael@0 | 148 | const char* s = ordinalStr; |
michael@0 | 149 | if (PL_strncmp(s, kRDFNameSpaceURI, sizeof(kRDFNameSpaceURI) - 1) != 0) { |
michael@0 | 150 | NS_ERROR("not an ordinal"); |
michael@0 | 151 | return NS_ERROR_UNEXPECTED; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | s += sizeof(kRDFNameSpaceURI) - 1; |
michael@0 | 155 | if (*s != '_') { |
michael@0 | 156 | NS_ERROR("not an ordinal"); |
michael@0 | 157 | return NS_ERROR_UNEXPECTED; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | int32_t idx = 0; |
michael@0 | 161 | |
michael@0 | 162 | ++s; |
michael@0 | 163 | while (*s) { |
michael@0 | 164 | if (*s < '0' || *s > '9') { |
michael@0 | 165 | NS_ERROR("not an ordinal"); |
michael@0 | 166 | return NS_ERROR_UNEXPECTED; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | idx *= 10; |
michael@0 | 170 | idx += (*s - '0'); |
michael@0 | 171 | |
michael@0 | 172 | ++s; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | *aIndex = idx; |
michael@0 | 176 | return NS_OK; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | NS_IMETHODIMP |
michael@0 | 180 | RDFContainerUtilsImpl::IsContainer(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, bool *_retval) |
michael@0 | 181 | { |
michael@0 | 182 | NS_PRECONDITION(aDataSource != nullptr, "null ptr"); |
michael@0 | 183 | if (! aDataSource) |
michael@0 | 184 | return NS_ERROR_NULL_POINTER; |
michael@0 | 185 | |
michael@0 | 186 | NS_PRECONDITION(aResource != nullptr, "null ptr"); |
michael@0 | 187 | if (! aResource) |
michael@0 | 188 | return NS_ERROR_NULL_POINTER; |
michael@0 | 189 | |
michael@0 | 190 | NS_PRECONDITION(_retval != nullptr, "null ptr"); |
michael@0 | 191 | if (! _retval) |
michael@0 | 192 | return NS_ERROR_NULL_POINTER; |
michael@0 | 193 | |
michael@0 | 194 | if (IsA(aDataSource, aResource, kRDF_Seq) || |
michael@0 | 195 | IsA(aDataSource, aResource, kRDF_Bag) || |
michael@0 | 196 | IsA(aDataSource, aResource, kRDF_Alt)) { |
michael@0 | 197 | *_retval = true; |
michael@0 | 198 | } |
michael@0 | 199 | else { |
michael@0 | 200 | *_retval = false; |
michael@0 | 201 | } |
michael@0 | 202 | return NS_OK; |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | |
michael@0 | 206 | NS_IMETHODIMP |
michael@0 | 207 | RDFContainerUtilsImpl::IsEmpty(nsIRDFDataSource* aDataSource, nsIRDFResource* aResource, bool* _retval) |
michael@0 | 208 | { |
michael@0 | 209 | if (! aDataSource) |
michael@0 | 210 | return NS_ERROR_NULL_POINTER; |
michael@0 | 211 | |
michael@0 | 212 | nsresult rv; |
michael@0 | 213 | |
michael@0 | 214 | // By default, say that we're an empty container. Even if we're not |
michael@0 | 215 | // really even a container. |
michael@0 | 216 | *_retval = true; |
michael@0 | 217 | |
michael@0 | 218 | nsCOMPtr<nsIRDFNode> nextValNode; |
michael@0 | 219 | rv = aDataSource->GetTarget(aResource, kRDF_nextVal, true, getter_AddRefs(nextValNode)); |
michael@0 | 220 | if (NS_FAILED(rv)) return rv; |
michael@0 | 221 | |
michael@0 | 222 | if (rv == NS_RDF_NO_VALUE) |
michael@0 | 223 | return NS_OK; |
michael@0 | 224 | |
michael@0 | 225 | nsCOMPtr<nsIRDFLiteral> nextValLiteral; |
michael@0 | 226 | rv = nextValNode->QueryInterface(NS_GET_IID(nsIRDFLiteral), getter_AddRefs(nextValLiteral)); |
michael@0 | 227 | if (NS_FAILED(rv)) return rv; |
michael@0 | 228 | |
michael@0 | 229 | if (nextValLiteral.get() != kOne) |
michael@0 | 230 | *_retval = false; |
michael@0 | 231 | |
michael@0 | 232 | return NS_OK; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | |
michael@0 | 236 | NS_IMETHODIMP |
michael@0 | 237 | RDFContainerUtilsImpl::IsBag(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, bool *_retval) |
michael@0 | 238 | { |
michael@0 | 239 | NS_PRECONDITION(aDataSource != nullptr, "null ptr"); |
michael@0 | 240 | if (! aDataSource) |
michael@0 | 241 | return NS_ERROR_NULL_POINTER; |
michael@0 | 242 | |
michael@0 | 243 | NS_PRECONDITION(aResource != nullptr, "null ptr"); |
michael@0 | 244 | if (! aResource) |
michael@0 | 245 | return NS_ERROR_NULL_POINTER; |
michael@0 | 246 | |
michael@0 | 247 | NS_PRECONDITION(_retval != nullptr, "null ptr"); |
michael@0 | 248 | if (! _retval) |
michael@0 | 249 | return NS_ERROR_NULL_POINTER; |
michael@0 | 250 | |
michael@0 | 251 | *_retval = IsA(aDataSource, aResource, kRDF_Bag); |
michael@0 | 252 | return NS_OK; |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | |
michael@0 | 256 | NS_IMETHODIMP |
michael@0 | 257 | RDFContainerUtilsImpl::IsSeq(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, bool *_retval) |
michael@0 | 258 | { |
michael@0 | 259 | NS_PRECONDITION(aDataSource != nullptr, "null ptr"); |
michael@0 | 260 | if (! aDataSource) |
michael@0 | 261 | return NS_ERROR_NULL_POINTER; |
michael@0 | 262 | |
michael@0 | 263 | NS_PRECONDITION(aResource != nullptr, "null ptr"); |
michael@0 | 264 | if (! aResource) |
michael@0 | 265 | return NS_ERROR_NULL_POINTER; |
michael@0 | 266 | |
michael@0 | 267 | NS_PRECONDITION(_retval != nullptr, "null ptr"); |
michael@0 | 268 | if (! _retval) |
michael@0 | 269 | return NS_ERROR_NULL_POINTER; |
michael@0 | 270 | |
michael@0 | 271 | *_retval = IsA(aDataSource, aResource, kRDF_Seq); |
michael@0 | 272 | return NS_OK; |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | |
michael@0 | 276 | NS_IMETHODIMP |
michael@0 | 277 | RDFContainerUtilsImpl::IsAlt(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, bool *_retval) |
michael@0 | 278 | { |
michael@0 | 279 | NS_PRECONDITION(aDataSource != nullptr, "null ptr"); |
michael@0 | 280 | if (! aDataSource) |
michael@0 | 281 | return NS_ERROR_NULL_POINTER; |
michael@0 | 282 | |
michael@0 | 283 | NS_PRECONDITION(aResource != nullptr, "null ptr"); |
michael@0 | 284 | if (! aResource) |
michael@0 | 285 | return NS_ERROR_NULL_POINTER; |
michael@0 | 286 | |
michael@0 | 287 | NS_PRECONDITION(_retval != nullptr, "null ptr"); |
michael@0 | 288 | if (! _retval) |
michael@0 | 289 | return NS_ERROR_NULL_POINTER; |
michael@0 | 290 | |
michael@0 | 291 | *_retval = IsA(aDataSource, aResource, kRDF_Alt); |
michael@0 | 292 | return NS_OK; |
michael@0 | 293 | } |
michael@0 | 294 | |
michael@0 | 295 | |
michael@0 | 296 | NS_IMETHODIMP |
michael@0 | 297 | RDFContainerUtilsImpl::MakeBag(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, nsIRDFContainer **_retval) |
michael@0 | 298 | { |
michael@0 | 299 | return MakeContainer(aDataSource, aResource, kRDF_Bag, _retval); |
michael@0 | 300 | } |
michael@0 | 301 | |
michael@0 | 302 | |
michael@0 | 303 | NS_IMETHODIMP |
michael@0 | 304 | RDFContainerUtilsImpl::MakeSeq(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, nsIRDFContainer **_retval) |
michael@0 | 305 | { |
michael@0 | 306 | return MakeContainer(aDataSource, aResource, kRDF_Seq, _retval); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | |
michael@0 | 310 | NS_IMETHODIMP |
michael@0 | 311 | RDFContainerUtilsImpl::MakeAlt(nsIRDFDataSource *aDataSource, nsIRDFResource *aResource, nsIRDFContainer **_retval) |
michael@0 | 312 | { |
michael@0 | 313 | return MakeContainer(aDataSource, aResource, kRDF_Alt, _retval); |
michael@0 | 314 | } |
michael@0 | 315 | |
michael@0 | 316 | |
michael@0 | 317 | |
michael@0 | 318 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 319 | |
michael@0 | 320 | |
michael@0 | 321 | RDFContainerUtilsImpl::RDFContainerUtilsImpl() |
michael@0 | 322 | { |
michael@0 | 323 | if (gRefCnt++ == 0) { |
michael@0 | 324 | nsresult rv; |
michael@0 | 325 | |
michael@0 | 326 | NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID); |
michael@0 | 327 | rv = CallGetService(kRDFServiceCID, &gRDFService); |
michael@0 | 328 | |
michael@0 | 329 | NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get RDF service"); |
michael@0 | 330 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 331 | gRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "instanceOf"), |
michael@0 | 332 | &kRDF_instanceOf); |
michael@0 | 333 | gRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "nextVal"), |
michael@0 | 334 | &kRDF_nextVal); |
michael@0 | 335 | gRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "Bag"), |
michael@0 | 336 | &kRDF_Bag); |
michael@0 | 337 | gRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "Seq"), |
michael@0 | 338 | &kRDF_Seq); |
michael@0 | 339 | gRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "Alt"), |
michael@0 | 340 | &kRDF_Alt); |
michael@0 | 341 | gRDFService->GetLiteral(MOZ_UTF16("1"), &kOne); |
michael@0 | 342 | } |
michael@0 | 343 | } |
michael@0 | 344 | } |
michael@0 | 345 | |
michael@0 | 346 | |
michael@0 | 347 | RDFContainerUtilsImpl::~RDFContainerUtilsImpl() |
michael@0 | 348 | { |
michael@0 | 349 | #ifdef DEBUG_REFS |
michael@0 | 350 | --gInstanceCount; |
michael@0 | 351 | fprintf(stdout, "%d - RDF: RDFContainerUtilsImpl\n", gInstanceCount); |
michael@0 | 352 | #endif |
michael@0 | 353 | |
michael@0 | 354 | if (--gRefCnt == 0) { |
michael@0 | 355 | NS_IF_RELEASE(gRDFService); |
michael@0 | 356 | NS_IF_RELEASE(kRDF_instanceOf); |
michael@0 | 357 | NS_IF_RELEASE(kRDF_nextVal); |
michael@0 | 358 | NS_IF_RELEASE(kRDF_Bag); |
michael@0 | 359 | NS_IF_RELEASE(kRDF_Seq); |
michael@0 | 360 | NS_IF_RELEASE(kRDF_Alt); |
michael@0 | 361 | NS_IF_RELEASE(kOne); |
michael@0 | 362 | } |
michael@0 | 363 | } |
michael@0 | 364 | |
michael@0 | 365 | |
michael@0 | 366 | |
michael@0 | 367 | nsresult |
michael@0 | 368 | NS_NewRDFContainerUtils(nsIRDFContainerUtils** aResult) |
michael@0 | 369 | { |
michael@0 | 370 | NS_PRECONDITION(aResult != nullptr, "null ptr"); |
michael@0 | 371 | if (! aResult) |
michael@0 | 372 | return NS_ERROR_NULL_POINTER; |
michael@0 | 373 | |
michael@0 | 374 | RDFContainerUtilsImpl* result = |
michael@0 | 375 | new RDFContainerUtilsImpl(); |
michael@0 | 376 | |
michael@0 | 377 | if (! result) |
michael@0 | 378 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 379 | |
michael@0 | 380 | NS_ADDREF(result); |
michael@0 | 381 | *aResult = result; |
michael@0 | 382 | return NS_OK; |
michael@0 | 383 | } |
michael@0 | 384 | |
michael@0 | 385 | |
michael@0 | 386 | nsresult |
michael@0 | 387 | RDFContainerUtilsImpl::MakeContainer(nsIRDFDataSource* aDataSource, nsIRDFResource* aResource, nsIRDFResource* aType, nsIRDFContainer** aResult) |
michael@0 | 388 | { |
michael@0 | 389 | NS_PRECONDITION(aDataSource != nullptr, "null ptr"); |
michael@0 | 390 | if (! aDataSource) return NS_ERROR_NULL_POINTER; |
michael@0 | 391 | |
michael@0 | 392 | NS_PRECONDITION(aResource != nullptr, "null ptr"); |
michael@0 | 393 | if (! aResource) return NS_ERROR_NULL_POINTER; |
michael@0 | 394 | |
michael@0 | 395 | NS_PRECONDITION(aType != nullptr, "null ptr"); |
michael@0 | 396 | if (! aType) return NS_ERROR_NULL_POINTER; |
michael@0 | 397 | |
michael@0 | 398 | if (aResult) *aResult = nullptr; |
michael@0 | 399 | |
michael@0 | 400 | nsresult rv; |
michael@0 | 401 | |
michael@0 | 402 | // Check to see if somebody has already turned it into a container; if so |
michael@0 | 403 | // don't try to do it again. |
michael@0 | 404 | bool isContainer; |
michael@0 | 405 | rv = IsContainer(aDataSource, aResource, &isContainer); |
michael@0 | 406 | if (NS_FAILED(rv)) return rv; |
michael@0 | 407 | |
michael@0 | 408 | if (!isContainer) |
michael@0 | 409 | { |
michael@0 | 410 | rv = aDataSource->Assert(aResource, kRDF_instanceOf, aType, true); |
michael@0 | 411 | if (NS_FAILED(rv)) return rv; |
michael@0 | 412 | |
michael@0 | 413 | rv = aDataSource->Assert(aResource, kRDF_nextVal, kOne, true); |
michael@0 | 414 | if (NS_FAILED(rv)) return rv; |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | if (aResult) { |
michael@0 | 418 | rv = NS_NewRDFContainer(aResult); |
michael@0 | 419 | if (NS_FAILED(rv)) return rv; |
michael@0 | 420 | |
michael@0 | 421 | rv = (*aResult)->Init(aDataSource, aResource); |
michael@0 | 422 | if (NS_FAILED(rv)) return rv; |
michael@0 | 423 | } |
michael@0 | 424 | |
michael@0 | 425 | return NS_OK; |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | bool |
michael@0 | 429 | RDFContainerUtilsImpl::IsA(nsIRDFDataSource* aDataSource, nsIRDFResource* aResource, nsIRDFResource* aType) |
michael@0 | 430 | { |
michael@0 | 431 | if (!aDataSource || !aResource || !aType) { |
michael@0 | 432 | NS_WARNING("Unexpected null argument"); |
michael@0 | 433 | return false; |
michael@0 | 434 | } |
michael@0 | 435 | |
michael@0 | 436 | nsresult rv; |
michael@0 | 437 | |
michael@0 | 438 | bool result; |
michael@0 | 439 | rv = aDataSource->HasAssertion(aResource, kRDF_instanceOf, aType, true, &result); |
michael@0 | 440 | if (NS_FAILED(rv)) |
michael@0 | 441 | return false; |
michael@0 | 442 | |
michael@0 | 443 | return result; |
michael@0 | 444 | } |
michael@0 | 445 | |
michael@0 | 446 | NS_IMETHODIMP |
michael@0 | 447 | RDFContainerUtilsImpl::IndexOf(nsIRDFDataSource* aDataSource, nsIRDFResource* aContainer, nsIRDFNode* aElement, int32_t* aIndex) |
michael@0 | 448 | { |
michael@0 | 449 | if (!aDataSource || !aContainer) |
michael@0 | 450 | return NS_ERROR_NULL_POINTER; |
michael@0 | 451 | |
michael@0 | 452 | // Assume we can't find it. |
michael@0 | 453 | *aIndex = -1; |
michael@0 | 454 | |
michael@0 | 455 | // If the resource is null, bail quietly |
michael@0 | 456 | if (! aElement) |
michael@0 | 457 | return NS_OK; |
michael@0 | 458 | |
michael@0 | 459 | // We'll assume that fan-out is much higher than fan-in, so grovel |
michael@0 | 460 | // through the inbound arcs, look for an ordinal resource, and |
michael@0 | 461 | // decode it. |
michael@0 | 462 | nsCOMPtr<nsISimpleEnumerator> arcsIn; |
michael@0 | 463 | aDataSource->ArcLabelsIn(aElement, getter_AddRefs(arcsIn)); |
michael@0 | 464 | if (! arcsIn) |
michael@0 | 465 | return NS_OK; |
michael@0 | 466 | |
michael@0 | 467 | while (1) { |
michael@0 | 468 | bool hasMoreArcs = false; |
michael@0 | 469 | arcsIn->HasMoreElements(&hasMoreArcs); |
michael@0 | 470 | if (! hasMoreArcs) |
michael@0 | 471 | break; |
michael@0 | 472 | |
michael@0 | 473 | nsCOMPtr<nsISupports> isupports; |
michael@0 | 474 | arcsIn->GetNext(getter_AddRefs(isupports)); |
michael@0 | 475 | if (! isupports) |
michael@0 | 476 | break; |
michael@0 | 477 | |
michael@0 | 478 | nsCOMPtr<nsIRDFResource> property = |
michael@0 | 479 | do_QueryInterface(isupports); |
michael@0 | 480 | |
michael@0 | 481 | if (! property) |
michael@0 | 482 | continue; |
michael@0 | 483 | |
michael@0 | 484 | bool isOrdinal; |
michael@0 | 485 | IsOrdinalProperty(property, &isOrdinal); |
michael@0 | 486 | if (! isOrdinal) |
michael@0 | 487 | continue; |
michael@0 | 488 | |
michael@0 | 489 | nsCOMPtr<nsISimpleEnumerator> sources; |
michael@0 | 490 | aDataSource->GetSources(property, aElement, true, getter_AddRefs(sources)); |
michael@0 | 491 | if (! sources) |
michael@0 | 492 | continue; |
michael@0 | 493 | |
michael@0 | 494 | while (1) { |
michael@0 | 495 | bool hasMoreSources = false; |
michael@0 | 496 | sources->HasMoreElements(&hasMoreSources); |
michael@0 | 497 | if (! hasMoreSources) |
michael@0 | 498 | break; |
michael@0 | 499 | |
michael@0 | 500 | nsCOMPtr<nsISupports> isupports2; |
michael@0 | 501 | sources->GetNext(getter_AddRefs(isupports2)); |
michael@0 | 502 | if (! isupports2) |
michael@0 | 503 | break; |
michael@0 | 504 | |
michael@0 | 505 | nsCOMPtr<nsIRDFResource> source = |
michael@0 | 506 | do_QueryInterface(isupports2); |
michael@0 | 507 | |
michael@0 | 508 | if (source == aContainer) |
michael@0 | 509 | // Found it. |
michael@0 | 510 | return OrdinalResourceToIndex(property, aIndex); |
michael@0 | 511 | } |
michael@0 | 512 | } |
michael@0 | 513 | |
michael@0 | 514 | return NS_OK; |
michael@0 | 515 | } |