forked from SVGKit/SVGKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVGKSourceURL.m
More file actions
75 lines (57 loc) · 2.04 KB
/
Copy pathSVGKSourceURL.m
File metadata and controls
75 lines (57 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#import "SVGKSourceURL.h"
@implementation SVGKSourceURL
-(NSString *)keyForAppleDictionaries
{
return [self.URL absoluteString];
}
+ (SVGKSource*)sourceFromURL:(NSURL*)u {
NSInputStream* stream = [self internalCreateInputStreamFromURL:u];
SVGKSourceURL* s = [[[SVGKSourceURL alloc] initWithInputSteam:stream] autorelease];
s.URL = u;
return s;
}
+(NSInputStream*) internalCreateInputStreamFromURL:(NSURL*) u
{
NSInputStream* stream = [NSInputStream inputStreamWithURL:u];
if( stream == nil )
{
/* Thanks, Apple, for not implementing your own method.
c.f. http://stackoverflow.com/questions/20571069/i-cannot-initialize-a-nsinputstream
NB: current Apple docs don't seem to mention this - certainly not in the inputStreamWithURL: method? */
NSError* errorWithNSData;
NSData *tempData = [NSData dataWithContentsOfURL:u options:0 error:&errorWithNSData];
if( tempData == nil )
{
@throw [NSException exceptionWithName:@"NSDataCrashed" reason:[NSString stringWithFormat:@"Error internally in Apple's NSData trying to read from URL '%@'. Error = %@", u, errorWithNSData] userInfo:@{NSLocalizedDescriptionKey:errorWithNSData}];
}
else
stream = [[[NSInputStream alloc] initWithData:tempData] autorelease];
}
//DO NOT DO THIS: let the parser do it at last possible moment (Apple has threading problems otherwise!) [stream open];
return stream;
}
-(id)copyWithZone:(NSZone *)zone
{
id copy = [super copyWithZone:zone];
if( copy )
{
/** clone bits */
[copy setURL:[[self.URL copy] autorelease]];
/** Finally, manually intialize the input stream, as required by super class */
[copy setStream:[[self class] internalCreateInputStreamFromURL:((SVGKSourceURL*)copy).URL]];
}
return copy;
}
- (SVGKSource *)sourceFromRelativePath:(NSString *)path {
NSURL *url = [NSURL URLWithString:path relativeToURL:self.URL];
return [SVGKSourceURL sourceFromURL:url];
}
-(NSString *)description
{
return [NSString stringWithFormat:@"[SVGKSource: URL = \"%@\"]", self.URL ];
}
- (void)dealloc {
self.URL = nil;
[super dealloc];
}
@end