SVGKSourceURL.m 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #import "SVGKSourceURL.h"
  2. @implementation SVGKSourceURL
  3. -(NSString *)keyForAppleDictionaries
  4. {
  5. return [self.URL absoluteString];
  6. }
  7. + (SVGKSource*)sourceFromURL:(NSURL*)u {
  8. NSInputStream* stream = [self internalCreateInputStreamFromURL:u];
  9. if (!stream) {
  10. return nil;
  11. }
  12. SVGKSourceURL* s = [[SVGKSourceURL alloc] initWithInputSteam:stream];
  13. s.URL = u;
  14. return s;
  15. }
  16. +(nullable NSInputStream*) internalCreateInputStreamFromURL:(nullable NSURL*) u
  17. {
  18. if (!u) {
  19. return nil;
  20. }
  21. NSInputStream* stream = [NSInputStream inputStreamWithURL:u];
  22. if( stream == nil )
  23. {
  24. /* Thanks, Apple, for not implementing your own method.
  25. c.f. http://stackoverflow.com/questions/20571069/i-cannot-initialize-a-nsinputstream
  26. NB: current Apple docs don't seem to mention this - certainly not in the inputStreamWithURL: method? */
  27. NSError* errorWithNSData;
  28. NSData *tempData = [NSData dataWithContentsOfURL:u options:0 error:&errorWithNSData];
  29. if( tempData == nil )
  30. {
  31. SVGKitLogError(@"Error internally in Apple's NSData trying to read from URL '%@'. Error = %@", u, errorWithNSData);
  32. }
  33. else
  34. stream = [[NSInputStream alloc] initWithData:tempData];
  35. }
  36. //DO NOT DO THIS: let the parser do it at last possible moment (Apple has threading problems otherwise!) [stream open];
  37. return stream;
  38. }
  39. -(id)copyWithZone:(NSZone *)zone
  40. {
  41. id copy = [super copyWithZone:zone];
  42. if( copy )
  43. {
  44. /** clone bits */
  45. [copy setURL:[self.URL copy]];
  46. /** Finally, manually intialize the input stream, as required by super class */
  47. [copy setStream:[[self class] internalCreateInputStreamFromURL:((SVGKSourceURL*)copy).URL]];
  48. }
  49. return copy;
  50. }
  51. - (SVGKSource *)sourceFromRelativePath:(NSString *)path {
  52. NSURL *url = [NSURL URLWithString:path relativeToURL:self.URL];
  53. return [SVGKSourceURL sourceFromURL:url];
  54. }
  55. -(NSString *)description
  56. {
  57. return [NSString stringWithFormat:@"[SVGKSource: URL = \"%@\"]", self.URL ];
  58. }
  59. @end