Oolite
Loading...
Searching...
No Matches
OOPListSchemaVerifier Class Reference

#include <OOPListSchemaVerifier.h>

+ Inheritance diagram for OOPListSchemaVerifier:
+ Collaboration diagram for OOPListSchemaVerifier:

Instance Methods

(id) - initWithSchema:
 
(void) - setDelegate:
 
(id) - delegate
 
(BOOL) - verifyPropertyList:named:
 
(void) - dealloc [implementation]
 
(BOOL) - delegateVerifierWithPropertyList:named:testProperty:atPath:againstType:error: [implementation]
 
(BOOL) - delegateVerifierWithPropertyList:named:failedForProperty:withError:expectedType: [implementation]
 
(BOOL) - verifyPList:named:subProperty:againstSchemaType:atPath:tentative:error:stop: [implementation]
 
(NSDictionary *) - resolveSchemaType:atPath:error: [implementation]
 

Class Methods

(instancetype) + verifierWithSchema:
 
(NSString *) + descriptionForKeyPath:
 

Private Attributes

NSDictionary * _schema
 
NSDictionary * _definitions
 
id _delegate
 
uint32_t _badDelegateWarning: 1
 

Detailed Description

Definition at line 39 of file OOPListSchemaVerifier.h.

Method Documentation

◆ dealloc

- (void) dealloc
implementation

Definition at line 115 of file OOPListSchemaVerifier.m.

243{
244 [_schema release];
245 [_definitions release];
246
247 [super dealloc];
248}

◆ delegate

- (id) delegate

Definition at line 115 of file OOPListSchemaVerifier.m.

262{
263 return _delegate;
264}

◆ delegateVerifierWithPropertyList:named:failedForProperty:withError:expectedType:

- (BOOL) delegateVerifierWithPropertyList: (id)  rootPList
named: (NSString *)  name
failedForProperty: (id)  subPList
withError: (NSError *)  error
expectedType: (NSDictionary *)  localSchema 
implementation

Provided by category OOPListSchemaVerifier(OOPrivate).

Definition at line 115 of file OOPListSchemaVerifier.m.

375 :(id)rootPList
376 named:(NSString *)name
377 failedForProperty:(id)subPList
378 withError:(NSError *)error
379 expectedType:(NSDictionary *)localSchema
380{
381 BOOL result;
382
383 if ([_delegate respondsToSelector:@selector(verifier:withPropertyList:named:failedForProperty:withError:expectedType:)])
384 {
385 @try
386 {
387 result = [_delegate verifier:self
388 withPropertyList:rootPList
389 named:name
390 failedForProperty:subPList
391 withError:error
392 expectedType:localSchema];
393 }
394 @catch (NSException *exception)
395 {
396 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);
397 result = NO;
398 }
399 }
400 else
401 {
402 OOLog(@"plistVerifier.failed", @"Verification of property list \"%@\" failed at %@: %@", name, [error plistKeyPathDescription], [error localizedFailureReason]);
403 result = NO;
404 }
405 return result;
406}
#define OOLog(class, format,...)
Definition OOLogging.h:88

Referenced by Verify_Dictionary().

+ Here is the caller graph for this function:

◆ delegateVerifierWithPropertyList:named:testProperty:atPath:againstType:error:

- (BOOL) delegateVerifierWithPropertyList: (id)  rootPList
named: (NSString *)  name
testProperty: (id)  subPList
atPath: (BackLinkChain keyPath
againstType: (NSString *)  typeKey
error: (NSError **)  outError 
implementation

Provided by category OOPListSchemaVerifier(OOPrivate).

Definition at line 115 of file OOPListSchemaVerifier.m.

322 :(id)rootPList
323 named:(NSString *)name
324 testProperty:(id)subPList
325 atPath:(BackLinkChain)keyPath
326 againstType:(NSString *)typeKey
327 error:(NSError **)outError
328{
329 BOOL result;
330 NSError *error = nil;
331
332 if ([_delegate respondsToSelector:@selector(verifier:withPropertyList:named:testProperty:atPath:againstType:error:)])
333 {
334 @try
335 {
336 result = [_delegate verifier:self
337 withPropertyList:rootPList
338 named:name
339 testProperty:subPList
340 atPath:KeyPathToArray(keyPath)
341 againstType:typeKey
342 error:&error];
343 }
344 @catch (NSException *exception)
345 {
346 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);
347 result = NO;
348 error = nil;
349 }
350
351 if (outError != NULL)
352 {
353 if (!result || error != nil)
354 {
355 // Note: Generates an error if delegate returned NO (meaning stop) or if delegate produced an error but did not request a stop.
356 *outError = ErrorWithProperty(kPListDelegatedTypeError, &keyPath, NSUnderlyingErrorKey, error, @"Value at %@ does not match delegated type \"%@\".", KeyPathToString(keyPath), typeKey);
357 }
358 else *outError = nil;
359 }
360 }
361 else
362 {
363 if (!_badDelegateWarning)
364 {
365 OOLog(@"plistVerifier.badDelegate", @"%@", @"Property list schema verifier: delegate does not handle delegated types.");
366 _badDelegateWarning = YES;
367 }
368 result = YES;
369 }
370
371 return result;
372}
@ kPListDelegatedTypeError
static NSString * KeyPathToString(BackLinkChain keyPath)
static NSError * ErrorWithProperty(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain *keyPath, NSString *propKey, id propValue, NSString *format,...)
return nil

Referenced by Verify_DelegatedType().

+ Here is the caller graph for this function:

◆ descriptionForKeyPath:

+ (NSString *) descriptionForKeyPath: (NSArray *)  keyPath

Definition at line 115 of file OOPListSchemaVerifier.m.

285 :(NSArray *)keyPath
286{
287 NSMutableString *result = nil;
288 id component = nil;
289 BOOL first = YES;
290
291 result = [NSMutableString string];
292
293 foreach (component, keyPath)
294 {
295 if ([component isKindOfClass:[NSNumber class]])
296 {
297 [result appendFormat:@"[%@]", component];
298 }
299 else if ([component isKindOfClass:[NSString class]])
300 {
301 if (!first) [result appendString:@"."];
302 [result appendString:component];
303 }
304 else return nil;
305 first = NO;
306 }
307
308 if (first)
309 {
310 // Empty path
311 return @"root";
312 }
313
314 return result;
315}

Referenced by KeyPathToString().

+ Here is the caller graph for this function:

◆ initWithSchema:

- (id) initWithSchema: (NSDictionary *)  schema

Definition at line 115 of file OOPListSchemaVerifier.m.

221 :(NSDictionary *)schema
222{
223 self = [super init];
224 if (self != nil)
225 {
226 _schema = [schema retain];
227 _definitions = [[_schema oo_dictionaryForKey:@"$definitions"] retain];
228 sDebugDump = [[NSUserDefaults standardUserDefaults] boolForKey:@"plist-schema-verifier-dump-structure"];
229 if (sDebugDump) OOLogSetDisplayMessagesInClass(@"verifyOXP.verbose.plistDebugDump", YES);
230
231 if (_schema == nil)
232 {
233 [self release];
234 self = nil;
235 }
236 }
237
238 return self;
239}
void OOLogSetDisplayMessagesInClass(NSString *inClass, BOOL inFlag)
Definition OOLogging.m:182
static BOOL sDebugDump

◆ resolveSchemaType:atPath:error:

- (NSDictionary *) resolveSchemaType: (id)  specifier
atPath: (BackLinkChain keyPath
error: (NSError **)  outError 
implementation

Provided by category OOPListSchemaVerifier(OOPrivate).

Definition at line 115 of file OOPListSchemaVerifier.m.

495 :(id)specifier
496 atPath:(BackLinkChain)keyPath
497 error:(NSError **)outError
498{
499 id typeVal = nil;
500 NSString *complaint = nil;
501
502 assert(outError != NULL);
503
504 if (![specifier isKindOfClass:[NSString class]] && ![specifier isKindOfClass:[NSDictionary class]]) goto BAD_TYPE;
505
506 for (;;)
507 {
508 if ([specifier isKindOfClass:[NSString class]]) specifier = [NSDictionary dictionaryWithObject:specifier forKey:@"type"];
509 typeVal = [(NSDictionary *)specifier objectForKey:@"type"];
510
511 if ([typeVal isKindOfClass:[NSString class]])
512 {
513 if ([typeVal hasPrefix:@"$"])
514 {
515 // Macro reference; look it up in $definitions
516 specifier = [_definitions objectForKey:typeVal];
517 if (specifier == nil)
518 {
519 *outError = ErrorWithProperty(kPListErrorSchemaUndefiniedMacroReference, &keyPath, kUndefinedMacroErrorKey, typeVal, @"Bad schema: reference to undefined macro \"%@\".", StringForErrorReport(typeVal));
520 return nil;
521 }
522 }
523 else
524 {
525 // Non-macro string
526 return specifier;
527 }
528 }
529 else if ([typeVal isKindOfClass:[NSDictionary class]])
530 {
531 specifier = typeVal;
532 }
533 else
534 {
535 goto BAD_TYPE;
536 }
537 }
538
539BAD_TYPE:
540 // Error: bad type
541 if (typeVal == nil) complaint = @"no type specified";
542 else complaint = @"not string or dictionary";
543
544 *outError = Error(kPListErrorSchemaBadTypeSpecifier, &keyPath, @"Bad schema: invalid type specifier for path %@ (%@).", KeyPathToString(keyPath), complaint);
545 return nil;
546}
@ kPListErrorSchemaBadTypeSpecifier
@ kPListErrorSchemaUndefiniedMacroReference
NSString *const kUndefinedMacroErrorKey
static NSString * StringForErrorReport(NSString *string)
static NSError * Error(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain *keyPath, NSString *format,...)

◆ setDelegate:

- (void) setDelegate: (id)  delegate

Definition at line 115 of file OOPListSchemaVerifier.m.

251 :(id)delegate
252{
253 if (_delegate != delegate)
254 {
257 }
258}

◆ verifierWithSchema:

+ (id) verifierWithSchema: (NSDictionary *)  schema

Definition at line 115 of file OOPListSchemaVerifier.m.

215 :(NSDictionary *)schema
216{
217 return [[[self alloc] initWithSchema:schema] autorelease];
218}

◆ verifyPList:named:subProperty:againstSchemaType:atPath:tentative:error:stop:

- (BOOL) verifyPList: (id)  rootPList
named: (NSString *)  name
subProperty: (id)  subProperty
againstSchemaType: (id)  subSchema
atPath: (BackLinkChain keyPath
tentative: (BOOL)  tentative
error: (NSError **)  outError
stop: (BOOL *)  outStop 
implementation

Provided by category OOPListSchemaVerifier(OOPrivate).

Definition at line 115 of file OOPListSchemaVerifier.m.

409 :(id)rootPList
410 named:(NSString *)name
411 subProperty:(id)subProperty
412 againstSchemaType:(id)subSchema
413 atPath:(BackLinkChain)keyPath
414 tentative:(BOOL)tentative
415 error:(NSError **)outError
416 stop:(BOOL *)outStop
417{
419 NSError *error = nil;
420 NSDictionary *resolvedSpecifier = nil;
421 NSAutoreleasePool *pool = nil;
422
423 assert(outStop != NULL);
424
425 pool = [[NSAutoreleasePool alloc] init];
426
428
429 @try
430 {
432
433 resolvedSpecifier = [self resolveSchemaType:subSchema atPath:keyPath error:&error];
434 if (resolvedSpecifier != nil) type = StringToSchemaType([resolvedSpecifier objectForKey:@"type"], &error);
435
436 #define VERIFY_CASE(T) case kType##T: error = Verify_##T(self, subProperty, resolvedSpecifier, rootPList, name, keyPath, tentative, outStop); break;
437
438 switch (type)
439 {
440 VERIFY_CASE(String);
441 VERIFY_CASE(Array);
442 VERIFY_CASE(Dictionary);
443 VERIFY_CASE(Integer);
444 VERIFY_CASE(PositiveInteger);
445 VERIFY_CASE(Float);
446 VERIFY_CASE(PositiveFloat);
447 VERIFY_CASE(OneOf);
448 VERIFY_CASE(Enumeration);
449 VERIFY_CASE(Boolean);
450 VERIFY_CASE(FuzzyBoolean);
451 VERIFY_CASE(Vector);
452 VERIFY_CASE(Quaternion);
453 VERIFY_CASE(DelegatedType);
454
455 case kTypeUnknown:
456 // resolveSchemaType:... or StringToSchemaType() should have provided an error.
457 *outStop = YES;
458 }
459 }
460 @catch (NSException *exception)
461 {
462 error = Error(kPListErrorInternal, (BackLinkChain *)&keyPath, @"Uncaught exception %@: %@ in plist verifier for \"%@\" at %@.", [exception name], [exception reason], name, KeyPathToString(keyPath));
463 }
464
466
467 if (error != nil)
468 {
469 if (!tentative && !IsFailureAlreadyReportedError(error))
470 {
471 *outStop = ![self delegateVerifierWithPropertyList:rootPList
472 named:name
473 failedForProperty:subProperty
474 withError:error
475 expectedType:subSchema];
476 }
477 else if (tentative) *outStop = YES;
478 }
479
480 if (outError != NULL && error != nil)
481 {
482 *outError = [error retain];
483 [pool release];
484 [error autorelease];
485 }
486 else
487 {
488 [pool release];
489 }
490
491 return error == nil;
492}
@ kPListErrorInternal
static SchemaType StringToSchemaType(NSString *string, NSError **outError)
#define DebugDumpPopIndent()
#define DebugDumpIndent()
#define VERIFY_CASE(T)
#define DebugDumpPushIndent()
static BOOL IsFailureAlreadyReportedError(NSError *error)

◆ verifyPropertyList:named:

- (BOOL) verifyPropertyList: (id)  plist
named: (NSString *)  name 

Definition at line 115 of file OOPListSchemaVerifier.m.

267 :(id)plist named:(NSString *)name
268{
269 BOOL OK;
270 BOOL stop = NO;
271
272 OK = [self verifyPList:plist
273 named:name
274 subProperty:plist
275 againstSchemaType:_schema
276 atPath:BackLinkRoot()
277 tentative:NO
278 error:NULL
279 stop:&stop];
280
281 return OK;
282}

Member Data Documentation

◆ _badDelegateWarning

- (uint32_t) _badDelegateWarning
private

Definition at line 46 of file OOPListSchemaVerifier.h.

◆ _definitions

- (NSDictionary*) _definitions
private

Definition at line 43 of file OOPListSchemaVerifier.h.

◆ _delegate

- (id) _delegate
private

Definition at line 45 of file OOPListSchemaVerifier.h.

◆ _schema

- (NSDictionary*) _schema
private

Definition at line 42 of file OOPListSchemaVerifier.h.


The documentation for this class was generated from the following files: