This repository was archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathGDataObject.m
2822 lines (2231 loc) · 89.8 KB
/
GDataObject.m
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
/* Copyright (c) 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// GDataObject.m
//
#define GDATAOBJECT_DEFINE_GLOBALS 1
#import "GDataObject.h"
#import "GDataDateTime.h"
// for automatic-determination of feed and entry class types
#import "GDataFeedBase.h"
#import "GDataEntryBase.h"
#import "GDataCategory.h"
static inline NSMutableDictionary *GDataCreateStaticDictionary(void) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
#if !GDATA_IPHONE
Class cls = NSClassFromString(@"NSGarbageCollector");
if (cls) {
id collector = [cls performSelector:@selector(defaultCollector)];
[collector performSelector:@selector(disableCollectorForPointer:)
withObject:dict];
}
#endif
return dict;
}
// in a cache of attribute declarations, this marker indicates that the class
// also declared that it wants child text parsed as the content value or child
// xml held as xml element objects
//
// these start with a space to avoid colliding with any real attribute name
static NSString* const kContentValueDeclarationMarker = @" __content";
static NSString* const kChildXMLDeclarationMarker = @" __childXML";
// Elements may call -addExtensionDeclarationForParentClass:childClass: and
// addAttributeExtensionDeclarationForParentClass: to declare extensions to be
// parsed; the declaration applies in the element and all children of the element.
@interface GDataExtensionDeclaration : NSObject {
Class parentClass_;
Class childClass_;
BOOL isAttribute_;
}
- (id)initWithParentClass:(Class)parentClass childClass:(Class)childClass isAttribute:(BOOL)attrFlag;
- (Class)parentClass;
- (Class)childClass;
- (BOOL)isAttribute;
@end
@interface GDataObject (PrivateMethods)
// array of local attribute names to be automatically parsed and
// generated
- (void)setAttributeDeclarationsCache:(NSDictionary *)decls;
- (NSMutableDictionary *)attributeDeclarationsCache;
// array of attribute declarations for the current class, from the cache
- (void)setAttributeDeclarations:(NSArray *)array;
- (NSMutableArray *)attributeDeclarations;
- (void)parseAttributesForElement:(NSXMLElement *)element;
- (void)addAttributesToElement:(NSXMLElement *)element;
// routines for comparing attributes
- (BOOL)hasAttributesEqualToAttributesOf:(GDataObject *)other;
- (NSArray *)attributesIgnoredForEquality;
// element string content
- (void)parseContentValueForElement:(NSXMLElement *)element;
- (void)addContentValueToElement:(NSXMLElement *)element;
- (BOOL)hasContentValueEqualToContentValueOf:(GDataObject *)other;
// XML values content (kept unparsed)
- (void)keepChildXMLElementsForElement:(NSXMLElement *)element;
- (void)addChildXMLElementsToElement:(NSXMLElement *)element;
- (BOOL)hasChildXMLElementsEqualToChildXMLElementsOf:(GDataObject *)other;
// dictionary of all extensions actually found in the XML element
- (void)setExtensions:(NSDictionary *)extensions;
- (NSDictionary *)extensions;
// cache of arrays of extensions that may be found in this class and in
// subclasses of this class.
- (void)setExtensionDeclarationsCache:(NSDictionary *)decls;
- (NSMutableDictionary *)extensionDeclarationsCache;
- (NSMutableArray *)extensionDeclarationsForParentClass:(Class)parentClass;
- (void)addExtensionDeclarationForParentClass:(Class)parentClass
childClass:(Class)childClass
isAttribute:(BOOL)isAttribute;
- (void)addUnknownChildNodesForElement:(NSXMLElement *)element;
- (void)parseExtensionsForElement:(NSXMLElement *)element;
- (void)handleParsedElement:(NSXMLNode *)element;
- (void)handleParsedElements:(NSArray *)array;
- (NSString *)qualifiedNameForExtensionClass:(Class)theClass;
+ (Class)classForCategoryWithScheme:(NSString *)scheme
term:(NSString *)term
fromMap:(NSDictionary *)map;
@end
@implementation GDataObject
// The qualified name map avoids the need to regenerate qualified
// element names (foo:bar) repeatedly
static NSMutableDictionary *gQualifiedNameMap = nil;
+ (void)load {
// Initialize gQualifiedNameMap early so we can @synchronize on accesses
// to it
gQualifiedNameMap = GDataCreateStaticDictionary();
}
+ (id)object {
return [[[self alloc] init] autorelease];
}
- (id)init {
self = [super init];
if (self) {
// there is no parent
extensionDeclarationsCache_ = [[NSMutableDictionary alloc] init];
attributeDeclarationsCache_ = [[NSMutableDictionary alloc] init];
[self addParseDeclarations];
}
return self;
}
// intended mainly for testing, initWithServiceVersion allows the service
// version to be set prior to declaring extensions; this is useful
// for overriding the default service version for the class when
// manually allocating a copy of the object
- (id)initWithServiceVersion:(NSString *)serviceVersion {
[self setServiceVersion:serviceVersion];
return [self init];
}
// this init routine is only used when passing in a top-level surrogates
// dictionary
- (id)initWithXMLElement:(NSXMLElement *)element
parent:(GDataObject *)parent
serviceVersion:(NSString *)serviceVersion
surrogates:(NSDictionary *)surrogates
shouldIgnoreUnknowns:(BOOL)shouldIgnoreUnknowns {
[self setServiceVersion:serviceVersion];
[self setSurrogates:surrogates];
[self setShouldIgnoreUnknowns:shouldIgnoreUnknowns];
id obj = [self initWithXMLElement:element
parent:parent];
return obj;
}
// subclasses will typically override initWithXMLElement:parent:
// and do their own parsing after this method returns
- (id)initWithXMLElement:(NSXMLElement *)element
parent:(GDataObject *)parent {
self = [super init];
if (self) {
[self setParent:parent];
if (parent != nil) {
// top-level objects (feeds and entries) have nil parents, and
// have their service version set previously in
// initWithXMLElement:parent:serviceVersion:surrogates:; child
// objects have their service version set here
[self setServiceVersion:[parent serviceVersion]];
// feeds may specify that contained entries and their child elements
// should ignore any unparsed XML
[self setShouldIgnoreUnknowns:[parent shouldIgnoreUnknowns]];
// get the parent's declaration caches, and temporarily hang on to them
// in our ivar to avoid the need to get them recursively from the topmost
// parent
//
// We'll release these below, so that only the topmost parent retains
// them. The topmost parent retains them in case some subclass code still
// wants to do parsing after we return.
extensionDeclarationsCache_ = [[parent extensionDeclarationsCache] retain];
GDATA_DEBUG_ASSERT(extensionDeclarationsCache_ != nil, @"missing extn decl");
attributeDeclarationsCache_ = [[parent attributeDeclarationsCache] retain];
GDATA_DEBUG_ASSERT(extensionDeclarationsCache_ != nil, @"missing attr decl");
} else {
// parent is nil, so this is the topmost parent
extensionDeclarationsCache_ = [[NSMutableDictionary alloc] init];
attributeDeclarationsCache_ = [[NSMutableDictionary alloc] init];
}
[self setNamespaces:[[self class] dictionaryForElementNamespaces:element]];
[self addUnknownChildNodesForElement:element];
// if we've not previously cached declarations for this class,
// add the declarations now
Class currClass = [self class];
NSDictionary *prevExtnDecls = [extensionDeclarationsCache_ objectForKey:currClass];
if (prevExtnDecls == nil) {
[self addExtensionDeclarations];
}
NSMutableArray *prevAttrDecls = [attributeDeclarationsCache_ objectForKey:currClass];
if (prevAttrDecls == nil) {
[self addParseDeclarations];
// if any parse declarations are added, attributeDeclarations_ will be set
// to the cached copy of this object's attribute decls
} else {
GDATA_DEBUG_ASSERT(attributeDeclarations_ == nil, @"attrDecls previously set");
attributeDeclarations_ = [prevAttrDecls retain];
}
[self parseExtensionsForElement:element];
[self parseAttributesForElement:element];
[self parseContentValueForElement:element];
[self keepChildXMLElementsForElement:element];
[self setElementName:[element name]];
if (parent != nil) {
// rather than keep a reference to the cache of declarations in the
// parent, set our pointer to nil; if a subclass continues to parse, the
// getter will obtain them by calling into the parent. This lets callers
// free up the extensionDeclarations_ when parsing is done by just
// freeing them in the topmost parent with clearExtensionDeclarationsCache
[extensionDeclarationsCache_ release];
extensionDeclarationsCache_ = nil;
[attributeDeclarationsCache_ release];
attributeDeclarationsCache_ = nil;
}
#if GDATA_USES_LIBXML
if (!shouldIgnoreUnknowns_) {
// retain the element so that pointers to internal nodes remain valid
[self setProperty:element forKey:kGDataXMLElementPropertyKey];
}
#endif
}
return self;
}
- (BOOL)isEqual:(GDataObject *)other {
if (self == other) return YES;
if (![other isKindOfClass:[self class]]) return NO;
// We used to compare the local names of the objects with
// NSXMLNode's localNameForName: on each object's elementName, but that
// prevents us from comparing the contents of a manually-constructed object
// (which lacks a specific local name) with one found in an actual XML feed.
#if GDATA_USES_LIBXML
// libxml adds namespaces when copying elements, so we can't rely
// on those when comparing nodes
return AreEqualOrBothNil([self extensions], [other extensions])
&& [self hasAttributesEqualToAttributesOf:other]
&& [self hasContentValueEqualToContentValueOf:other]
&& [self hasChildXMLElementsEqualToChildXMLElementsOf:other];
#else
return AreEqualOrBothNil([self extensions], [other extensions])
&& [self hasAttributesEqualToAttributesOf:other]
&& [self hasContentValueEqualToContentValueOf:other]
&& [self hasChildXMLElementsEqualToChildXMLElementsOf:other]
&& AreEqualOrBothNil([self namespaces], [other namespaces]);
#endif
// What we're not comparing here:
// parent object pointers
// extension declarations
// unknown attributes & children
// local element names
// service version
// userData
}
// By definition, for two objects to potentially be considered equal,
// they must have the same hash value. The hash is mostly ignored,
// but removeObjectsInArray: in Leopard does seem to check the hash,
// and NSObject's default hash method just returns the instance pointer.
// We'll define hash here for all of our GDataObjects.
- (NSUInteger)hash {
return (NSUInteger) (void *) [GDataObject class];
}
- (id)copyWithZone:(NSZone *)zone {
GDataObject* newObject = [[[self class] allocWithZone:zone] init];
[newObject setElementName:[self elementName]];
[newObject setParent:nil];
[newObject setServiceVersion:[self serviceVersion]];
NSDictionary *namespaces =
[GDataUtilities mutableDictionaryWithCopiesOfObjectsInDictionary:[self namespaces]];
[newObject setNamespaces:namespaces];
NSDictionary *extensions =
[GDataUtilities mutableDictionaryWithCopiesOfArraysInDictionary:[self extensions]];
[newObject setExtensions:extensions];
NSDictionary *attributes =
[GDataUtilities mutableDictionaryWithCopiesOfObjectsInDictionary:[self attributes]];
[newObject setAttributes:attributes];
[newObject setAttributeDeclarations:[self attributeDeclarations]];
// we copy the attribute declarations, which are retained by this object,
// but we do not copy not the caches of extension or attribute declarations,
// as those will be invalid once the top parent is released
// a marker in the attributes cache indicates the content value and
// and child XML declaration settings
if ([self hasDeclaredContentValue]) {
[newObject setContentStringValue:[self contentStringValue]];
}
if ([self hasDeclaredChildXMLElements]) {
NSArray *childElements = [self childXMLElements];
NSArray *arr = [GDataUtilities arrayWithCopiesOfObjectsInArray:childElements];
[newObject setChildXMLElements:arr];
}
BOOL shouldIgnoreUnknowns = [self shouldIgnoreUnknowns];
[newObject setShouldIgnoreUnknowns:shouldIgnoreUnknowns];
if (!shouldIgnoreUnknowns) {
NSArray *unknownChildren =
[GDataUtilities mutableArrayWithCopiesOfObjectsInArray:[self unknownChildren]];
[newObject setUnknownChildren:unknownChildren];
NSArray *unknownAttributes =
[GDataUtilities mutableArrayWithCopiesOfObjectsInArray:[self unknownAttributes]];
[newObject setUnknownAttributes:unknownAttributes];
}
return newObject;
// What we're not copying:
// parent object pointer
// surrogates
// userData
// userProperties
}
- (void)dealloc {
[elementName_ release];
[namespaces_ release];
[extensionDeclarationsCache_ release];
[attributeDeclarationsCache_ release];
[attributeDeclarations_ release];
[extensions_ release];
[attributes_ release];
[contentValue_ release];
[childXMLElements_ release];
[unknownChildren_ release];
[unknownAttributes_ release];
[surrogates_ release];
[serviceVersion_ release];
[coreProtocolVersion_ release];
[userData_ release];
[userProperties_ release];
[super dealloc];
}
// XMLElement must be implemented by subclasses
- (NSXMLElement *)XMLElement {
// subclass should override if they have custom elements or attributes
NSXMLElement *element = [self XMLElementWithExtensionsAndDefaultName:nil];
return element;
}
- (NSXMLDocument *)XMLDocument {
NSXMLElement *element = [self XMLElement];
NSXMLDocument *doc = [[[NSXMLDocument alloc] initWithRootElement:(id)element] autorelease];
[doc setVersion:@"1.0"];
[doc setCharacterEncoding:@"UTF-8"];
return doc;
}
- (BOOL)generateContentInputStream:(NSInputStream **)outInputStream
length:(unsigned long long *)outLength
headers:(NSDictionary **)outHeaders {
// subclasses may return a data stream representing this object
// for uploading
return NO;
}
- (NSString *)uploadMIMEType {
// subclasses may return the type of data to be uploaded
return nil;
}
- (NSData *)uploadData {
// subclasses may return data to be uploaded along with the object
return nil;
}
- (NSFileHandle *)uploadFileHandle {
// subclasses may return a file handle to be uploaded along with the object
return nil;
}
- (NSURL *)uploadLocationURL {
// subclasses may return a resumable upload location URL for restarting
// uploads
return nil;
}
- (BOOL)shouldUploadDataOnly {
return NO;
}
#pragma mark -
- (void)setElementName:(NSString *)name {
[elementName_ release];
elementName_ = [name copy];
}
- (NSString *)elementName {
return elementName_;
}
- (void)setNamespaces:(NSDictionary *)dict {
[namespaces_ release];
namespaces_ = [dict mutableCopy];
}
- (void)addNamespaces:(NSDictionary *)dict {
if (namespaces_ == nil) {
namespaces_ = [[NSMutableDictionary alloc] init];
}
[namespaces_ addEntriesFromDictionary:dict];
}
- (NSDictionary *)namespaces {
return namespaces_;
}
- (NSDictionary *)completeNamespaces {
// return a dictionary containing all namespaces
// in this object and its parent objects
NSDictionary *parentNamespaces = [parent_ completeNamespaces];
NSDictionary *ownNamespaces = namespaces_;
if (ownNamespaces == nil) return parentNamespaces;
if (parentNamespaces == nil) return ownNamespaces;
// combine them, replacing parent-defined prefixes with own ones
NSMutableDictionary *mutableDict;
mutableDict = [NSMutableDictionary dictionaryWithDictionary:parentNamespaces];
[mutableDict addEntriesFromDictionary:ownNamespaces];
return mutableDict;
}
- (void)pruneInheritedNamespaces {
if (parent_ == nil || [namespaces_ count] == 0) return;
// if a prefix is explicitly defined the same for the parent as it is locally,
// remove it, since we can rely on the parent's definition
NSMutableDictionary *prunedNamespaces
= [NSMutableDictionary dictionaryWithDictionary:namespaces_];
NSDictionary *parentNamespaces = [parent_ completeNamespaces];
for (NSString *prefix in namespaces_) {
NSString *ownURI = [namespaces_ objectForKey:prefix];
NSString *parentURI = [parentNamespaces objectForKey:prefix];
if (AreEqualOrBothNil(ownURI, parentURI)) {
[prunedNamespaces removeObjectForKey:prefix];
}
}
[self setNamespaces:prunedNamespaces];
}
- (void)setParent:(GDataObject *)obj {
parent_ = obj; // parent_ is a weak (not retained) reference
}
- (GDataObject *)parent {
return parent_;
}
- (void)setAttributeDeclarationsCache:(NSDictionary *)cache {
[attributeDeclarationsCache_ autorelease];
attributeDeclarationsCache_ = [cache mutableCopy];
}
- (NSMutableDictionary *)attributeDeclarationsCache {
// warning: rely on this only during parsing; it will not be safe if the
// top parent is no longer allocated
if (attributeDeclarationsCache_) {
return attributeDeclarationsCache_;
}
return [[self parent] attributeDeclarationsCache];
}
- (void)setAttributeDeclarations:(NSArray *)array {
[attributeDeclarations_ autorelease];
attributeDeclarations_ = [array mutableCopy];
}
- (NSMutableArray *)attributeDeclarations {
return attributeDeclarations_;
}
- (void)setAttributes:(NSDictionary *)dict {
[attributes_ autorelease];
attributes_ = [dict mutableCopy];
}
- (NSDictionary *)attributes {
return attributes_;
}
- (void)setExtensions:(NSDictionary *)extensions {
[extensions_ autorelease];
extensions_ = [extensions mutableCopy];
}
- (NSDictionary *)extensions {
return extensions_;
}
- (void)setExtensionDeclarationsCache:(NSDictionary *)decls {
[extensionDeclarationsCache_ autorelease];
extensionDeclarationsCache_ = [decls mutableCopy];
}
- (NSMutableDictionary *)extensionDeclarationsCache {
// warning: rely on this only during parsing; it will not be safe if the
// top parent is no longer allocated
if (extensionDeclarationsCache_) {
return extensionDeclarationsCache_;
}
return [[self parent] extensionDeclarationsCache];
}
- (void)clearExtensionDeclarationsCache {
// allows external classes to free up the declarations
[self setExtensionDeclarationsCache:nil];
}
- (void)setUnknownChildren:(NSArray *)arr {
[unknownChildren_ autorelease];
unknownChildren_ = [arr mutableCopy];
}
- (NSArray *)unknownChildren {
return unknownChildren_;
}
- (void)setUnknownAttributes:(NSArray *)arr {
[unknownAttributes_ autorelease];
unknownAttributes_ = [arr mutableCopy];
}
- (NSArray *)unknownAttributes {
return unknownAttributes_;
}
- (void)setShouldIgnoreUnknowns:(BOOL)flag {
shouldIgnoreUnknowns_ = flag;
}
- (BOOL)shouldIgnoreUnknowns {
return shouldIgnoreUnknowns_;
}
- (void)setSurrogates:(NSDictionary *)surrogates {
[surrogates_ autorelease];
surrogates_ = [surrogates retain];
}
- (NSDictionary *)surrogates {
return surrogates_;
}
+ (NSString *)defaultServiceVersion {
return nil;
}
- (void)setServiceVersion:(NSString *)str {
if (!AreEqualOrBothNil(str, serviceVersion_)) {
// reset the core protocol version, since it's based on the service version
[self setCoreProtocolVersion:nil];
[serviceVersion_ autorelease];
serviceVersion_ = [str copy];
}
}
- (NSString *)serviceVersion {
if (serviceVersion_ != nil) {
return serviceVersion_;
}
NSString *str = [[self class] defaultServiceVersion];
return str;
}
- (BOOL)isServiceVersionAtLeast:(NSString *)otherVersion {
NSString *serviceVersion = [self serviceVersion];
NSComparisonResult result = [GDataUtilities compareVersion:serviceVersion
toVersion:otherVersion];
return (result != NSOrderedAscending);
}
- (BOOL)isServiceVersionAtMost:(NSString *)otherVersion {
NSString *serviceVersion = [self serviceVersion];
NSComparisonResult result = [GDataUtilities compareVersion:serviceVersion
toVersion:otherVersion];
return (result != NSOrderedDescending);
}
- (void)setCoreProtocolVersion:(NSString *)str {
[coreProtocolVersion_ autorelease];
coreProtocolVersion_ = [str copy];
}
- (NSString *)coreProtocolVersion {
if (coreProtocolVersion_ != nil) {
return coreProtocolVersion_;
}
NSString *serviceVersion = [self serviceVersion];
NSString *coreVersion = [[self class] coreProtocolVersionForServiceVersion:serviceVersion];
[self setCoreProtocolVersion:coreVersion];
return coreVersion;
}
- (BOOL)isCoreProtocolVersion1 {
NSString *coreVersion = [self coreProtocolVersion];
// technically the version number is <integer>.<integer> rather than a float,
// but intValue is a simple way to test just the major portion
int majorVer = [coreVersion intValue];
return (majorVer <= 1);
}
+ (NSString *)coreProtocolVersionForServiceVersion:(NSString *)str {
// subclasses may override this when their service versions
// do not match the core protocol version
return str;
}
#pragma mark userData and properties
- (void)setUserData:(id)userData {
[userData_ autorelease];
userData_ = [userData retain];
}
- (id)userData {
// be sure the returned pointer has the life of the autorelease pool,
// in case self is released immediately
return [[userData_ retain] autorelease];
}
- (void)setProperties:(NSDictionary *)dict {
[userProperties_ autorelease];
userProperties_ = [dict mutableCopy];
}
- (NSDictionary *)properties {
// be sure the returned pointer has the life of the autorelease pool,
// in case self is released immediately
return [[userProperties_ retain] autorelease];
}
- (void)setProperty:(id)obj forKey:(NSString *)key {
if (obj == nil) {
// user passed in nil, so delete the property
[userProperties_ removeObjectForKey:key];
} else {
// be sure the property dictionary exists
if (userProperties_ == nil) {
[self setProperties:[NSDictionary dictionary]];
}
[userProperties_ setObject:obj forKey:key];
}
}
- (id)propertyForKey:(NSString *)key {
id obj = [userProperties_ objectForKey:key];
// be sure the returned pointer has the life of the autorelease pool,
// in case self is released immediately
return [[obj retain] autorelease];
}
#pragma mark XML generation helpers
- (void)addNamespacesToElement:(NSXMLElement *)element {
// we keep namespaces in a dictionary with prefixes
// as keys. We'll step through our namespaces and convert them
// to NSXML-stype namespaces.
for (NSString *prefix in namespaces_) {
NSString *uri = [namespaces_ objectForKey:prefix];
// no per-version namespace transforms are currently needed
// uri = [self updatedVersionedNamespaceURIForPrefix:prefix
// URI:uri];
[element addNamespace:[NSXMLElement namespaceWithName:prefix
stringValue:uri]];
}
}
- (void)addExtensionsToElement:(NSXMLElement *)element {
// extensions are in a dictionary of arrays, keyed by the class
// of each kind of element
// note: this adds actual extensions, not declarations
NSDictionary *extensions = [self extensions];
// step through each extension, by class, and add those
// objects to the XML element
for (Class oneClass in extensions) {
id objectOrArray = [extensions_ objectForKey:oneClass];
if ([objectOrArray isKindOfClass:[NSArray class]]) {
[self addToElement:element XMLElementsForArray:objectOrArray];
} else {
[self addToElement:element XMLElementForObject:objectOrArray];
}
}
}
- (void)addUnknownChildNodesToElement:(NSXMLElement *)element {
// we'll add every element and attribute as "unknown", then remove them
// from this list as we parse them to create the GData object. Anything
// left remaining in this list is considered unknown.
if (shouldIgnoreUnknowns_) return;
// we have to copy the children so they don't point at the previous parent
// nodes
for (NSXMLNode *child in unknownChildren_) {
[element addChild:[[child copy] autorelease]];
}
for (NSXMLNode *attr in unknownAttributes_) {
GDATA_DEBUG_ASSERT([element attributeForName:[attr name]] == nil,
@"adding duplicate of attribute %@ (perhaps an object parsed with"
"attributeForName: instead of attributeForName:fromElement:)",
attr);
[element addAttribute:[[attr copy] autorelease]];
}
}
// this method creates a basic XML element from this GData object.
//
// this is called by the XMLElement method of subclasses; they will add their
// own attributes and children to the element returned by this method
//
// extensions may pass nil for defaultName to use the name specified in their
// extensionElementLocalName and extensionElementPrefix
- (NSXMLElement *)XMLElementWithExtensionsAndDefaultName:(NSString *)defaultName {
#if 0
// code sometimes useful for finding unparsed xml; this can be turned on
// during testing
if ([unknownAttributes_ count]) {
NSLog(@"%@ %p: unknown attributes %@\n%@\n", [self class], self, unknownAttributes_, self);
}
if ([unknownChildren_ count]) {
NSLog(@"%@ %p: unknown children %@\n%@\n", [self class], self, unknownChildren_, self);
}
#endif
// use the name from the XML
NSString *elementName = [self elementName];
if (!elementName) {
// if no name from the XML, use the name our class's XML element
// routine supplied as a default
if (defaultName) {
elementName = defaultName;
} else {
// if no default name from the class, and this class is an extension,
// use the extension's default element name
if ([[self class] conformsToProtocol:@protocol(GDataExtension)]) {
elementName = [self qualifiedNameForExtensionClass:[self class]];
} else {
// if not an extension, just use the class name
elementName = NSStringFromClass([self class]);
GDATA_DEBUG_LOG(@"GDataObject generating XML element with unknown name for class %@",
elementName);
}
}
}
NSXMLElement *element = [NSXMLNode elementWithName:elementName];
[self addNamespacesToElement:element];
[self addAttributesToElement:element];
[self addContentValueToElement:element];
[self addChildXMLElementsToElement:element];
[self addExtensionsToElement:element];
[self addUnknownChildNodesToElement:element];
return element;
}
- (NSXMLNode *)addToElement:(NSXMLElement *)element
attributeValueIfNonNil:(NSString *)val
withName:(NSString *)name {
if (val) {
NSString *filtered = [GDataUtilities stringWithControlsFilteredForString:val];
NSXMLNode* attr = [NSXMLNode attributeWithName:name stringValue:filtered];
[element addAttribute:attr];
return attr;
}
return nil;
}
- (NSXMLNode *)addToElement:(NSXMLElement *)element
attributeValueIfNonNil:(NSString *)val
withQualifiedName:(NSString *)qName
URI:(NSString *)attributeURI {
if (attributeURI == nil) {
return [self addToElement:element
attributeValueIfNonNil:val
withName:qName];
}
if (val) {
NSString *filtered = [GDataUtilities stringWithControlsFilteredForString:val];
NSXMLNode *attr = [NSXMLNode attributeWithName:qName
URI:attributeURI
stringValue:filtered];
if (attr != nil) {
[element addAttribute:attr];
return attr;
}
}
return nil;
}
- (NSXMLNode *)addToElement:(NSXMLElement *)element
attributeValueWithInteger:(NSInteger)val
withName:(NSString *)name {
NSString* str = [NSString stringWithFormat:@"%ld", (long)val];
NSXMLNode* attr = [NSXMLNode attributeWithName:name stringValue:str];
[element addAttribute:attr];
return attr;
}
// adding a child to an XML element
- (NSXMLNode *)addToElement:(NSXMLElement *)element
childWithStringValueIfNonEmpty:(NSString *)str
withName:(NSString *)name {
if ([str length] > 0) {
NSXMLNode *child = [NSXMLElement elementWithName:name stringValue:str];
[element addChild:child];
return child;
}
return nil;
}
// call the object's XMLElement method, and add the result as a new XML child
// element
- (void)addToElement:(NSXMLElement *)element
XMLElementForObject:(id)object {
if ([object isKindOfClass:[GDataAttribute class]]) {
// attribute extensions are not GDataObjects and don't implement
// XMLElement; we just get the attribute value from them
NSString *str = [object stringValue];
NSString *qName = [self qualifiedNameForExtensionClass:[object class]];
NSString *theURI = [[object class] extensionElementURI];
[self addToElement:element
attributeValueIfNonNil:str
withQualifiedName:qName
URI:theURI];
} else {
// element extension
NSXMLElement *child = [object XMLElement];
if (child) {
[element addChild:child];
}
}
}
// call the XMLElement method for each object in the array
- (void)addToElement:(NSXMLElement *)element
XMLElementsForArray:(NSArray *)arrayOfGDataObjects {
for(id item in arrayOfGDataObjects) {
[self addToElement:element XMLElementForObject:item];
}
}
#pragma mark description method helpers
#if !GDATA_SIMPLE_DESCRIPTIONS
// if the description label begins with version<= or version>= then do a service
// version check
//
// returns the label with any version prefix removed, or returns nil if the
// description fails the version check and should not be evaluated
- (NSString *)labelAdjustedForVersion:(NSString *)origLabel {
BOOL checkMinVersion = NO;
BOOL checkMaxVersion = NO;
NSString *prefix = nil;
static NSString *const kMinVersionPrefix = @"version>=";
static NSString *const kMaxVersionPrefix = @"version<=";
if ([origLabel hasPrefix:kMinVersionPrefix]) {
checkMinVersion = YES;
prefix = kMinVersionPrefix;
} else if ([origLabel hasPrefix:kMaxVersionPrefix]) {
checkMaxVersion = YES;
prefix = kMaxVersionPrefix;
}
if (!checkMaxVersion && !checkMinVersion) return origLabel;
// there is a version prefix; scan and test the version string,
// and if the test succeeds, return the label without the prefix
NSString *newLabel = origLabel;
NSString *versionStr = nil;
NSScanner *scanner = [NSScanner scannerWithString:origLabel];
if ([scanner scanString:prefix intoString:NULL]
&& [scanner scanUpToString:@":" intoString:&versionStr]
&& [scanner scanString:@":" intoString:NULL]
&& [scanner scanUpToString:@"\n" intoString:&newLabel]) {
if ((checkMinVersion && ![self isServiceVersionAtLeast:versionStr])
|| (checkMaxVersion && ![self isServiceVersionAtMost:versionStr])) {
// version test failed
return nil;
}
}
return newLabel;
}
#endif
- (void)addDescriptionRecords:(GDataDescriptionRecord *)descRecordList
toItems:(NSMutableArray *)items {
#if !GDATA_SIMPLE_DESCRIPTIONS
// the final descRecord in the list should be { nil, nil, 0 }
for (NSUInteger idx = 0; descRecordList[idx].label != nil; idx++) {
GDataDescRecTypes reportType = descRecordList[idx].reportType;
NSString *label = descRecordList[idx].label;
NSString *keyPath = descRecordList[idx].keyPath;
label = [self labelAdjustedForVersion:label];
if (label == nil) continue;