|
| 1 | +import Foundation |
| 2 | +import pugixml |
| 3 | + |
| 4 | +public extension Document { |
| 5 | + /// A set of namespace names that are referenced in the document but have not been declared. |
| 6 | + /// |
| 7 | + /// - Note: This property helps identify undeclared namespaces that need to be resolved to generate XML output. |
| 8 | + var undeclaredNamespaceNames: Set<String> { |
| 9 | + Set(pendingNamespaceRecords.flatMap(\.value.namespaceNames)) |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +internal extension Document { |
| 14 | + typealias Prefix = NamespaceDeclaration.Prefix |
| 15 | + |
| 16 | + static let xmlNamespace = (prefix: Prefix("xml"), name: "http://www.w3.org/XML/1998/namespace") |
| 17 | + static let xmlnsNamespace = (prefix: Prefix("xmlns"), name: "http://www.w3.org/2000/xmlns/") |
| 18 | + |
| 19 | + struct NamespaceDeclaration { |
| 20 | + var node: pugi.xml_node |
| 21 | + var prefix: Prefix |
| 22 | + var namespaceName: String |
| 23 | + |
| 24 | + enum Prefix: Hashable { |
| 25 | + case defaultNamespace |
| 26 | + case named (String) |
| 27 | + |
| 28 | + init(_ string: String?) { |
| 29 | + self = if let string { .named(string) } else { .defaultNamespace } |
| 30 | + } |
| 31 | + |
| 32 | + var string: String? { |
| 33 | + switch self { |
| 34 | + case .named (let string): string |
| 35 | + case .defaultNamespace: nil |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + func namespaceDeclarationCount(for node: Node? = nil) -> Int { |
| 42 | + namespaceDeclarationsByPrefix.reduce(0) { result, item in |
| 43 | + result + item.value.filter { if let node { $0.node == node.node } else { true } }.count |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + func namespaceName(forPrefix prefix: Prefix, in element: pugi.xml_node) -> String? { |
| 48 | + if prefix == Self.xmlNamespace.prefix { return Self.xmlNamespace.name } |
| 49 | + if prefix == Self.xmlnsNamespace.prefix { return Self.xmlnsNamespace.name } |
| 50 | + |
| 51 | + guard let candidates = namespaceDeclarationsByPrefix[prefix] else { |
| 52 | + return nil |
| 53 | + } |
| 54 | + |
| 55 | + // Optimization: If the only candidate is the root element, then it's guaranteed to be right |
| 56 | + if candidates.count == 1, candidates[0].node == pugiDocument.documentElement { |
| 57 | + return candidates[0].namespaceName |
| 58 | + } |
| 59 | + |
| 60 | + var node = element |
| 61 | + while(!node.empty()) { |
| 62 | + for candidate in candidates where candidate.node == node { |
| 63 | + return candidate.namespaceName |
| 64 | + } |
| 65 | + node = node.parent() |
| 66 | + } |
| 67 | + return nil |
| 68 | + } |
| 69 | + |
| 70 | + func namespacePrefix(forName name: String, in element: pugi.xml_node) -> Prefix? { |
| 71 | + if name == Self.xmlNamespace.name { return Self.xmlNamespace.prefix } |
| 72 | + if name == Self.xmlnsNamespace.name { return Self.xmlnsNamespace.prefix } |
| 73 | + |
| 74 | + guard let candidates = namespaceDeclarationsByName[name], !candidates.isEmpty else { |
| 75 | + return nil |
| 76 | + } |
| 77 | + |
| 78 | + // Optimization: If the only candidate is the root element, and the found prefix |
| 79 | + // is the only declaration for that prefix (no shadowing), then we've found our match |
| 80 | + if candidates.count == 1, candidates[0].node == pugiDocument.documentElement { |
| 81 | + let prefix = candidates[0].prefix |
| 82 | + if namespaceDeclarationsByPrefix[prefix]?.count == 1 { |
| 83 | + return candidates[0].prefix |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + var node = element |
| 88 | + while(!node.empty()) { |
| 89 | + for candidate in candidates where candidate.node == node { |
| 90 | + if namespaceName(forPrefix: candidate.prefix, in: element) == name { |
| 91 | + return candidate.prefix |
| 92 | + } |
| 93 | + } |
| 94 | + node = node.parent() |
| 95 | + } |
| 96 | + return nil |
| 97 | + } |
| 98 | + |
| 99 | + func resetNamespaceDeclarationCache() { |
| 100 | + namespaceDeclarationsByName = [:] |
| 101 | + namespaceDeclarationsByPrefix = [:] |
| 102 | + } |
| 103 | + |
| 104 | + private func addNamespaceDeclarations(for node: pugi.xml_node) { |
| 105 | + for attribute in node.attributes { |
| 106 | + let name = attribute.name()! |
| 107 | + guard strncmp(name, "xmlns", 5) == 0 else { continue } |
| 108 | + |
| 109 | + let (prefix, localName) = name.qualifiedNameParts |
| 110 | + let declaration = NamespaceDeclaration( |
| 111 | + node: node, |
| 112 | + prefix: prefix == nil ? .defaultNamespace : .named(localName), |
| 113 | + namespaceName: String(cString: attribute.value()) |
| 114 | + ) |
| 115 | + namespaceDeclarationsByName[declaration.namespaceName, default: []].append(declaration) |
| 116 | + namespaceDeclarationsByPrefix[declaration.prefix, default: []].append(declaration) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private func removeNamespaceDeclarations(for nodes: Set<pugi.xml_node>) { |
| 121 | + namespaceDeclarationsByName = namespaceDeclarationsByName.mapValues { |
| 122 | + $0.filter { !nodes.contains($0.node) } |
| 123 | + } |
| 124 | + namespaceDeclarationsByPrefix = namespaceDeclarationsByPrefix.mapValues { |
| 125 | + $0.filter { !nodes.contains($0.node) } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + func removeNamespaceDeclarations(for tree: pugi.xml_node, excludingTarget: Bool = false) { |
| 130 | + let descendants = Set(tree.descendants.filter { $0.type() == pugi.node_element && (!excludingTarget || $0 != tree) }) |
| 131 | + removeNamespaceDeclarations(for: descendants) |
| 132 | + } |
| 133 | + |
| 134 | + func rebuildNamespaceDeclarationCache(for element: Node) { |
| 135 | + removeNamespaceDeclarations(for: [element.node]) |
| 136 | + addNamespaceDeclarations(for: element.node) |
| 137 | + } |
| 138 | + |
| 139 | + func rebuildNamespaceDeclarationCache() { |
| 140 | + resetNamespaceDeclarationCache() |
| 141 | + |
| 142 | + for node in pugiDocument.documentElement.descendants { |
| 143 | + guard node.type() == pugi.node_element else { continue } |
| 144 | + addNamespaceDeclarations(for: node) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + func declaredNamespacesDidChange(for element: Node) { |
| 149 | + rebuildNamespaceDeclarationCache(for: element) |
| 150 | + for (element, record) in pendingNameRecords(forDescendantsOf: element) { |
| 151 | + if record.attemptResolution(for: element, in: self) { |
| 152 | + removePendingNameRecord(for: element) |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments