Skip to content

Commit

Permalink
refactor: split out XMLHashOptions to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
drmohundro committed May 3, 2022
1 parent 8bc4d1a commit d7aba5a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 35 deletions.
10 changes: 10 additions & 0 deletions SWXMLHash.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -176,6 +180,7 @@
63ED3C052821A723006A8A08 /* XMLIndexerDeserializable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XMLIndexerDeserializable.swift; sourceTree = "<group>"; };
63ED3C062821A723006A8A08 /* Bool+XMLDeserialization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Bool+XMLDeserialization.swift"; sourceTree = "<group>"; };
63ED3C072821A723006A8A08 /* Int+XMLDeserialization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Int+XMLDeserialization.swift"; sourceTree = "<group>"; };
63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XMLHashOptions.swift; sourceTree = "<group>"; };
6C0CE0F01D7440F8005F1248 /* LinuxShims.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinuxShims.swift; sourceTree = "<group>"; };
6C42BECC2051834B00137D31 /* shim.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = shim.swift; sourceTree = "<group>"; };
6C42BED4205183AF00137D31 /* shim.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = shim.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -295,6 +300,7 @@
CD6083F1196CA106000B4F8D /* Source */ = {
isa = PBXGroup;
children = (
63ED3C282821AF4D006A8A08 /* XMLHashOptions.swift */,
63ED3C062821A723006A8A08 /* Bool+XMLDeserialization.swift */,
63ED3C032821A723006A8A08 /* Double+XMLDeserialization.swift */,
6317F1A5282179E200F6C364 /* Float+XMLDeserialization.swift */,
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down
26 changes: 0 additions & 26 deletions Source/XMLHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions Source/XMLHashOptions.swift
Original file line number Diff line number Diff line change
@@ -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
}

40 changes: 31 additions & 9 deletions Source/XMLIndexer+XMLIndexerDeserializable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ public extension XMLIndexer {
func value<T: XMLAttributeDeserializable>(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):
Expand All @@ -102,9 +106,13 @@ public extension XMLIndexer {
func value<T: XMLAttributeDeserializable>(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:
Expand All @@ -123,9 +131,13 @@ public extension XMLIndexer {
func value<T: XMLAttributeDeserializable>(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):
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -483,7 +499,9 @@ extension XMLElement {
public func value<T: XMLAttributeDeserializable>(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
Expand All @@ -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)
Expand Down

0 comments on commit d7aba5a

Please sign in to comment.