Skip to content

Commit

Permalink
Adds creatorName to GPXExporter
Browse files Browse the repository at this point in the history
  • Loading branch information
mmllr committed Mar 1, 2024
1 parent 5dd2566 commit ac6644a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ A library for parsing and exporting GPX files with no dependencies besides Found
To use the `GPXKit` library in a SwiftPM project, add the following line to the dependencies in your `Package.swift` file:

```swift
.package(url: "https://github.com/mmllr/GPXKit", from: "2.2.2")
.package(url: "https://github.com/mmllr/GPXKit", from: "2.2.3")
```

## Usage examples
Expand Down
7 changes: 5 additions & 2 deletions Sources/GPXKit/GPXExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ import FoundationXML
public final class GPXExporter {
private let track: GPXTrack
private let exportDate: Bool
private let creatorName: String

/// Initializes a GPXExporter
/// - Parameters:
/// - track: The ``GPXTrack`` to export.
/// - shouldExportDate: Flag indicating whether it should export the timestamps in the track. Set it to false if you want to omit the values. This would decrease the exported xml's file size and protects privacy. Defaults to true.
/// - creatorName: The value for the creator tag in the header. Defaults to GPXKit
///
/// If the track cannot be exported, the resulting ``GPXExporter/xmlString`` property of the exporter is an empty GPX track xml.
public init(track: GPXTrack, shouldExportDate: Bool = true) {
public init(track: GPXTrack, shouldExportDate: Bool = true, creatorName: String = "GPXKit") {
self.track = track
self.exportDate = shouldExportDate
self.creatorName = creatorName
}

/// The exported GPX xml string. If the track cannot be exported, its value is an empty GPX track xml.
Expand Down Expand Up @@ -90,7 +93,7 @@ public final class GPXExporter {

private var headerAttributes: String {
return """
creator="GPXKit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
creator="\(creatorName)" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
"""
}
}
Expand Down
38 changes: 20 additions & 18 deletions Tests/GPXKitTests/GPXExporterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ final class GPXExporterTests: XCTestCase {
}()

private var result: GPXKit.XMLNode!
private let expectedHeaderAttributes = [
"creator": "GPXKit",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xsi:schemaLocation": "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd",
"version": "1.1",
"xmlns": "http://www.topografix.com/GPX/1/1",
"xmlns:gpxtpx": "http://www.garmin.com/xmlschemas/TrackPointExtension/v1",
"xmlns:gpxx": "http://www.garmin.com/xmlschemas/GpxExtensions/v3",
]
private func expectedHeaderAttributes(creator: String = "GPXKit") -> [String:String] {
[
"creator": creator,
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xsi:schemaLocation": "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd",
"version": "1.1",
"xmlns": "http://www.topografix.com/GPX/1/1",
"xmlns:gpxtpx": "http://www.garmin.com/xmlschemas/TrackPointExtension/v1",
"xmlns:gpxx": "http://www.garmin.com/xmlschemas/GpxExtensions/v3",
]
}

private func parseResult(_ xmlString: String) {
let xmlParser = BasicXMLParser(xml: xmlString)
Expand All @@ -48,7 +50,7 @@ final class GPXExporterTests: XCTestCase {

let expectedContent: GPXKit.XMLNode = XMLNode(
name: GPXTags.gpx.rawValue,
attributes: expectedHeaderAttributes,
attributes: expectedHeaderAttributes(),
children: [
XMLNode(name: GPXTags.metadata.rawValue, children: [
XMLNode(name: GPXTags.time.rawValue, content: expectedString(for: date)),
Expand All @@ -72,7 +74,7 @@ final class GPXExporterTests: XCTestCase {

let expectedContent: GPXKit.XMLNode = XMLNode(
name: GPXTags.gpx.rawValue,
attributes: expectedHeaderAttributes,
attributes: expectedHeaderAttributes(),
children: [
XMLNode(name: GPXTags.metadata.rawValue, children: [
XMLNode(name: GPXTags.time.rawValue, content: expectedString(for: date)),
Expand All @@ -96,11 +98,11 @@ final class GPXExporterTests: XCTestCase {
trackPoints: [],
keywords: ["one", "two"]
)
sut = GPXExporter(track: track)
sut = GPXExporter(track: track, creatorName: "Custom creator name")

let expectedContent: GPXKit.XMLNode = XMLNode(
name: GPXTags.gpx.rawValue,
attributes: expectedHeaderAttributes,
attributes: expectedHeaderAttributes(creator: "Custom creator name"),
children: [
XMLNode(name: GPXTags.metadata.rawValue, children: [
XMLNode(name: GPXTags.keywords.rawValue, content: "one two"),
Expand Down Expand Up @@ -132,7 +134,7 @@ final class GPXExporterTests: XCTestCase {

let expectedContent: GPXKit.XMLNode = XMLNode(
name: GPXTags.gpx.rawValue,
attributes: expectedHeaderAttributes,
attributes: expectedHeaderAttributes(),
children: [
XMLNode(name: GPXTags.metadata.rawValue, children: [
XMLNode(name: GPXTags.time.rawValue, content: expectedString(for: date)),
Expand Down Expand Up @@ -180,7 +182,7 @@ final class GPXExporterTests: XCTestCase {
sut = GPXExporter(track: track, shouldExportDate: false)
let expectedContent: GPXKit.XMLNode = XMLNode(
name: GPXTags.gpx.rawValue,
attributes: expectedHeaderAttributes,
attributes: expectedHeaderAttributes(),
children: [
XMLNode(name: GPXTags.metadata.rawValue),
XMLNode(name: GPXTags.track.rawValue, children: [
Expand All @@ -205,7 +207,7 @@ final class GPXExporterTests: XCTestCase {
sut = GPXExporter(track: track, shouldExportDate: false)
let expectedContent: GPXKit.XMLNode = XMLNode(
name: GPXTags.gpx.rawValue,
attributes: expectedHeaderAttributes,
attributes: expectedHeaderAttributes(),
children: [
XMLNode(name: GPXTags.metadata.rawValue),
XMLNode(name: GPXTags.track.rawValue, children: [
Expand Down Expand Up @@ -233,7 +235,7 @@ final class GPXExporterTests: XCTestCase {
sut = GPXExporter(track: track, shouldExportDate: false)
let expectedContent: GPXKit.XMLNode = XMLNode(
name: GPXTags.gpx.rawValue,
attributes: expectedHeaderAttributes,
attributes: expectedHeaderAttributes(),
children: [
XMLNode(name: GPXTags.metadata.rawValue),
XMLNode(name: GPXTags.waypoint.rawValue, attributes: [
Expand Down Expand Up @@ -326,7 +328,7 @@ final class GPXExporterTests: XCTestCase {

let expectedContent: GPXKit.XMLNode = XMLNode(
name: GPXTags.gpx.rawValue,
attributes: expectedHeaderAttributes,
attributes: expectedHeaderAttributes(),
children: [
XMLNode(name: GPXTags.metadata.rawValue, children: [
XMLNode(name: GPXTags.time.rawValue, content: expectedString(for: date)),
Expand Down

0 comments on commit ac6644a

Please sign in to comment.