DemoSVGObject.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // DemoSVGObject.m
  3. // Demo-OSX
  4. //
  5. // Created by C.W. Betts on 6/7/13.
  6. // Copyright (c) 2013 C.W. Betts. All rights reserved.
  7. //
  8. #import "DemoSVGObject.h"
  9. @implementation DemoSVGObject
  10. #define NotImplemented() \
  11. if ([self isMemberOfClass:[DemoSVGObject class]]) { \
  12. NSLog(@"The class %@ is meant to be subclassed, and not accessed directly.", [self class]); \
  13. } else { \
  14. NSLog(@"The subclass %@ of class %@ should implement %s.", [self class], [DemoSVGObject class], sel_getName(_cmd)); \
  15. } \
  16. [self doesNotRecognizeSelector:_cmd]; \
  17. return nil
  18. - (NSURL *)svgURL
  19. {
  20. NotImplemented();
  21. }
  22. - (NSString *)fileName
  23. {
  24. NotImplemented();
  25. }
  26. - (NSString *)fullFileName
  27. {
  28. NotImplemented();
  29. }
  30. - (BOOL)isEqualToURL:(NSURL*)theURL
  31. {
  32. if ([self.svgURL isFileURL] && [theURL isFileURL]) {
  33. id dat1, dat2;
  34. BOOL bothareValid = YES;
  35. BOOL theSame = NO;
  36. if (![[self svgURL] getResourceValue:&dat1 forKey:NSURLFileResourceIdentifierKey error:NULL]) {
  37. bothareValid = NO;
  38. }
  39. if (![theURL getResourceValue:&dat2 forKey:NSURLFileResourceIdentifierKey error:NULL]) {
  40. bothareValid = NO;
  41. }
  42. if (bothareValid) {
  43. theSame = [dat1 isEqual:dat2];
  44. }
  45. return theSame;
  46. } else if (![self.svgURL isFileURL] && ![theURL isFileURL]) {
  47. return [[self.svgURL absoluteURL] isEqual:[theURL absoluteURL]];
  48. } else
  49. return NO;
  50. }
  51. - (NSUInteger)hash
  52. {
  53. return [[[self svgURL] absoluteURL] hash];
  54. }
  55. @end
  56. @interface DemoSVGBundleObject ()
  57. @property (readwrite, copy) NSString* fullFileName;
  58. @property (readwrite, strong) NSBundle *theBundle;
  59. @end
  60. @implementation DemoSVGBundleObject
  61. - (id)initWithName:(NSString *)theName
  62. {
  63. return [self initWithName:theName bundle:[NSBundle mainBundle]];
  64. }
  65. - (id)initWithName:(NSString *)theName bundle:(NSBundle*)aBundle
  66. {
  67. if (self = [super init]) {
  68. self.fullFileName = theName;
  69. self.theBundle = aBundle;
  70. }
  71. return self;
  72. }
  73. - (void)getFileName:(out NSString **)filNam extension:(out NSString **)ext
  74. {
  75. NSParameterAssert(filNam != nil);
  76. NSParameterAssert(ext != nil);
  77. *filNam = [self.fullFileName stringByDeletingPathExtension];
  78. NSString *extension = [self.fullFileName pathExtension];
  79. *ext = extension ? extension : @"svg";
  80. }
  81. - (NSURL*)svgURL
  82. {
  83. NSString *newName;
  84. NSString *extension;
  85. [self getFileName:&newName extension:&extension];
  86. NSURL *retURL = [self.theBundle URLForResource:newName withExtension:extension];
  87. return retURL;
  88. }
  89. - (NSString*)fileName
  90. {
  91. NSString *newName;
  92. NSString *extension;
  93. [self getFileName:&newName extension:&extension];
  94. NSString *fullPath = [self.theBundle pathForResource:newName ofType:extension];
  95. NSString *retShortName = [[NSFileManager defaultManager] displayNameAtPath:fullPath];
  96. return retShortName;
  97. }
  98. - (BOOL)isEqual:(id)object
  99. {
  100. if ([object isKindOfClass:[DemoSVGBundleObject class]]) {
  101. DemoSVGBundleObject* bundObj = object;
  102. return [bundObj.fullFileName isEqualToString:self.fullFileName] && [bundObj.theBundle isEqual:self.theBundle];
  103. } else if ([object conformsToProtocol:@protocol(DemoSVGObject)] || [object isKindOfClass:[DemoSVGObject class]]) {
  104. return [self isEqualToURL:[object svgURL]];
  105. } else {
  106. return NO;
  107. }
  108. }
  109. @end
  110. @interface DemoSVGURLObject ()
  111. @property (strong, readwrite) NSURL *svgURL;
  112. @end
  113. @implementation DemoSVGURLObject
  114. - (id)initWithURL:(NSURL *)aURL
  115. {
  116. if (self = [super init]) {
  117. self.svgURL = aURL;
  118. }
  119. return self;
  120. }
  121. - (NSString *)fileName
  122. {
  123. NSURL *tmpURL = self.svgURL;
  124. if([tmpURL isFileURL]){
  125. NSString *val;
  126. NSError *err;
  127. if([tmpURL getResourceValue:&val forKey:NSURLLocalizedNameKey error:&err] == NO)
  128. {
  129. NSLog(@"DemoSVGObject: Could not find out if extension is hidden in file \"%@\", error: %@", [tmpURL path], [err localizedDescription]);
  130. return [tmpURL lastPathComponent];
  131. } else {
  132. return val;
  133. }
  134. }
  135. else return [tmpURL lastPathComponent];
  136. }
  137. - (NSString*)fullFileName
  138. {
  139. return [self.svgURL lastPathComponent];
  140. }
  141. - (BOOL)isEqual:(id)object
  142. {
  143. if (/*[object isKindOfClass:[DemoSVGURLObject class]] ||*/ [object conformsToProtocol:@protocol(DemoSVGObject)] || [object isKindOfClass:[DemoSVGObject class]]) {
  144. return [self isEqualToURL:[object svgURL]];
  145. } else {
  146. return NO;
  147. }
  148. }
  149. @end