e30861ad18
- Added more Documentation, specially initializer - Added option to conserve sessions after provider deinit to finish tasks - Dropbox and OneDrive init are not bailable anymore - Updated AEXML
102 lines
3.5 KiB
Swift
102 lines
3.5 KiB
Swift
//
|
|
// Parser.swift
|
|
//
|
|
// Copyright (c) 2014-2016 Marko Tadić <tadija@me.com> http://tadija.net
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
// copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
/// Simple wrapper around `Foundation.XMLParser`.
|
|
internal class AEXMLParser: NSObject, XMLParserDelegate {
|
|
|
|
// MARK: - Properties
|
|
|
|
let document: AEXMLDocument
|
|
let data: Data
|
|
|
|
var currentParent: AEXMLElement?
|
|
var currentElement: AEXMLElement?
|
|
var currentValue = String()
|
|
|
|
var parseError: Error?
|
|
|
|
// MARK: - Lifecycle
|
|
|
|
init(document: AEXMLDocument, data: Data) {
|
|
self.document = document
|
|
self.data = data
|
|
currentParent = document
|
|
|
|
super.init()
|
|
}
|
|
|
|
// MARK: - API
|
|
|
|
func parse() throws {
|
|
let parser = XMLParser(data: data)
|
|
parser.delegate = self
|
|
|
|
parser.shouldProcessNamespaces = document.options.parserSettings.shouldProcessNamespaces
|
|
parser.shouldReportNamespacePrefixes = document.options.parserSettings.shouldReportNamespacePrefixes
|
|
parser.shouldResolveExternalEntities = document.options.parserSettings.shouldResolveExternalEntities
|
|
|
|
let success = parser.parse()
|
|
|
|
if !success {
|
|
guard let error = parseError else { throw AEXMLError.parsingFailed }
|
|
throw error
|
|
}
|
|
}
|
|
|
|
// MARK: - XMLParserDelegate
|
|
|
|
@objc func parser(_ parser: XMLParser,
|
|
didStartElement elementName: String,
|
|
namespaceURI: String?,
|
|
qualifiedName qName: String?,
|
|
attributes attributeDict: [String : String])
|
|
{
|
|
currentValue = String()
|
|
currentElement = currentParent?.addChild(name: elementName, attributes: attributeDict)
|
|
currentParent = currentElement
|
|
}
|
|
|
|
@objc func parser(_ parser: XMLParser, foundCharacters string: String) {
|
|
currentValue += string
|
|
let newValue = currentValue.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
|
|
currentElement?.value = newValue == String() ? nil : newValue
|
|
}
|
|
|
|
@objc func parser(_ parser: XMLParser,
|
|
didEndElement elementName: String,
|
|
namespaceURI: String?,
|
|
qualifiedName qName: String?)
|
|
{
|
|
currentParent = currentParent?.parent
|
|
currentElement = nil
|
|
}
|
|
|
|
@objc func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
|
|
self.parseError = parseError
|
|
}
|
|
|
|
}
|