forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsRDFService.cpp
1694 lines (1385 loc) · 46.1 KB
/
nsRDFService.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla 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/MPL/
*
* 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.
*
* The Original Code is mozilla.org code.
*
* 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.
*
* Contributor(s):
* Benjamin Smedberg <benjamin@smedbergs.us>
*
* Alternatively, the contents of this file may be used under the terms of
* either of 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 MPL, 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 MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK *****
*
*
* This Original Code has been modified by IBM Corporation.
* Modifications made by IBM described herein are
* Copyright (c) International Business Machines
* Corporation, 2000
*
* Modifications to Mozilla code or documentation
* identified per MPL Section 3.3
*
* Date Modified by Description of modification
* 03/27/2000 IBM Corp. Added PR_CALLBACK for Optlink
* use in OS2
*/
/*
This file provides the implementation for the RDF service manager.
TO DO
-----
1) Implement the CreateDataBase() methods.
2) Cache date and int literals.
*/
#include "nsRDFService.h"
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "nsMemory.h"
#include "nsIAtom.h"
#include "nsIComponentManager.h"
#include "nsIRDFDataSource.h"
#include "nsIRDFNode.h"
#include "nsIRDFRemoteDataSource.h"
#include "nsIServiceManager.h"
#include "nsIFactory.h"
#include "nsRDFCID.h"
#include "nsString.h"
#include "nsXPIDLString.h"
#include "nsNetUtil.h"
#include "pldhash.h"
#include "plhash.h"
#include "plstr.h"
#include "prlog.h"
#include "prprf.h"
#include "prmem.h"
#include "rdf.h"
#include "nsCRT.h"
#include "nsCRTGlue.h"
#include "prbit.h"
////////////////////////////////////////////////////////////////////////
static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
static NS_DEFINE_CID(kRDFDefaultResourceCID, NS_RDFDEFAULTRESOURCE_CID);
static NS_DEFINE_IID(kIRDFLiteralIID, NS_IRDFLITERAL_IID);
static NS_DEFINE_IID(kIRDFDateIID, NS_IRDFDATE_IID);
static NS_DEFINE_IID(kIRDFIntIID, NS_IRDFINT_IID);
static NS_DEFINE_IID(kIRDFNodeIID, NS_IRDFNODE_IID);
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
#ifdef PR_LOGGING
static PRLogModuleInfo* gLog = nsnull;
#endif
class BlobImpl;
// These functions are copied from nsprpub/lib/ds/plhash.c, with one
// change to free the key in DataSourceFreeEntry.
// XXX sigh, why were DefaultAllocTable et. al. declared static, anyway?
static void *
DataSourceAllocTable(void *pool, PRSize size)
{
return PR_MALLOC(size);
}
static void
DataSourceFreeTable(void *pool, void *item)
{
PR_Free(item);
}
static PLHashEntry *
DataSourceAllocEntry(void *pool, const void *key)
{
return PR_NEW(PLHashEntry);
}
static void
DataSourceFreeEntry(void *pool, PLHashEntry *he, PRUintn flag)
{
if (flag == HT_FREE_ENTRY) {
PL_strfree((char*) he->key);
PR_Free(he);
}
}
static PLHashAllocOps dataSourceHashAllocOps = {
DataSourceAllocTable, DataSourceFreeTable,
DataSourceAllocEntry, DataSourceFreeEntry
};
//----------------------------------------------------------------------
//
// For the mResources hashtable.
//
struct ResourceHashEntry : public PLDHashEntryHdr {
const char *mKey;
nsIRDFResource *mResource;
static PLDHashNumber
HashKey(PLDHashTable *table, const void *key)
{
return nsCRT::HashCode(static_cast<const char *>(key));
}
static PRBool
MatchEntry(PLDHashTable *table, const PLDHashEntryHdr *hdr,
const void *key)
{
const ResourceHashEntry *entry =
static_cast<const ResourceHashEntry *>(hdr);
return 0 == nsCRT::strcmp(static_cast<const char *>(key),
entry->mKey);
}
};
static PLDHashTableOps gResourceTableOps = {
PL_DHashAllocTable,
PL_DHashFreeTable,
ResourceHashEntry::HashKey,
ResourceHashEntry::MatchEntry,
PL_DHashMoveEntryStub,
PL_DHashClearEntryStub,
PL_DHashFinalizeStub,
nsnull
};
// ----------------------------------------------------------------------
//
// For the mLiterals hashtable.
//
struct LiteralHashEntry : public PLDHashEntryHdr {
nsIRDFLiteral *mLiteral;
const PRUnichar *mKey;
static PLDHashNumber
HashKey(PLDHashTable *table, const void *key)
{
return nsCRT::HashCode(static_cast<const PRUnichar *>(key));
}
static PRBool
MatchEntry(PLDHashTable *table, const PLDHashEntryHdr *hdr,
const void *key)
{
const LiteralHashEntry *entry =
static_cast<const LiteralHashEntry *>(hdr);
return 0 == nsCRT::strcmp(static_cast<const PRUnichar *>(key),
entry->mKey);
}
};
static PLDHashTableOps gLiteralTableOps = {
PL_DHashAllocTable,
PL_DHashFreeTable,
LiteralHashEntry::HashKey,
LiteralHashEntry::MatchEntry,
PL_DHashMoveEntryStub,
PL_DHashClearEntryStub,
PL_DHashFinalizeStub,
nsnull
};
// ----------------------------------------------------------------------
//
// For the mInts hashtable.
//
struct IntHashEntry : public PLDHashEntryHdr {
nsIRDFInt *mInt;
PRInt32 mKey;
static PLDHashNumber
HashKey(PLDHashTable *table, const void *key)
{
return PLDHashNumber(*static_cast<const PRInt32 *>(key));
}
static PRBool
MatchEntry(PLDHashTable *table, const PLDHashEntryHdr *hdr,
const void *key)
{
const IntHashEntry *entry =
static_cast<const IntHashEntry *>(hdr);
return *static_cast<const PRInt32 *>(key) == entry->mKey;
}
};
static PLDHashTableOps gIntTableOps = {
PL_DHashAllocTable,
PL_DHashFreeTable,
IntHashEntry::HashKey,
IntHashEntry::MatchEntry,
PL_DHashMoveEntryStub,
PL_DHashClearEntryStub,
PL_DHashFinalizeStub,
nsnull
};
// ----------------------------------------------------------------------
//
// For the mDates hashtable.
//
struct DateHashEntry : public PLDHashEntryHdr {
nsIRDFDate *mDate;
PRTime mKey;
static PLDHashNumber
HashKey(PLDHashTable *table, const void *key)
{
// xor the low 32 bits with the high 32 bits.
PRTime t = *static_cast<const PRTime *>(key);
PRInt64 h64, l64;
LL_USHR(h64, t, 32);
l64 = LL_INIT(0, 0xffffffff);
LL_AND(l64, l64, t);
PRInt32 h32, l32;
LL_L2I(h32, h64);
LL_L2I(l32, l64);
return PLDHashNumber(l32 ^ h32);
}
static PRBool
MatchEntry(PLDHashTable *table, const PLDHashEntryHdr *hdr,
const void *key)
{
const DateHashEntry *entry =
static_cast<const DateHashEntry *>(hdr);
return LL_EQ(*static_cast<const PRTime *>(key), entry->mKey);
}
};
static PLDHashTableOps gDateTableOps = {
PL_DHashAllocTable,
PL_DHashFreeTable,
DateHashEntry::HashKey,
DateHashEntry::MatchEntry,
PL_DHashMoveEntryStub,
PL_DHashClearEntryStub,
PL_DHashFinalizeStub,
nsnull
};
class BlobImpl : public nsIRDFBlob
{
public:
struct Data {
PRInt32 mLength;
PRUint8 *mBytes;
};
BlobImpl(const PRUint8 *aBytes, PRInt32 aLength)
{
mData.mLength = aLength;
mData.mBytes = new PRUint8[aLength];
memcpy(mData.mBytes, aBytes, aLength);
NS_ADDREF(RDFServiceImpl::gRDFService);
RDFServiceImpl::gRDFService->RegisterBlob(this);
}
virtual ~BlobImpl()
{
RDFServiceImpl::gRDFService->UnregisterBlob(this);
// Use NS_RELEASE2() here, because we want to decrease the
// refcount, but not null out the gRDFService pointer (which is
// what a vanilla NS_RELEASE() would do).
nsrefcnt refcnt;
NS_RELEASE2(RDFServiceImpl::gRDFService, refcnt);
delete[] mData.mBytes;
}
NS_DECL_ISUPPORTS
NS_DECL_NSIRDFNODE
NS_DECL_NSIRDFBLOB
Data mData;
};
NS_IMPL_ISUPPORTS2(BlobImpl, nsIRDFNode, nsIRDFBlob)
NS_IMETHODIMP
BlobImpl::EqualsNode(nsIRDFNode *aNode, PRBool *aEquals)
{
nsCOMPtr<nsIRDFBlob> blob = do_QueryInterface(aNode);
if (blob) {
PRInt32 length;
blob->GetLength(&length);
if (length == mData.mLength) {
const PRUint8 *bytes;
blob->GetValue(&bytes);
if (0 == memcmp(bytes, mData.mBytes, length)) {
*aEquals = PR_TRUE;
return NS_OK;
}
}
}
*aEquals = PR_FALSE;
return NS_OK;
}
NS_IMETHODIMP
BlobImpl::GetValue(const PRUint8 **aResult)
{
*aResult = mData.mBytes;
return NS_OK;
}
NS_IMETHODIMP
BlobImpl::GetLength(PRInt32 *aResult)
{
*aResult = mData.mLength;
return NS_OK;
}
// ----------------------------------------------------------------------
//
// For the mBlobs hashtable.
//
struct BlobHashEntry : public PLDHashEntryHdr {
BlobImpl *mBlob;
static PLDHashNumber
HashKey(PLDHashTable *table, const void *key)
{
const BlobImpl::Data *data =
static_cast<const BlobImpl::Data *>(key);
const PRUint8 *p = data->mBytes, *limit = p + data->mLength;
PLDHashNumber h = 0;
for ( ; p < limit; ++p)
h = PR_ROTATE_LEFT32(h, 4) ^ *p;
return h;
}
static PRBool
MatchEntry(PLDHashTable *table, const PLDHashEntryHdr *hdr,
const void *key)
{
const BlobHashEntry *entry =
static_cast<const BlobHashEntry *>(hdr);
const BlobImpl::Data *left = &entry->mBlob->mData;
const BlobImpl::Data *right =
static_cast<const BlobImpl::Data *>(key);
return (left->mLength == right->mLength)
&& 0 == memcmp(left->mBytes, right->mBytes, right->mLength);
}
};
static PLDHashTableOps gBlobTableOps = {
PL_DHashAllocTable,
PL_DHashFreeTable,
BlobHashEntry::HashKey,
BlobHashEntry::MatchEntry,
PL_DHashMoveEntryStub,
PL_DHashClearEntryStub,
PL_DHashFinalizeStub,
nsnull
};
////////////////////////////////////////////////////////////////////////
// LiteralImpl
//
// Currently, all literals are implemented exactly the same way;
// i.e., there is are no resource factories to allow you to generate
// customer resources. I doubt that makes sense, anyway.
//
class LiteralImpl : public nsIRDFLiteral {
public:
static nsresult
Create(const PRUnichar* aValue, nsIRDFLiteral** aResult);
// nsISupports
NS_DECL_ISUPPORTS
// nsIRDFNode
NS_DECL_NSIRDFNODE
// nsIRDFLiteral
NS_DECL_NSIRDFLITERAL
protected:
LiteralImpl(const PRUnichar* s);
virtual ~LiteralImpl();
const PRUnichar* GetValue() const {
size_t objectSize = ((sizeof(LiteralImpl) + sizeof(PRUnichar) - 1) / sizeof(PRUnichar)) * sizeof(PRUnichar);
return reinterpret_cast<const PRUnichar*>(reinterpret_cast<const unsigned char*>(this) + objectSize);
}
};
nsresult
LiteralImpl::Create(const PRUnichar* aValue, nsIRDFLiteral** aResult)
{
// Goofy math to get alignment right. Copied from nsSharedString.h.
size_t objectSize = ((sizeof(LiteralImpl) + sizeof(PRUnichar) - 1) / sizeof(PRUnichar)) * sizeof(PRUnichar);
size_t stringLen = nsCharTraits<PRUnichar>::length(aValue);
size_t stringSize = (stringLen + 1) * sizeof(PRUnichar);
void* objectPtr = operator new(objectSize + stringSize);
if (! objectPtr)
return NS_ERROR_NULL_POINTER;
PRUnichar* buf = reinterpret_cast<PRUnichar*>(static_cast<unsigned char*>(objectPtr) + objectSize);
nsCharTraits<PRUnichar>::copy(buf, aValue, stringLen + 1);
NS_ADDREF(*aResult = new (objectPtr) LiteralImpl(buf));
return NS_OK;
}
LiteralImpl::LiteralImpl(const PRUnichar* s)
{
RDFServiceImpl::gRDFService->RegisterLiteral(this);
NS_ADDREF(RDFServiceImpl::gRDFService);
}
LiteralImpl::~LiteralImpl()
{
RDFServiceImpl::gRDFService->UnregisterLiteral(this);
// Use NS_RELEASE2() here, because we want to decrease the
// refcount, but not null out the gRDFService pointer (which is
// what a vanilla NS_RELEASE() would do).
nsrefcnt refcnt;
NS_RELEASE2(RDFServiceImpl::gRDFService, refcnt);
}
NS_IMPL_THREADSAFE_ADDREF(LiteralImpl)
NS_IMPL_THREADSAFE_RELEASE(LiteralImpl)
nsresult
LiteralImpl::QueryInterface(REFNSIID iid, void** result)
{
if (! result)
return NS_ERROR_NULL_POINTER;
*result = nsnull;
if (iid.Equals(kIRDFLiteralIID) ||
iid.Equals(kIRDFNodeIID) ||
iid.Equals(kISupportsIID)) {
*result = static_cast<nsIRDFLiteral*>(this);
AddRef();
return NS_OK;
}
return NS_NOINTERFACE;
}
NS_IMETHODIMP
LiteralImpl::EqualsNode(nsIRDFNode* aNode, PRBool* aResult)
{
nsresult rv;
nsIRDFLiteral* literal;
rv = aNode->QueryInterface(kIRDFLiteralIID, (void**) &literal);
if (NS_SUCCEEDED(rv)) {
*aResult = (static_cast<nsIRDFLiteral*>(this) == literal);
NS_RELEASE(literal);
return NS_OK;
}
else if (rv == NS_NOINTERFACE) {
*aResult = PR_FALSE;
return NS_OK;
}
else {
return rv;
}
}
NS_IMETHODIMP
LiteralImpl::GetValue(PRUnichar* *value)
{
NS_ASSERTION(value, "null ptr");
if (! value)
return NS_ERROR_NULL_POINTER;
const PRUnichar *temp = GetValue();
*value = temp? NS_strdup(temp) : 0;
return NS_OK;
}
NS_IMETHODIMP
LiteralImpl::GetValueConst(const PRUnichar** aValue)
{
*aValue = GetValue();
return NS_OK;
}
////////////////////////////////////////////////////////////////////////
// DateImpl
//
class DateImpl : public nsIRDFDate {
public:
DateImpl(const PRTime s);
virtual ~DateImpl();
// nsISupports
NS_DECL_ISUPPORTS
// nsIRDFNode
NS_DECL_NSIRDFNODE
// nsIRDFDate
NS_IMETHOD GetValue(PRTime *value);
private:
nsresult EqualsDate(nsIRDFDate* date, PRBool* result);
PRTime mValue;
};
DateImpl::DateImpl(const PRTime s)
: mValue(s)
{
RDFServiceImpl::gRDFService->RegisterDate(this);
NS_ADDREF(RDFServiceImpl::gRDFService);
}
DateImpl::~DateImpl()
{
RDFServiceImpl::gRDFService->UnregisterDate(this);
// Use NS_RELEASE2() here, because we want to decrease the
// refcount, but not null out the gRDFService pointer (which is
// what a vanilla NS_RELEASE() would do).
nsrefcnt refcnt;
NS_RELEASE2(RDFServiceImpl::gRDFService, refcnt);
}
NS_IMPL_ADDREF(DateImpl)
NS_IMPL_RELEASE(DateImpl)
nsresult
DateImpl::QueryInterface(REFNSIID iid, void** result)
{
if (! result)
return NS_ERROR_NULL_POINTER;
*result = nsnull;
if (iid.Equals(kIRDFDateIID) ||
iid.Equals(kIRDFNodeIID) ||
iid.Equals(kISupportsIID)) {
*result = static_cast<nsIRDFDate*>(this);
AddRef();
return NS_OK;
}
return NS_NOINTERFACE;
}
NS_IMETHODIMP
DateImpl::EqualsNode(nsIRDFNode* node, PRBool* result)
{
nsresult rv;
nsIRDFDate* date;
if (NS_SUCCEEDED(node->QueryInterface(kIRDFDateIID, (void**) &date))) {
rv = EqualsDate(date, result);
NS_RELEASE(date);
}
else {
*result = PR_FALSE;
rv = NS_OK;
}
return rv;
}
NS_IMETHODIMP
DateImpl::GetValue(PRTime *value)
{
NS_ASSERTION(value, "null ptr");
if (! value)
return NS_ERROR_NULL_POINTER;
*value = mValue;
return NS_OK;
}
nsresult
DateImpl::EqualsDate(nsIRDFDate* date, PRBool* result)
{
NS_ASSERTION(date && result, "null ptr");
if (!date || !result)
return NS_ERROR_NULL_POINTER;
nsresult rv;
PRTime p;
if (NS_FAILED(rv = date->GetValue(&p)))
return rv;
*result = LL_EQ(p, mValue);
return NS_OK;
}
////////////////////////////////////////////////////////////////////////
// IntImpl
//
class IntImpl : public nsIRDFInt {
public:
IntImpl(PRInt32 s);
virtual ~IntImpl();
// nsISupports
NS_DECL_ISUPPORTS
// nsIRDFNode
NS_DECL_NSIRDFNODE
// nsIRDFInt
NS_IMETHOD GetValue(PRInt32 *value);
private:
nsresult EqualsInt(nsIRDFInt* value, PRBool* result);
PRInt32 mValue;
};
IntImpl::IntImpl(PRInt32 s)
: mValue(s)
{
RDFServiceImpl::gRDFService->RegisterInt(this);
NS_ADDREF(RDFServiceImpl::gRDFService);
}
IntImpl::~IntImpl()
{
RDFServiceImpl::gRDFService->UnregisterInt(this);
// Use NS_RELEASE2() here, because we want to decrease the
// refcount, but not null out the gRDFService pointer (which is
// what a vanilla NS_RELEASE() would do).
nsrefcnt refcnt;
NS_RELEASE2(RDFServiceImpl::gRDFService, refcnt);
}
NS_IMPL_ADDREF(IntImpl)
NS_IMPL_RELEASE(IntImpl)
nsresult
IntImpl::QueryInterface(REFNSIID iid, void** result)
{
if (! result)
return NS_ERROR_NULL_POINTER;
*result = nsnull;
if (iid.Equals(kIRDFIntIID) ||
iid.Equals(kIRDFNodeIID) ||
iid.Equals(kISupportsIID)) {
*result = static_cast<nsIRDFInt*>(this);
AddRef();
return NS_OK;
}
return NS_NOINTERFACE;
}
NS_IMETHODIMP
IntImpl::EqualsNode(nsIRDFNode* node, PRBool* result)
{
nsresult rv;
nsIRDFInt* intValue;
if (NS_SUCCEEDED(node->QueryInterface(kIRDFIntIID, (void**) &intValue))) {
rv = EqualsInt(intValue, result);
NS_RELEASE(intValue);
}
else {
*result = PR_FALSE;
rv = NS_OK;
}
return rv;
}
NS_IMETHODIMP
IntImpl::GetValue(PRInt32 *value)
{
NS_ASSERTION(value, "null ptr");
if (! value)
return NS_ERROR_NULL_POINTER;
*value = mValue;
return NS_OK;
}
nsresult
IntImpl::EqualsInt(nsIRDFInt* intValue, PRBool* result)
{
NS_ASSERTION(intValue && result, "null ptr");
if (!intValue || !result)
return NS_ERROR_NULL_POINTER;
nsresult rv;
PRInt32 p;
if (NS_FAILED(rv = intValue->GetValue(&p)))
return rv;
*result = (p == mValue);
return NS_OK;
}
////////////////////////////////////////////////////////////////////////
// RDFServiceImpl
RDFServiceImpl*
RDFServiceImpl::gRDFService;
RDFServiceImpl::RDFServiceImpl()
: mNamedDataSources(nsnull)
{
mResources.ops = nsnull;
mLiterals.ops = nsnull;
mInts.ops = nsnull;
mDates.ops = nsnull;
mBlobs.ops = nsnull;
gRDFService = this;
}
nsresult
RDFServiceImpl::Init()
{
nsresult rv;
mNamedDataSources = PL_NewHashTable(23,
PL_HashString,
PL_CompareStrings,
PL_CompareValues,
&dataSourceHashAllocOps, nsnull);
if (! mNamedDataSources)
return NS_ERROR_OUT_OF_MEMORY;
if (!PL_DHashTableInit(&mResources, &gResourceTableOps, nsnull,
sizeof(ResourceHashEntry), PL_DHASH_MIN_SIZE)) {
mResources.ops = nsnull;
return NS_ERROR_OUT_OF_MEMORY;
}
if (!PL_DHashTableInit(&mLiterals, &gLiteralTableOps, nsnull,
sizeof(LiteralHashEntry), PL_DHASH_MIN_SIZE)) {
mLiterals.ops = nsnull;
return NS_ERROR_OUT_OF_MEMORY;
}
if (!PL_DHashTableInit(&mInts, &gIntTableOps, nsnull,
sizeof(IntHashEntry), PL_DHASH_MIN_SIZE)) {
mInts.ops = nsnull;
return NS_ERROR_OUT_OF_MEMORY;
}
if (!PL_DHashTableInit(&mDates, &gDateTableOps, nsnull,
sizeof(DateHashEntry), PL_DHASH_MIN_SIZE)) {
mDates.ops = nsnull;
return NS_ERROR_OUT_OF_MEMORY;
}
if (!PL_DHashTableInit(&mBlobs, &gBlobTableOps, nsnull,
sizeof(BlobHashEntry), PL_DHASH_MIN_SIZE)) {
mBlobs.ops = nsnull;
return NS_ERROR_OUT_OF_MEMORY;
}
mDefaultResourceFactory = do_GetClassObject(kRDFDefaultResourceCID, &rv);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get default resource factory");
if (NS_FAILED(rv)) return rv;
#ifdef PR_LOGGING
if (! gLog)
gLog = PR_NewLogModule("nsRDFService");
#endif
return NS_OK;
}
RDFServiceImpl::~RDFServiceImpl()
{
if (mNamedDataSources) {
PL_HashTableDestroy(mNamedDataSources);
mNamedDataSources = nsnull;
}
if (mResources.ops)
PL_DHashTableFinish(&mResources);
if (mLiterals.ops)
PL_DHashTableFinish(&mLiterals);
if (mInts.ops)
PL_DHashTableFinish(&mInts);
if (mDates.ops)
PL_DHashTableFinish(&mDates);
if (mBlobs.ops)
PL_DHashTableFinish(&mBlobs);
gRDFService = nsnull;
}
// static
nsresult
RDFServiceImpl::CreateSingleton(nsISupports* aOuter,
const nsIID& aIID, void **aResult)
{
NS_ENSURE_NO_AGGREGATION(aOuter);
if (gRDFService) {
NS_ERROR("Trying to create RDF serviec twice.");
return gRDFService->QueryInterface(aIID, aResult);
}
nsRefPtr<RDFServiceImpl> serv = new RDFServiceImpl();
if (!serv)
return NS_ERROR_OUT_OF_MEMORY;
nsresult rv = serv->Init();
if (NS_FAILED(rv))
return rv;
return serv->QueryInterface(aIID, aResult);
}
NS_IMPL_THREADSAFE_ISUPPORTS2(RDFServiceImpl, nsIRDFService, nsISupportsWeakReference)
// Per RFC2396.
static const PRUint8
kLegalSchemeChars[] = {
// ASCII Bits Ordered Hex
// 01234567 76543210
0x00, // 00-07
0x00, // 08-0F
0x00, // 10-17
0x00, // 18-1F
0x00, // 20-27 !"#$%&' 00000000 00000000
0x28, // 28-2F ()*+,-./ 00010100 00101000 0x28
0xff, // 30-37 01234567 11111111 11111111 0xFF
0x03, // 38-3F 89:;<=>? 11000000 00000011 0x03
0xfe, // 40-47 @ABCDEFG 01111111 11111110 0xFE
0xff, // 48-4F HIJKLMNO 11111111 11111111 0xFF
0xff, // 50-57 PQRSTUVW 11111111 11111111 0xFF
0x87, // 58-5F XYZ[\]^_ 11100001 10000111 0x87
0xfe, // 60-67 `abcdefg 01111111 11111110 0xFE
0xff, // 68-6F hijklmno 11111111 11111111 0xFF
0xff, // 70-77 pqrstuvw 11111111 11111111 0xFF
0x07, // 78-7F xyz{|}~ 11100000 00000111 0x07
0x00, 0x00, 0x00, 0x00, // >= 80
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
static inline PRBool
IsLegalSchemeCharacter(const char aChar)
{
PRUint8 mask = kLegalSchemeChars[aChar >> 3];
PRUint8 bit = PR_BIT(aChar & 0x7);
return PRBool((mask & bit) != 0);
}
NS_IMETHODIMP
RDFServiceImpl::GetResource(const nsACString& aURI, nsIRDFResource** aResource)
{
// Sanity checks
NS_PRECONDITION(aResource != nsnull, "null ptr");
NS_PRECONDITION(!aURI.IsEmpty(), "URI is empty");
if (! aResource)
return NS_ERROR_NULL_POINTER;
if (aURI.IsEmpty())
return NS_ERROR_INVALID_ARG;
const nsAFlatCString& flatURI = PromiseFlatCString(aURI);
PR_LOG(gLog, PR_LOG_DEBUG, ("rdfserv get-resource %s", flatURI.get()));
// First, check the cache to see if we've already created and
// registered this thing.
PLDHashEntryHdr *hdr =
PL_DHashTableOperate(&mResources, flatURI.get(), PL_DHASH_LOOKUP);
if (PL_DHASH_ENTRY_IS_BUSY(hdr)) {
ResourceHashEntry *entry = static_cast<ResourceHashEntry *>(hdr);
NS_ADDREF(*aResource = entry->mResource);
return NS_OK;
}
// Nope. So go to the repository to create it.
// Compute the scheme of the URI. Scan forward until we either:
//
// 1. Reach the end of the string
// 2. Encounter a non-alpha character
// 3. Encouter a colon.
//
// If we encounter a colon _before_ encountering a non-alpha
// character, then assume it's the scheme.
//
// XXX Although it's really not correct, we'll allow underscore
// characters ('_'), too.
nsACString::const_iterator p, end;
aURI.BeginReading(p);
aURI.EndReading(end);
while (p != end && IsLegalSchemeCharacter(*p))
++p;
nsresult rv;
nsCOMPtr<nsIFactory> factory;
nsACString::const_iterator begin;
aURI.BeginReading(begin);
if (*p == ':') {
// There _was_ a scheme. First see if it's the same scheme
// that we just tried to use...
if (mLastFactory && mLastURIPrefix.Equals(Substring(begin, p)))
factory = mLastFactory;
else {
// Try to find a factory using the component manager.
nsACString::const_iterator begin;
aURI.BeginReading(begin);
nsCAutoString contractID;
contractID = NS_LITERAL_CSTRING(NS_RDF_RESOURCE_FACTORY_CONTRACTID_PREFIX) +
Substring(begin, p);
factory = do_GetClassObject(contractID.get());
if (factory) {
// Store the factory in our one-element cache.
if (p != begin) {
mLastFactory = factory;
mLastURIPrefix = Substring(begin, p);
}
}
}
}
if (! factory) {
// fall through to using the "default" resource factory if either:
//
// 1. The URI didn't have a scheme, or
// 2. There was no resource factory registered for the scheme.
factory = mDefaultResourceFactory;
// Store the factory in our one-element cache.
if (p != begin) {