-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBMPropertyListFileChecker.m
More file actions
executable file
·604 lines (439 loc) · 16.8 KB
/
BMPropertyListFileChecker.m
File metadata and controls
executable file
·604 lines (439 loc) · 16.8 KB
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
/*
Copyright (c) 2004-2010, Stephane Sudre
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 of the WhiteBox nor the names of its 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.
*/
#import "BMPropertyListFileChecker.h"
#import "BMReportingConstants.h"
#import "BMReportingUtilities.h"
#include "CFPropertyList_Debug.h"
NSString * const BM_PROPERTYLIST_KEY_TYPE=@"Type";
NSString * const BM_PROPERTYLIST_KEY_FORMAT=@"Format";
NSString * const BM_PROPERTYLIST_KEY_DEPRECATED=@"Deprecated";
NSString * const BM_PROPERTYLIST_KEY_DEPRECATED_OS_VERSION=@"DeprecatedByMacOSXVersion";
NSString * const BM_PROPERTYLIST_KEY_DEPRECATED_ALTERNATIVES=@"DeprecatedAlternatives";
NSString * const BM_PROPERTYLIST_KEY_PRIVATE=@"Private";
NSString * const BM_PROPERTYLIST_KEY_SHOULD_CONFORM_TO_FORMAT=@"ShouldConformToFormat";
NSString * const BM_PROPERTYLIST_KEY_CAN_NOT_BE_EMPTY=@"CanNotBeEmpty";
NSString * const BM_PROPERTYLIST_KEY_CHILDREN_VIRTUAL_KEY=@"ChildrenVirtualKey";
NSString * const BM_PROPERTYLIST_KEY_AUTHORIZED_CHILDREN_TYPES=@"AuthorizedChildrenTypes";
NSString * const BM_PROPERTYLIST_KEY_AUTHORIZED_CHILDREN_KEYS=@"AuthorizedChildrenKeys";
NSString * const BM_PROPERTYLIST_KEY_REQUIRED_CHILDREN_KEYS=@"RequiredChildrenKeys";
NSString * const BM_PROPERTYLIST_KEY_AUTHORIZED_VALUES=@"AuthorizedValues";
@implementation BMPropertyListFileChecker
- (id) initWithCheckListAtPath:(NSString *) inPath
{
self=[super init];
if (self!=nil)
{
checkListDictionary_=[NSDictionary dictionaryWithContentsOfFile:inPath];
if (checkListDictionary_!=nil)
{
[checkListDictionary_ retain];
}
else
{
// A COMPLETER
}
}
return self;
}
- (void) dealloc
{
[checkedFilePath_ release];
[checkListDictionary_ release];
[super dealloc];
}
#pragma mark -
- (void) setProblemLevel:(NSUInteger) inLevel
{
if (inLevel>problemLevel_)
{
problemLevel_=inLevel;
}
}
- (NSUInteger) problemLevel
{
return problemLevel_;
}
#pragma mark -
- (void) reportProblemLevel:(NSUInteger) inLevel title:(NSString *) inTitle description:(NSString *) inDescription
{
[BMReportingUtilities reportProblemTo:delegate
file:checkedFilePath_
level:inLevel
title:inTitle
description:inDescription
tags:[NSArray arrayWithObject:BM_PROBLEM_TAG_GENERIC_PROPERTYLIST]
extras:nil];
}
- (void) checkObject:(id) inObject forKey:(NSString *) inKey
{
[self checkObject:inObject forKey:inKey supportDeviceSpecificKey:NO];
}
- (void) checkObject:(id) inObject forKey:(NSString *) inKey supportDeviceSpecificKey:(BOOL) inSupportDeviceSpecificKey
{
NSDictionary * tKeyCheckList;
NSString * tTitle;
NSString * tDescription;
tKeyCheckList=[checkListDictionary_ objectForKey:inKey];
if (tKeyCheckList==nil && inSupportDeviceSpecificKey==YES)
{
if ([inKey hasSuffix:@"˜iphone"]==YES)
{
inKey=[inKey substringToIndex:[inKey length]-7];
}
else if ([inKey hasSuffix:@"˜ipod"]==YES)
{
inKey=[inKey substringToIndex:[inKey length]-5];
}
else if ([inKey hasSuffix:@"˜ipad"]==YES)
{
inKey=[inKey substringToIndex:[inKey length]-5];
}
}
if (tKeyCheckList!=nil)
{
NSString * tTypeString;
NSArray * tAuthorizedValues;
NSString * tRealKey;
NSNumber * tDeprecatedNumber;
tRealKey=[[inKey componentsSeparatedByString:@"."] lastObject];
if ([tRealKey isEqualToString:@"__ROOT__"]==YES)
{
tRealKey=@"property list";
}
tDeprecatedNumber=[tKeyCheckList objectForKey:BM_PROPERTYLIST_KEY_DEPRECATED];
if ([tDeprecatedNumber boolValue]==YES)
{
NSArray * tArray;
NSString * tOSVersion;
NSArray * tAlternativesArray;
NSMutableString * tAlternativeSuggestions=nil;
NSUInteger tCount;
[self setProblemLevel:BM_PROBLEM_LEVEL_WARNING];
tAlternativesArray=[tKeyCheckList objectForKey:BM_PROPERTYLIST_KEY_DEPRECATED_ALTERNATIVES];
tCount=[tAlternativesArray count];
if (tCount>0)
{
NSUInteger i;
tAlternativeSuggestions=[[[tAlternativesArray objectAtIndex:0] mutableCopy] autorelease];
i=1;
while (i<(tCount-1))
{
NSString * tOtherAlternative;
tOtherAlternative=[tAlternativesArray objectAtIndex:i];
[tAlternativeSuggestions appendFormat:NSLocalizedStringFromTable(@", %@",@"CommonPropertyList",@""),tOtherAlternative];
}
if (i>1)
{
[tAlternativeSuggestions appendFormat:NSLocalizedStringFromTable(@" and %@",@"CommonPropertyList",@""),[tAlternativesArray lastObject]];
}
}
tOSVersion=[tKeyCheckList objectForKey:BM_PROPERTYLIST_KEY_DEPRECATED_OS_VERSION];
if ([tOSVersion length]==0)
{
tTitle=[NSString stringWithFormat:NSLocalizedStringFromTable(@"%@ is deprecated.",@"CommonPropertyList",@""),tRealKey];
}
else
{
tTitle=[NSString stringWithFormat:NSLocalizedStringFromTable(@"%@ is deprecated in Mac OS X v%@.",@"CommonPropertyList",@""),tRealKey,tOSVersion];
}
if (tAlternativeSuggestions!=nil)
{
tTitle=[tTitle stringByAppendingFormat:NSLocalizedStringFromTable(@" Use %@ instead.",@"CommonPropertyList",@""),tAlternativeSuggestions];
}
tArray=[NSArray arrayWithObjects:BM_PROBLEM_TAG_GENERIC_DEPRECATED,BM_PROBLEM_TAG_GENERIC_PROPERTYLIST,nil];
BM_REPORT_WARNING_TAGS(delegate,checkedFilePath_,tTitle,nil,tArray);
}
tDeprecatedNumber=[tKeyCheckList objectForKey:BM_PROPERTYLIST_KEY_PRIVATE];
if ([tDeprecatedNumber boolValue]==YES)
{
NSArray * tArray;
[self setProblemLevel:BM_PROBLEM_LEVEL_ERROR];
tTitle=[NSString stringWithFormat:NSLocalizedStringFromTable(@"Private key \"%@\" used",@"CommonPropertyList",@""),tRealKey];
tArray=[NSArray arrayWithObjects:BM_PROBLEM_TAG_GENERIC_DEPRECATED,BM_PROBLEM_TAG_GENERIC_PROPERTYLIST,nil];
BM_REPORT_ERROR_TAGS(delegate,checkedFilePath_,tTitle,nil,tArray);
}
// Check Type
tTypeString=[tKeyCheckList objectForKey:BM_PROPERTYLIST_KEY_TYPE];
if (tTypeString!=nil)
{
NSNumber * tFormatNumber;
NSNumber * tCanNotBeEmptyNumber;
if ([inObject isKindOfClass:NSClassFromString(tTypeString)]==NO)
{
[self setProblemLevel:BM_PROBLEM_LEVEL_ERROR];
tTitle=[NSString stringWithFormat:NSLocalizedStringFromTable(@"Invalid object type for key \"%@\"",@"CommonPropertyList",@""),tRealKey];
tDescription=[NSString stringWithFormat:NSLocalizedStringFromTable(@"Object is a %@ but should be a %@",@"CommonPropertyList",@""),NSStringFromClass([inObject class]),tTypeString];
[self reportProblemLevel:BM_PROBLEM_LEVEL_ERROR
title:tTitle
description:tDescription];
return;
}
// Can not be empty
tCanNotBeEmptyNumber=[tKeyCheckList objectForKey:BM_PROPERTYLIST_KEY_CAN_NOT_BE_EMPTY];
if ([tCanNotBeEmptyNumber boolValue]==YES)
{
if ([inObject length]==0)
{
tTitle=[NSString stringWithFormat:NSLocalizedStringFromTable(@"Empty object for key \"%@\"",@"CommonPropertyList",@""),tRealKey];
[self reportProblemLevel:BM_PROBLEM_LEVEL_ERROR
title:tTitle
description:@""];
}
}
// Format
tFormatNumber=[tKeyCheckList objectForKey:BM_PROPERTYLIST_KEY_FORMAT];
if (tFormatNumber!=nil)
{
NSString * tClassName;
Class tClass;
tClassName=[NSString stringWithFormat:@"BM%@FormatUtilities",tTypeString];
tClass=NSClassFromString(tClassName);
if (tClass!=nil)
{
if ([tClass object:inObject conformsToFormat:[tFormatNumber unsignedLongValue]]==NO) // A FAIRE (PROTOCOL A DEFINIR)
{
NSNumber * tNumber;
NSUInteger tProblemLevel;
tNumber=[tKeyCheckList objectForKey:BM_PROPERTYLIST_KEY_SHOULD_CONFORM_TO_FORMAT];
tProblemLevel=([tNumber boolValue]==YES) ? BM_PROBLEM_LEVEL_ERROR : BM_PROBLEM_LEVEL_WARNING;
[self setProblemLevel:tProblemLevel];
tTitle=[NSString stringWithFormat:NSLocalizedStringFromTable(@"Non conforming object value for key \"%@\"",@"CommonPropertyList",@""),tRealKey];
tDescription=@""; // A COMPLETER
[self reportProblemLevel:tProblemLevel
title:tTitle
description:tDescription];
if (tProblemLevel==BM_PROBLEM_LEVEL_ERROR)
{
return;
}
}
}
}
}
// Authorized Values
tAuthorizedValues=[tKeyCheckList objectForKey:BM_PROPERTYLIST_KEY_AUTHORIZED_VALUES];
if (tAuthorizedValues!=nil)
{
if ([tAuthorizedValues containsObject:inObject]==NO)
{
[self setProblemLevel:BM_PROBLEM_LEVEL_ERROR];
tTitle=[NSString stringWithFormat:NSLocalizedStringFromTable(@"Unauthorized object value for key \"%@\"",@"CommonPropertyList",@""),tRealKey];
tDescription=@""; // A COMPLETER
[self reportProblemLevel:BM_PROBLEM_LEVEL_ERROR
title:tTitle
description:tDescription];
return;
}
}
if ([inKey isEqualToString:@"__ROOT__"]==YES)
{
inKey=@"";
}
// Children
if ([inObject isKindOfClass:[NSDictionary class]]==YES)
{
NSArray * tOnlyAuthorizedKeysArray;
NSMutableArray * tRequiredKeysArray;
NSUInteger tCount;
NSEnumerator * tKeyEnumerator;
tOnlyAuthorizedKeysArray=[tKeyCheckList objectForKey:BM_PROPERTYLIST_KEY_AUTHORIZED_CHILDREN_KEYS];
tCount=[tOnlyAuthorizedKeysArray count];
if (tCount>0)
{
NSMutableArray * tAllKeysMutable;
tAllKeysMutable=[[[inObject allKeys] mutableCopy] autorelease];
[tAllKeysMutable removeObjectsInArray:tOnlyAuthorizedKeysArray];
tCount=[tAllKeysMutable count];
if (tCount>0)
{
NSUInteger i;
[self setProblemLevel:BM_PROBLEM_LEVEL_ERROR];
for(i=0;i<tCount;i++)
{
tTitle=[NSString stringWithFormat:NSLocalizedStringFromTable(@"Unauthorized key \"%@\" used for the \"%@\" dictionary",@"CommonPropertyList",@""),[tAllKeysMutable objectAtIndex:i],tRealKey];
tDescription=@""; // A COMPLETER
[self reportProblemLevel:BM_PROBLEM_LEVEL_ERROR
title:tTitle
description:tDescription];
}
return;
}
}
tRequiredKeysArray=[[[tKeyCheckList objectForKey:BM_PROPERTYLIST_KEY_REQUIRED_CHILDREN_KEYS] mutableCopy] autorelease];
tCount=[tRequiredKeysArray count];
if (tCount>0)
{
[tRequiredKeysArray removeObjectsInArray:[inObject allKeys]];
tCount=[tRequiredKeysArray count];
if (tCount>0)
{
NSUInteger i;
[self setProblemLevel:BM_PROBLEM_LEVEL_ERROR];
for(i=0;i<tCount;i++)
{
tTitle=[NSString stringWithFormat:NSLocalizedStringFromTable(@"Missing required \"%@\" for the \"%@\" dictionary",@"CommonPropertyList",@""),[tRequiredKeysArray objectAtIndex:i],tRealKey];
tDescription=@""; // A COMPLETER
[self reportProblemLevel:BM_PROBLEM_LEVEL_ERROR
title:tTitle
description:tDescription];
}
return;
}
}
tKeyEnumerator=[inObject keyEnumerator];
if (tKeyEnumerator!=nil)
{
NSString * tKey;
while (tKey=[tKeyEnumerator nextObject])
{
[self checkObject:[inObject objectForKey:tKey] forKey:[NSString stringWithFormat:@"%@.%@",inKey,tKey] supportDeviceSpecificKey:inSupportDeviceSpecificKey];
if ([self problemLevel]==BM_PROBLEM_LEVEL_ERROR)
{
return;
}
}
}
}
else if ([inObject isKindOfClass:[NSArray class]]==YES)
{
NSString * tVirtualName;
NSArray * tTypesArray;
NSUInteger tCount;
tTypesArray=[tKeyCheckList objectForKey:BM_PROPERTYLIST_KEY_AUTHORIZED_CHILDREN_TYPES];
tCount=[tTypesArray count];
if (tCount>0)
{
NSEnumerator * tObjectEnumerator;
tObjectEnumerator=[inObject objectEnumerator];
if (tObjectEnumerator!=nil)
{
id tObject;
NSUInteger j;
while (tObject=[tObjectEnumerator nextObject])
{
for(j=0;j<tCount;j++)
{
if ([tObject isKindOfClass:NSClassFromString([tTypesArray objectAtIndex:j])]==YES)
{
break;
}
}
if (j==tCount)
{
tTitle=[NSString stringWithFormat:NSLocalizedStringFromTable(@"Incorrect object type(s) for the \"%@\" array",@"CommonPropertyList",@""),tRealKey];
tDescription=@""; // A COMPLETER
[self reportProblemLevel:BM_PROBLEM_LEVEL_ERROR
title:tTitle
description:tDescription];
if ([self problemLevel]==BM_PROBLEM_LEVEL_ERROR)
{
return;
}
}
}
}
}
// Virtual Names
tVirtualName=[tKeyCheckList objectForKey:BM_PROPERTYLIST_KEY_CHILDREN_VIRTUAL_KEY];
if (tVirtualName!=nil)
{
NSUInteger i;
tCount=[inObject count];
for(i=0;i<tCount;i++)
{
[self checkObject:[inObject objectAtIndex:i] forKey:[NSString stringWithFormat:@"%@.%@",inKey,tVirtualName] supportDeviceSpecificKey:inSupportDeviceSpecificKey];
if ([self problemLevel]==BM_PROBLEM_LEVEL_ERROR)
{
return;
}
}
}
}
}
/*else
{
if (inRoot==YES)
{
}
}*/
}
- (BOOL) checkPropertyListFileAtPath:(NSString *) inPath withDelegate:(id) inDelegate
{
return [self checkPropertyListFileAtPath:inPath withDelegate:inDelegate supportDeviceSpecificKey:NO];
}
- (BOOL) checkPropertyListFileAtPath:(NSString *) inPath withDelegate:(id) inDelegate supportDeviceSpecificKey:(BOOL) inSupportDeviceSpecificDevice
{
NSData * tData;
delegate=inDelegate;
problemLevel_=0;
[checkedFilePath_ release];
checkedFilePath_=[inPath retain];
tData=[NSData dataWithContentsOfFile:inPath];
if (tData!=nil)
{
id tRootObject;
NSPropertyListFormat tFormat;
NSString * tErrorString;
tRootObject=[NSPropertyListSerialization propertyListFromData:tData
mutabilityOption:NSPropertyListImmutable
format:&tFormat
errorDescription:&tErrorString];
if (tRootObject!=nil)
{
[self checkObject:tRootObject forKey:@"__ROOT__" supportDeviceSpecificKey:inSupportDeviceSpecificDevice];
return ([self problemLevel]==0);
}
else
{
UInt32 tLineNumber;
[tErrorString release];
[self setProblemLevel:BM_PROBLEM_LEVEL_ERROR];
FindParsingErrorWithData((CFDataRef) tData,(CFStringRef *) &tErrorString,&tLineNumber);
if (tErrorString!=nil)
{
NSDictionary * tExtraDictionary;
NSString * tLocalizedErrorString;
tLocalizedErrorString=NSLocalizedStringFromTable(tErrorString,@"CommonPropertyList",@"");
if (tLineNumber==-1)
{
tExtraDictionary=nil;
}
else
{
tLocalizedErrorString=[NSString stringWithFormat:tLocalizedErrorString,tLineNumber];
tExtraDictionary=[NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedInt:tLineNumber] forKey:BM_PROBLEM_EXTRA_LINE_NUMBER];
}
[BMReportingUtilities reportProblemTo:delegate
file:checkedFilePath_
level:BM_PROBLEM_LEVEL_ERROR
title:tLocalizedErrorString
description:nil
tags:[NSArray arrayWithObject:BM_PROBLEM_TAG_GENERIC_PROPERTYLIST]
extras:tExtraDictionary];
[tErrorString release];
}
else
{
[self reportProblemLevel:BM_PROBLEM_LEVEL_ERROR
title:NSLocalizedStringFromTable(@"Does not know how to read .plist file",@"CommonPropertyList",@"")
description:@""];
}
}
}
else
{
[self setProblemLevel:BM_PROBLEM_LEVEL_ERROR];
[self reportProblemLevel:BM_PROBLEM_LEVEL_ERROR
title:NSLocalizedStringFromTable(@"Can not read file",@"Common",@"")
description:NSLocalizedStringFromTable(@"",@"Common",@"")];
}
return NO;
}
@end