Element.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. return 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. /// Returns all of the elements with equal name as `self` **(nil if not exists)**.
  61. open var all: [Element]? { return parent?.children.filter { $0.name == self.name } }
  62. /// Returns the first element with equal name as `self` **(nil if not exists)**.
  63. open var first: Element? { return all?.first }
  64. /// Returns the last element with equal name as `self` **(nil if not exists)**.
  65. open var last: Element? { return all?.last }
  66. /// Returns number of all elements with equal name as `self`.
  67. open var count: Int { return all?.count ?? 0 }
  68. /**
  69. Returns all elements with given value.
  70. - parameter value: XML element value.
  71. - returns: Optional Array of found XML elements.
  72. */
  73. open func all(withValue value: String) -> [Element]? {
  74. return all?.compactMap { $0.value == value ? $0 : nil }
  75. }
  76. /**
  77. Returns all elements containing given attributes.
  78. - parameter attributes: Array of attribute names.
  79. - returns: Optional Array of found XML elements.
  80. */
  81. open func all(containingAttributeKeys keys: [String]) -> [Element]? {
  82. return all?.compactMap { element in
  83. keys.reduce(true) { (result, key) in
  84. result && Array(element.attributes.keys).contains(key)
  85. } ? element : nil
  86. }
  87. }
  88. /**
  89. Returns all elements with given attributes.
  90. - parameter attributes: Dictionary of Keys and Values of attributes.
  91. - returns: Optional Array of found XML elements.
  92. */
  93. open func all(withAttributes attributes: [String : String]) -> [Element]? {
  94. let keys = Array(attributes.keys)
  95. return all(containingAttributeKeys: keys)?.compactMap { element in
  96. attributes.reduce(true) { (result, attribute) in
  97. result && element.attributes[attribute.key] == attribute.value
  98. } ? element : nil
  99. }
  100. }
  101. /**
  102. Returns all descendant elements which satisfy the given predicate.
  103. Searching is done vertically; children are tested before siblings. Elements appear in the list
  104. in the order in which they are found.
  105. - parameter predicate: Function which returns `true` for a desired element and `false` otherwise.
  106. - returns: Array of found XML elements.
  107. */
  108. open func allDescendants(where predicate: (Element) -> Bool) -> [Element] {
  109. var result: [Element] = []
  110. for child in children {
  111. if predicate(child) {
  112. result.append(child)
  113. }
  114. result.append(contentsOf: child.allDescendants(where: predicate))
  115. }
  116. return result
  117. }
  118. /**
  119. Returns the first descendant element which satisfies the given predicate, or nil if no such element is found.
  120. Searching is done vertically; children are tested before siblings.
  121. - parameter predicate: Function which returns `true` for the desired element and `false` otherwise.
  122. - returns: Optional Element.
  123. */
  124. open func firstDescendant(where predicate: (Element) -> Bool) -> Element? {
  125. for child in children {
  126. if predicate(child) {
  127. return child
  128. } else if let descendant = child.firstDescendant(where: predicate) {
  129. return descendant
  130. }
  131. }
  132. return nil
  133. }
  134. /**
  135. Indicates whether the element has a descendant satisfying the given predicate.
  136. - parameter predicate: Function which returns `true` for the desired element and `false` otherwise.
  137. - returns: Bool.
  138. */
  139. open func hasDescendant(where predicate: (Element) -> Bool) -> Bool {
  140. return firstDescendant(where: predicate) != nil
  141. }
  142. // MARK: - XML Write
  143. /**
  144. Adds child XML element to `self`.
  145. - parameter child: Child XML element to add.
  146. - returns: Child XML element with `self` as `parent`.
  147. */
  148. @discardableResult
  149. open func addChild(_ child: Element) -> Element {
  150. child.parent = self
  151. children.append(child)
  152. return child
  153. }
  154. /**
  155. Adds child XML element to `self`.
  156. - parameter name: Child XML element name.
  157. - parameter value: Child XML element value (defaults to `nil`).
  158. - parameter attributes: Child XML element attributes (defaults to empty dictionary).
  159. - returns: Child XML element with `self` as `parent`.
  160. */
  161. @discardableResult
  162. open func addChild(name: String,
  163. value: String? = nil,
  164. attributes: [String : String] = [:]) -> Element {
  165. let child = Element(name: name, value: value, attributes: attributes)
  166. return addChild(child)
  167. }
  168. /**
  169. Adds an array of XML elements to `self`.
  170. - parameter children: Child XML element array to add.
  171. - returns: Child XML elements with `self` as `parent`.
  172. */
  173. @discardableResult
  174. open func addChildren(_ children: [Element]) -> [Element] {
  175. children.forEach{ addChild($0) }
  176. return children
  177. }
  178. /// Removes `self` from `parent` XML element.
  179. open func removeFromParent() {
  180. if let index = parent?.children.firstIndex(where: { $0 === self }) {
  181. parent?.children.remove(at: index)
  182. }
  183. }
  184. /// Complete hierarchy of `self` and `children` in **XML** escaped and formatted String
  185. open var xml: String {
  186. var xml = String()
  187. // open element
  188. xml += indent(withDepth: parentsCount - 1)
  189. xml += "<\(name)"
  190. if attributes.count > 0 {
  191. // insert attributes
  192. for (key, value) in attributes.sorted(by: { $0.key < $1.key }) {
  193. xml += " \(key)=\"\(value.xmlEscaped)\""
  194. }
  195. }
  196. if value == nil && children.count == 0 {
  197. // close element
  198. xml += " />"
  199. } else {
  200. if children.count > 0 {
  201. // add children
  202. xml += ">\n"
  203. for child in children {
  204. xml += "\(child.xml)\n"
  205. }
  206. // add indentation
  207. xml += indent(withDepth: parentsCount - 1)
  208. xml += "</\(name)>"
  209. } else if let string = value {
  210. // insert string value and close element
  211. xml += ">\(string.xmlEscaped)</\(name)>"
  212. } else {
  213. // insert empty value and close element
  214. xml += "></\(name)>"
  215. }
  216. }
  217. return xml
  218. }
  219. /// Same as `xmlString` but without `\n` and `\t` characters
  220. open var xmlCompact: String {
  221. let chars = CharacterSet(charactersIn: "\n\t")
  222. return xml.components(separatedBy: chars).joined(separator: "")
  223. }
  224. /// Same as `xmlString` but with 4 spaces instead '\t' characters
  225. open var xmlSpaces: String {
  226. let chars = CharacterSet(charactersIn: "\t")
  227. return xml.components(separatedBy: chars).joined(separator: " ")
  228. }
  229. // MARK: - Helpers
  230. private var parentsCount: Int {
  231. var count = 0
  232. var element = self
  233. while let parent = element.parent {
  234. count += 1
  235. element = parent
  236. }
  237. return count
  238. }
  239. private func indent(withDepth depth: Int) -> String {
  240. var count = depth
  241. var indent = String()
  242. while count > 0 {
  243. indent += "\t"
  244. count -= 1
  245. }
  246. return indent
  247. }
  248. }
  249. public extension String {
  250. /// String representation of self with XML special characters escaped.
  251. var xmlEscaped: String {
  252. // we need to make sure "&" is escaped first. Not doing this may break escaping the other characters
  253. var escaped = replacingOccurrences(of: "&", with: "&amp;", options: .literal)
  254. // replace the other four special characters
  255. let escapeChars = ["<" : "&lt;", ">" : "&gt;", "'" : "&apos;", "\"" : "&quot;"]
  256. for (char, echar) in escapeChars {
  257. escaped = escaped.replacingOccurrences(of: char, with: echar, options: .literal)
  258. }
  259. return escaped
  260. }
  261. }