From d7aba5a687bcb68b827a8af4d5033a77ab7e1449 Mon Sep 17 00:00:00 2001 From: David Mohundro Date: Tue, 3 May 2022 13:41:21 -0500 Subject: [PATCH] refactor: split out XMLHashOptions to own file --- SWXMLHash.xcodeproj/project.pbxproj | 10 ++++ Source/XMLHash.swift | 26 --------- Source/XMLHashOptions.swift | 53 +++++++++++++++++++ .../XMLIndexer+XMLIndexerDeserializable.swift | 40 ++++++++++---- 4 files changed, 94 insertions(+), 35 deletions(-) create mode 100644 Source/XMLHashOptions.swift diff --git a/SWXMLHash.xcodeproj/project.pbxproj b/SWXMLHash.xcodeproj/project.pbxproj index e3a1699d..0e804ee9 100644 --- a/SWXMLHash.xcodeproj/project.pbxproj +++ b/SWXMLHash.xcodeproj/project.pbxproj @@ -51,6 +51,10 @@ 63ED3C252821A723006A8A08 /* Int+XMLDeserialization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C072821A723006A8A08 /* Int+XMLDeserialization.swift */; }; 63ED3C262821A723006A8A08 /* Int+XMLDeserialization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C072821A723006A8A08 /* Int+XMLDeserialization.swift */; }; 63ED3C272821A723006A8A08 /* Int+XMLDeserialization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C072821A723006A8A08 /* Int+XMLDeserialization.swift */; }; + 63ED3C292821AF4D006A8A08 /* XMLHashOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */; }; + 63ED3C2A2821AF4D006A8A08 /* XMLHashOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */; }; + 63ED3C2B2821AF4D006A8A08 /* XMLHashOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */; }; + 63ED3C2C2821AF4D006A8A08 /* XMLHashOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */; }; 6C42BED1205183A100137D31 /* shim.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C42BECC2051834B00137D31 /* shim.swift */; }; 6C42BED2205183A100137D31 /* shim.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C42BECC2051834B00137D31 /* shim.swift */; }; 6C42BED3205183A100137D31 /* shim.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C42BECC2051834B00137D31 /* shim.swift */; }; @@ -176,6 +180,7 @@ 63ED3C052821A723006A8A08 /* XMLIndexerDeserializable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XMLIndexerDeserializable.swift; sourceTree = ""; }; 63ED3C062821A723006A8A08 /* Bool+XMLDeserialization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Bool+XMLDeserialization.swift"; sourceTree = ""; }; 63ED3C072821A723006A8A08 /* Int+XMLDeserialization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Int+XMLDeserialization.swift"; sourceTree = ""; }; + 63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XMLHashOptions.swift; sourceTree = ""; }; 6C0CE0F01D7440F8005F1248 /* LinuxShims.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinuxShims.swift; sourceTree = ""; }; 6C42BECC2051834B00137D31 /* shim.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = shim.swift; sourceTree = ""; }; 6C42BED4205183AF00137D31 /* shim.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = shim.swift; sourceTree = ""; }; @@ -295,6 +300,7 @@ CD6083F1196CA106000B4F8D /* Source */ = { isa = PBXGroup; children = ( + 63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */, 63ED3C062821A723006A8A08 /* Bool+XMLDeserialization.swift */, 63ED3C032821A723006A8A08 /* Double+XMLDeserialization.swift */, 6317F1A5282179E200F6C364 /* Float+XMLDeserialization.swift */, @@ -669,6 +675,7 @@ 63ED3C202821A723006A8A08 /* Bool+XMLDeserialization.swift in Sources */, 6C42BED8205183B000137D31 /* shim.swift in Sources */, 6317F1A6282179E200F6C364 /* Float+XMLDeserialization.swift in Sources */, + 63ED3C292821AF4D006A8A08 /* XMLHashOptions.swift in Sources */, 63ED3C182821A723006A8A08 /* XMLElementDeserializable.swift in Sources */, CD60840C196CA11D000B4F8D /* XMLHash.swift in Sources */, 63ED3C0C2821A723006A8A08 /* XMLDeserialization.swift in Sources */, @@ -709,6 +716,7 @@ 63ED3C212821A723006A8A08 /* Bool+XMLDeserialization.swift in Sources */, 6C42BED5205183B000137D31 /* shim.swift in Sources */, 6317F1A7282179E200F6C364 /* Float+XMLDeserialization.swift in Sources */, + 63ED3C2A2821AF4D006A8A08 /* XMLHashOptions.swift in Sources */, 63ED3C192821A723006A8A08 /* XMLElementDeserializable.swift in Sources */, CD7934C61A7581F500867857 /* XMLHash.swift in Sources */, 63ED3C0D2821A723006A8A08 /* XMLDeserialization.swift in Sources */, @@ -749,6 +757,7 @@ 63ED3C222821A723006A8A08 /* Bool+XMLDeserialization.swift in Sources */, 6C42BED6205183B000137D31 /* shim.swift in Sources */, 6317F1A8282179E200F6C364 /* Float+XMLDeserialization.swift in Sources */, + 63ED3C2B2821AF4D006A8A08 /* XMLHashOptions.swift in Sources */, 63ED3C1A2821A723006A8A08 /* XMLElementDeserializable.swift in Sources */, CDEA72731C00B0D900C10B28 /* XMLHash.swift in Sources */, 63ED3C0E2821A723006A8A08 /* XMLDeserialization.swift in Sources */, @@ -789,6 +798,7 @@ 63ED3C232821A723006A8A08 /* Bool+XMLDeserialization.swift in Sources */, 6C42BED7205183B000137D31 /* shim.swift in Sources */, 6317F1A9282179E200F6C364 /* Float+XMLDeserialization.swift in Sources */, + 63ED3C2C2821AF4D006A8A08 /* XMLHashOptions.swift in Sources */, 63ED3C1B2821A723006A8A08 /* XMLElementDeserializable.swift in Sources */, CDEA72741C00B0E300C10B28 /* XMLHash.swift in Sources */, 63ED3C0F2821A723006A8A08 /* XMLDeserialization.swift in Sources */, diff --git a/Source/XMLHash.swift b/Source/XMLHash.swift index 5ea2acf6..b5dd073b 100644 --- a/Source/XMLHash.swift +++ b/Source/XMLHash.swift @@ -36,32 +36,6 @@ import FoundationXML let rootElementName = "SWXMLHash_Root_Element" -/// Parser options -public class XMLHashOptions { - internal init() {} - - /// determines whether to parse the XML with lazy parsing or not - public var shouldProcessLazily = false - - /// determines whether to parse XML namespaces or not (forwards to - /// `XMLParser.shouldProcessNamespaces`) - public var shouldProcessNamespaces = false - - /// Matching element names, element values, attribute names, attribute values - /// will be case insensitive. This will not affect parsing (data does not change) - public var caseInsensitive = false - - /// Encoding used for XML parsing. Default is set to UTF8 - public var encoding = String.Encoding.utf8 - - /// Any contextual information set by the user for encoding - public var userInfo = [CodingUserInfoKey: Any]() - - /// Detect XML parsing errors... defaults to false as this library will - /// attempt to handle HTML which isn't always XML-compatible - public var detectParsingErrors = false -} - /// Simple XML parser public class XMLHash { let options: XMLHashOptions diff --git a/Source/XMLHashOptions.swift b/Source/XMLHashOptions.swift new file mode 100644 index 00000000..8b49dd38 --- /dev/null +++ b/Source/XMLHashOptions.swift @@ -0,0 +1,53 @@ +// +// XMLHashOptions.swift +// SWXMLHash +// +// Copyright (c) 2022 David Mohundro +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +import Foundation + +/// Parser options +public class XMLHashOptions { + internal init() {} + + /// determines whether to parse the XML with lazy parsing or not + public var shouldProcessLazily = false + + /// determines whether to parse XML namespaces or not (forwards to + /// `XMLParser.shouldProcessNamespaces`) + public var shouldProcessNamespaces = false + + /// Matching element names, element values, attribute names, attribute values + /// will be case insensitive. This will not affect parsing (data does not change) + public var caseInsensitive = false + + /// Encoding used for XML parsing. Default is set to UTF8 + public var encoding = String.Encoding.utf8 + + /// Any contextual information set by the user for encoding + public var userInfo = [CodingUserInfoKey: Any]() + + /// Detect XML parsing errors... defaults to false as this library will + /// attempt to handle HTML which isn't always XML-compatible + public var detectParsingErrors = false +} + diff --git a/Source/XMLIndexer+XMLIndexerDeserializable.swift b/Source/XMLIndexer+XMLIndexerDeserializable.swift index 8adecd36..96ddfb90 100644 --- a/Source/XMLIndexer+XMLIndexerDeserializable.swift +++ b/Source/XMLIndexer+XMLIndexerDeserializable.swift @@ -79,9 +79,13 @@ public extension XMLIndexer { func value(ofAttribute attr: String) throws -> [T] { switch self { case .list(let elements): - return try elements.map { try $0.value(ofAttribute: attr) } + return try elements.map { + try $0.value(ofAttribute: attr) + } case .element(let element): - return try [element].map { try $0.value(ofAttribute: attr) } + return try [element].map { + try $0.value(ofAttribute: attr) + } case .stream(let opStream): return try opStream.findElements().value(ofAttribute: attr) case .xmlError(let indexingError): @@ -102,9 +106,13 @@ public extension XMLIndexer { func value(ofAttribute attr: String) throws -> [T]? { switch self { case .list(let elements): - return try elements.map { try $0.value(ofAttribute: attr) } + return try elements.map { + try $0.value(ofAttribute: attr) + } case .element(let element): - return try [element].map { try $0.value(ofAttribute: attr) } + return try [element].map { + try $0.value(ofAttribute: attr) + } case .stream(let opStream): return try opStream.findElements().value(ofAttribute: attr) default: @@ -123,9 +131,13 @@ public extension XMLIndexer { func value(ofAttribute attr: String) throws -> [T?] { switch self { case .list(let elements): - return elements.map { $0.value(ofAttribute: attr) } + return elements.map { + $0.value(ofAttribute: attr) + } case .element(let element): - return [element].map { $0.value(ofAttribute: attr) } + return [element].map { + $0.value(ofAttribute: attr) + } case .stream(let opStream): return try opStream.findElements().value(ofAttribute: attr) case .xmlError(let indexingError): @@ -306,13 +318,13 @@ public extension XMLIndexer { switch self { case .list(let elements): return try elements.map { - let deserialized = try T.deserialize( XMLIndexer($0) ) + let deserialized = try T.deserialize(XMLIndexer($0)) try deserialized.validate() return deserialized } case .element(let element): return try [element].map { - let deserialized = try T.deserialize( XMLIndexer($0) ) + let deserialized = try T.deserialize(XMLIndexer($0)) try deserialized.validate() return deserialized } @@ -380,7 +392,11 @@ public extension XMLIndexer { throw XMLDeserializationError.nodeIsInvalid(node: "Unexpected error deserializing XMLIndexer -> [T?]") } } +} +/*: Provides XMLIndexer XMLAttributeDeserializable deserialization from String backed RawRepresentables + Added by [PeeJWeeJ](https://github.com/PeeJWeeJ) */ +public extension XMLIndexer { /** Attempts to deserialize the value of the specified attribute of the current XMLIndexer element to `T` using a String backed RawRepresentable (E.g. `String` backed `enum` cases) @@ -483,7 +499,9 @@ extension XMLElement { public func value(ofAttribute attr: String) -> T? { if let attr = self.attribute(by: attr) { let deserialized = try? T.deserialize(attr) - if deserialized != nil { try? deserialized?.validate() } + if deserialized != nil { + try? deserialized?.validate() + } return deserialized } else { return nil @@ -504,7 +522,11 @@ extension XMLElement { throw XMLDeserializationError.nodeHasNoValue } +} +/*: Provides XMLIndexer XMLAttributeDeserializable deserialization from String backed RawRepresentables + Added by [PeeJWeeJ](https://github.com/PeeJWeeJ) */ +public extension XMLElement { /** Attempts to deserialize the specified attribute of the current XMLElement to `T` using a String backed RawRepresentable (E.g. `String` backed `enum` cases)