Element.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * https://github.com/tadija/AEXML
  3. * Copyright (c) Marko Tadić 2014-2019
  4. * Licensed under the MIT license. See LICENSE file.
  5. */
  6. import Foundation
  7. #if canImport(FoundationXML)
  8. import FoundationXML
  9. #endif
  10. /**
  11. This is base class for holding XML structure.
  12. You can access its structure by using subscript like this: `element["foo"]["bar"]` which would
  13. return `<bar></bar>` element from `<element><foo><bar></bar></foo></element>` XML as an `Element` object.
  14. */
  15. @dynamicMemberLookup
  16. open class Element {
  17. // MARK: - Properties
  18. /// Every `Element` should have its parent element instead of `XMLDocument` which parent is `nil`.
  19. open internal(set) weak var parent: Element?
  20. /// Child XML elements.
  21. open internal(set) var children = [Element]()
  22. /// XML Element name.
  23. open var name: String
  24. /// XML Element value.
  25. open var value: String?
  26. /// XML Element attributes.
  27. open var attributes: [String : String]
  28. /// Error value (`nil` if there is no error).
  29. open var error: XMLError?
  30. // MARK: - Init
  31. /**
  32. Designated initializer - all parameters are optional.
  33. - parameter name: XML element name.
  34. - parameter value: XML element value (defaults to `nil`).
  35. - parameter attributes: XML element attributes (defaults to empty dictionary).
  36. - returns: An initialized `Element` object.
  37. */
  38. public init(name: String, value: String? = nil, attributes: [String : String] = [:]) {
  39. self.name = name
  40. self.value = value
  41. self.attributes = attributes
  42. }
  43. // MARK: - XML Read
  44. /// The first element with given name **(Empty element with error if not exists)**.
  45. open subscript(key: String) -> Element {
  46. if let first = children.first(where: { $0.name == key }) {
  47. return first
  48. }
  49. let element = Element(name: key)
  50. element.error = XMLError.elementNotFound
  51. return element
  52. }
  53. public subscript(dynamicMember member: String) -> Element {
  54. self[member]
  55. }
  56. open func get<T>() throws -> T where T: LosslessStringConvertible {
  57. guard let string = value, let value = T(string) else { throw XMLError.valueConversionFailed }
  58. return value
  59. }
  60. open func date(formatter: DateFormatter) -> Date? {
  61. guard let value = value else { return nil }
  62. return formatter.date(from: value)
  63. }
  64. open func date(formatter: ISO8601DateFormatter = ISO8601DateFormatter()) -> Date? {
  65. guard let value = value else { return nil }
  66. return formatter.date(from: value)
  67. }
  68. /// Returns all of the elements with equal name as `self` **(nil if not exists)**.
  69. open var all: [Element]? { return parent?.children.filter { $0.name == self.name } }
  70. /// Returns the first element with equal name as `self` **(nil if not exists)**.
  71. open var first: Element? { return all?.first }
  72. /// Returns the last element with equal name as `self` **(nil if not exists)**.
  73. open var last: Element? { return all?.last }
  74. /// Returns number of all elements with equal name as `self`.
  75. open var count: Int { return all?.count ?? 0 }
  76. /**
  77. Returns all elements with given value.
  78. - parameter value: XML element value.
  79. - returns: Optional Array of found XML elements.
  80. */
  81. open func all(withValue value: String) -> [Element]? {
  82. return all?.compactMap { $0.value == value ? $0 : nil }
  83. }
  84. /**
  85. Returns all elements containing given attributes.
  86. - parameter attributes: Array of attribute names.
  87. - returns: Optional Array of found XML elements.
  88. */
  89. open func all(containingAttributeKeys keys: [String]) -> [Element]? {
  90. return all?.compactMap { element in
  91. keys.reduce(true) { (result, key) in
  92. result && Array(element.attributes.keys).contains(key)
  93. } ? element : nil
  94. }
  95. }
  96. /**
  97. Returns all elements with given attributes.
  98. - parameter attributes: Dictionary of Keys and Values of attributes.
  99. - returns: Optional Array of found XML elements.
  100. */
  101. open func all(withAttributes attributes: [String : String]) -> [Element]? {
  102. let keys = Array(attributes.keys)
  103. return all(containingAttributeKeys: keys)?.compactMap { element in
  104. attributes.reduce(true) { (result, attribute) in
  105. result && element.attributes[attribute.key] == attribute.value
  106. } ? element : nil
  107. }
  108. }
  109. /**
  110. Returns all descendant elements which satisfy the given predicate.
  111. Searching is done vertically; children are tested before siblings. Elements appear in the list
  112. in the order in which they are found.
  113. - parameter predicate: Function which returns `true` for a desired element and `false` otherwise.
  114. - returns: Array of found XML elements.
  115. */
  116. open func allDescendants(where predicate: (Element) -> Bool) -> [Element] {
  117. var result: [Element] = []
  118. for child in children {
  119. if predicate(child) {
  120. result.append(child)
  121. }
  122. result.append(contentsOf: child.allDescendants(where: predicate))
  123. }
  124. return result
  125. }
  126. /**
  127. Returns the first descendant element which satisfies the given predicate, or nil if no such element is found.
  128. Searching is done vertically; children are tested before siblings.
  129. - parameter predicate: Function which returns `true` for the desired element and `false` otherwise.
  130. - returns: Optional Element.
  131. */
  132. open func firstDescendant(where predicate: (Element) -> Bool) -> Element? {
  133. for child in children {
  134. if predicate(child) {
  135. return child
  136. } else if let descendant = child.firstDescendant(where: predicate) {
  137. return descendant
  138. }
  139. }
  140. return nil
  141. }
  142. /**
  143. Indicates whether the element has a descendant satisfying the given predicate.
  144. - parameter predicate: Function which returns `true` for the desired element and `false` otherwise.
  145. - returns: Bool.
  146. */
  147. open func hasDescendant(where predicate: (Element) -> Bool) -> Bool {
  148. return firstDescendant(where: predicate) != nil
  149. }
  150. // MARK: - XML Write
  151. /**
  152. Adds child XML element to `self`.
  153. - parameter child: Child XML element to add.
  154. - returns: Child XML element with `self` as `parent`.
  155. */
  156. @discardableResult
  157. open func addChild(_ child: Element) -> Element {
  158. child.parent = self
  159. children.append(child)
  160. return child
  161. }
  162. /**
  163. Adds child XML element to `self`.
  164. - parameter name: Child XML element name.
  165. - parameter value: Child XML element value (defaults to `nil`).
  166. - parameter attributes: Child XML element attributes (defaults to empty dictionary).
  167. - returns: Child XML element with `self` as `parent`.
  168. */
  169. @discardableResult
  170. open func addChild(name: String,
  171. value: String? = nil,
  172. attributes: [String : String] = [:]) -> Element {
  173. let child = Element(name: name, value: value, attributes: attributes)
  174. return addChild(child)
  175. }
  176. /**
  177. Adds an array of XML elements to `self`.
  178. - parameter children: Child XML element array to add.
  179. - returns: Child XML elements with `self` as `parent`.
  180. */
  181. @discardableResult
  182. open func addChildren(_ children: [Element]) -> [Element] {
  183. children.forEach{ addChild($0) }
  184. return children
  185. }
  186. /// Removes `self` from `parent` XML element.
  187. open func removeFromParent() {
  188. if let index = parent?.children.firstIndex(where: { $0 === self }) {
  189. parent?.children.remove(at: index)
  190. }
  191. }
  192. /// Complete hierarchy of `self` and `children` in **XML** escaped and formatted String
  193. open var xml: String {
  194. var xml = String()
  195. // open element
  196. xml += indent(withDepth: parentsCount - 1)
  197. xml += "<\(name)"
  198. if attributes.count > 0 {
  199. // insert attributes
  200. for (key, value) in attributes.sorted(by: { $0.key < $1.key }) {
  201. xml += " \(key)=\"\(value.xmlEscaped)\""
  202. }
  203. }
  204. if value == nil && children.count == 0 {
  205. // close element
  206. xml += " />"
  207. } else {
  208. if children.count > 0 {
  209. // add children
  210. xml += ">\n"
  211. for child in children {
  212. xml += "\(child.xml)\n"
  213. }
  214. // add indentation
  215. xml += indent(withDepth: parentsCount - 1)
  216. xml += "</\(name)>"
  217. } else if let string = value {
  218. // insert string value and close element
  219. xml += ">\(string.xmlEscaped)</\(name)>"
  220. } else {
  221. // insert empty value and close element
  222. xml += "></\(name)>"
  223. }
  224. }
  225. return xml
  226. }
  227. /// Same as `xmlString` but without `\n` and `\t` characters
  228. open var xmlCompact: String {
  229. let chars = CharacterSet(charactersIn: "\n\t")
  230. return xml.components(separatedBy: chars).joined(separator: "")
  231. }
  232. /// Same as `xmlString` but with 4 spaces instead '\t' characters
  233. open var xmlSpaces: String {
  234. let chars = CharacterSet(charactersIn: "\t")
  235. return xml.components(separatedBy: chars).joined(separator: " ")
  236. }
  237. // MARK: - Helpers
  238. private var parentsCount: Int {
  239. var count = 0
  240. var element = self
  241. while let parent = element.parent {
  242. count += 1
  243. element = parent
  244. }
  245. return count
  246. }
  247. private func indent(withDepth depth: Int) -> String {
  248. var count = depth
  249. var indent = String()
  250. while count > 0 {
  251. indent += "\t"
  252. count -= 1
  253. }
  254. return indent
  255. }
  256. }
  257. public extension String {
  258. /// String representation of self with XML special characters escaped.
  259. var xmlEscaped: String {
  260. // we need to make sure "&" is escaped first. Not doing this may break escaping the other characters
  261. var escaped = replacingOccurrences(of: "&", with: "&amp;", options: .literal)
  262. // replace the other four special characters
  263. let escapeChars = ["<" : "&lt;", ">" : "&gt;", "'" : "&apos;", "\"" : "&quot;"]
  264. for (char, echar) in escapeChars {
  265. escaped = escaped.replacingOccurrences(of: char, with: echar, options: .literal)
  266. }
  267. return escaped
  268. }
  269. }