SVGKSourceURL.m 1.9 KB

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