Skip to content

XML: correct API signatures #1116

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

Merged
merged 1 commit into from
Jul 14, 2017
Merged
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
4 changes: 2 additions & 2 deletions Foundation/NSXMLDTD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class XMLDTD : XMLNode {
NSUnimplemented()
}

public convenience init(contentsOf url: URL, options: Options = []) throws {
public convenience init(contentsOf url: URL, options mask: XMLNode.Options = []) throws {
let urlString = url.absoluteString

guard let node = _CFXMLParseDTD(urlString) else {
Expand All @@ -32,7 +32,7 @@ open class XMLDTD : XMLNode {
self.init(ptr: node)
}

public convenience init(data: Data, options: Options = []) throws {
public convenience init(data: Data, options mask: XMLNode.Options = []) throws {
var unmanagedError: Unmanaged<CFError>? = nil

guard let node = _CFXMLParseDTDFromData(data._cfObject, &unmanagedError) else {
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSXMLDTDNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ open class XMLDTDNode: XMLNode {
super.init(ptr: ptr)
} //primitive

public override init(kind: Kind, options: Options = []) {
public override init(kind: XMLNode.Kind, options: XMLNode.Options = []) {
let ptr: _CFXMLNodePtr

switch kind {
Expand All @@ -99,7 +99,7 @@ open class XMLDTDNode: XMLNode {
@method dtdKind
@abstract Sets the DTD sub kind.
*/
open var dtdKind: DTDKind {
open var dtdKind: XMLDTDNode.DTDKind {
switch _CFXMLNodeGetType(_xmlNode) {
case _kCFXMLDTDNodeTypeElement:
switch _CFXMLDTDElementNodeGetType(_xmlNode) {
Expand Down
22 changes: 11 additions & 11 deletions Foundation/NSXMLDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,34 @@ open class XMLDocument : XMLNode {
@method initWithXMLString:options:error:
@abstract Returns a document created from either XML or HTML, if the HTMLTidy option is set. Parse errors are returned in <tt>error</tt>.
*/
public convenience init(xmlString string: String, options: Options) throws {
public convenience init(xmlString string: String, options mask: XMLNode.Options = []) throws {
guard let data = string.data(using: .utf8) else {
// TODO: Throw an error
fatalError("String: '\(string)' could not be converted to NSData using UTF-8 encoding")
}

try self.init(data: data, options: options)
try self.init(data: data, options: mask)
}

/*!
@method initWithContentsOfURL:options:error:
@abstract Returns a document created from the contents of an XML or HTML URL. Connection problems such as 404, parse errors are returned in <tt>error</tt>.
*/
public convenience init(contentsOf url: URL, options: Options) throws {
public convenience init(contentsOf url: URL, options mask: XMLNode.Options = []) throws {
let data = try Data(contentsOf: url, options: .mappedIfSafe)

try self.init(data: data, options: options)
try self.init(data: data, options: mask)
}

/*!
@method initWithData:options:error:
@abstract Returns a document created from data. Parse errors are returned in <tt>error</tt>.
*/
public init(data: Data, options: Options) throws {
let docPtr = _CFXMLDocPtrFromDataWithOptions(data._cfObject, Int32(options.rawValue))
public init(data: Data, options mask: XMLNode.Options = []) throws {
let docPtr = _CFXMLDocPtrFromDataWithOptions(data._cfObject, Int32(mask.rawValue))
super.init(ptr: _CFXMLNodePtr(docPtr))

if options.contains(.documentValidate) {
if mask.contains(.documentValidate) {
try validate()
}
}
Expand Down Expand Up @@ -170,7 +170,7 @@ open class XMLDocument : XMLNode {
@method documentContentKind
@abstract The kind of document.
*/
open var documentContentKind: ContentKind {
open var documentContentKind: XMLDocument.ContentKind {
get {
let properties = _CFXMLDocProperties(_xmlDoc)

Expand Down Expand Up @@ -311,14 +311,14 @@ open class XMLDocument : XMLNode {
@method XMLData
@abstract Invokes XMLDataWithOptions with NSXMLNodeOptionsNone.
*/
/*@NSCopying*/ open var xmlData: Data { return xmlData(withOptions: []) }
/*@NSCopying*/ open var xmlData: Data { return xmlData() }

/*!
@method XMLDataWithOptions:
@abstract The representation of this node as it would appear in an XML document, encoded based on characterEncoding.
*/
open func xmlData(withOptions options: Options) -> Data {
let string = xmlString(withOptions: options)
open func xmlData(options: XMLNode.Options = []) -> Data {
let string = xmlString(options: options)
// TODO: support encodings other than UTF-8

return string.data(using: .utf8) ?? Data()
Expand Down
10 changes: 5 additions & 5 deletions Foundation/NSXMLElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ open class XMLElement: XMLNode {
@method initWithName:URI:
@abstract Returns an element whose full QName is specified.
*/
public init(name: String, uri: String?) {
public init(name: String, uri URI: String?) {
super.init(kind: .element, options: [])
self.uri = uri
self.uri = URI
self.name = name
}

Expand All @@ -53,7 +53,7 @@ open class XMLElement: XMLNode {
NSUnimplemented()
}

public convenience override init(kind: Kind, options: Options = []) {
public convenience override init(kind: XMLNode.Kind, options: XMLNode.Options = []) {
self.init(name: "", uri: nil)
}

Expand All @@ -69,7 +69,7 @@ open class XMLElement: XMLNode {
@method elementsForLocalName:URI
@abstract Returns all of the child elements that match this localname URI pair.
*/
open func elements(forLocalName localName: String, uri: String?) -> [XMLElement] { NSUnimplemented() }
open func elements(forLocalName localName: String, uri URI: String?) -> [XMLElement] { NSUnimplemented() }

/*!
@method addAttribute:
Expand Down Expand Up @@ -169,7 +169,7 @@ open class XMLElement: XMLNode {
@method attributeForLocalName:URI:
@abstract Returns an attribute matching this localname URI pair.
*/
open func attribute(forLocalName localName: String, uri: String?) -> XMLNode? {
open func attribute(forLocalName localName: String, uri URI: String?) -> XMLNode? {
NSUnimplemented()
}

Expand Down
24 changes: 12 additions & 12 deletions Foundation/NSXMLNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ open class XMLNode: NSObject, NSCopying {
case processingInstruction
case comment
case text
case dtd
case DTDKind
case entityDeclaration
case attributeDeclaration
case elementDeclaration
Expand Down Expand Up @@ -101,15 +101,15 @@ open class XMLNode: NSObject, NSCopying {
@method initWithKind:
@abstract Invokes @link initWithKind:options: @/link with options set to NSXMLNodeOptionsNone
*/
public convenience init(kind: Kind) {
public convenience init(kind: XMLNode.Kind) {
self.init(kind: kind, options: [])
}

/*!
@method initWithKind:options:
@abstract Inits a node with fidelity options as description NSXMLNodeOptions.h
*/
public init(kind: Kind, options: Options = []) {
public init(kind: XMLNode.Kind, options: XMLNode.Options = []) {

switch kind {
case .document:
Expand All @@ -123,7 +123,7 @@ open class XMLNode: NSObject, NSCopying {
case .attribute:
_xmlNode = _CFXMLNodePtr(_CFXMLNewProperty(nil, "", ""))

case .dtd:
case .DTDKind:
_xmlNode = _CFXMLNewDTD(nil, "", "", "")

default:
Expand Down Expand Up @@ -260,7 +260,7 @@ open class XMLNode: NSObject, NSCopying {
@method kind
@abstract Returns an element, attribute, entity, or notation DTD node based on the full XML string.
*/
open var kind: Kind {
open var kind: XMLNode.Kind {
switch _CFXMLNodeGetType(_xmlNode) {
case _kCFXMLTypeElement:
return .element
Expand All @@ -272,7 +272,7 @@ open class XMLNode: NSObject, NSCopying {
return .document

case _kCFXMLTypeDTD:
return .dtd
return .DTDKind

case _kCFXMLDTDNodeTypeElement:
return .elementDeclaration
Expand Down Expand Up @@ -503,7 +503,7 @@ open class XMLNode: NSObject, NSCopying {
fallthrough
case .element:
fallthrough
case .dtd:
case .DTDKind:
return Array<XMLNode>(self as XMLNode)

default:
Expand Down Expand Up @@ -546,7 +546,7 @@ open class XMLNode: NSObject, NSCopying {
@method previousNode:
@abstract Returns the previous node in document order. This can be used to walk the tree backwards.
*/
/*@NSCopying*/ open var previousNode: XMLNode? {
/*@NSCopying*/ open var previous: XMLNode? {
if let previousSibling = self.previousSibling {
if let lastChild = _CFXMLNodeGetLastChild(previousSibling._xmlNode) {
return XMLNode._objectNodeForNode(lastChild)
Expand All @@ -564,7 +564,7 @@ open class XMLNode: NSObject, NSCopying {
@method nextNode:
@abstract Returns the next node in document order. This can be used to walk the tree forwards.
*/
/*@NSCopying*/ open var nextNode: XMLNode? {
/*@NSCopying*/ open var next: XMLNode? {
if let children = _CFXMLNodeGetFirstChild(_xmlNode) {
return XMLNode._objectNodeForNode(children)
} else if let next = nextSibling {
Expand Down Expand Up @@ -728,14 +728,14 @@ open class XMLNode: NSObject, NSCopying {
@abstract The representation of this node as it would appear in an XML document.
*/
open var xmlString: String {
return xmlString(withOptions: [])
return xmlString()
}

/*!
@method XMLStringWithOptions:
@abstract The representation of this node as it would appear in an XML document, with various output options available.
*/
open func xmlString(withOptions options: Options) -> String {
open func xmlString(options: XMLNode.Options = []) -> String {
return _CFXMLStringWithOptions(_xmlNode, UInt32(options.rawValue))._swiftObject
}

Expand Down Expand Up @@ -788,7 +788,7 @@ open class XMLNode: NSObject, NSCopying {
case .document:
_CFXMLFreeDocument(_CFXMLDocPtr(_xmlNode))

case .dtd:
case .DTDKind:
_CFXMLFreeDTD(_CFXMLDTDPtr(_xmlNode))

case .attribute:
Expand Down
27 changes: 13 additions & 14 deletions Foundation/NSXMLParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ import CoreFoundation

extension XMLParser {
public enum ExternalEntityResolvingPolicy : UInt {

case resolveExternalEntitiesNever // default
case resolveExternalEntitiesNoNetwork
case resolveExternalEntitiesSameOriginOnly //only applies to NSXMLParser instances initialized with -initWithContentsOfURL:
case resolveExternalEntitiesAlways
case never // default
case noNetwork
case sameOriginOnly //only applies to NSXMLParser instances initialized with -initWithContentsOfURL:
case always
}
}

Expand All @@ -44,8 +43,8 @@ extension XMLParser {

private func UTF8STRING(_ bytes: UnsafePointer<UInt8>?) -> String? {
guard let bytes = bytes else { return nil }
// strlen operates on the wrong type, char*. We can't rebind the memory to a different type without knowing it's length,
// but since we know strlen is in libc, its safe to directly bitcast the pointer without worrying about multiple accesses
// strlen operates on the wrong type, char*. We can't rebind the memory to a different type without knowing its length,
// but since we know strlen is in libc, it's safe to directly bitcast the pointer without worrying about multiple accesses
// of different types visible to the compiler.
let len = strlen(unsafeBitCast(bytes, to: UnsafePointer<Int8>.self))
let str = String._fromCodeUnitSequence(UTF8.self, input: UnsafeBufferPointer(start: bytes, count: Int(len)))
Expand Down Expand Up @@ -75,7 +74,7 @@ internal func _NSXMLParserExternalEntityWithURL(_ interface: _CFXMLInterface, ur
}
if let url = a {
let allowed = allowedEntityURLs.contains(url)
if allowed || policy != .resolveExternalEntitiesSameOriginOnly {
if allowed || policy != .sameOriginOnly {
if allowed {
return originalLoaderFunction(urlStr, identifier, context)
}
Expand All @@ -84,7 +83,7 @@ internal func _NSXMLParserExternalEntityWithURL(_ interface: _CFXMLInterface, ur
}

switch policy {
case .resolveExternalEntitiesSameOriginOnly:
case .sameOriginOnly:
guard let url = parser._url else { break }

if a == nil {
Expand Down Expand Up @@ -119,11 +118,11 @@ internal func _NSXMLParserExternalEntityWithURL(_ interface: _CFXMLInterface, ur
if !matches {
return nil
}
case .resolveExternalEntitiesAlways:
case .always:
break
case .resolveExternalEntitiesNever:
case .never:
return nil
case .resolveExternalEntitiesNoNetwork:
case .noNetwork:
return _CFXMLInterfaceNoNetExternalEntityLoader(urlStr, identifier, context)
}

Expand Down Expand Up @@ -456,7 +455,7 @@ open class XMLParser : NSObject {
open var shouldReportNamespacePrefixes: Bool = false

//defaults to NSXMLNodeLoadExternalEntitiesNever
open var externalEntityResolvingPolicy: ExternalEntityResolvingPolicy = .resolveExternalEntitiesNever
open var externalEntityResolvingPolicy: ExternalEntityResolvingPolicy = .never

open var allowedExternalEntityURLs: Set<URL>?

Expand Down Expand Up @@ -954,7 +953,7 @@ extension XMLParser {

case uriFragmentError

case nodtdError
case noDTDError

case delegateAbortedParseError
}
Expand Down
18 changes: 9 additions & 9 deletions TestFoundation/TestNSXMLDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ class TestNSXMLDocument : XCTestCase {
fooNode.addChild(bazNode)
node.addChild(barNode)

XCTAssert(doc.nextNode === node)
XCTAssert(doc.nextNode?.nextNode === fooNode)
XCTAssert(doc.nextNode?.nextNode?.nextNode === bazNode)
XCTAssert(doc.nextNode?.nextNode?.nextNode?.nextNode === barNode)

XCTAssert(barNode.previousNode === bazNode)
XCTAssert(barNode.previousNode?.previousNode === fooNode)
XCTAssert(barNode.previousNode?.previousNode?.previousNode === node)
XCTAssert(barNode.previousNode?.previousNode?.previousNode?.previousNode === doc)
XCTAssert(doc.next === node)
XCTAssert(doc.next?.next === fooNode)
XCTAssert(doc.next?.next?.next === bazNode)
XCTAssert(doc.next?.next?.next?.next === barNode)

XCTAssert(barNode.previous === bazNode)
XCTAssert(barNode.previous?.previous === fooNode)
XCTAssert(barNode.previous?.previous?.previous === node)
XCTAssert(barNode.previous?.previous?.previous?.previous === doc)
}

func test_xpath() {
Expand Down