Oolite
Loading...
Searching...
No Matches
OOPListSchemaVerifier.m
Go to the documentation of this file.
1/*
2
3OOPListSchemaVerifier.m
4
5
6Copyright (C) 2007-2013 Jens Ayton
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED ìAS ISî, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
29
30#if OO_OXP_VERIFIER_ENABLED
31
33#import "OOLoggingExtended.h"
34#import "OOMaths.h"
35#include <limits.h>
36
37#define PLIST_VERIFIER_DEBUG_DUMP_ENABLED 1
38
39enum {
40 // Largest allowable number of characters for string included in error message.
42};
43
44// Internal error codes.
45enum {
47
49};
50
51#if PLIST_VERIFIER_DEBUG_DUMP_ENABLED
52static BOOL sDebugDump = NO;
53
54#define DebugDumpIndent() \
55 do { \
56 if (sDebugDump) \
57 OOLogIndent(); \
58 } while (0)
59#define DebugDumpOutdent() \
60 do { \
61 if (sDebugDump) \
62 OOLogOutdent(); \
63 } while (0)
64#define DebugDumpPushIndent() \
65 do { \
66 if (sDebugDump) \
67 OOLogPushIndent(); \
68 } while (0)
69#define DebugDumpPopIndent() \
70 do { \
71 if (sDebugDump) \
72 OOLogPopIndent(); \
73 } while (0)
74#define DebugDump(...) \
75 do { \
76 if (sDebugDump) \
77 OOLog(@"verifyOXP.verbose.plistDebugDump", __VA_ARGS__); \
78 } while (0)
79#else
80#define DebugDumpIndent() \
81 do { \
82 } while (0)
83#define DebugDumpOutdent() \
84 do { \
85 } while (0)
86#define DebugDumpPushIndent() \
87 do { \
88 } while (0)
89#define DebugDumpPopIndent() \
90 do { \
91 } while (0)
92#define DebugDump(...) \
93 do { \
94 } while (0)
95#endif
96
97NSString* const kOOPListSchemaVerifierErrorDomain = @"org.aegidian.oolite.OOPListSchemaVerifier.ErrorDomain";
98
99NSString* const kPListKeyPathErrorKey = @"org.aegidian.oolite.OOPListSchemaVerifier plist key path";
100NSString* const kSchemaKeyPathErrorKey = @"org.aegidian.oolite.OOPListSchemaVerifier schema key path";
101
102NSString* const kExpectedClassErrorKey = @"org.aegidian.oolite.OOPListSchemaVerifier expected class";
103NSString* const kExpectedClassNameErrorKey = @"org.aegidian.oolite.OOPListSchemaVerifier expected class name";
104NSString* const kUnknownKeyErrorKey = @"org.aegidian.oolite.OOPListSchemaVerifier unknown key";
105NSString* const kMissingRequiredKeysErrorKey = @"org.aegidian.oolite.OOPListSchemaVerifier missing required keys";
106NSString* const kMissingSubStringErrorKey = @"org.aegidian.oolite.OOPListSchemaVerifier missing substring";
107NSString* const kUnnownFilterErrorKey = @"org.aegidian.oolite.OOPListSchemaVerifier unknown filter";
108NSString* const kErrorsByOptionErrorKey = @"org.aegidian.oolite.OOPListSchemaVerifier errors by option";
109
110NSString* const kUnknownTypeErrorKey = @"org.aegidian.oolite.OOPListSchemaVerifier unknown type";
111NSString* const kUndefinedMacroErrorKey = @"org.aegidian.oolite.OOPListSchemaVerifier undefined macro";
112
130
136
138{
139 BackLinkChain result = { link, element };
140 return result;
141}
142
144{
145 BackLinkChain result = { link, [NSNumber numberWithInteger:index] };
146 return result;
147}
148
150{
151 BackLinkChain result = { NULL, NULL };
152 return result;
153}
154
155static SchemaType StringToSchemaType(NSString* string, NSError** outError);
156static NSString* ApplyStringFilter(NSString* string, id filterSpec, BackLinkChain keyPath, NSError** outError);
157static BOOL ApplyStringTest(NSString* string, id test, SEL testSelector, NSString* testDescription, BackLinkChain keyPath, NSError** outError);
158static NSArray* KeyPathToArray(BackLinkChain keyPath);
159static NSString* KeyPathToString(BackLinkChain keyPath);
160static NSString* StringForErrorReport(NSString* string);
161static NSString* ArrayForErrorReport(NSArray* array);
162static NSString* SetForErrorReport(NSSet* set);
163static NSString* StringOrArrayForErrorReport(id value, NSString* arrayPrefix);
164
165static NSError* Error(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain* keyPath, NSString* format, ...);
166static NSError* ErrorWithProperty(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain* keyPath, NSString* propKey, id propValue, NSString* format, ...);
167static NSError* ErrorWithDictionary(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain* keyPath, NSDictionary* dict, NSString* format, ...);
168static NSError* ErrorWithDictionaryAndArguments(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain* keyPath, NSDictionary* dict, NSString* format, va_list arguments);
169
170static NSError* ErrorTypeMismatch(Class expectedClass, NSString* expectedClassName, id actualObject, BackLinkChain keyPath);
171static NSError* ErrorFailureAlreadyReported(void);
172static BOOL IsFailureAlreadyReportedError(NSError* error);
173
174@interface OOPListSchemaVerifier (OOPrivate)
175
176// Call delegate methods.
177- (BOOL)delegateVerifierWithPropertyList:(id)rootPList
178 named:(NSString*)name
179 testProperty:(id)subPList
180 atPath:(BackLinkChain)keyPath
181 againstType:(NSString*)typeKey
182 error:(NSError**)outError;
183
184- (BOOL)delegateVerifierWithPropertyList:(id)rootPList
185 named:(NSString*)name
186 failedForProperty:(id)subPList
187 withError:(NSError*)error
188 expectedType:(NSDictionary*)localSchema;
189
190- (BOOL)verifyPList:(id)rootPList
191 named:(NSString*)name
192 subProperty:(id)subProperty
193 againstSchemaType:(id)subSchema
194 atPath:(BackLinkChain)keyPath
195 tentative:(BOOL)tentative
196 error:(NSError**)outError
197 stop:(BOOL*)outStop;
198
199- (NSDictionary*)resolveSchemaType:(id)specifier
200 atPath:(BackLinkChain)keyPath
201 error:(NSError**)outError;
202
203@end
204
205@interface NSString (OOPListSchemaVerifierHelpers)
206
207- (BOOL)ooPListVerifierHasSubString:(NSString*)string;
208
209@end
210
211#define VERIFY_PROTO(T) static NSError* Verify_##T(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
214VERIFY_PROTO(Dictionary);
216VERIFY_PROTO(PositiveInteger);
218VERIFY_PROTO(PositiveFloat);
220VERIFY_PROTO(Enumeration);
222VERIFY_PROTO(FuzzyBoolean);
225VERIFY_PROTO(DelegatedType);
226
227@implementation OOPListSchemaVerifier
228
229+ (id)verifierWithSchema:(NSDictionary*)schema
230{
231 return [[[self alloc] initWithSchema:schema] autorelease];
232}
233
234- (id)initWithSchema:(NSDictionary*)schema
235{
236 self = [super init];
237 if (self != nil) {
238 _schema = [schema retain];
239 _definitions = [[_schema oo_dictionaryForKey:@"$w2"] retain];
240 sDebugDump = [[NSUserDefaults standardUserDefaults] boolForKey:@"plist-schema-verifier-dump-structure"];
241 if (sDebugDump)
242 OOLogSetDisplayMessagesInClass(@"verifyOXP.verbose.plistDebugDump", YES);
243
244 if (_schema == nil) {
245 [self release];
246 self = nil;
247 }
248 }
249
250 return self;
251}
252
253- (void)dealloc
254{
255 [_schema release];
256 [_definitions release];
257
258 [super dealloc];
259}
260
261- (void)setDelegate:(id)delegate
262{
263 if (_delegate != delegate) {
266 }
267}
268
270{
271 return _delegate;
272}
273
274- (BOOL)verifyPropertyList:(id)plist named:(NSString*)name
275{
276 BOOL OK;
277 BOOL stop = NO;
278
279 OK = [self verifyPList:plist
280 named:name
281 subProperty:plist
282 againstSchemaType:_schema
283 atPath:BackLinkRoot()
284 tentative:NO
285 error:NULL
286 stop:&stop];
287
288 return OK;
289}
290
291+ (NSString*)descriptionForKeyPath:(NSArray*)keyPath
292{
293 NSMutableString* result = nil;
294 id component = nil;
295 BOOL first = YES;
296
297 result = [NSMutableString string];
298
299 foreach (component, keyPath) {
300 if ([component isKindOfClass:[NSNumber class]]) {
301 [result appendFormat:@"[%@]", component];
302 } else if ([component isKindOfClass:[NSString class]]) {
303 if (!first)
304 [result appendString:@"."];
305 [result appendString:component];
306 } else
307 return nil;
308 first = NO;
309 }
310
311 if (first) {
312 // Empty path
313 return @"root";
314 }
315
316 return result;
317}
318
319@end
320
321@implementation OOPListSchemaVerifier (OOPrivate)
322
323- (BOOL)delegateVerifierWithPropertyList:(id)rootPList
324 named:(NSString*)name
325 testProperty:(id)subPList
326 atPath:(BackLinkChain)keyPath
327 againstType:(NSString*)typeKey
328 error:(NSError**)outError
329{
330 BOOL result;
331 NSError* error = nil;
332
333 if ([_delegate respondsToSelector:@selector(verifier:withPropertyList:named:testProperty:atPath:againstType:error:)]) {
334 @try {
335 result = [_delegate verifier:self
336 withPropertyList:rootPList
337 named:name
338 testProperty:subPList
339 atPath:KeyPathToArray(keyPath)
340 againstType:typeKey
341 error:&error];
342 }
343 @catch (NSException* exception) {
344 OOLog(@"plistVerifier.delegateException", @"Property list schema verifier: delegate threw exception (%@) in -verifier:withPropertyList:named:testProperty:atPath:againstType: for type \"%@\" at %@ in %@ -- treating as failure.", [exception name], typeKey, KeyPathToString(keyPath), name);
345 result = NO;
346 error = nil;
347 }
348
349 if (outError != NULL) {
350 if (!result || error != nil) {
351 // Note: Generates an error if delegate returned NO (meaning stop) or if delegate produced an error but did not request a stop.
352 *outError = ErrorWithProperty(kPListDelegatedTypeError, &keyPath, NSUnderlyingErrorKey, error, @"Value at %@ does not match delegated type \"%@\".", KeyPathToString(keyPath), typeKey);
353 } else
354 *outError = nil;
355 }
356 } else {
357 if (!_badDelegateWarning) {
358 OOLog(@"plistVerifier.badDelegate", @"%@", @"Property list schema verifier: delegate does not handle delegated types.");
359 _badDelegateWarning = YES;
360 }
361 result = YES;
362 }
363
364 return result;
365}
366
367- (BOOL)delegateVerifierWithPropertyList:(id)rootPList
368 named:(NSString*)name
369 failedForProperty:(id)subPList
370 withError:(NSError*)error
371 expectedType:(NSDictionary*)localSchema
372{
373 BOOL result;
374
375 if ([_delegate respondsToSelector:@selector(verifier:withPropertyList:named:failedForProperty:withError:expectedType:)]) {
376 @try {
377 result = [_delegate verifier:self
378 withPropertyList:rootPList
379 named:name
380 failedForProperty:subPList
381 withError:error
382 expectedType:localSchema];
383 }
384 @catch (NSException* exception) {
385 OOLog(@"plistVerifier.delegateException", @"Property list schema verifier: delegate threw exception (%@) in -verifier:withPropertyList:named:failedForProperty:atPath:expectedType: at %@ in %@ -- stopping.", [exception name], [error plistKeyPathDescription], name);
386 result = NO;
387 }
388 } else {
389 OOLog(@"plistVerifier.failed", @"Verification of property list \"%@\" failed at %@: %@", name, [error plistKeyPathDescription], [error localizedFailureReason]);
390 result = NO;
391 }
392 return result;
393}
394
395- (BOOL)verifyPList:(id)rootPList
396 named:(NSString*)name
397 subProperty:(id)subProperty
398 againstSchemaType:(id)subSchema
399 atPath:(BackLinkChain)keyPath
400 tentative:(BOOL)tentative
401 error:(NSError**)outError
402 stop:(BOOL*)outStop
403{
405 NSError* error = nil;
406 NSDictionary* resolvedSpecifier = nil;
407 NSAutoreleasePool* pool = nil;
408
409 assert(outStop != NULL);
410
411 pool = [[NSAutoreleasePool alloc] init];
412
414
415 @try {
417
418 resolvedSpecifier = [self resolveSchemaType:subSchema atPath:keyPath error:&error];
419 if (resolvedSpecifier != nil)
420 type = StringToSchemaType([resolvedSpecifier objectForKey:@"type"], &error);
421
422#define VERIFY_CASE(T) \
423 case kType##T: \
424 error = Verify_##T(self, subProperty, resolvedSpecifier, rootPList, name, keyPath, tentative, outStop); \
425 break;
426
427 switch (type) {
428 VERIFY_CASE(String);
429 VERIFY_CASE(Array);
430 VERIFY_CASE(Dictionary);
431 VERIFY_CASE(Integer);
432 VERIFY_CASE(PositiveInteger);
433 VERIFY_CASE(Float);
434 VERIFY_CASE(PositiveFloat);
435 VERIFY_CASE(OneOf);
436 VERIFY_CASE(Enumeration);
437 VERIFY_CASE(Boolean);
438 VERIFY_CASE(FuzzyBoolean);
441 VERIFY_CASE(DelegatedType);
442
443 case kTypeUnknown:
444 // resolveSchemaType:... or StringToSchemaType() should have provided an error.
445 *outStop = YES;
446 }
447 }
448 @catch (NSException* exception) {
449 error = Error(kPListErrorInternal, (BackLinkChain*)&keyPath, @"Uncaught exception %@: %@ in plist verifier for \"%@\" at %@.", [exception name], [exception reason], name, KeyPathToString(keyPath));
450 }
451
453
454 if (error != nil) {
455 if (!tentative && !IsFailureAlreadyReportedError(error)) {
456 *outStop = ![self delegateVerifierWithPropertyList:rootPList
457 named:name
458 failedForProperty:subProperty
459 withError:error
460 expectedType:subSchema];
461 } else if (tentative)
462 *outStop = YES;
463 }
464
465 if (outError != NULL && error != nil) {
466 *outError = [error retain];
467 [pool release];
468 [error autorelease];
469 } else {
470 [pool release];
471 }
472
473 return error == nil;
474}
475
476- (NSDictionary*)resolveSchemaType:(id)specifier
477 atPath:(BackLinkChain)keyPath
478 error:(NSError**)outError
479{
480 id typeVal = nil;
481 NSString* complaint = nil;
482
483 assert(outError != NULL);
484
485 if (![specifier isKindOfClass:[NSString class]] && ![specifier isKindOfClass:[NSDictionary class]])
486 goto BAD_TYPE;
487
488 for (;;) {
489 if ([specifier isKindOfClass:[NSString class]])
490 specifier = [NSDictionary dictionaryWithObject:specifier forKey:@"type"];
491 typeVal = [(NSDictionary*)specifier objectForKey:@"type"];
492
493 if ([typeVal isKindOfClass:[NSString class]]) {
494 if ([typeVal hasPrefix:@"$"]) {
495 // Macro reference; look it up in $definitions
496 specifier = [_definitions objectForKey:typeVal];
497 if (specifier == nil) {
498 *outError = ErrorWithProperty(kPListErrorSchemaUndefiniedMacroReference, &keyPath, kUndefinedMacroErrorKey, typeVal, @"Bad schema: reference to undefined macro \"%@\".", StringForErrorReport(typeVal));
499 return nil;
500 }
501 } else {
502 // Non-macro string
503 return specifier;
504 }
505 } else if ([typeVal isKindOfClass:[NSDictionary class]]) {
506 specifier = typeVal;
507 } else {
508 goto BAD_TYPE;
509 }
510 }
511
512BAD_TYPE:
513 // Error: bad type
514 if (typeVal == nil)
515 complaint = @"no type specified";
516 else
517 complaint = @"not string or dictionary";
518
519 *outError = Error(kPListErrorSchemaBadTypeSpecifier, &keyPath, @"Bad schema: invalid type specifier for path %@ (%@).", KeyPathToString(keyPath), complaint);
520 return nil;
521}
522
523@end
524
525static SchemaType StringToSchemaType(NSString* string, NSError** outError)
526{
527 static NSDictionary* typeMap = nil;
528 SchemaType result;
529
530 if (typeMap == nil) {
531 typeMap =
532 [[NSDictionary dictionaryWithObjectsAndKeys:
533 [NSNumber numberWithUnsignedInt:kTypeString], @"string",
534 [NSNumber numberWithUnsignedInt:kTypeArray], @"array",
535 [NSNumber numberWithUnsignedInt:kTypeDictionary], @"dictionary",
536 [NSNumber numberWithUnsignedInt:kTypeInteger], @"integer",
537 [NSNumber numberWithUnsignedInt:kTypePositiveInteger], @"positiveInteger",
538 [NSNumber numberWithUnsignedInt:kTypeFloat], @"float",
539 [NSNumber numberWithUnsignedInt:kTypePositiveFloat], @"positiveFloat",
540 [NSNumber numberWithUnsignedInt:kTypeOneOf], @"oneOf",
541 [NSNumber numberWithUnsignedInt:kTypeEnumeration], @"enumeration",
542 [NSNumber numberWithUnsignedInt:kTypeBoolean], @"boolean",
543 [NSNumber numberWithUnsignedInt:kTypeFuzzyBoolean], @"fuzzyBoolean",
544 [NSNumber numberWithUnsignedInt:kTypeVector], @"vector",
545 [NSNumber numberWithUnsignedInt:kTypeQuaternion], @"quaternion",
546 [NSNumber numberWithUnsignedInt:kTypeDelegatedType], @"delegatedType",
547 nil] retain];
548 }
549
550 result = [[typeMap objectForKey:string] unsignedIntValue];
551 if (result == kTypeUnknown && outError != NULL) {
552 if ([string hasPrefix:@"$"]) {
553 *outError = ErrorWithProperty(kPListErrorSchemaUnknownType, NULL, kUnknownTypeErrorKey, string, @"Bad schema: unresolved macro reference \"%@\".", string);
554 } else {
555 *outError = ErrorWithProperty(kPListErrorSchemaUnknownType, NULL, kUnknownTypeErrorKey, string, @"Bad schema: unknown type \"%@\".", string);
556 }
557 }
558
559 return result;
560}
561
562static NSString* ApplyStringFilter(NSString* string, id filterSpec, BackLinkChain keyPath, NSError** outError)
563{
564 NSEnumerator* filterEnum = nil;
565 id filter = nil;
566 NSRange range;
567
568 assert(outError != NULL);
569
570 if (filterSpec == nil)
571 return string;
572
573 if ([filterSpec isKindOfClass:[NSString class]]) {
574 filterSpec = [NSArray arrayWithObject:filterSpec];
575 }
576 if ([filterSpec isKindOfClass:[NSArray class]]) {
577 for (filterEnum = [filterSpec objectEnumerator]; (filter = [filterEnum nextObject]);) {
578 if ([filter isKindOfClass:[NSString class]]) {
579 if ([filter isEqual:@"lowerCase"])
580 string = [string lowercaseString];
581 else if ([filter isEqual:@"upperCase"])
582 string = [string uppercaseString];
583 else if ([filter isEqual:@"capitalized"])
584 string = [string capitalizedString];
585 else if ([filter hasPrefix:@"truncFront:"]) {
586 string = [string substringToIndex:[[filter substringFromIndex:11] intValue]];
587 } else if ([filter hasPrefix:@"truncBack:"]) {
588 string = [string substringToIndex:[[filter substringFromIndex:10] intValue]];
589 } else if ([filter hasPrefix:@"subStringTo:"]) {
590 range = [string rangeOfString:[filter substringFromIndex:12]];
591 if (range.location != NSNotFound) {
592 string = [string substringToIndex:range.location];
593 }
594 } else if ([filter hasPrefix:@"subStringFrom:"]) {
595 range = [string rangeOfString:[filter substringFromIndex:14]];
596 if (range.location != NSNotFound) {
597 string = [string substringFromIndex:range.location + range.length];
598 }
599 } else if ([filter hasPrefix:@"subStringToInclusive:"]) {
600 range = [string rangeOfString:[filter substringFromIndex:21]];
601 if (range.location != NSNotFound) {
602 string = [string substringToIndex:range.location + range.length];
603 }
604 } else if ([filter hasPrefix:@"subStringFromInclusive:"]) {
605 range = [string rangeOfString:[filter substringFromIndex:23]];
606 if (range.location != NSNotFound) {
607 string = [string substringFromIndex:range.location];
608 }
609 } else {
610 *outError = ErrorWithProperty(kPListErrorSchemaUnknownFilter, &keyPath, kUnnownFilterErrorKey, filter, @"Bad schema: unknown string filter specifier \"%@\".", filter);
611 }
612 } else {
613 *outError = Error(kPListErrorSchemaUnknownFilter, &keyPath, @"Bad schema: filter specifier is not a string.");
614 }
615 }
616 } else {
617 *outError = Error(kPListErrorSchemaUnknownFilter, &keyPath, @"Bad schema: \"filter\" must be a string or an array.");
618 }
619
620 return string;
621}
622
623static BOOL ApplyStringTest(NSString* string, id test, SEL testSelector, NSString* testDescription, BackLinkChain keyPath, NSError** outError)
624{
625 BOOL (*testIMP)(id, SEL, NSString*);
626 NSEnumerator* testEnum = nil;
627 id subTest = nil;
628
629 assert(outError != NULL);
630
631 if (test == nil)
632 return YES;
633
634 testIMP = (BOOL (*)(id, SEL, NSString*))[string methodForSelector:testSelector];
635 if (testIMP == NULL) {
636 *outError = Error(kPListErrorInternal, &keyPath, @"OOPListSchemaVerifier internal error: NSString does not respond to test selector %@.", NSStringFromSelector(testSelector));
637 return NO;
638 }
639
640 if ([test isKindOfClass:[NSString class]]) {
641 test = [NSArray arrayWithObject:test];
642 }
643
644 if ([test isKindOfClass:[NSArray class]]) {
645 for (testEnum = [test objectEnumerator]; (subTest = [testEnum nextObject]);) {
646 if ([subTest isKindOfClass:[NSString class]]) {
647 if (testIMP(string, testSelector, subTest))
648 return YES;
649 } else {
650 *outError = Error(kPListErrorSchemaBadComparator, &keyPath, @"Bad schema: required %@ is not a string.", testDescription);
651 return NO;
652 }
653 }
654 } else {
655 *outError = Error(kPListErrorSchemaBadComparator, &keyPath, @"Bad schema: %@ requirement specification is not a string or array.", testDescription);
656 }
657 return NO;
658}
659
660static NSArray* KeyPathToArray(BackLinkChain keyPath)
661{
662 NSMutableArray* result = nil;
663 BackLinkChain* curr = NULL;
664
665 result = [NSMutableArray array];
666 for (curr = &keyPath; curr != NULL; curr = curr->link) {
667 if (curr->element != nil)
668 [result insertObject:curr->element atIndex:0];
669 }
670
671 return result;
672}
673
674static NSString* KeyPathToString(BackLinkChain keyPath)
675{
676 return [OOPListSchemaVerifier descriptionForKeyPath:KeyPathToArray(keyPath)];
677}
678
679static NSString* StringForErrorReport(NSString* string)
680{
681 id result = nil;
682
683 if (kMaximumLengthForStringInErrorMessage < [string length]) {
684 string = [string substringToIndex:kMaximumLengthForStringInErrorMessage];
685 }
686 result = [NSMutableString stringWithString:string];
687 [result replaceOccurrencesOfString:@"\t" withString:@" " options:0 range:NSMakeRange(0, [string length])];
688 [result replaceOccurrencesOfString:@"\r\n" withString:@" \\ " options:0 range:NSMakeRange(0, [string length])];
689 [result replaceOccurrencesOfString:@"\n" withString:@" \\ " options:0 range:NSMakeRange(0, [string length])];
690 [result replaceOccurrencesOfString:@"\r" withString:@" \\ " options:0 range:NSMakeRange(0, [string length])];
691
692 if (kMaximumLengthForStringInErrorMessage < [result length]) {
693 result = [result substringToIndex:kMaximumLengthForStringInErrorMessage - 3];
694 result = [result stringByAppendingString:@"..."];
695 }
696
697 return result;
698}
699
700static NSString* ArrayForErrorReport(NSArray* array)
701{
702 NSString* result = nil;
703 NSString* string = nil;
704 NSUInteger i, count;
705 NSAutoreleasePool* pool = nil;
706
707 count = [array count];
708 if (count == 0)
709 return @"( )";
710
711 pool = [[NSAutoreleasePool alloc] init];
712
713 result = [NSString stringWithFormat:@"(%@", [array objectAtIndex:0]];
714
715 for (i = 1; i != count; ++i) {
716 string = [result stringByAppendingFormat:@", %@", [array objectAtIndex:i]];
717 if (kMaximumLengthForStringInErrorMessage < [string length]) {
718 result = [result stringByAppendingString:@", ..."];
719 break;
720 }
721 result = string;
722 }
723
724 result = [result stringByAppendingString:@")"];
725
726 [result retain];
727 [pool release];
728 return [result autorelease];
729}
730
731static NSString* SetForErrorReport(NSSet* set)
732{
733 return ArrayForErrorReport([[set allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]);
734}
735
736static NSString* StringOrArrayForErrorReport(id value, NSString* arrayPrefix)
737{
738 if ([value isKindOfClass:[NSString class]]) {
739 return [NSString stringWithFormat:@"\"%@\"", StringForErrorReport(value)];
740 }
741
742 if (arrayPrefix == nil)
743 arrayPrefix = @"";
744 if ([value isKindOfClass:[NSArray class]]) {
745 return [arrayPrefix stringByAppendingString:ArrayForErrorReport(value)];
746 }
747 if ([value isKindOfClass:[NSSet class]]) {
748 return [arrayPrefix stringByAppendingString:SetForErrorReport(value)];
749 }
750 if (value == nil)
751 return @"(null)";
752 return @"<?>";
753}
754
755// Specific type verifiers
756
757#define REQUIRE_TYPE(CLASSNAME, NAMESTRING) \
758 do { \
759 if (![value isKindOfClass:[CLASSNAME class]]) { \
760 return ErrorTypeMismatch([CLASSNAME class], NAMESTRING, value, keyPath); \
761 } \
762 } while (0)
763
764static NSError* Verify_String(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
765{
766 NSString* filteredString = nil;
767 id testValue = nil;
768 NSUInteger length;
769 NSUInteger lengthConstraint;
770 NSError* error = nil;
771
772 REQUIRE_TYPE(NSString, @"string");
773
774 DebugDump(@"* string: \"%@\"", StringForErrorReport(value));
775
776 // Apply filters
777 filteredString = ApplyStringFilter(value, [params objectForKey:@"filter"], keyPath, &error);
778 if (filteredString == nil)
779 return error;
780
781 // Apply substring requirements
782 testValue = [params objectForKey:@"requiredPrefix"];
783 if (testValue != nil) {
784 if (!ApplyStringTest(filteredString, testValue, @selector(hasPrefix:), @"prefix", keyPath, &error)) {
785 if (error == nil)
786 error = ErrorWithProperty(kPListErrorStringPrefixMissing, &keyPath, kMissingSubStringErrorKey, testValue, @"String \"%@\" does not have required %@ %@.", StringForErrorReport(value), @"prefix", StringOrArrayForErrorReport(testValue, @"in "));
787 return error;
788 }
789 }
790
791 testValue = [params objectForKey:@"requiredSuffix"];
792 if (testValue != nil) {
793 if (!ApplyStringTest(filteredString, testValue, @selector(hasSuffix:), @"suffix", keyPath, &error)) {
794 if (error == nil)
795 error = ErrorWithProperty(kPListErrorStringSuffixMissing, &keyPath, kMissingSubStringErrorKey, testValue, @"String \"%@\" does not have required %@ %@.", StringForErrorReport(value), @"suffix", StringOrArrayForErrorReport(testValue, @"in "));
796 return error;
797 }
798 }
799
800 testValue = [params objectForKey:@"requiredSubString"];
801 if (testValue != nil) {
802 if (!ApplyStringTest(filteredString, testValue, @selector(ooPListVerifierHasSubString:), @"substring", keyPath, &error)) {
803 if (error == nil)
804 error = ErrorWithProperty(kPListErrorStringSubstringMissing, &keyPath, kMissingSubStringErrorKey, testValue, @"String \"%@\" does not have required %@ %@.", StringForErrorReport(value), @"substring", StringOrArrayForErrorReport(testValue, @"in "));
805 return error;
806 }
807 }
808
809 // Apply length bounds.
810 length = [filteredString length];
811 lengthConstraint = [params oo_unsignedIntegerForKey:@"minLength"];
812 if (length < lengthConstraint) {
813 return Error(kPListErrorMinimumConstraintNotMet, &keyPath, @"String \"%@\" is too short (%u bytes, minimum is %u).", StringForErrorReport(filteredString), length, lengthConstraint);
814 }
815
816 lengthConstraint = [params oo_unsignedIntegerForKey:@"maxLength" defaultValue:NSUIntegerMax];
817 if (lengthConstraint < length) {
818 return Error(kPListErrorMaximumConstraintNotMet, &keyPath, @"String \"%@\" is too long (%u bytes, maximum is %u).", StringForErrorReport(filteredString), length, lengthConstraint);
819 }
820
821 // All tests passed.
822 return nil;
823}
824
825static NSError* Verify_Array(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
826{
827 id valueType = nil;
828 BOOL OK = YES, stop = NO;
829 NSUInteger i, count;
830 id subProperty = nil;
831 NSUInteger constraint;
832
833 REQUIRE_TYPE(NSArray, @"array");
834
835 DebugDump(@"%@", @"* array");
836
837 // Apply count bounds.
838 count = [value count];
839 constraint = [params oo_unsignedIntegerForKey:@"minCount" defaultValue:0];
840 if (count < constraint) {
841 return Error(kPListErrorMinimumConstraintNotMet, &keyPath, @"Array has too few members (%u, minimum is %u).", count, constraint);
842 }
843
844 constraint = [params oo_unsignedIntegerForKey:@"maxCount" defaultValue:NSUIntegerMax];
845 if (constraint < count) {
846 return Error(kPListErrorMaximumConstraintNotMet, &keyPath, @"Array has too many members (%u, maximum is %u).", count, constraint);
847 }
848
849 // Test member objects.
850 valueType = [params objectForKey:@"valueType"];
851 if (valueType != nil) {
852 for (i = 0; i != count; ++i) {
853 subProperty = [value objectAtIndex:i];
854
855 if (![verifier verifyPList:rootPList
856 named:name
857 subProperty:subProperty
858 againstSchemaType:valueType
859 atPath:BackLinkIndex(&keyPath, i)
860 tentative:tentative
861 error:NULL
862 stop:&stop]) {
863 OK = NO;
864 }
865
866 if ((stop && !tentative) || (tentative && !OK))
867 break;
868 }
869 }
870
871 *outStop = stop && !tentative;
872
873 if (!OK)
875 else
876 return nil;
877}
878
879static NSError* Verify_Dictionary(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
880{
881 NSDictionary* schema = nil;
882 id valueType = nil,
883 typeSpec = nil;
884 NSEnumerator* keyEnum = nil;
885 NSString* key = nil;
886 id subProperty = nil;
887 BOOL OK = YES, stop = NO, prematureExit = NO;
888 BOOL allowOthers;
889 NSMutableSet* requiredKeys = nil;
890 NSArray* requiredKeyList = nil;
891 NSUInteger count, constraint;
892
893 REQUIRE_TYPE(NSDictionary, @"dictionary");
894
895 DebugDump(@"%@", @"* dictionary");
896
897 // Apply count bounds.
898 count = [value count];
899 constraint = [params oo_unsignedIntegerForKey:@"minCount" defaultValue:0];
900 if (count < constraint) {
901 return Error(kPListErrorMinimumConstraintNotMet, &keyPath, @"Dictionary has too few pairs (%u, minimum is %u).", count, constraint);
902 }
903 constraint = [params oo_unsignedIntegerForKey:@"maxCount" defaultValue:NSUIntegerMax];
904 if (constraint < count) {
905 return Error(kPListErrorMaximumConstraintNotMet, &keyPath, @"Dictionary has too manu pairs (%u, maximum is %u).", count, constraint);
906 }
907
908 // Get schema.
909 schema = [params oo_dictionaryForKey:@"schema"];
910 valueType = [params objectForKey:@"valueType"];
911 allowOthers = [params oo_boolForKey:@"allowOthers" defaultValue:YES];
912 requiredKeyList = [params oo_arrayForKey:@"requiredKeys"];
913
914 // If these conditions are met, all members must pass:
915 if (schema == nil && valueType == nil && requiredKeyList == nil && allowOthers)
916 return nil;
917
918 if (requiredKeyList != nil) {
919 requiredKeys = [NSMutableSet setWithArray:requiredKeyList];
920 }
921
923
924 // Test member objects.
925 for (keyEnum = [value keyEnumerator]; (key = [keyEnum nextObject]) && !stop;) {
926 subProperty = [(NSDictionary*)value objectForKey:key];
927 typeSpec = [schema objectForKey:key];
928 if (typeSpec == nil)
929 typeSpec = valueType;
930
931 DebugDump(@"- \"%@\"", key);
933
934 if (typeSpec != nil) {
935 if (![verifier verifyPList:rootPList
936 named:name
937 subProperty:subProperty
938 againstSchemaType:typeSpec
939 atPath:BackLink(&keyPath, key)
940 tentative:tentative
941 error:NULL
942 stop:&stop]) {
943 OK = NO;
944 }
945 } else if (!allowOthers && ![requiredKeys containsObject:key] && [schema objectForKey:key] == nil) {
946 // Report error now rather than returning it, since there may be several unknown keys.
947 if (!tentative) {
948 NSError* error = ErrorWithProperty(kPListErrorDictionaryUnknownKey, &keyPath, kUnknownKeyErrorKey, key, @"Unpermitted key \"%@\" in dictionary.", StringForErrorReport(key));
949 stop = ![verifier delegateVerifierWithPropertyList:rootPList
950 named:name
952 withError:error
953 expectedType:params];
954 }
955 OK = NO;
956 }
957
959
960 [requiredKeys removeObject:key];
961
962 if ((stop && !tentative) || (tentative && !OK)) {
963 prematureExit = YES;
964 break;
965 }
966 }
967
969
970 // Check that all required keys were present.
971 if (!prematureExit && [requiredKeys count] != 0) {
972 return ErrorWithProperty(kPListErrorDictionaryMissingRequiredKeys, &keyPath, kMissingRequiredKeysErrorKey, requiredKeys, @"Required keys %@ missing from dictionary.", SetForErrorReport(requiredKeys));
973 }
974
975 *outStop = stop && !tentative;
976
977 if (!OK)
979 else
980 return nil;
981}
982
983static NSError* Verify_Integer(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
984{
985 long long numericValue;
986 long long constraint;
987
988 numericValue = OOLongLongFromObject(value, 0);
989
990 DebugDump(@"* integer: %lli", numericValue);
991
992 // Check basic parseability. If there's inequality here, the default value is being returned.
993 if (numericValue != OOLongLongFromObject(value, 1)) {
994 return ErrorTypeMismatch([NSNumber class], @"integer", value, keyPath);
995 }
996
997 // Check constraints.
998 constraint = [params oo_longLongForKey:@"minimum" defaultValue:LLONG_MIN];
999 if (numericValue < constraint) {
1000 return Error(kPListErrorMinimumConstraintNotMet, &keyPath, @"Number is too small (%lli, minimum is %lli).", numericValue, constraint);
1001 }
1002
1003 constraint = [params oo_longLongForKey:@"maximum" defaultValue:LLONG_MAX];
1004 if (constraint < numericValue) {
1005 return Error(kPListErrorMaximumConstraintNotMet, &keyPath, @"Number is too large (%lli, maximum is %lli).", numericValue, constraint);
1006 }
1007
1008 return nil;
1009}
1010
1011static NSError* Verify_PositiveInteger(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
1012{
1013 unsigned long long numericValue;
1014 unsigned long long constraint;
1015
1016 numericValue = OOUnsignedLongLongFromObject(value, 0);
1017
1018 DebugDump(@"* positive integer: %llu", numericValue);
1019
1020 // Check basic parseability. If there's inequality here, the default value is being returned.
1021 if (numericValue != OOUnsignedLongLongFromObject(value, 1)) {
1022 return ErrorTypeMismatch([NSNumber class], @"positive integer", value, keyPath);
1023 }
1024
1025 // Check constraints.
1026 constraint = [params oo_unsignedLongLongForKey:@"minimum" defaultValue:0];
1027 if (numericValue < constraint) {
1028 return Error(kPListErrorMinimumConstraintNotMet, &keyPath, @"Number is too small (%zu, minimum is %zu).", numericValue, constraint);
1029 }
1030
1031 constraint = [params oo_unsignedLongLongForKey:@"maximum" defaultValue:ULLONG_MAX];
1032 if (constraint < numericValue) {
1033 return Error(kPListErrorMaximumConstraintNotMet, &keyPath, @"Number is too large (%zu, maximum is %zu).", numericValue, constraint);
1034 }
1035
1036 return nil;
1037}
1038
1039static NSError* Verify_Float(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
1040{
1041 double numericValue;
1042 double constraint;
1043
1044 numericValue = OODoubleFromObject(value, 0);
1045
1046 DebugDump(@"* float: %g", numericValue);
1047
1048 // Check basic parseability. If there's inequality here, the default value is being returned.
1049 if (numericValue != OODoubleFromObject(value, 1)) {
1050 return ErrorTypeMismatch([NSNumber class], @"number", value, keyPath);
1051 }
1052
1053 // Check constraints.
1054 constraint = [params oo_doubleForKey:@"minimum" defaultValue:-INFINITY];
1055 if (numericValue < constraint) {
1056 return Error(kPListErrorMinimumConstraintNotMet, &keyPath, @"Number is too small (%g, minimum is %g).", numericValue, constraint);
1057 }
1058
1059 constraint = [params oo_doubleForKey:@"maximum" defaultValue:INFINITY];
1060 if (constraint < numericValue) {
1061 return Error(kPListErrorMaximumConstraintNotMet, &keyPath, @"Number is too large (%g, maximum is %g).", numericValue, constraint);
1062 }
1063
1064 return nil;
1065}
1066
1067static NSError* Verify_PositiveFloat(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
1068{
1069 double numericValue;
1070 double constraint;
1071
1072 numericValue = OODoubleFromObject(value, 0);
1073
1074 DebugDump(@"* positive float: %g", numericValue);
1075
1076 // Check basic parseability. If there's inequality here, the default value is being returned.
1077 if (numericValue != OODoubleFromObject(value, 1)) {
1078 return ErrorTypeMismatch([NSNumber class], @"positive number", value, keyPath);
1079 }
1080
1081 if (numericValue < 0) {
1082 return Error(kPListErrorNumberIsNegative, &keyPath, @"Expected non-negative number, found %g.", numericValue);
1083 }
1084
1085 // Check constraints.
1086 constraint = [params oo_doubleForKey:@"minimum" defaultValue:0];
1087 if (numericValue < constraint) {
1088 return Error(kPListErrorMinimumConstraintNotMet, &keyPath, @"Number is too small (%g, minimum is %g).", numericValue, constraint);
1089 }
1090
1091 constraint = [params oo_doubleForKey:@"maximum" defaultValue:INFINITY];
1092 if (constraint < numericValue) {
1093 return Error(kPListErrorMaximumConstraintNotMet, &keyPath, @"Number is too large (%g, maximum is %g).", numericValue, constraint);
1094 }
1095
1096 return nil;
1097}
1098
1099static NSError* Verify_OneOf(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
1100{
1101 NSArray* options = nil;
1102 BOOL OK = NO, stop = NO;
1103 id option = nil;
1104 NSError* error;
1105 NSMutableDictionary* errors = nil;
1106
1107 DebugDump(@"%@", @"* oneOf");
1108
1109 options = [params oo_arrayForKey:@"options"];
1110 if (options == nil) {
1111 *outStop = YES;
1112 return Error(kPListErrorSchemaNoOneOfOptions, &keyPath, @"Bad schema: no options specified for oneOf type.");
1113 }
1114
1115 errors = [[NSMutableDictionary alloc] initWithCapacity:[options count]];
1116
1117 foreach (option, options) {
1118 if ([verifier verifyPList:rootPList
1119 named:name
1120 subProperty:value
1121 againstSchemaType:option
1122 atPath:keyPath
1123 tentative:YES
1124 error:&error
1125 stop:&stop]) {
1126 DebugDump(@"%@", @"> Match.");
1127 OK = YES;
1128 break;
1129 }
1130 [errors setObject:error forKey:option];
1131 }
1132
1133 if (!OK) {
1134 DebugDump(@"%@", @"! No match.");
1135 return ErrorWithProperty(kPListErrorOneOfNoMatch, &keyPath, kErrorsByOptionErrorKey, [errors autorelease], @"No matching type rule could be found.");
1136 }
1137
1138 // Ignore stop in tentatives.
1139 [errors release];
1140 return nil;
1141}
1142
1143static NSError* Verify_Enumeration(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
1144{
1145 NSArray* values = nil;
1146 NSString* filteredString = nil;
1147 NSError* error = nil;
1148
1149 DebugDump(@"%@", @"* enumeration");
1150
1151 REQUIRE_TYPE(NSString, @"string");
1152
1153 values = [params oo_arrayForKey:@"values"];
1154 DebugDump(@" - \"%@\" in %@", StringForErrorReport(value), ArrayForErrorReport(values));
1155
1156 if (values == nil) {
1157 *outStop = YES;
1158 return Error(kPListErrorSchemaNoEnumerationValues, &keyPath, @"Bad schema: no options specified for oneOf type.");
1159 }
1160
1161 filteredString = ApplyStringFilter(value, [params objectForKey:@"filter"], keyPath, &error);
1162 if (filteredString == nil)
1163 return error;
1164
1165 if ([values containsObject:filteredString])
1166 return nil;
1167
1168 return Error(kPListErrorEnumerationBadValue, &keyPath, @"Value \"%@\" not recognized, should be one of %@.", StringForErrorReport(value), ArrayForErrorReport(values));
1169}
1170
1171static NSError* Verify_Boolean(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
1172{
1173 DebugDump(@"* boolean: %@", value);
1174
1175 // Check basic parseability. If there's inequality here, the default value is being returned.
1176 if (OOBooleanFromObject(value, 0) == OOBooleanFromObject(value, 1))
1177 return nil;
1178 else
1179 return ErrorTypeMismatch([NSNumber class], @"boolean", value, keyPath);
1180}
1181
1182static NSError* Verify_FuzzyBoolean(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
1183{
1184 DebugDump(@"* fuzzy boolean: %@", value);
1185
1186 // Check basic parseability. If there's inequality here, the default value is being returned.
1187 if (OODoubleFromObject(value, 0) == OODoubleFromObject(value, 1))
1188 return nil;
1189 else if (OOBooleanFromObject(value, 0) == OOBooleanFromObject(value, 1))
1190 return nil;
1191 else
1192 return ErrorTypeMismatch([NSNumber class], @"fuzzy boolean", value, keyPath);
1193}
1194
1195static NSError* Verify_Vector(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
1196{
1197 DebugDump(@"* vector: %@", value);
1198
1199 // Check basic parseability. If there's inequality here, the default value is being returned.
1201 return nil;
1202 else
1203 return ErrorTypeMismatch(Nil, @"vector", value, keyPath);
1204}
1205
1206static NSError* Verify_Quaternion(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
1207{
1208 DebugDump(@"* quaternion: %@", value);
1209
1210 // Check basic parseability. If there's inequality here, the default value is being returned.
1212 return nil;
1213 else
1214 return ErrorTypeMismatch(Nil, @"quaternion", value, keyPath);
1215}
1216
1217static NSError* Verify_DelegatedType(OOPListSchemaVerifier* verifier, id value, NSDictionary* params, id rootPList, NSString* name, BackLinkChain keyPath, BOOL tentative, BOOL* outStop)
1218{
1219 id baseType = nil;
1220 NSString* key = nil;
1221 BOOL stop = NO;
1222 NSError* error = nil;
1223
1224 DebugDump(@"* delegated type: %@", [params objectForKey:@"key"]);
1225
1226 baseType = [params objectForKey:@"baseType"];
1227 if (baseType != nil) {
1228 if (![verifier verifyPList:rootPList
1229 named:name
1230 subProperty:value
1231 againstSchemaType:baseType
1232 atPath:keyPath
1233 tentative:tentative
1234 error:NULL
1235 stop:&stop]) {
1236 *outStop = stop;
1237 return nil;
1238 }
1239 }
1240
1241 key = [params objectForKey:@"key"];
1242 *outStop = ![verifier delegateVerifierWithPropertyList:rootPList
1243 named:name
1244 testProperty:value
1245 atPath:keyPath
1246 againstType:key
1247 error:&error];
1248 return error;
1249}
1250
1251@implementation NSString (OOPListSchemaVerifierHelpers)
1252
1253- (BOOL)ooPListVerifierHasSubString:(NSString*)string
1254{
1255 return [self rangeOfString:string].location != NSNotFound;
1256}
1257
1258@end
1259
1260@implementation NSError (OOPListSchemaVerifierConveniences)
1261
1262- (NSArray*)plistKeyPath
1263{
1264 return [[self userInfo] oo_arrayForKey:kPListKeyPathErrorKey];
1265}
1266
1271
1273{
1274 return [[self userInfo] oo_setForKey:kMissingRequiredKeysErrorKey];
1275}
1276
1278{
1279 return [[self userInfo] objectForKey:kExpectedClassErrorKey];
1280}
1281
1283{
1284 NSString* result = [[self userInfo] objectForKey:kExpectedClassNameErrorKey];
1285 if (result == nil)
1286 result = [[self expectedClass] description];
1287 return result;
1288}
1289
1290@end
1291
1292static NSError* Error(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain* keyPath, NSString* format, ...)
1293{
1294 NSError* result = nil;
1295 va_list args;
1296
1297 va_start(args, format);
1298 result = ErrorWithDictionaryAndArguments(errorCode, keyPath, nil, format, args);
1299 va_end(args);
1300
1301 return result;
1302}
1303
1304static NSError* ErrorWithProperty(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain* keyPath, NSString* propKey, id propValue, NSString* format, ...)
1305{
1306 NSError* result = nil;
1307 va_list args;
1308 NSDictionary* dict = nil;
1309
1310 if (propKey != nil && propValue != nil) {
1311 dict = [NSDictionary dictionaryWithObject:propValue forKey:propKey];
1312 }
1313 va_start(args, format);
1314 result = ErrorWithDictionaryAndArguments(errorCode, keyPath, dict, format, args);
1315 va_end(args);
1316
1317 return result;
1318}
1319
1320static NSError* ErrorWithDictionary(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain* keyPath, NSDictionary* dict, NSString* format, ...)
1321{
1322 NSError* result = nil;
1323 va_list args;
1324
1325 va_start(args, format);
1326 result = ErrorWithDictionaryAndArguments(errorCode, keyPath, dict, format, args);
1327 va_end(args);
1328
1329 return result;
1330}
1331
1332static NSError* ErrorWithDictionaryAndArguments(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain* keyPath, NSDictionary* dict, NSString* format, va_list arguments)
1333{
1334 NSString* message = nil;
1335 NSMutableDictionary* userInfo = nil;
1336
1337 message = [[NSString alloc] initWithFormat:format arguments:arguments];
1338
1339 userInfo = [NSMutableDictionary dictionaryWithDictionary:dict];
1340 [userInfo setObject:message forKey:NSLocalizedFailureReasonErrorKey];
1341 if (keyPath != NULL) {
1342 [userInfo setObject:KeyPathToArray(*keyPath) forKey:kPListKeyPathErrorKey];
1343 }
1344
1345 [message release];
1346
1347 return [NSError errorWithDomain:kOOPListSchemaVerifierErrorDomain code:errorCode userInfo:userInfo];
1348}
1349
1350static NSError* ErrorTypeMismatch(Class expectedClass, NSString* expectedClassName, id actualObject, BackLinkChain keyPath)
1351{
1352 NSDictionary* dict = nil;
1353 NSString* className = nil;
1354
1355 if (expectedClassName == nil)
1356 expectedClassName = [expectedClass description];
1357
1358 dict = [NSDictionary dictionaryWithObjectsAndKeys:
1359 expectedClassName, kExpectedClassNameErrorKey,
1360 expectedClass, kExpectedClassErrorKey,
1361 nil];
1362
1363 if (actualObject == nil)
1364 className = @"nothing";
1365 else if ([actualObject isKindOfClass:[NSString class]])
1366 className = @"string";
1367 else if ([actualObject isKindOfClass:[NSNumber class]])
1368 className = @"number";
1369 else if ([actualObject isKindOfClass:[NSArray class]])
1370 className = @"array";
1371 else if ([actualObject isKindOfClass:[NSDictionary class]])
1372 className = @"dictionary";
1373 else if ([actualObject isKindOfClass:[NSData class]])
1374 className = @"data";
1375 else if ([actualObject isKindOfClass:[NSDate class]])
1376 className = @"date";
1377 else
1378 className = [[actualObject class] description];
1379
1380 return ErrorWithDictionary(kPListErrorTypeMismatch, &keyPath, dict, @"Expected %@, found %@.", expectedClassName, className);
1381}
1382
1383static NSError* ErrorFailureAlreadyReported(void)
1384{
1385 return [NSError errorWithDomain:kOOPListSchemaVerifierErrorDomain code:kPListErrorFailedAndErrorHasBeenReported userInfo:nil];
1386}
1387
1388static BOOL IsFailureAlreadyReportedError(NSError* error)
1389{
1390 return [[error domain] isEqualToString:kOOPListSchemaVerifierErrorDomain] && [error code] == kPListErrorFailedAndErrorHasBeenReported;
1391}
1392
1393#endif // OO_OXP_VERIFIER_ENABLED
unsigned long long OOUnsignedLongLongFromObject(id object, unsigned long long defaultValue)
Vector OOVectorFromObject(id object, Vector defaultValue)
double OODoubleFromObject(id object, double defaultValue)
Quaternion OOQuaternionFromObject(id object, Quaternion defaultValue)
BOOL OOBooleanFromObject(id object, BOOL defaultValue)
long long OOLongLongFromObject(id object, long long defaultValue)
#define OOINLINE
#define OOLog(class, format,...)
Definition OOLogging.h:84
void OOLogSetDisplayMessagesInClass(NSString *inClass, BOOL inFlag)
Definition OOLogging.m:172
NSString *const kMissingRequiredKeysErrorKey
NSString *const kExpectedClassErrorKey
NSString *const kUndefinedMacroErrorKey
NSString *const kSchemaKeyPathErrorKey
NSString *const kErrorsByOptionErrorKey
NSString *const kExpectedClassNameErrorKey
OOPListSchemaVerifierErrorCode
@ kPListErrorDictionaryMissingRequiredKeys
@ kPListErrorSchemaNoEnumerationValues
@ kPListErrorNumberIsNegative
@ kPListErrorEnumerationBadValue
@ kPListErrorSchemaBadTypeSpecifier
@ kPListErrorInternal
@ kPListErrorSchemaNoOneOfOptions
@ kPListErrorTypeMismatch
@ kPListErrorStringSubstringMissing
@ kPListErrorSchemaUnknownType
@ kPListErrorMaximumConstraintNotMet
@ kPListErrorDictionaryUnknownKey
@ kPListErrorMinimumConstraintNotMet
@ kPListErrorLastErrorCode
@ kPListDelegatedTypeError
@ kPListErrorStringSuffixMissing
@ kPListErrorOneOfNoMatch
@ kPListErrorSchemaUndefiniedMacroReference
@ kPListErrorSchemaBadComparator
@ kPListErrorSchemaUnknownFilter
@ kPListErrorStringPrefixMissing
NSString *const kUnnownFilterErrorKey
NSString *const kPListKeyPathErrorKey
NSString *const kUnknownTypeErrorKey
NSString *const kOOPListSchemaVerifierErrorDomain
NSString *const kMissingSubStringErrorKey
NSString *const kUnknownKeyErrorKey
static NSError * ErrorWithDictionaryAndArguments(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain *keyPath, NSDictionary *dict, NSString *format, va_list arguments)
static NSString * SetForErrorReport(NSSet *set)
NSString *const kUndefinedMacroErrorKey
@ kMaximumLengthForStringInErrorMessage
static NSString * KeyPathToString(BackLinkChain keyPath)
static NSError * ErrorFailureAlreadyReported(void)
static NSError * Verify_OneOf(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
static SchemaType StringToSchemaType(NSString *string, NSError **outError)
static NSError * Verify_Boolean(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
static NSString * StringForErrorReport(NSString *string)
@ kPListErrorFailedAndErrorHasBeenReported
@ kStartOfPrivateErrorCodes
OOINLINE BackLinkChain BackLink(BackLinkChain *link, id element)
static NSError * Verify_Integer(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
static NSError * Verify_Quaternion(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
static NSError * Verify_String(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
static NSString * ArrayForErrorReport(NSArray *array)
static NSError * Verify_Array(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
static NSArray * KeyPathToArray(BackLinkChain keyPath)
static NSError * Verify_PositiveFloat(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
#define DebugDumpPopIndent()
static BOOL ApplyStringTest(NSString *string, id test, SEL testSelector, NSString *testDescription, BackLinkChain keyPath, NSError **outError)
#define DebugDumpIndent()
@ kTypeFuzzyBoolean
@ kTypeEnumeration
@ kTypeQuaternion
@ kTypeDictionary
@ kTypePositiveFloat
@ kTypeDelegatedType
@ kTypePositiveInteger
static BOOL sDebugDump
OOINLINE BackLinkChain BackLinkRoot(void)
#define VERIFY_PROTO(T)
static NSError * Error(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain *keyPath, NSString *format,...)
static NSError * ErrorWithDictionary(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain *keyPath, NSDictionary *dict, NSString *format,...)
static NSError * Verify_Enumeration(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
#define DebugDump(...)
static NSError * Verify_Vector(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
static NSString * StringOrArrayForErrorReport(id value, NSString *arrayPrefix)
static NSError * Verify_PositiveInteger(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
static NSError * Verify_FuzzyBoolean(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
#define VERIFY_CASE(T)
static NSError * ErrorWithProperty(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain *keyPath, NSString *propKey, id propValue, NSString *format,...)
static NSError * Verify_Float(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
#define DebugDumpPushIndent()
static NSError * ErrorTypeMismatch(Class expectedClass, NSString *expectedClassName, id actualObject, BackLinkChain keyPath)
static BOOL IsFailureAlreadyReportedError(NSError *error)
static NSError * Verify_Dictionary(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
OOINLINE BackLinkChain BackLinkIndex(BackLinkChain *link, NSUInteger index)
static NSError * Verify_DelegatedType(OOPListSchemaVerifier *verifier, id value, NSDictionary *params, id rootPList, NSString *name, BackLinkChain keyPath, BOOL tentative, BOOL *outStop)
#define DebugDumpOutdent()
#define REQUIRE_TYPE(CLASSNAME, NAMESTRING)
static NSString * ApplyStringFilter(NSString *string, id filterSpec, BackLinkChain keyPath, NSError **outError)
unsigned count
return nil
OOINLINE bool quaternion_equal(Quaternion a, Quaternion b) INLINE_CONST_FUNC
const Quaternion kIdentityQuaternion
const Quaternion kZeroQuaternion
const Vector kZeroVector
Definition OOVector.m:27
OOINLINE bool vector_equal(Vector a, Vector b) INLINE_CONST_FUNC
Definition OOVector.h:232
const Vector kBasisXVector
Definition OOVector.h:45
BOOL verifyPList:named:subProperty:againstSchemaType:atPath:tentative:error:stop:(id rootPList,[named] NSString *name,[subProperty] id subProperty,[againstSchemaType] id subSchema,[atPath] BackLinkChain keyPath,[tentative] BOOL tentative,[error] NSError **outError,[stop] BOOL *outStop)
NSString * descriptionForKeyPath:(NSArray *keyPath)
BOOL delegateVerifierWithPropertyList:named:testProperty:atPath:againstType:error:(id rootPList,[named] NSString *name,[testProperty] id subPList,[atPath] BackLinkChain keyPath,[againstType] NSString *typeKey,[error] NSError **outError)
NSDictionary * resolveSchemaType:atPath:error:(id specifier,[atPath] BackLinkChain keyPath,[error] NSError **outError)
BOOL delegateVerifierWithPropertyList:named:failedForProperty:withError:expectedType:(id rootPList,[named] NSString *name,[failedForProperty] id subPList,[withError] NSError *error,[expectedType] NSDictionary *localSchema)
BackLinkChain * link