-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcpdump.swift
executable file
·63 lines (52 loc) · 1.67 KB
/
fcpdump.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/swift
import Foundation
guard CommandLine.argc == 2 else {
print("Usage: \(CommandLine.arguments[0]) <FCPXML file>")
exit(1)
}
/* read XML document */
let url = URL(fileURLWithPath: CommandLine.arguments[1])
let xml = try XMLDocument(contentsOf: url)
/* delete nodes that change non-deterministically between exports */
// FIXME: check what’s really going on here
let changingPaths = "//@format|//@id|//@ref" + "|" +
"/fcpxml/resources/asset/bookmark"
try xml.nodes(forXPath: changingPaths).forEach { $0.detach() }
/* sort nodes whose child nodes flip non-deterministically */
let flippingPaths = "//clip|//sync-clip" + "|" +
"/fcpxml/resources|/fcpxml/library/event"
try xml.nodes(forXPath: flippingPaths).forEach { node in
var elements: [XMLElement] = []
node.children?.forEach {
if $0.kind == .element {
elements.append($0 as! XMLElement)
}
}
elements.sort { $0.xmlString < $1.xmlString }
(node as! XMLElement).setChildren(elements)
}
/* dump document in normalized form */
func dump(node: XMLNode, prefix: String) {
switch node.kind {
case .document:
node.children?.forEach { dump(node: $0, prefix: prefix) }
case .element:
let element = node as! XMLElement
var newPrefix = prefix + "/" + node.name!
if let name = element.attribute(forName: "name") {
newPrefix += ":" + name.stringValue!
}
print("\(newPrefix)")
element.attributes?.forEach {
if $0.name! != "name" {
dump(node: $0, prefix: newPrefix)
}
}
element.children?.forEach { dump(node: $0, prefix: newPrefix) }
case .attribute:
print("\(prefix)/\(node.name!)=\(node.stringValue!)")
default:
print("\(prefix)/\"\(node.xmlString)\"")
}
}
dump(node: xml, prefix: "")