@@ -50,6 +50,9 @@ public class SWXMLHashOptions {
50
50
51
51
/// Encoding used for XML parsing. Default is set to UTF8
52
52
public var encoding = String . Encoding. utf8
53
+
54
+ /// Any contextual information set by the user for encoding
55
+ public var userInfo = [ CodingUserInfoKey: Any] ( )
53
56
}
54
57
55
58
/// Simple XML parser
@@ -250,12 +253,12 @@ extension XMLParserDelegate {
250
253
/// The implementation of XMLParserDelegate and where the lazy parsing actually happens.
251
254
class LazyXMLParser : NSObject , SimpleXmlParser , XMLParserDelegate {
252
255
required init ( _ options: SWXMLHashOptions ) {
256
+ root = XMLElement ( name: rootElementName, options: options)
253
257
self . options = options
254
- self . root. caseInsensitive = options. caseInsensitive
255
258
super. init ( )
256
259
}
257
260
258
- var root = XMLElement ( name : rootElementName , caseInsensitive : false )
261
+ let root : XMLElement
259
262
var parentStack = Stack < XMLElement > ( )
260
263
var elementStack = Stack < String > ( )
261
264
@@ -272,7 +275,6 @@ class LazyXMLParser: NSObject, SimpleXmlParser, XMLParserDelegate {
272
275
// clear any prior runs of parse... expected that this won't be necessary,
273
276
// but you never know
274
277
parentStack. removeAll ( )
275
- root = XMLElement ( name: rootElementName, caseInsensitive: options. caseInsensitive)
276
278
parentStack. push ( root)
277
279
278
280
self . ops = ops
@@ -338,12 +340,12 @@ class LazyXMLParser: NSObject, SimpleXmlParser, XMLParserDelegate {
338
340
/// The implementation of XMLParserDelegate and where the parsing actually happens.
339
341
class FullXMLParser : NSObject , SimpleXmlParser , XMLParserDelegate {
340
342
required init ( _ options: SWXMLHashOptions ) {
343
+ root = XMLElement ( name: rootElementName, options: options)
341
344
self . options = options
342
- self . root. caseInsensitive = options. caseInsensitive
343
345
super. init ( )
344
346
}
345
347
346
- var root = XMLElement ( name : rootElementName , caseInsensitive : false )
348
+ let root : XMLElement
347
349
var parentStack = Stack < XMLElement > ( )
348
350
let options : SWXMLHashOptions
349
351
@@ -556,6 +558,15 @@ public enum XMLIndexer {
556
558
return list
557
559
}
558
560
561
+ public var userInfo : [ CodingUserInfoKey : Any ] {
562
+ switch self {
563
+ case . element( let elem) :
564
+ return elem. userInfo
565
+ default :
566
+ return [ : ]
567
+ }
568
+ }
569
+
559
570
/**
560
571
Allows for element lookup by matching attribute values.
561
572
@@ -775,11 +786,19 @@ public class XMLElement: XMLContent {
775
786
/// The name of the element
776
787
public let name : String
777
788
778
- public var caseInsensitive : Bool
789
+ /// Whether the element is case insensitive or not
790
+ public var caseInsensitive : Bool {
791
+ return options. caseInsensitive
792
+ }
793
+
794
+ var userInfo : [ CodingUserInfoKey : Any ] {
795
+ return options. userInfo
796
+ }
779
797
780
798
/// All attributes
781
799
public var allAttributes = [ String: XMLAttribute] ( )
782
800
801
+ /// Find an attribute by name
783
802
public func attribute( by name: String ) -> XMLAttribute ? {
784
803
if caseInsensitive {
785
804
return allAttributes. first ( where: { $0. key. compare ( name, true ) } ) ? . value
@@ -813,8 +832,10 @@ public class XMLElement: XMLContent {
813
832
814
833
/// All child elements (text or XML)
815
834
public var children = [ XMLContent] ( )
835
+
816
836
var count : Int = 0
817
837
var index : Int
838
+ let options : SWXMLHashOptions
818
839
819
840
var xmlChildren : [ XMLElement ] {
820
841
return children. flatMap { $0 as? XMLElement }
@@ -827,10 +848,10 @@ public class XMLElement: XMLContent {
827
848
- name: The name of the element to be initialized
828
849
- index: The index of the element to be initialized
829
850
*/
830
- init ( name: String , index: Int = 0 , caseInsensitive : Bool ) {
851
+ init ( name: String , index: Int = 0 , options : SWXMLHashOptions ) {
831
852
self . name = name
832
- self . caseInsensitive = caseInsensitive
833
853
self . index = index
854
+ self . options = options
834
855
}
835
856
836
857
/**
@@ -843,7 +864,7 @@ public class XMLElement: XMLContent {
843
864
*/
844
865
845
866
func addElement( _ name: String , withAttributes attributes: [ String : String ] , caseInsensitive: Bool ) -> XMLElement {
846
- let element = XMLElement ( name: name, index: count, caseInsensitive : caseInsensitive )
867
+ let element = XMLElement ( name: name, index: count, options : options )
847
868
count += 1
848
869
849
870
children. append ( element)
0 commit comments