Skip to content

Commit eec238e

Browse files
authored
Merge pull request #63 from stevelandeyasana/line-numbers
Store line numbers and CDATA on Element
2 parents b7beef4 + f1317db commit eec238e

File tree

5 files changed

+72
-42
lines changed

5 files changed

+72
-42
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ playground.xcworkspace
4141
# Package.pins
4242
# Package.resolved
4343
.build/
44+
.swiftpm/
4445

4546
# CocoaPods
4647
#

SwiftyXMLParser/Element.swift

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ extension XML {
3030
open var text: String?
3131
open var attributes = [String: String]()
3232
open var childElements = [Element]()
33+
open var lineNumberStart = -1
34+
open var lineNumberEnd = -1
35+
open var CDATA: Data?
3336

3437
// for println
3538
open weak var parentElement: Element?

SwiftyXMLParser/Parser.swift

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ extension XML {
6262

6363
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
6464
let node = Element(name: elementName)
65+
node.lineNumberStart = parser.lineNumber
6566
if !attributeDict.isEmpty {
6667
node.attributes = attributeDict
6768
}
@@ -80,8 +81,13 @@ extension XML {
8081
stack.last?.text = "" + string
8182
}
8283
}
84+
85+
func parser(_ parser: XMLParser, foundCDATA CDATABlock: Data) {
86+
stack.last?.CDATA = CDATABlock
87+
}
8388

8489
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
90+
stack.last?.lineNumberEnd = parser.lineNumber
8591
if let trimmingManner = self.trimmingManner {
8692
stack.last?.text = stack.last?.text?.trimmingCharacters(in: trimmingManner)
8793
}

SwiftyXMLParserTests/ParserTests.swift

+45-28
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ import XCTest
2727

2828

2929
class ParserTests: XCTestCase {
30+
fileprivate let packageRootPath = URL(fileURLWithPath: #file)
31+
.pathComponents
32+
.dropLast()
33+
.joined(separator: "/")
34+
.dropFirst()
3035

3136
override func setUp() {
3237
super.setUp()
@@ -35,12 +40,15 @@ class ParserTests: XCTestCase {
3540
override func tearDown() {
3641
super.tearDown()
3742
}
43+
44+
private func getPath(_ name: String) -> String {
45+
"\(packageRootPath)/\(name)"
46+
}
3847

3948
func testSuccessParse() {
40-
guard let path = Bundle(for: type(of: self)).path(forResource: "XMLDocument", ofType: "xml"),
41-
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
42-
XCTFail("fail to parse")
43-
return
49+
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("XMLDocument.xml"))) else {
50+
XCTFail("fail to parse")
51+
return
4452
}
4553

4654
let xml = XML.Parser().parse(data)
@@ -54,10 +62,9 @@ class ParserTests: XCTestCase {
5462
}
5563

5664
func testFailParse() {
57-
guard let path = Bundle(for: type(of: self)).path(forResource: "BrokenXMLDocument", ofType: "xml"),
58-
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
59-
XCTFail("fail to parse")
60-
return
65+
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("BrokenXMLDocument.xml"))) else {
66+
XCTFail("fail to parse")
67+
return
6168
}
6269

6370
let xml = XML.Parser().parse(data)
@@ -69,10 +76,9 @@ class ParserTests: XCTestCase {
6976
}
7077

7178
func testTextParseWithMockData() {
72-
guard let path = Bundle(for: type(of: self)).path(forResource: "SimpleDocument", ofType: "xml"),
73-
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
74-
XCTFail("fail to parse")
75-
return
79+
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("SimpleDocument.xml"))) else {
80+
XCTFail("fail to parse")
81+
return
7682
}
7783

7884
let xml = XML.Parser().parse(data)
@@ -84,10 +90,9 @@ class ParserTests: XCTestCase {
8490
}
8591

8692
func testWhitespaceParseWithMockData() {
87-
guard let path = Bundle(for: type(of: self)).path(forResource: "SimpleDocument", ofType: "xml"),
88-
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
89-
XCTFail("fail to parse")
90-
return
93+
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("SimpleDocument.xml"))) else {
94+
XCTFail("fail to parse")
95+
return
9196
}
9297

9398
let xml = XML.Parser().parse(data)
@@ -99,10 +104,9 @@ class ParserTests: XCTestCase {
99104
}
100105

101106
func testReturnParseWithMockData() {
102-
guard let path = Bundle(for: type(of: self)).path(forResource: "SimpleDocument", ofType: "xml"),
103-
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
104-
XCTFail("fail to parse")
105-
return
107+
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("SimpleDocument.xml"))) else {
108+
XCTFail("fail to parse")
109+
return
106110
}
107111

108112
let xml = XML.Parser().parse(data)
@@ -114,10 +118,9 @@ class ParserTests: XCTestCase {
114118
}
115119

116120
func testWhitespaceAndReturnParseWithMockData() {
117-
guard let path = Bundle(for: type(of: self)).path(forResource: "SimpleDocument", ofType: "xml"),
118-
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
119-
XCTFail("fail to parse")
120-
return
121+
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("SimpleDocument.xml"))) else {
122+
XCTFail("fail to parse")
123+
return
121124
}
122125

123126
let xml = XML.Parser().parse(data)
@@ -130,10 +133,9 @@ class ParserTests: XCTestCase {
130133
}
131134

132135
func testWhitespaceAndReturnParseWithMockDataAndTrimmingWhitespaceAndLineBreak() {
133-
guard let path = Bundle(for: type(of: self)).path(forResource: "SimpleDocument", ofType: "xml"),
134-
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
135-
XCTFail("fail to parse")
136-
return
136+
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("SimpleDocument.xml"))) else {
137+
XCTFail("fail to parse")
138+
return
137139
}
138140

139141
let xml = XML.Parser(trimming: .whitespacesAndNewlines).parse(data)
@@ -160,4 +162,19 @@ class ParserTests: XCTestCase {
160162
let xml = XML.Parser().parse(str.data(using: .utf8)!)
161163
XCTAssertEqual("@ß123\u{1c}", xml["xmlopening"].text?.removingPercentEncoding, "Parsed Success and trim them")
162164
}
165+
166+
func testLineNumbers() {
167+
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("SimpleDocument.xml"))) else {
168+
XCTFail("fail to parse")
169+
return
170+
}
171+
172+
let xml = XML.Parser().parse(data)
173+
guard let whitespaceReturnElement = xml["Result"]["WhitespaceReturn"].element else {
174+
XCTFail("Element not found")
175+
return
176+
}
177+
XCTAssertEqual(whitespaceReturnElement.lineNumberStart, 4)
178+
XCTAssertEqual(whitespaceReturnElement.lineNumberEnd, 6)
179+
}
163180
}

SwiftyXMLParserTests/XMLTests.swift

+17-14
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,41 @@ import XCTest
2626
@testable import SwiftyXMLParser
2727

2828
class XMLTests: XCTestCase {
29+
fileprivate let packageRootPath = URL(fileURLWithPath: #file)
30+
.pathComponents
31+
.dropLast()
32+
.joined(separator: "/")
33+
.dropFirst()
2934

3035
override func setUp() {
3136
super.setUp()
3237
}
33-
38+
3439
override func tearDown() {
3540
super.tearDown()
3641
}
3742

43+
private func getPath(_ name: String) -> String {
44+
"\(packageRootPath)/\(name)"
45+
}
46+
3847
func testParse() {
39-
if let path = Bundle(for: type(of: self)).path(forResource: "XMLDocument", ofType: "xml") {
40-
if let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
41-
let xml = XML.parse(data)
42-
if let _ = xml["ResultSet"].error {
43-
XCTFail("fail to parse")
48+
if let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("XMLDocument.xml"))) {
49+
let xml = XML.parse(data)
50+
if let _ = xml["ResultSet"].error {
51+
XCTFail("fail to parse")
4452

45-
} else {
46-
XCTAssert(true, "sucess to Parse")
47-
}
4853
} else {
49-
XCTFail("fail to generate data")
54+
XCTAssert(true, "sucess to Parse")
5055
}
5156
} else {
52-
XCTFail("fail to parse")
57+
XCTFail("fail to generate data")
5358
}
5459
}
5560

5661

5762
func testSuccessParseFromString() {
58-
if let path = Bundle(for: type(of: self)).path(forResource: "XMLDocument", ofType: "xml"),
59-
let string = try? String(contentsOfFile: path, encoding: String.Encoding.utf8),
63+
if let string = try? String(contentsOfFile: getPath("XMLDocument.xml"), encoding: String.Encoding.utf8),
6064
let xml = try? XML.parse(string) {
6165
if let _ = xml["ResultSet"].error {
6266
XCTFail("fail to parse")
@@ -81,5 +85,4 @@ class XMLTests: XCTestCase {
8185
XCTFail("Fail Parse")
8286
}
8387
}
84-
8588
}

0 commit comments

Comments
 (0)