/
GPXExtensionsElement.swift
80 lines (64 loc) · 2.22 KB
/
GPXExtensionsElement.swift
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
76
77
78
79
80
//
// GPXExtensionsElement.swift
// Pods
//
// Created by Vincent on 14/7/19.
//
import Foundation
/// A duplicated class of `GPXRawElement`
///
/// This class is a public class as it is representative of all child extension tag types.
///
/// It is also inherits `GPXElement`, and therefore, works like any other 'native' element types.
open class GPXExtensionsElement: GPXElement, Codable {
/// Tag name of extension element.
public var name: String
/// Text data content of the element.
public var text: String?
/// Attributes data of the element.
public var attributes = [String : String]()
/// Children tags of this element.
public var children = [GPXExtensionsElement]()
/// Easily get child tags via subscript.
public subscript(name: String) -> GPXExtensionsElement {
get {
for child in children {
if child.name == name {
return child
}
}
return GPXExtensionsElement()
}
}
/// Initialize with a tagName.
public init(name: String) {
self.name = name
super.init()
}
/// Default initializer.
required public init() {
self.name = "Undefined"
}
// MARK:- GPX File Mutation
override func tagName() -> String {
return name
}
override func addOpenTag(toGPX gpx: NSMutableString, indentationLevel: Int) {
let attribute = NSMutableString()
if !attributes.isEmpty {
for (key, value) in attributes {
attribute.appendFormat(" %@=\"%@\"", key, value)
}
gpx.appendOpenTag(indentation: indent(forIndentationLevel: indentationLevel), tag: tagName(), attribute: attribute)
}
else if let text = text {
self.addProperty(forValue: text, gpx: gpx, tagName: tagName(), indentationLevel: indentationLevel)
}
}
override func addChildTag(toGPX gpx: NSMutableString, indentationLevel: Int) {
super.addChildTag(toGPX: gpx, indentationLevel: indentationLevel)
for child in children {
child.gpx(gpx, indentationLevel: indentationLevel)
}
}
}