This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
MPWXmlArchiver.m
472 lines (404 loc) · 14.9 KB
/
MPWXmlArchiver.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
/* MPWXmlArchiver.m Copyright (c) Marcel P. Weiher 1999-2006, All Rights Reserved,
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
Neither the name Marcel Weiher nor the names of contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
, created on Sat 24-Jul-1999 */
#import "MPWXmlArchiver.h"
#import "MPWXmlGeneratorStream.h"
#import "MPWXmlUnarchiver.h"
#import <objc/objc.h>
#import "mpwfoundation_imports.h"
#import <objc/runtime.h>
@implementation MPWXmlArchiver
idAccessor( target, setTarget )
idAccessor( todo, setTodo )
idAccessor( classVersionTable, setClassVersionTable )
scalarAccessor( NSMapTable* , objectTable, setObjectTable )
static void retainObject(NSMapTable *table, const void *obj)
{
[(id)obj retain];
}
static void releaseObject(NSMapTable *table, void *obj)
{
[(id)obj release];
}
static NSString* describeObject(NSMapTable *table, const void *obj)
{
return [(id)obj description];
}
static NSString* describeInteger(NSMapTable *table, const void *obj)
{
return [NSString stringWithFormat:@"%ld",(long)obj];
}
-initWithTarget:newTarget
{
static NSMapTableKeyCallBacks keyCallBacks = {
NULL,NULL,retainObject,releaseObject,describeObject
};
static NSMapTableValueCallBacks valueCallBacks = {
NULL,NULL,describeInteger,
};
self = [super init];
[self setTarget:newTarget];
[target setShouldIndent:YES];
[self setTodo:[NSMutableArray array]];
[self setObjectTable:NSCreateMapTable(keyCallBacks, valueCallBacks, 50) ];
[self setClassVersionTable:[NSMutableDictionary dictionary]];
return self;
}
+archivedDataWithRootObject:root
{
id archiver;
id data;
archiver = [[self alloc] initWithTarget:[MPWXmlGeneratorStream streamWithTarget:[MPWXMLByteStream stream]]];
// NSLog(@"did encode");
data = [[archiver resultOfEncodingRootObject:root] retain];
// NSLog(@"value is %@, %x, will release archiver",[data class],data);
[archiver release];
// NSLog(@"did release archiver");
return [data autorelease];
}
-resultOfEncodingRootObject:root
{
[self encodeRootObject:root];
return [[[self target] target] target];
}
-init
{
return [self initWithTarget:[MPWXmlGeneratorStream streamWithTarget:[MPWXMLByteStream Stdout]]];
}
-(void)dealloc
{
[target release];
NSFreeMapTable( objectTable );
[classVersionTable release];
[super dealloc];
}
-(void)writeClass:(Class)aClass
{
id className;
className = NSStringFromClass(aClass);
// NSLog(@"writeClass: %@, found version: %@",aClass,[classVersionTable objectForKey:className]);
if ( ![classVersionTable objectForKey:className] ) {
long version=[aClass version];
// NSLog(@"version = %d",version);
if ( version != 0 ) {
[target writeProcessingInstruction:@"Class" attributes:[NSString stringWithFormat:@"name=\"%@\" version=\"%ld\"",className,version]];
}
[classVersionTable setObject:[NSNumber numberWithLong:version] forKey:className];
aClass=[aClass superclass];
if ( aClass ) {
[self writeClass:aClass];
}
}
}
-(void)encodeAnObject:anObject
{
int oldIndex=ivarIndex;
id oldObject=currentObject;
id encodedObject = [anObject replacementObjectForCoder:self];
Class objectClass=[encodedObject classForArchiver];
// const char* className=class_getName( objectClass );
const char* className=class_getName( objectClass);
long objid = NSCountMapTable(objectTable);
NSMapInsert( objectTable, anObject, (void*)objid );
currentObject = encodedObject;
[self writeClass:objectClass];
[target beginStartTag:className];
[target writeCStrAttribute:"id" value:[NSString stringWithFormat:@"%lx",objid]];
[target endStartTag:className single:NO];
// [target indent];
// [target cr];
ivarIndex=0;
[currentObject encodeWithXmlCoder:self];
// [currentObject release];
// [target outdent];
[target closeTag];
ivarIndex=oldIndex;
currentObject=oldObject;
}
-(void)addReference:anObject name:(const char*)cname
{
long ref;
// const char *cname=[name cString];
if ( !(ref=(NSInteger)NSMapGet(objectTable,anObject )) ) {
// [objectTable addObject:anObject];
if ( cname ) {
[target writeStartTag:cname attributes:@"t='@'" single:NO];
[target indent];
[target cr];
}
[self encodeAnObject:anObject];
if ( cname ) {
[target outdent];
[target closeTag];
}
} else {
[target writeElementName:cname attributes:[NSString stringWithFormat:@"idref='%lx'",ref] contents:nil];
[target cr];
}
}
-(void)encodePropertyList:plist
{
}
-(void)encodeValueOfObjCType:(const char *)itemType at:(const void*)address withName:(const char*)name
{
if ( *itemType == '@' && *(id*)address!=nil ) {
[*(id*)address encodeXmlOn:self withName:name];
} else {
id content=nil;
id valueType=nil;
char charcontent[100]="";
switch ( *itemType ) {
case 'c':
case 'C':
valueType=@"valuetype='c'";
// content = [NSString stringWithFormat:@"%d",*(char*)address];
sprintf(charcontent, "%d",*(char*)address);
break;
case 'i':
valueType=@"valuetype='i'";
// content = [NSString stringWithFormat:@"%d",*(int*)address];
sprintf(charcontent, "%d",*(int*)address);
break;
case 'I':
valueType=@"valuetype='i'";
// content = [NSString stringWithFormat:@"%D",*(unsigned int*)address];
sprintf(charcontent, "%u",*(unsigned int*)address);
break;
case 'q':
content = [NSString stringWithFormat:@"%ld",*( long*)address];
break;
case 'Q':
content = [NSString stringWithFormat:@"%ld",*( long*)address];
break;
case 's':
valueType=@"valuetype='s'";
// content = [NSString stringWithFormat:@"%d",*(short*)address];
sprintf(charcontent, "%d",*(short*)address);
break;
case 'S':
valueType=@"valuetype='S'";
// content = [NSString stringWithFormat:@"%D",*(unsigned short*)address];
sprintf(charcontent, "%u",*(unsigned short*)address);
break;
case 'f':
valueType=@"valuetype='f'";
// content = [NSString stringWithFormat:@"%g",(double)*(float*)address];
sprintf(charcontent, "%g",(double)*(float*)address);
break;
case 'd':
valueType=@"valuetype='d'";
// content = [NSString stringWithFormat:@"%g",*(double*)address];
sprintf(charcontent, "%g",*(double*)address);
break;
case '@':
valueType=@"valuetype='@'";
content = @"";
break;
case ':':
content = NSStringFromSelector( *(SEL*)address );
break;
case '*':
content = [NSString stringWithCString:*(char**)address encoding:NSASCIIStringEncoding];
break;
case '#':
// NSLog(@"encode class");
content = NSStringFromClass( *(Class*)address) ;;
// NSLog(@"content: %@",content);
break;
default:
if ( !strcmp( itemType, "{?={?=ff}{?=ff}}" ) ||
!strcmp( itemType, "{_NSRect={_NSPoint=ff}{_NSSize=ff}}") ) {
content = NSStringFromRect( *(NSRect*)address );
} else if ( !strcmp( itemType, "{?=ff}}" ) ||
!strcmp( itemType, "{_NSPoint=ff}") ) {
content = NSStringFromPoint( *(NSPoint*)address );
} else if ( !strcmp( itemType, "{_NSSize=ff}") ) {
content = NSStringFromSize( *(NSSize*)address );
} else{
[NSException raise:@"UnknownType"
format:@"tried to encode unknown type-code: %s",
itemType] ;
content = @"";
}
break;
;
}
if ( content) {
[content getCString:charcontent maxLength:120 encoding:NSASCIIStringEncoding];
}
[target beginStartTag:name];
[target writeCStrAttribute:"t" value:[NSString stringWithCString:itemType encoding:NSASCIIStringEncoding]];
[target endStartTag:name single:NO];
[target appendBytes:charcontent length:strlen(charcontent)];
// [target writeElementName:name attributes:valueType contents:content];
[target writeCloseTag:name];
}
}
-(void)encodeKey:aKey ofObject:anObject
{
id object=[anObject valueForKey:aKey];
[self encodeValueOfObjCType:"@" at:&object withName:[aKey cString]];
}
-(void)encodeArrayOfObjCType:(const char *)itemType count:(unsigned)count at:(const void*)address withName:(const char*)name
{
int i;
int elemSize;
if ( *itemType == 'c' || *itemType=='C' ) {
[self encodeString:[[[NSString alloc] initWithBytes:address length:count encoding:NSUTF8StringEncoding] autorelease] name:name];
} else {
elemSize=4;
for (i=0;i<count;i++) {
[self encodeValueOfObjCType:itemType at:address withName:name];
address=((char*)address)+elemSize;
}
}
}
-(void)encodeArrayOfObjCType:(const char *)itemType count:(unsigned)count at:(const void*)address
{
[self encodeArrayOfObjCType:itemType count:count at:address withName:[[currentObject ivarNameForVarPointer:address orIndex:++ivarIndex] cStringUsingEncoding:NSASCIIStringEncoding ]] ;
}
-(void)encodeString:(NSString*)value name:(const char*)name
{
id utf8Data = [value dataUsingEncoding:NSUTF8StringEncoding];
id utf8String = [[[NSString alloc] initWithBytes:[utf8Data bytes] length:[utf8Data length] encoding:NSUTF8StringEncoding] autorelease];
[target writeElementName:name attributes:nil contents:utf8String];
}
-(void)encodeObjects
{
while ( [todo count] > 0 ) {
id anObject = [[todo lastObject] retain];
[todo removeLastObject];
[self encodeAnObject:anObject];
[anObject release];
}
}
-(void)encodeRootObject:someObject
{
[target writeStandardXmlHeader];
[todo addObject:someObject];
[self encodeObjects];
}
-(void)encodeValueOfObjCType:(const char *)itemType at:(const void*)address
{
// NSLog( @"%x - %x = %d",address,currentObject,address-(void*)currentObject);
[self encodeValueOfObjCType:itemType at:address withName:[[currentObject ivarNameForVarPointer:address orIndex:++ivarIndex] cStringUsingEncoding:NSASCIIStringEncoding]];
}
-(void)decodeValueOfObjCType:(const char *)itemType at:(void*)address
{
[NSException raise:@"DecodeOnArchiver" format:@"tried to decode from an archiver"];
}
-(void)encodeDataObject:(NSData*)theObject
{
[target writeElementName:"data" attributes:[NSString stringWithFormat:@"length=%lu",(unsigned long)[theObject length]]
contents:theObject];
}
-(NSData*)decodeDataObject
{
return nil;
}
-(NSInteger)versionForClassName:(NSString*)className
{
return 0;
}
-(void)close
{
[target close];
}
@end
@implementation NSObject(xmlArchiving)
-(void)encodeXmlOn:aCoder withName:(const char*)name
{
[aCoder addReference:self name:name];
}
-(void)encodeWithXmlCoder:(NSCoder*)aCoder
{
[(id)self encodeWithCoder:aCoder];
}
@end
@implementation NSString(xmlArchiving)
-(void)encodeXmlOn:aCoder withName:(const char*)name
{
[aCoder encodeString:self name:name];
}
@end
@implementation MPWSubData(xmlArchiving)
-(void)encodeXmlOn:aCoder withName:(const char*)name
{
[aCoder addReference:self name:name];
}
@end
@implementation MPWXmlArchiver(testing)
+_simpleTestString
{
return @"hi";
}
+_encodedRepOfSimpleTestString
{
return @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?Class name=\"NSString\" version=\"1\"?>\n<NSString id='0'>\n\t<unnamed_1 valuetype='I'>2</unnamed_1>\n\t<unnamed_2>hi</unnamed_2>\n</NSString>\n";
}
+(void)testArchiveSimpleString
{
id encodedString = [[self archivedDataWithRootObject:[self _simpleTestString]] stringValue];
NSAssert3( [encodedString isEqual:[self _encodedRepOfSimpleTestString]] ,@"encoding '%@' yielded '%@' instead of the expected '%@'",[self _simpleTestString],encodedString,[self _encodedRepOfSimpleTestString]);
}
+_archiveAndUnarchive:objectGraph
{
id data = [self archivedDataWithRootObject:objectGraph];
id unarchived;
// NSLog(@"archived: %@",[data stringValue]);
unarchived = [MPWXmlUnarchiver unarchiveObjectWithData:data];
return unarchived;
}
+(void)testSubstitutedClassArchiving
{
id pool=[[NSAutoreleasePool alloc] init];
id data = [@"The wonderful test data" asData];
id sub1=[[MPWSubData alloc] initWithData:data bytes:[data bytes]+2 length:5];
id result=nil;
[pool release];
[sub1 autorelease];
NS_DURING
result = [[self _archiveAndUnarchive:sub1] stringValue];
NS_HANDLER
NSAssert1( NO, @"en/de-coding a sub-data (with substitution) raise: %@",localException);
NS_ENDHANDLER
NSAssert2( [result isEqual:sub1] , @"en/de-coding '%@' resulted in '%@'",sub1,result);
}
+(void)testEncodingNSNumber
{
NSNumber *num=[NSNumber numberWithInt:3];
NSNumber *result;
NSLog(@"num objCType: %s",[num objCType]);
result=[self _archiveAndUnarchive:num];
IDEXPECT( result, num , @"unarchived number should equal original");
}
+testSelectors
{
return @[
// @"testArchiveSimpleString",
// @"testSubstitutedClassArchiving",
// @"testEncodingNSNumber",
];
}
@end