/
CPKeyedArchiver.j
602 lines (488 loc) · 19.7 KB
/
CPKeyedArchiver.j
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
/*
* CPKeyedArchiver.j
* Foundation
*
* Created by Francisco Tolmasky.
* Copyright 2008, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import "CPData.j"
@import "CPCoder.j"
@import "CPArray.j"
@import "CPString.j"
@import "CPNumber.j"
@import "CPDictionary.j"
@import "CPValue.j"
var CPArchiverReplacementClassNames = nil;
var _CPKeyedArchiverDidEncodeObjectSelector = 1,
_CPKeyedArchiverWillEncodeObjectSelector = 2,
_CPKeyedArchiverWillReplaceObjectWithObjectSelector = 4,
_CPKeyedArchiverDidFinishSelector = 8,
_CPKeyedArchiverWillFinishSelector = 16;
var _CPKeyedArchiverNullString = "$null",
_CPKeyedArchiverNullReference = nil,
_CPKeyedArchiverUIDKey = "CP$UID",
_CPKeyedArchiverTopKey = "$top",
_CPKeyedArchiverObjectsKey = "$objects",
_CPKeyedArchiverArchiverKey = "$archiver",
_CPKeyedArchiverVersionKey = "$version",
_CPKeyedArchiverClassNameKey = "$classname",
_CPKeyedArchiverClassesKey = "$classes",
_CPKeyedArchiverClassKey = "$class";
var _CPKeyedArchiverStringClass = Nil,
_CPKeyedArchiverNumberClass = Nil;
/* @ignore */
@implementation _CPKeyedArchiverValue : CPValue
{
}
@end
/*!
@class CPKeyedArchiver
@ingroup foundation
@brief Implements keyed archiving of object graphs (e.g. for storing data).
Implements keyed archiving of object graphs. Archiving means to
write data out in a format that be read in again later, or possibly
stored in a file. To read the data back in, use a
\c CPKeyedUnarchiver.
@par Delegate Methods
@delegate -(void)archiverWillFinish:(CPKeyedArchiver)archiver;
Called when the encoding is about to finish.
@param archiver the archiver that's about to finish
@delegate -(void)archiver:(CPKeyedArchiver)archiver didEncodeObject:(id)object;
Called when an object is encoded into the archiver.
@param archiver the archiver that encoded the object
@param object the object that was encoded
@delegate -(void)archiverDidFinish:(CPKeyedArchiver)archiver;
Called when the archiver finishes encoding.
@param archiver the arhiver that finished encoding
@delegate -(id)archiver:(CPKeyedArchiver)archiver willEncodeObject:(id)object;
Called when an object is about to be encoded. Allows the delegate to replace
the object that gets encoded with a substitute or \c nil.
@param archiver the archiver encoding the object
@param object the candidate object for encoding
@return the object to encode
@delegate -(void)archiver:(CPKeyedArchiver)archiver willReplaceObject:(id)object withObject:(id)newObject;
Called when an object is being replaced in the archiver.
@param archiver the archiver encoding the object
@param object the object to be replaced
@param newObject the replacement object
*/
@implementation CPKeyedArchiver : CPCoder
{
id _delegate;
unsigned _delegateSelectors;
CPData _data;
CPArray _objects;
CPDictionary _UIDs;
CPDictionary _conditionalUIDs;
CPDictionary _replacementObjects;
CPDictionary _replacementClassNames;
id _plistObject;
CPMutableArray _plistObjects;
CPPropertyListFormat _outputFormat;
}
/*
@ignore
*/
+ (void)initialize
{
if (self != [CPKeyedArchiver class])
return;
_CPKeyedArchiverStringClass = [CPString class];
_CPKeyedArchiverNumberClass = [CPNumber class];
_CPKeyedArchiverNullReference = [CPDictionary dictionaryWithObject:0 forKey:_CPKeyedArchiverUIDKey];
}
+ (BOOL)allowsKeyedCoding
{
return YES;
}
/*!
Archives the specified object.
@param anObject the object to archive
@return the archived object
*/
+ (CPData)archivedDataWithRootObject:(id)anObject
{
var data = [CPData dataWithPlistObject:nil],
archiver = [[self alloc] initForWritingWithMutableData:data];
[archiver encodeObject:anObject forKey:@"root"];
[archiver finishEncoding];
return data;
}
// Initializing an NSKeyedArchiver object
/*!
Initializes the keyed archiver with the specified \c CPMutableData for writing.
@param data the object to archive to
@return the initialized keyed archiver
*/
- (id)initForWritingWithMutableData:(CPMutableData)data
{
self = [super init];
if (self)
{
_data = data;
_objects = [];
_UIDs = [CPDictionary dictionary];
_conditionalUIDs = [CPDictionary dictionary];
_replacementObjects = [CPDictionary dictionary];
_data = data;
_plistObject = [CPDictionary dictionary];
_plistObjects = [CPArray arrayWithObject:_CPKeyedArchiverNullString];
}
return self;
}
// Archiving Data
/*!
Finishes up writing any left over data, and notifies delegates.
After calling this method, the archiver can not encode anymore objects.
*/
- (void)finishEncoding
{
if (_delegate && _delegateSelectors & _CPKeyedArchiverWillFinishSelector)
[_delegate archiverWillFinish:self];
var i = 0,
topObject = _plistObject,
classes = [];
for (; i < _objects.length; ++i)
{
var object = _objects[i],
theClass = [object classForKeyedArchiver];
// Do whatever with the class, yo.
// We call willEncodeObject previously.
_plistObject = _plistObjects[[_UIDs objectForKey:[object UID]]];
[object encodeWithCoder:self];
if (_delegate && _delegateSelectors & _CPKeyedArchiverDidEncodeObjectSelector)
[_delegate archiver:self didEncodeObject:object];
}
_plistObject = [CPDictionary dictionary];
[_plistObject setObject:topObject forKey:_CPKeyedArchiverTopKey];
[_plistObject setObject:_plistObjects forKey:_CPKeyedArchiverObjectsKey];
[_plistObject setObject:[self className] forKey:_CPKeyedArchiverArchiverKey];
[_plistObject setObject:@"100000" forKey:_CPKeyedArchiverVersionKey];
[_data setPlistObject:_plistObject];
if (_delegate && _delegateSelectors & _CPKeyedArchiverDidFinishSelector)
[_delegate archiverDidFinish:self];
}
/*!
Returns the property list format used to archive objects.
*/
- (CPPropertyListFormat)outputFormat
{
return _outputFormat;
}
/*!
Sets the property list format the archiver should use to archive objects.
@param aPropertyListFormat the format to use
*/
- (void)setOutputFormat:(CPPropertyListFormat)aPropertyListFormat
{
_outputFormat = aPropertyListFormat;
}
/*!
Encodes a \c BOOL value
@param aBool the \c BOOL value
@param aKey the key to associate with the \c BOOL
*/
- (void)encodeBool:(BOOL)aBOOL forKey:(CPString)aKey
{
[_plistObject setObject:_CPKeyedArchiverEncodeObject(self, aBOOL, NO) forKey:aKey];
}
/*!
Encodes a \c double value
@param aDouble the \c double value
@param aKey the key to associate with the \c double
*/
- (void)encodeDouble:(double)aDouble forKey:(CPString)aKey
{
[_plistObject setObject:_CPKeyedArchiverEncodeObject(self, aDouble, NO) forKey:aKey];
}
/*!
Encodes a \c float value
@param aFloat the \c float value
@param aKey the key to associate with the \c float
*/
- (void)encodeFloat:(float)aFloat forKey:(CPString)aKey
{
[_plistObject setObject:_CPKeyedArchiverEncodeObject(self, aFloat, NO) forKey:aKey];
}
/*!
Encodes a \c int value
@param anInt the \c int value
@param aKey the key to associate with the \c int
*/
- (void)encodeInt:(float)anInt forKey:(CPString)aKey
{
[_plistObject setObject:_CPKeyedArchiverEncodeObject(self, anInt, NO) forKey:aKey];
}
// Managing Delegates
/*!
Sets the keyed archiver's delegate
*/
- (void)setDelegate:(id)aDelegate
{
_delegate = aDelegate;
if ([_delegate respondsToSelector:@selector(archiver:didEncodeObject:)])
_delegateSelectors |= _CPKeyedArchiverDidEncodeObjectSelector;
if ([_delegate respondsToSelector:@selector(archiver:willEncodeObject:)])
_delegateSelectors |= _CPKeyedArchiverWillEncodeObjectSelector;
if ([_delegate respondsToSelector:@selector(archiver:willReplaceObject:withObject:)])
_delegateSelectors |= _CPKeyedArchiverWillReplaceObjectWithObjectSelector;
if ([_delegate respondsToSelector:@selector(archiver:didFinishEncoding:)])
_delegateSelectors |= _CPKeyedArchiverDidFinishEncodingSelector;
if ([_delegate respondsToSelector:@selector(archiver:willFinishEncoding:)])
_delegateSelectors |= _CPKeyedArchiverWillFinishEncodingSelector;
}
/*!
Returns the keyed archiver's delegate
*/
- (id)delegate
{
return _delegate;
}
/*!
Encodes a CGPoint
@param aPoint the point to encode
@param aKey the key to associate with the point
*/
- (void)encodePoint:(CGPoint)aPoint forKey:(CPString)aKey
{
[_plistObject setObject:_CPKeyedArchiverEncodeObject(self, CPStringFromPoint(aPoint), NO) forKey:aKey];
}
/*!
Encodes a CGRect
@param aRect the rectangle to encode
@param aKey the key to associate with the rectangle
*/
- (void)encodeRect:(CGRect)aRect forKey:(CPString)aKey
{
[_plistObject setObject:_CPKeyedArchiverEncodeObject(self, CPStringFromRect(aRect), NO) forKey:aKey];
}
/*!
Encodes a CGSize
@param aSize the size to encode
@param aKey the key to associate with the size
*/
- (void)encodeSize:(CGSize)aSize forKey:(CPString)aKey
{
[_plistObject setObject:_CPKeyedArchiverEncodeObject(self, CPStringFromSize(aSize), NO) forKey:aKey];
}
/*!
Encodes an conditionally. The method checks if the object has already been
coded into this data stream before. If so, it will only encode a reference
to that first object to save memory.
@param anObject the object to to conditionally encode
@param aKey the key to associate with the object
*/
- (void)encodeConditionalObject:(id)anObject forKey:(CPString)aKey
{
[_plistObject setObject:_CPKeyedArchiverEncodeObject(self, anObject, YES) forKey:aKey];
}
/*!
Encodes a number
@param aNumber the number to encode
@param aKey the key to associate with the object
*/
- (void)encodeNumber:(CPNumber)aNumber forKey:(CPString)aKey
{
[_plistObject setObject:_CPKeyedArchiverEncodeObject(self, aNumber, NO) forKey:aKey];
}
/*!
Encdoes an object
@param anObject the object to encode
@param aKey the key to associate with the object
*/
- (void)encodeObject:(id)anObject forKey:(CPString)aKey
{
[_plistObject setObject:_CPKeyedArchiverEncodeObject(self, anObject, NO) forKey:aKey];
}
/* @ignore */
- (void)_encodeArrayOfObjects:(CPArray)objects forKey:(CPString)aKey
{
var i = 0,
count = objects.length,
references = [CPArray arrayWithCapacity:count];
for (; i < count; ++i)
[references addObject:_CPKeyedArchiverEncodeObject(self, objects[i], NO)];
[_plistObject setObject:references forKey:aKey];
}
/* @ignore */
- (void)_encodeDictionaryOfObjects:(CPDictionary)aDictionary forKey:(CPString)aKey
{
var key,
keys = [aDictionary keyEnumerator],
references = [CPDictionary dictionary];
while (key = [keys nextObject])
[references setObject:_CPKeyedArchiverEncodeObject(self, [aDictionary objectForKey:key], NO) forKey:key];
[_plistObject setObject:references forKey:aKey];
}
// Managing classes and class names
/*!
Allows substitution of class types for encoding. Specifically classes
of type \c aClass encountered by all keyed archivers will
instead be archived as a class of type \c aClassName.
@param aClassName the substitute class name
@param aClass the class to substitute
*/
+ (void)setClassName:(CPString)aClassName forClass:(Class)aClass
{
if (!CPArchiverReplacementClassNames)
CPArchiverReplacementClassNames = [CPDictionary dictionary];
[CPArchiverReplacementClassNames setObject:aClassName forKey:CPStringFromClass(aClass)];
}
/*!
Returns the name of the substitute class used for encoding
\c aClass by all keyed archivers.
@param aClass the class to substitute
@return the name of the substitute class, or \c nil if there
is no substitute class
*/
+ (CPString)classNameForClass:(Class)aClass
{
if (!CPArchiverReplacementClassNames)
return aClass.name;
var className = [CPArchiverReplacementClassNames objectForKey:CPStringFromClass(aClassName)];
return className ? className : aClass.name;
}
/*!
Allows substitution of class types for encoding. Specifically classes
of type \c aClass encountered by this keyed archiver will
instead be archived as a class of type \c aClassName.
@param aClassName the substitute class name
@param aClass the class to substitute
*/
- (void)setClassName:(CPString)aClassName forClass:(Class)aClass
{
if (!_replacementClassNames)
_replacementClassNames = [CPDictionary dictionary];
[_replacementClassNames setObject:aClassName forKey:CPStringFromClass(aClass)];
}
/*!
Returns the name of the substitute class used for encoding \c aClass by this keyed archiver.
@param aClass the class to substitute
@return the name of the substitute class, or \c nil if there is no substitute class
*/
- (CPString)classNameForClass:(Class)aClass
{
if (!_replacementClassNames)
return aClass.name;
var className = [_replacementClassNames objectForKey:CPStringFromClass(aClassName)];
return className ? className : aClass.name;
}
@end
var _CPKeyedArchiverEncodeObject = function(self, anObject, isConditional)
{
// We wrap primitive JavaScript objects in a unique subclass of CPValue.
// This way, when we unarchive, we know to unwrap it, since
// _CPKeyedArchiverValue should not be used anywhere else.
if (anObject !== nil && !anObject.isa)
anObject = [_CPKeyedArchiverValue valueWithJSObject:anObject];
// Get the proper replacement object
var GUID = [anObject UID],
object = [self._replacementObjects objectForKey:GUID];
// If a replacement object doesn't exist, then actually ask for one.
// Explicitly compare to nil because object could be === 0.
if (object === nil)
{
object = [anObject replacementObjectForKeyedArchiver:self];
// Notify our delegate of this replacement.
if (self._delegate)
{
if (object !== anObject && self._delegateSelectors & _CPKeyedArchiverWillReplaceObjectWithObjectSelector)
[self._delegate archiver:self willReplaceObject:anObject withObject:object];
if (self._delegateSelectors & _CPKeyedArchiverWillEncodeObjectSelector)
{
anObject = [self._delegate archiver:self willEncodeObject:object];
if (anObject !== object && self._delegateSelectors & _CPKeyedArchiverWillReplaceObjectWithObjectSelector)
[self._delegate archiver:self willReplaceObject:object withObject:anObject];
object = anObject;
}
}
[self._replacementObjects setObject:object forKey:GUID];
}
// If we still don't have an object by this point, then return a
// reference to the null object.
// Explicitly compare to nil because object could be === 0.
if (object === nil)
return _CPKeyedArchiverNullReference;
// If not, then grab the object's UID
var UID = [self._UIDs objectForKey:GUID = [object UID]];
// If this object doesn't have a unique index in the object table yet,
// then it also hasn't been properly encoded. We explicitly compare
// index to nil since it could be 0, which would also evaluate to false.
if (UID === nil)
{
// If it is being conditionally encoded, then
if (isConditional)
{
// If we haven't already noted this conditional object...
if ((UID = [self._conditionalUIDs objectForKey:GUID]) === nil)
{
// Use the null object as a placeholder.
[self._conditionalUIDs setObject:UID = [self._plistObjects count] forKey:GUID];
[self._plistObjects addObject:_CPKeyedArchiverNullString];
}
}
else
{
var theClass = [object classForKeyedArchiver],
plistObject = nil;
if ((theClass === _CPKeyedArchiverStringClass) || (theClass === _CPKeyedArchiverNumberClass))// || theClass == _CPKeyedArchiverBooleanClass)
plistObject = object;
else
{
// Only actually encode the object and create a plist representation if it is not a simple type.
plistObject = [CPDictionary dictionary];
[self._objects addObject:object];
var className = [self classNameForClass:theClass];
if (!className)
className = [[self class] classNameForClass:theClass];
if (!className)
className = theClass.name;
else
theClass = CPClassFromString(className);
var classUID = [self._UIDs objectForKey:className];
if (!classUID)
{
var plistClass = [CPDictionary dictionary],
hierarchy = [];
[plistClass setObject:className forKey:_CPKeyedArchiverClassNameKey];
do
{
[hierarchy addObject:CPStringFromClass(theClass)];
} while (theClass = [theClass superclass]);
[plistClass setObject:hierarchy forKey:_CPKeyedArchiverClassesKey];
classUID = [self._plistObjects count];
[self._plistObjects addObject:plistClass];
[self._UIDs setObject:classUID forKey:className];
}
[plistObject setObject:[CPDictionary dictionaryWithObject:classUID forKey:_CPKeyedArchiverUIDKey] forKey:_CPKeyedArchiverClassKey];
}
UID = [self._conditionalUIDs objectForKey:GUID];
// If this object WAS previously encoded conditionally...
if (UID !== nil)
{
[self._UIDs setObject:UID forKey:GUID];
[self._plistObjects replaceObjectAtIndex:UID withObject:plistObject];
}
else
{
[self._UIDs setObject:UID = [self._plistObjects count] forKey:GUID];
[self._plistObjects addObject:plistObject];
}
}
}
return [CPDictionary dictionaryWithObject:UID forKey:_CPKeyedArchiverUIDKey];
}