SVGKSourceNSData.m 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #import "SVGKSourceNSData.h"
  2. #import "SVGKSourceURL.h" // used for delegating when asked to construct relative links
  3. #import "SVGKDefine_Private.h"
  4. @implementation SVGKSourceNSData
  5. -(NSString *)keyForAppleDictionaries
  6. {
  7. return [[NSString alloc] initWithData:self.rawData encoding:NSUTF8StringEncoding];
  8. }
  9. + (SVGKSource*)sourceFromData:(NSData*)data URLForRelativeLinks:(NSURL*) url
  10. {
  11. NSInputStream* stream = [NSInputStream inputStreamWithData:data];
  12. //DO NOT DO THIS: let the parser do it at last possible moment (Apple has threading problems otherwise!) [stream open];
  13. SVGKSourceNSData* s = [[SVGKSourceNSData alloc] initWithInputSteam:stream];
  14. s.rawData = data;
  15. s.effectiveURL = url;
  16. return s;
  17. }
  18. -(id)copyWithZone:(NSZone *)zone
  19. {
  20. id copy = [super copyWithZone:zone];
  21. if( copy )
  22. {
  23. /** clone bits */
  24. [copy setRawData:[self.rawData copy]];
  25. /** Finally, manually intialize the input stream, as required by super class */
  26. [copy setStream:[NSInputStream inputStreamWithData:((SVGKSourceNSData*)copy).rawData]];
  27. }
  28. return copy;
  29. }
  30. -(SVGKSource *)sourceFromRelativePath:(NSString *)path
  31. {
  32. if( self.effectiveURL != nil )
  33. {
  34. NSURL *url = [NSURL URLWithString:path relativeToURL:self.effectiveURL];
  35. return [SVGKSourceURL sourceFromURL:url];
  36. }
  37. else
  38. {
  39. SVGKitLogError(@"Cannot construct a relative link for this SVGKSource; it was created from anonymous NSData with no source URL provided. Source = %@", self);
  40. return nil;
  41. }
  42. }
  43. @end