-
Notifications
You must be signed in to change notification settings - Fork 618
/
XMLParser.swift
executable file
·265 lines (233 loc) · 7.21 KB
/
XMLParser.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright © 2017 Schibsted. All rights reserved.
import Foundation
enum XMLNode {
case node(
name: String,
attributes: [String: String],
children: [XMLNode]
)
case text(String)
case comment(String)
public var isEmpty: Bool {
switch self {
case let .node(_, _, children):
return !children.contains {
guard case .text = $0 else {
return true
}
return !$0.isEmpty
}
case let .text(text):
return text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
default:
return false
}
}
public var isText: Bool {
guard case .text = self else {
return false
}
return !isLinebreak
}
public var isHTML: Bool {
guard case let .node(name, _, _) = self else {
return false
}
return name.lowercased() == name
}
public var isLinebreak: Bool {
guard case .text("\n") = self else {
return false
}
return true
}
public var isComment: Bool {
guard case .comment = self else {
return false
}
return true
}
var name: String? {
guard case let .node(name, _, _) = self else {
return nil
}
return name
}
public var children: [XMLNode] {
if case let .node(_, _, children) = self {
return children
}
return []
}
public var attributes: [String: String] {
if case let .node(_, attributes, _) = self {
return attributes
}
return [:]
}
fileprivate mutating func append(_ node: XMLNode) {
switch self {
case let .node(name, attributes, children):
self = .node(
name: name,
attributes: attributes,
children: children + [node]
)
default:
preconditionFailure()
}
}
}
class XMLParser: NSObject, XMLParserDelegate {
private var options: Options = []
private var root: [XMLNode] = []
private var stack: [XMLNode] = []
private var top: XMLNode?
private var error: Error?
private var text = ""
public struct Error: Swift.Error, CustomStringConvertible {
public let description: String
fileprivate init(_ message: String) {
description = message
}
fileprivate init(_ error: Swift.Error) {
let nsError = error as NSError
guard let line = nsError.userInfo["NSXMLParserErrorLineNumber"] else {
self.init("\(nsError.localizedDescription.trimmingCharacters(in: .whitespacesAndNewlines))")
return
}
guard let message = nsError.userInfo["NSXMLParserErrorMessage"] else {
self.init("Malformed XML at line \(line)")
return
}
self.init("\("\(message)".trimmingCharacters(in: .whitespacesAndNewlines)) at line \(line)")
}
}
public struct Options: OptionSet {
public let rawValue: Int
public init(rawValue: Int) {
self.rawValue = rawValue
}
public static let skipComments = Options(rawValue: 1 << 0)
}
public static func parse(data: Data, options: Options = []) throws -> [XMLNode] {
let parser = XMLParser(options: options)
let foundationParser = Foundation.XMLParser(data: data)
foundationParser.delegate = parser
foundationParser.parse()
if let error = parser.error {
throw error
}
return parser.root
}
private init(options: Options) {
self.options = options
super.init()
}
override private init() {
preconditionFailure()
}
private func appendNode(_ node: XMLNode) {
if top != nil {
top?.append(node)
} else {
root.append(node)
}
}
private func appendText() {
if !text.isEmpty {
appendNode(.text(text
.replacingOccurrences(of: "[\\t ]+", with: " ", options: .regularExpression)
.replacingOccurrences(of: " ?\\n+ ?", with: "\n", options: .regularExpression)))
text = ""
}
}
// MARK: XMLParserDelegate methods
func parser(_: Foundation.XMLParser, didStartElement elementName: String, namespaceURI _: String?, qualifiedName _: String?, attributes: [String: String] = [:]) {
let node = XMLNode.node(
name: elementName,
attributes: attributes,
children: []
)
if top != nil { // Can't use if let binding here because we mutate top
if !node.isHTML {
text = text.trimmingCharacters(in: .whitespacesAndNewlines)
} else if !top!.isHTML, top!.children.last?.isHTML != true {
text = text.ltrim()
}
appendText()
stack.append(top!)
}
top = node
}
func parser(_: Foundation.XMLParser, didEndElement _: String, namespaceURI _: String?, qualifiedName _: String?) {
if !top!.isHTML {
if top!.children.isEmpty {
text = text.trimmingCharacters(in: .whitespacesAndNewlines)
} else {
text = text.rtrim()
}
}
appendText()
let node = top!
top = stack.popLast()
appendNode(node)
}
func parser(_: Foundation.XMLParser, foundCharacters string: String) {
text += string
}
func parser(_: Foundation.XMLParser, foundComment comment: String) {
appendText()
if !options.contains(.skipComments) {
appendNode(.comment(comment))
}
}
func parser(_: Foundation.XMLParser, parseErrorOccurred parseError: Swift.Error) {
guard error == nil else {
// Don't overwrite existing error
return
}
error = Error(parseError)
}
}
extension Collection where Iterator.Element == XMLNode {
var isHTML: Bool {
return contains(where: { $0.isHTML })
}
}
extension String {
func xmlEncoded(forAttribute: Bool = false) -> String {
var output = ""
for char in unicodeScalars {
switch char {
case "&":
output.append("&")
case "<":
output.append("<")
case "\"" where forAttribute:
output.append(""")
case _ where char.value > 127:
output.append(String(format: "&#x%2X;", char.value))
default:
output.append(String(char))
}
}
return output
}
}
private extension String {
func ltrim() -> String {
var chars = unicodeScalars
while let char = chars.first, NSCharacterSet.whitespacesAndNewlines.contains(char) {
chars.removeFirst()
}
return String(chars)
}
func rtrim() -> String {
var chars = unicodeScalars
while let char = chars.last, NSCharacterSet.whitespacesAndNewlines.contains(char) {
chars.removeLast()
}
return String(chars)
}
}