Skip to content

Commit

Permalink
Merge pull request #63 from stevelandeyasana/line-numbers
Browse files Browse the repository at this point in the history
Store line numbers and CDATA on Element
  • Loading branch information
kazuhiro4949 authored Sep 5, 2021
2 parents b7beef4 + f1317db commit eec238e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ playground.xcworkspace
# Package.pins
# Package.resolved
.build/
.swiftpm/

# CocoaPods
#
Expand Down
3 changes: 3 additions & 0 deletions SwiftyXMLParser/Element.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ extension XML {
open var text: String?
open var attributes = [String: String]()
open var childElements = [Element]()
open var lineNumberStart = -1
open var lineNumberEnd = -1
open var CDATA: Data?

// for println
open weak var parentElement: Element?
Expand Down
6 changes: 6 additions & 0 deletions SwiftyXMLParser/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ extension XML {

func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
let node = Element(name: elementName)
node.lineNumberStart = parser.lineNumber
if !attributeDict.isEmpty {
node.attributes = attributeDict
}
Expand All @@ -80,8 +81,13 @@ extension XML {
stack.last?.text = "" + string
}
}

func parser(_ parser: XMLParser, foundCDATA CDATABlock: Data) {
stack.last?.CDATA = CDATABlock
}

func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
stack.last?.lineNumberEnd = parser.lineNumber
if let trimmingManner = self.trimmingManner {
stack.last?.text = stack.last?.text?.trimmingCharacters(in: trimmingManner)
}
Expand Down
73 changes: 45 additions & 28 deletions SwiftyXMLParserTests/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import XCTest


class ParserTests: XCTestCase {
fileprivate let packageRootPath = URL(fileURLWithPath: #file)
.pathComponents
.dropLast()
.joined(separator: "/")
.dropFirst()

override func setUp() {
super.setUp()
Expand All @@ -35,12 +40,15 @@ class ParserTests: XCTestCase {
override func tearDown() {
super.tearDown()
}

private func getPath(_ name: String) -> String {
"\(packageRootPath)/\(name)"
}

func testSuccessParse() {
guard let path = Bundle(for: type(of: self)).path(forResource: "XMLDocument", ofType: "xml"),
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
XCTFail("fail to parse")
return
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("XMLDocument.xml"))) else {
XCTFail("fail to parse")
return
}

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

func testFailParse() {
guard let path = Bundle(for: type(of: self)).path(forResource: "BrokenXMLDocument", ofType: "xml"),
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
XCTFail("fail to parse")
return
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("BrokenXMLDocument.xml"))) else {
XCTFail("fail to parse")
return
}

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

func testTextParseWithMockData() {
guard let path = Bundle(for: type(of: self)).path(forResource: "SimpleDocument", ofType: "xml"),
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
XCTFail("fail to parse")
return
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("SimpleDocument.xml"))) else {
XCTFail("fail to parse")
return
}

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

func testWhitespaceParseWithMockData() {
guard let path = Bundle(for: type(of: self)).path(forResource: "SimpleDocument", ofType: "xml"),
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
XCTFail("fail to parse")
return
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("SimpleDocument.xml"))) else {
XCTFail("fail to parse")
return
}

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

func testReturnParseWithMockData() {
guard let path = Bundle(for: type(of: self)).path(forResource: "SimpleDocument", ofType: "xml"),
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
XCTFail("fail to parse")
return
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("SimpleDocument.xml"))) else {
XCTFail("fail to parse")
return
}

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

func testWhitespaceAndReturnParseWithMockData() {
guard let path = Bundle(for: type(of: self)).path(forResource: "SimpleDocument", ofType: "xml"),
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
XCTFail("fail to parse")
return
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("SimpleDocument.xml"))) else {
XCTFail("fail to parse")
return
}

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

func testWhitespaceAndReturnParseWithMockDataAndTrimmingWhitespaceAndLineBreak() {
guard let path = Bundle(for: type(of: self)).path(forResource: "SimpleDocument", ofType: "xml"),
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
XCTFail("fail to parse")
return
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("SimpleDocument.xml"))) else {
XCTFail("fail to parse")
return
}

let xml = XML.Parser(trimming: .whitespacesAndNewlines).parse(data)
Expand All @@ -160,4 +162,19 @@ class ParserTests: XCTestCase {
let xml = XML.Parser().parse(str.data(using: .utf8)!)
XCTAssertEqual("@ß123\u{1c}", xml["xmlopening"].text?.removingPercentEncoding, "Parsed Success and trim them")
}

func testLineNumbers() {
guard let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("SimpleDocument.xml"))) else {
XCTFail("fail to parse")
return
}

let xml = XML.Parser().parse(data)
guard let whitespaceReturnElement = xml["Result"]["WhitespaceReturn"].element else {
XCTFail("Element not found")
return
}
XCTAssertEqual(whitespaceReturnElement.lineNumberStart, 4)
XCTAssertEqual(whitespaceReturnElement.lineNumberEnd, 6)
}
}
31 changes: 17 additions & 14 deletions SwiftyXMLParserTests/XMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,41 @@ import XCTest
@testable import SwiftyXMLParser

class XMLTests: XCTestCase {
fileprivate let packageRootPath = URL(fileURLWithPath: #file)
.pathComponents
.dropLast()
.joined(separator: "/")
.dropFirst()

override func setUp() {
super.setUp()
}

override func tearDown() {
super.tearDown()
}

private func getPath(_ name: String) -> String {
"\(packageRootPath)/\(name)"
}

func testParse() {
if let path = Bundle(for: type(of: self)).path(forResource: "XMLDocument", ofType: "xml") {
if let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
let xml = XML.parse(data)
if let _ = xml["ResultSet"].error {
XCTFail("fail to parse")
if let data = try? Data(contentsOf: URL(fileURLWithPath: getPath("XMLDocument.xml"))) {
let xml = XML.parse(data)
if let _ = xml["ResultSet"].error {
XCTFail("fail to parse")

} else {
XCTAssert(true, "sucess to Parse")
}
} else {
XCTFail("fail to generate data")
XCTAssert(true, "sucess to Parse")
}
} else {
XCTFail("fail to parse")
XCTFail("fail to generate data")
}
}


func testSuccessParseFromString() {
if let path = Bundle(for: type(of: self)).path(forResource: "XMLDocument", ofType: "xml"),
let string = try? String(contentsOfFile: path, encoding: String.Encoding.utf8),
if let string = try? String(contentsOfFile: getPath("XMLDocument.xml"), encoding: String.Encoding.utf8),
let xml = try? XML.parse(string) {
if let _ = xml["ResultSet"].error {
XCTFail("fail to parse")
Expand All @@ -81,5 +85,4 @@ class XMLTests: XCTestCase {
XCTFail("Fail Parse")
}
}

}

0 comments on commit eec238e

Please sign in to comment.