Skip to content

Commit

Permalink
Clean up XMLElement init
Browse files Browse the repository at this point in the history
  • Loading branch information
drmohundro committed Jan 11, 2018
1 parent d010c0e commit a2511c1
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions Source/SWXMLHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,12 @@ extension XMLParserDelegate {
/// The implementation of XMLParserDelegate and where the lazy parsing actually happens.
class LazyXMLParser: NSObject, SimpleXmlParser, XMLParserDelegate {
required init(_ options: SWXMLHashOptions) {
root = XMLElement(name: rootElementName, options: options)
self.options = options
self.root.caseInsensitive = options.caseInsensitive
self.root.userInfo = options.userInfo
super.init()
}

var root = XMLElement(name: rootElementName, caseInsensitive: false)
let root: XMLElement
var parentStack = Stack<XMLElement>()
var elementStack = Stack<String>()

Expand All @@ -276,7 +275,6 @@ class LazyXMLParser: NSObject, SimpleXmlParser, XMLParserDelegate {
// clear any prior runs of parse... expected that this won't be necessary,
// but you never know
parentStack.removeAll()
root = XMLElement(name: rootElementName, caseInsensitive: options.caseInsensitive)
parentStack.push(root)

self.ops = ops
Expand Down Expand Up @@ -342,13 +340,12 @@ class LazyXMLParser: NSObject, SimpleXmlParser, XMLParserDelegate {
/// The implementation of XMLParserDelegate and where the parsing actually happens.
class FullXMLParser: NSObject, SimpleXmlParser, XMLParserDelegate {
required init(_ options: SWXMLHashOptions) {
root = XMLElement(name: rootElementName, options: options)
self.options = options
self.root.caseInsensitive = options.caseInsensitive
self.root.userInfo = options.userInfo
super.init()
}

var root = XMLElement(name: rootElementName, caseInsensitive: false)
let root: XMLElement
var parentStack = Stack<XMLElement>()
let options: SWXMLHashOptions

Expand Down Expand Up @@ -790,9 +787,13 @@ public class XMLElement: XMLContent {
public let name: String

/// Whether the element is case insensitive or not
public var caseInsensitive: Bool
public var caseInsensitive: Bool {
return options.caseInsensitive
}

var userInfo = [CodingUserInfoKey: Any]()
var userInfo: [CodingUserInfoKey: Any] {
return options.userInfo
}

/// All attributes
public var allAttributes = [String: XMLAttribute]()
Expand Down Expand Up @@ -834,6 +835,7 @@ public class XMLElement: XMLContent {

var count: Int = 0
var index: Int
let options: SWXMLHashOptions

var xmlChildren: [XMLElement] {
return children.flatMap { $0 as? XMLElement }
Expand All @@ -846,11 +848,10 @@ public class XMLElement: XMLContent {
- name: The name of the element to be initialized
- index: The index of the element to be initialized
*/
init(name: String, index: Int = 0, caseInsensitive: Bool, userInfo: [CodingUserInfoKey: Any] = [:]) {
init(name: String, index: Int = 0, options: SWXMLHashOptions) {
self.name = name
self.caseInsensitive = caseInsensitive
self.index = index
self.userInfo = userInfo
self.options = options
}

/**
Expand All @@ -863,7 +864,7 @@ public class XMLElement: XMLContent {
*/

func addElement(_ name: String, withAttributes attributes: [String: String], caseInsensitive: Bool) -> XMLElement {
let element = XMLElement(name: name, index: count, caseInsensitive: caseInsensitive, userInfo: userInfo)
let element = XMLElement(name: name, index: count, options: options)
count += 1

children.append(element)
Expand Down

0 comments on commit a2511c1

Please sign in to comment.