Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Sources/DocDOCX/DocxImporter.swift3// Description: WordprocessingML import, entry point and package level: opens the archive defensively, locates the parts,4// reads properties, settings, theme fonts and the ORIX extras, classifies every part as interpreted, preserved or dropped.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import DocModel10import DocContainer1112public enum DocxImporter {13 /// Imports a DOCX (or the document part of a DOCM / DOTX). The input is treated as hostile: archive and XML limits apply,14 /// nothing outside the package is ever read, macros and embedded objects are dropped and reported.15 public static func importDocument(_ data: Data, limits: ZipLimits = .standard) throws -> ImportResult {16 try DocxStack.run {17 do {18 let zip = try ZipReader(data: data, limits: limits)19 let ctx = DocxImportContext(zip: zip)20 try ctx.run()21 return ImportResult(document: ctx.doc, warnings: ctx.warnings.warnings)22 } catch let e as ZipError {23 throw DocxImporter.formatError(e)24 }25 }26 }2728 static func formatError(_ e: ZipError) -> FormatError {29 switch e {30 case .tooManyEntries, .entryTooLarge, .archiveTooLarge, .suspiciousCompressionRatio: return .tooLarge(e.description)31 case .checksumMismatch: return .integrityCheckFailed(e.description)32 default: return .unreadable(e.description)33 }34 }35}3637/// State of one import. Not shared across threads; lives for the duration of `importDocument`.38final class DocxImportContext {39 let zip: ZipReader40 var warnings = WarningCollector()41 var doc = DocumentModel(styles: StyleSheet(defaultChar: .none, defaultPara: .none, styles: [], themeName: "Imported"), body: [])42 var contentTypes = ContentTypes()43 var mainPart = "word/document.xml"44 var mainRels: [Relationship] = []45 /// Parts that were interpreted (and therefore are neither preserved nor reported as dropped).46 var consumed = Set<String>()4748 // Styles49 var themeFonts: [String: String] = [:]50 var styleIDMap: [String: String] = [:]51 var styleKinds: [String: Style.Kind] = [:]52 var defaultParaStyle = StyleID.normal53 var tableStyles: [String: ImportedTableStyle] = [:]54 var defaultTableStyle: String?55 /// Paragraph style id (model) → numbering reference found in the style.56 var styleNumbering: [String: (numId: String, level: Int)] = [:]57 // Numbering58 var numToList: [String: String] = [:]59 // Notes and comments60 var footnoteNodes: [String: XMLNode] = [:]61 var endnoteNodes: [String: XMLNode] = [:]62 var footnoteRels: [String: Relationship] = [:]63 var endnoteRels: [String: Relationship] = [:]64 var noteIDs: [String: String] = [:] // "f12" / "e3" → model note id65 var commentIDs: [String: String] = [:] // w:id → model comment id (top-level comments only)66 var replyCommentIDs = Set<String>()67 var commentAnchored = Set<String>()68 // Cross-references69 var bookmarkDisplay: [String: CrossRef.Display] = [:]70 var usedParagraphIDs = Set<String>()71 var assetByPart: [String: String] = [:]72 var evenAndOddHeaders = false73 var hasExtras = false74 var headingNumIDs = Set<String>()75 var tableSeparatorIDs = Set<String>()76 var knownBookmarks = Set<String>()77 var sectPrNodes: [XMLNode] = []78 var sectionBreakIDs: [String] = []79 var pendingBlockRuns: [Run] = []80 var headerCache: [String: [Paragraph]] = [:]81 var watermarkByPart: [String: String] = [:]8283 init(zip: ZipReader) { self.zip = zip }8485 func warn(_ s: FormatWarning.Severity, _ feature: String, _ detail: String) { warnings.add(s, feature, detail) }8687 func partData(_ name: String) throws -> Data? { try zip.data(for: name) }8889 func xml(_ name: String) throws -> XMLNode? {90 guard let d = try partData(name) else { return nil }91 return try XMLTree.parse(d, part: name)92 }9394 func relationships(of part: String) throws -> [Relationship] {95 let path = OPC.relsPath(for: part)96 consumed.insert(path)97 return try OPC.parseRelationships(try partData(path), part: part)98 }99100 // MARK: Run101102 func run() throws {103 guard zip.contains("[Content_Types].xml") else { throw FormatError.unreadable("not an Office Open XML package ([Content_Types].xml is missing)") }104 contentTypes = try ContentTypes(try partData("[Content_Types].xml"))105 consumed.insert("[Content_Types].xml")106107 let rootRels = try relationships(of: "")108 if let rel = rootRels.first(where: { $0.kind == "officeDocument" && !$0.external }), let p = OPC.resolve(target: rel.target, from: ""), zip.contains(p) {109 mainPart = p110 }111 guard let root = try xml(mainPart) else { throw FormatError.unreadable("the main document part is missing") }112 guard root.name == "w:document", let body = root.first("w:body") else {113 throw FormatError.unreadable("the main part is not a WordprocessingML document")114 }115 consumed.insert(mainPart)116 if let t = contentTypes.type(of: mainPart), t.contains("macroEnabled") {117 warn(.dropped, "VBA macros", "This is a macro-enabled document; its macros are not imported and never run.")118 }119 if let t = contentTypes.type(of: mainPart), t.contains("template") {120 warn(.info, "Templates", "The file is a Word template; it is imported as an ordinary document.")121 }122 mainRels = try relationships(of: mainPart)123124 func related(_ kind: String) -> String? {125 guard let rel = mainRels.first(where: { $0.kind == kind && !$0.external }) else { return nil }126 guard let p = OPC.resolve(target: rel.target, from: mainPart) else {127 warn(.dropped, "Unsafe relationship", "A \(kind) relationship points outside the package and was ignored.")128 return nil129 }130 return zip.contains(p) ? p : nil131 }132133 try importTheme(related("theme"))134 try importExtras()135 if let p = related("settings"), let n = try xml(p) { consumed.insert(p); importSettings(n) }136 if let p = related("styles"), let n = try xml(p) { consumed.insert(p); importStyles(n) }137 ensureNormalStyle()138 if let p = related("numbering"), let n = try xml(p) {139 consumed.insert(p)140 if try relationships(of: p).contains(where: { $0.kind == "image" }) {141 warn(.approximated, "Picture bullets", "Picture bullets are replaced by text bullets.")142 }143 importNumbering(n)144 }145 if let p = related("footnotes"), let n = try xml(p) {146 consumed.insert(p)147 footnoteRels = Dictionary(try relationships(of: p).map { ($0.id, $0) }, uniquingKeysWith: { a, _ in a })148 for f in n.all("w:footnote") { if let id = f.attr("w:id"), f.attr("w:type") == nil || f.attr("w:type") == "normal" { footnoteNodes[id] = f } }149 notePart[.footnote] = p150 }151 if let p = related("endnotes"), let n = try xml(p) {152 consumed.insert(p)153 endnoteRels = Dictionary(try relationships(of: p).map { ($0.id, $0) }, uniquingKeysWith: { a, _ in a })154 for f in n.all("w:endnote") { if let id = f.attr("w:id"), f.attr("w:type") == nil || f.attr("w:type") == "normal" { endnoteNodes[id] = f } }155 notePart[.endnote] = p156 }157 if let p = related("comments"), let n = try xml(p) {158 consumed.insert(p)159 _ = try relationships(of: p)160 var ext: XMLNode?161 if let e = related("commentsExtended") { consumed.insert(e); _ = try relationships(of: e); ext = try xml(e) }162 importComments(n, extended: ext)163 }164 for kind in ["commentsIds", "commentsExtensible", "people"] {165 if let p = related(kind) { consumed.insert(p); _ = try relationships(of: p) }166 }167168 try importBody(root: root, body: body)169 try importCoreProperties(rootRels)170 try classifyRemainingParts(rootRels)171172 if doc.body.isEmpty { doc.body = [.paragraph(Paragraph(style: defaultParaStyle))] }173 for c in doc.comments where !commentAnchored.contains(c.id) {174 warn(.approximated, "Comments", "The comment by \(c.author) has no anchor in the text; it is kept without a location.")175 }176 }177178 var notePart: [NoteRef.Kind: String] = [:]179180 // MARK: Theme, extras, settings, properties181182 func importTheme(_ part: String?) throws {183 guard let part, let theme = try xml(part) else { return }184 // The theme itself stays an opaque preserved part; only its font pairing is read to resolve theme font references.185 if let fonts = theme.firstDescendant("a:fontScheme") {186 if let major = fonts.path("a:majorFont", "a:latin")?.attr("typeface"), !major.isEmpty { themeFonts["major"] = major }187 if let minor = fonts.path("a:minorFont", "a:latin")?.attr("typeface"), !minor.isEmpty { themeFonts["minor"] = minor }188 }189 }190191 func importExtras() throws {192 for name in zip.names.sorted() where name.hasPrefix("customXml/item") && name.hasSuffix(".xml") && !name.contains("Props") {193 guard let entry = zip.entries.first(where: { $0.name == name }), entry.uncompressedSize < 48 * 1024 * 1024,194 let data = try partData(name), data.range(of: Data(XMLNS.orix.utf8)) != nil,195 let root = try? XMLTree.parse(data, part: name), let extras = OrixExtras.parse(root) else { continue }196 hasExtras = true197 consumed.insert(name)198 let relsPath = OPC.relsPath(for: name)199 for rel in (try? OPC.parseRelationships(try partData(relsPath), part: name)) ?? [] {200 if let p = OPC.resolve(target: rel.target, from: name) { consumed.insert(p) }201 }202 consumed.insert(relsPath)203 if let s = extras.citationStyle { doc.settings.citationStyle = s }204 if let v = extras.smartQuotes { doc.settings.smartQuotes = v }205 if let v = extras.smartDashes { doc.settings.smartDashes = v }206 if let t = extras.themeName { doc.styles.themeName = t }207 var seen = Set<String>()208 doc.bibliography = extras.bibliography.filter { seen.insert($0.key).inserted }209 return210 }211 }212213 func importSettings(_ n: XMLNode) {214 if docxOnOff(n.first("w:trackRevisions")) == true { doc.settings.trackChanges = true }215 if docxOnOff(n.first("w:evenAndOddHeaders")) == true { evenAndOddHeaders = true }216 if n.first("w:documentProtection") != nil || n.first("w:writeProtection") != nil {217 warn(.dropped, "Document protection", "Editing restrictions and write protection are not carried over.")218 }219 if n.first("w:mailMerge") != nil { warn(.dropped, "Mail merge", "The mail merge data source link is not imported.") }220 if n.first("w:attachedTemplate") != nil { warn(.info, "Attached template", "The link to the document template is not followed and is removed.") }221 }222223 func importCoreProperties(_ rootRels: [Relationship]) throws {224 var part = "docProps/core.xml"225 if let rel = rootRels.first(where: { $0.kind == "core-properties" && !$0.external }), let p = OPC.resolve(target: rel.target, from: "") { part = p }226 if let lang = doc.styles.defaultChar.language { doc.meta.language = lang }227 guard zip.contains(part), let n = try xml(part) else {228 doc.meta.created = DocxDate.unknown; doc.meta.modified = DocxDate.unknown229 return230 }231 consumed.insert(part)232 doc.meta.title = n.first("dc:title")?.text ?? ""233 doc.meta.author = n.first("dc:creator")?.text ?? ""234 doc.meta.subject = n.first("dc:subject")?.text ?? ""235 let kw = n.first("cp:keywords")?.text ?? ""236 doc.meta.keywords = kw.split(whereSeparator: { $0 == "," || $0 == ";" }).map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }237 if let l = n.first("dc:language")?.text.trimmingCharacters(in: .whitespaces), !l.isEmpty { doc.meta.language = l }238 doc.meta.created = DocxDate.parse(n.first("dcterms:created")?.text) ?? DocxDate.unknown239 doc.meta.modified = DocxDate.parse(n.first("dcterms:modified")?.text) ?? doc.meta.created240 }241242 // MARK: Preserved and dropped parts243244 static func isPreservable(_ name: String) -> Bool {245 if name == "docProps/app.xml" || name == "docProps/custom.xml" || name == "word/fontTable.xml" || name == "word/webSettings.xml" { return true }246 if name.hasPrefix("word/theme/") && name.hasSuffix(".xml") && !name.contains("_rels/") { return true }247 if name.hasPrefix("customXml/item") && name.hasSuffix(".xml") { return true }248 return false249 }250251 func classifyRemainingParts(_ rootRels: [Relationship]) throws {252 var preserved: [String: Data] = [:]253 let names = zip.names.sorted()254 for name in names where !consumed.contains(name) && DocxImportContext.isPreservable(name) {255 guard let entry = zip.entries.first(where: { $0.name == name }), entry.uncompressedSize <= 32 * 1024 * 1024 else {256 warn(.dropped, "Package parts", "\(name) is too large to keep and was dropped.")257 consumed.insert(name)258 continue259 }260 guard let data = try partData(name) else { continue }261 // A preserved part must be self-contained: no relationship that leaves the set of preserved parts, no DTD.262 guard (try? XMLTree.parse(data, part: name)) != nil else {263 warn(.dropped, "Package parts", "\(name) is not well-formed and was dropped.")264 consumed.insert(name)265 continue266 }267 let relsPath = OPC.relsPath(for: name)268 if zip.contains(relsPath) {269 let rels = (try? OPC.parseRelationships(try partData(relsPath), part: name)) ?? []270 let targets = rels.map { $0.external ? nil : OPC.resolve(target: $0.target, from: name) }271 let ok = !rels.isEmpty && targets.allSatisfy { $0 != nil && DocxImportContext.isPreservable($0!) && zip.contains($0!) && !consumed.contains($0!) }272 if ok, let relData = try partData(relsPath) {273 preserved[relsPath] = relData274 } else if !rels.isEmpty {275 let what = name == "word/fontTable.xml" ? "Embedded fonts" : "Package parts"276 warn(.dropped, what, "\(name) references content that cannot be kept safely; it was dropped.")277 consumed.insert(name); consumed.insert(relsPath)278 continue279 }280 consumed.insert(relsPath)281 }282 preserved[name] = data283 consumed.insert(name)284 }285 if !preserved.isEmpty {286 doc.preservedParts = preserved287 let n = preserved.keys.filter { !$0.contains("_rels/") }.count288 warn(.info, "Preserved parts", "\(n) package part(s) that ORIX Docs does not interpret (theme, font table, custom XML, extended properties) are kept unchanged for DOCX export.")289 }290 // Everything else is dropped; say what it was.291 var other = 0292 for name in names where !consumed.contains(name) {293 let lower = name.lowercased()294 let type = (contentTypes.type(of: name) ?? "").lowercased()295 if lower.contains("/embeddings/") || type.contains("oleobject") {296 warn(.dropped, "Embedded objects", "The embedded object \(name) is not imported and never opened.")297 } else if lower.contains("vbaproject") || lower.contains("vbadata") || type.contains("vba") || type.contains("macro") {298 warn(.dropped, "VBA macros", "The macro project (\(name)) is not imported and never run.")299 } else if lower.contains("/activex/") || type.contains("activex") {300 warn(.dropped, "ActiveX controls", "The ActiveX control data (\(name)) is not imported and never run.")301 } else if lower.contains("/charts/") {302 if !lower.contains("_rels/") && (lower.hasSuffix(".xml")) && !lower.contains("style") && !lower.contains("colors") { warn(.dropped, "Charts", "The chart \(name) is not imported.") }303 } else if lower.contains("/diagrams/") {304 if lower.contains("/data") { warn(.dropped, "SmartArt", "The SmartArt diagram \(name) is not imported.") }305 } else if lower.contains("/glossary/") {306 if lower.hasSuffix("glossary/document.xml") { warn(.dropped, "Building blocks", "The glossary (building blocks, AutoText) is not imported.") }307 } else if lower.contains("/media/") {308 warn(.info, "Unused media", "The media file \(name) is not referenced by the content that was imported and was left out.")309 } else if lower.contains("/fonts/") {310 warn(.dropped, "Embedded fonts", "The embedded font \(name) is not imported.")311 } else if lower.hasSuffix(".rels") || lower.hasPrefix("docprops/thumbnail") || lower.contains("stylesWithEffects".lowercased()) {312 continue313 } else if lower.hasPrefix("_xmlsignatures/") || type.contains("digital-signature") {314 warn(.dropped, "Digital signatures", "The digital signature is not imported; an edited copy could not keep it valid.")315 } else if lower.contains("customui") || lower.contains("attachedtoolbars") || lower.contains("keymapcustomizations") {316 warn(.dropped, "Interface customisations", "\(name) is not imported.")317 } else {318 other += 1319 if other <= 5 { warn(.dropped, "Package parts", "The part \(name) is not interpreted and was dropped.") }320 }321 }322 if other > 5 { warn(.dropped, "Package parts", "\(other - 5) more uninterpreted part(s) were dropped.") }323 }324}325