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