|
| 1 | +// |
| 2 | +// SwiftGenKit |
| 3 | +// Copyright (c) 2017 SwiftGen |
| 4 | +// MIT Licence |
| 5 | +// |
| 6 | + |
| 7 | +import Foundation |
| 8 | +import PathKit |
| 9 | + |
| 10 | +public final class XIBParser { |
| 11 | + var xibs: [String: (customClass: String?, module: String?)] = [:] |
| 12 | + |
| 13 | + public init() {} |
| 14 | + |
| 15 | + private class ParserDelegate: NSObject, XMLParserDelegate { |
| 16 | + fileprivate var fileOwnerClass: String? |
| 17 | + fileprivate var fileOwnerModule: String? |
| 18 | + |
| 19 | + func parser(_ parser: XMLParser, didStartElement elementName: String, |
| 20 | + namespaceURI: String?, qualifiedName qName: String?, |
| 21 | + attributes attributeDict: [String: String]) { |
| 22 | + if elementName == "placeholder" && attributeDict["placeholderIdentifier"] == "IBFilesOwner" { |
| 23 | + self.fileOwnerClass = attributeDict["customClass"] |
| 24 | + self.fileOwnerModule = attributeDict["customModule"] |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public func addXIB(at path: Path) throws { |
| 30 | + let parser = XMLParser(data: try path.read()) |
| 31 | + |
| 32 | + let delegate = ParserDelegate() |
| 33 | + parser.delegate = delegate |
| 34 | + parser.parse() |
| 35 | + |
| 36 | + let xibName = path.lastComponentWithoutExtension |
| 37 | + self.xibs[xibName] = (delegate.fileOwnerClass, delegate.fileOwnerModule) |
| 38 | + } |
| 39 | + |
| 40 | + public func parseDirectory(at path: Path) throws { |
| 41 | + let iterator = path.makeIterator() |
| 42 | + |
| 43 | + while let subPath = iterator.next() { |
| 44 | + if subPath.extension == "xib" { |
| 45 | + try addXIB(at: subPath) |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments