-
Notifications
You must be signed in to change notification settings - Fork 0
/
TLGPXFile.m
74 lines (66 loc) · 2.13 KB
/
TLGPXFile.m
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
//
// TLGPXFile.m
// Mercatalog
//
// Created by Nathan Vander Wilt on 3/17/08.
// Copyright 2008 Calf Trail Software, LLC. All rights reserved.
//
#import "TLGPXFile.h"
@implementation TLGPXFile
- (id)initGPXFileWithContentsOfURL:(NSURL*)url error:(NSError**)err {
self = [super init];
if (self) {
tracks = [[NSMutableArray array] retain];
waypoints = [[NSMutableArray array] retain];
NSXMLParser* parser = [[[NSXMLParser alloc] initWithContentsOfURL:url] autorelease];
[parser setDelegate:self];
BOOL success = [parser parse];
if (!success) {
if (err) {
NSDictionary* errInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[parser parserError], NSUnderlyingErrorKey,
@"Not a valid GPX file.", NSLocalizedDescriptionKey, nil];
*err = [NSError errorWithDomain:NSCocoaErrorDomain
code:NSFileReadCorruptFileError
userInfo:errInfo];
}
[self release];
return nil;
}
}
return self;
}
- (void)dealloc {
[tracks release];
[waypoints release];
[super dealloc];
}
- (void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName
namespaceURI:(NSString*)namespaceURI
qualifiedName:(NSString*)qualifiedName
attributes:(NSDictionary*)attributeDict
{
(void)namespaceURI; // TODO: check namespaceURI equal to GPX/1/1 ?
(void)qualifiedName;
if ([elementName isEqualToString:@"trk"]) {
TLGPXTracklog* newTrack = [[TLGPXTracklog alloc] initWithParent:self
forElement:elementName
namespaceURI:namespaceURI
qualifiedName:qualifiedName
attributes:attributeDict];
[parser setDelegate:newTrack];
[tracks addObject:[newTrack autorelease]];
}
else if ([elementName isEqualToString:@"wpt"]) {
TLGPXWaypoint* newWaypoint = [[TLGPXWaypoint alloc] initWithParent:self
forElement:elementName
namespaceURI:namespaceURI
qualifiedName:qualifiedName
attributes:attributeDict];
[parser setDelegate:newWaypoint];
[waypoints addObject:[newWaypoint autorelease]];
}
}
- (NSArray*)tracks { return tracks; }
- (NSArray*)waypoints { return waypoints; }
@end