SVGKSourceNSData.m 1.4 KB

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