Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP #807

Closed
wants to merge 1 commit into from
Closed

WIP #807

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions Sources/Indexer/XibParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,38 @@ import Foundation
import SourceGraph
import SystemPackage

final class XibParser {
final class XibParser: NSObject, XMLParserDelegate {
private var customClassNames: [String] = []
private var currentElementAttributes: [String: String] = [:]

private let path: FilePath

required init(path: FilePath) {
self.path = path
}

func parse() throws -> [AssetReference] {
func parse() -> [AssetReference] {
guard let data = FileManager.default.contents(atPath: path.string) else { return [] }
let structure = try AEXMLDocument(xml: data)
return references(from: structure.root).map {
AssetReference(absoluteName: $0, source: .interfaceBuilder)
}
let parser = XMLParser(data: data)
parser.delegate = self
parser.parse()
return customClassNames.map { AssetReference(absoluteName: $0, source: .interfaceBuilder) }
}

// MARK: - Private

private func references(from element: AEXMLElement) -> [String] {
var names: [String] = []

for child in element.children {
if let name = child.attributes["customClass"] {
names.append(name)
}
// MARK: - XMLParserDelegate

names += references(from: child)
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String: String]) {
currentElementAttributes = attributeDict
if let customClass = attributeDict["customClass"] {
customClassNames.append(customClass)
}
}

func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
currentElementAttributes = [:]
}

return names
func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
print("Parse error: \(parseError.localizedDescription)")
}
}
1 change: 1 addition & 0 deletions Tests/Shared/SourceGraphTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ open class SourceGraphTestCase: XCTestCase {
super.setUp()
configuration = Configuration.shared
configuration.quiet = true
configuration.cleanBuild = true
}

override open func setUp() {
Expand Down
Loading