SVGKSourceURL.m 2.0 KB

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