Skip to content

Commit 6a2836b

Browse files
committed
XML: correct API signatures
1 parent 54f47e4 commit 6a2836b

File tree

7 files changed

+54
-55
lines changed

7 files changed

+54
-55
lines changed

Foundation/NSXMLDTD.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ open class XMLDTD : XMLNode {
2222
NSUnimplemented()
2323
}
2424

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

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

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

3838
guard let node = _CFXMLParseDTDFromData(data._cfObject, &unmanagedError) else {

Foundation/NSXMLDTDNode.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ open class XMLDTDNode: XMLNode {
8080
super.init(ptr: ptr)
8181
} //primitive
8282

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

8686
switch kind {
@@ -99,7 +99,7 @@ open class XMLDTDNode: XMLNode {
9999
@method dtdKind
100100
@abstract Sets the DTD sub kind.
101101
*/
102-
open var dtdKind: DTDKind {
102+
open var dtdKind: XMLDTDNode.DTDKind {
103103
switch _CFXMLNodeGetType(_xmlNode) {
104104
case _kCFXMLDTDNodeTypeElement:
105105
switch _CFXMLDTDElementNodeGetType(_xmlNode) {

Foundation/NSXMLDocument.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,34 +68,34 @@ open class XMLDocument : XMLNode {
6868
@method initWithXMLString:options:error:
6969
@abstract Returns a document created from either XML or HTML, if the HTMLTidy option is set. Parse errors are returned in <tt>error</tt>.
7070
*/
71-
public convenience init(xmlString string: String, options: Options) throws {
71+
public convenience init(xmlString string: String, options mask: XMLNode.Options = []) throws {
7272
guard let data = string.data(using: .utf8) else {
7373
// TODO: Throw an error
7474
fatalError("String: '\(string)' could not be converted to NSData using UTF-8 encoding")
7575
}
7676

77-
try self.init(data: data, options: options)
77+
try self.init(data: data, options: mask)
7878
}
7979

8080
/*!
8181
@method initWithContentsOfURL:options:error:
8282
@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>.
8383
*/
84-
public convenience init(contentsOf url: URL, options: Options) throws {
84+
public convenience init(contentsOf url: URL, options mask: XMLNode.Options = []) throws {
8585
let data = try Data(contentsOf: url, options: .mappedIfSafe)
8686

87-
try self.init(data: data, options: options)
87+
try self.init(data: data, options: mask)
8888
}
8989

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

98-
if options.contains(.documentValidate) {
98+
if mask.contains(.documentValidate) {
9999
try validate()
100100
}
101101
}
@@ -170,7 +170,7 @@ open class XMLDocument : XMLNode {
170170
@method documentContentKind
171171
@abstract The kind of document.
172172
*/
173-
open var documentContentKind: ContentKind {
173+
open var documentContentKind: XMLDocument.ContentKind {
174174
get {
175175
let properties = _CFXMLDocProperties(_xmlDoc)
176176

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

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

324324
return string.data(using: .utf8) ?? Data()

Foundation/NSXMLElement.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ open class XMLElement: XMLNode {
2727
@method initWithName:URI:
2828
@abstract Returns an element whose full QName is specified.
2929
*/
30-
public init(name: String, uri: String?) {
30+
public init(name: String, uri URI: String?) {
3131
super.init(kind: .element, options: [])
32-
self.uri = uri
32+
self.uri = URI
3333
self.name = name
3434
}
3535

@@ -53,7 +53,7 @@ open class XMLElement: XMLNode {
5353
NSUnimplemented()
5454
}
5555

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

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

7474
/*!
7575
@method addAttribute:
@@ -169,7 +169,7 @@ open class XMLElement: XMLNode {
169169
@method attributeForLocalName:URI:
170170
@abstract Returns an attribute matching this localname URI pair.
171171
*/
172-
open func attribute(forLocalName localName: String, uri: String?) -> XMLNode? {
172+
open func attribute(forLocalName localName: String, uri URI: String?) -> XMLNode? {
173173
NSUnimplemented()
174174
}
175175

Foundation/NSXMLNode.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ open class XMLNode: NSObject, NSCopying {
4242
case processingInstruction
4343
case comment
4444
case text
45-
case dtd
45+
case DTDKind
4646
case entityDeclaration
4747
case attributeDeclaration
4848
case elementDeclaration
@@ -101,15 +101,15 @@ open class XMLNode: NSObject, NSCopying {
101101
@method initWithKind:
102102
@abstract Invokes @link initWithKind:options: @/link with options set to NSXMLNodeOptionsNone
103103
*/
104-
public convenience init(kind: Kind) {
104+
public convenience init(kind: XMLNode.Kind) {
105105
self.init(kind: kind, options: [])
106106
}
107107

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

114114
switch kind {
115115
case .document:
@@ -123,7 +123,7 @@ open class XMLNode: NSObject, NSCopying {
123123
case .attribute:
124124
_xmlNode = _CFXMLNodePtr(_CFXMLNewProperty(nil, "", ""))
125125

126-
case .dtd:
126+
case .DTDKind:
127127
_xmlNode = _CFXMLNewDTD(nil, "", "", "")
128128

129129
default:
@@ -260,7 +260,7 @@ open class XMLNode: NSObject, NSCopying {
260260
@method kind
261261
@abstract Returns an element, attribute, entity, or notation DTD node based on the full XML string.
262262
*/
263-
open var kind: Kind {
263+
open var kind: XMLNode.Kind {
264264
switch _CFXMLNodeGetType(_xmlNode) {
265265
case _kCFXMLTypeElement:
266266
return .element
@@ -272,7 +272,7 @@ open class XMLNode: NSObject, NSCopying {
272272
return .document
273273

274274
case _kCFXMLTypeDTD:
275-
return .dtd
275+
return .DTDKind
276276

277277
case _kCFXMLDTDNodeTypeElement:
278278
return .elementDeclaration
@@ -503,7 +503,7 @@ open class XMLNode: NSObject, NSCopying {
503503
fallthrough
504504
case .element:
505505
fallthrough
506-
case .dtd:
506+
case .DTDKind:
507507
return Array<XMLNode>(self as XMLNode)
508508

509509
default:
@@ -546,7 +546,7 @@ open class XMLNode: NSObject, NSCopying {
546546
@method previousNode:
547547
@abstract Returns the previous node in document order. This can be used to walk the tree backwards.
548548
*/
549-
/*@NSCopying*/ open var previousNode: XMLNode? {
549+
/*@NSCopying*/ open var previous: XMLNode? {
550550
if let previousSibling = self.previousSibling {
551551
if let lastChild = _CFXMLNodeGetLastChild(previousSibling._xmlNode) {
552552
return XMLNode._objectNodeForNode(lastChild)
@@ -564,7 +564,7 @@ open class XMLNode: NSObject, NSCopying {
564564
@method nextNode:
565565
@abstract Returns the next node in document order. This can be used to walk the tree forwards.
566566
*/
567-
/*@NSCopying*/ open var nextNode: XMLNode? {
567+
/*@NSCopying*/ open var next: XMLNode? {
568568
if let children = _CFXMLNodeGetFirstChild(_xmlNode) {
569569
return XMLNode._objectNodeForNode(children)
570570
} else if let next = nextSibling {
@@ -728,14 +728,14 @@ open class XMLNode: NSObject, NSCopying {
728728
@abstract The representation of this node as it would appear in an XML document.
729729
*/
730730
open var xmlString: String {
731-
return xmlString(withOptions: [])
731+
return xmlString()
732732
}
733733

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

@@ -788,7 +788,7 @@ open class XMLNode: NSObject, NSCopying {
788788
case .document:
789789
_CFXMLFreeDocument(_CFXMLDocPtr(_xmlNode))
790790

791-
case .dtd:
791+
case .DTDKind:
792792
_CFXMLFreeDTD(_CFXMLDTDPtr(_xmlNode))
793793

794794
case .attribute:

Foundation/NSXMLParser.swift

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ import CoreFoundation
2222

2323
extension XMLParser {
2424
public enum ExternalEntityResolvingPolicy : UInt {
25-
26-
case resolveExternalEntitiesNever // default
27-
case resolveExternalEntitiesNoNetwork
28-
case resolveExternalEntitiesSameOriginOnly //only applies to NSXMLParser instances initialized with -initWithContentsOfURL:
29-
case resolveExternalEntitiesAlways
25+
case never // default
26+
case noNetwork
27+
case sameOriginOnly //only applies to NSXMLParser instances initialized with -initWithContentsOfURL:
28+
case always
3029
}
3130
}
3231

@@ -44,8 +43,8 @@ extension XMLParser {
4443

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

8685
switch policy {
87-
case .resolveExternalEntitiesSameOriginOnly:
86+
case .sameOriginOnly:
8887
guard let url = parser._url else { break }
8988

9089
if a == nil {
@@ -119,11 +118,11 @@ internal func _NSXMLParserExternalEntityWithURL(_ interface: _CFXMLInterface, ur
119118
if !matches {
120119
return nil
121120
}
122-
case .resolveExternalEntitiesAlways:
121+
case .always:
123122
break
124-
case .resolveExternalEntitiesNever:
123+
case .never:
125124
return nil
126-
case .resolveExternalEntitiesNoNetwork:
125+
case .noNetwork:
127126
return _CFXMLInterfaceNoNetExternalEntityLoader(urlStr, identifier, context)
128127
}
129128

@@ -456,7 +455,7 @@ open class XMLParser : NSObject {
456455
open var shouldReportNamespacePrefixes: Bool = false
457456

458457
//defaults to NSXMLNodeLoadExternalEntitiesNever
459-
open var externalEntityResolvingPolicy: ExternalEntityResolvingPolicy = .resolveExternalEntitiesNever
458+
open var externalEntityResolvingPolicy: ExternalEntityResolvingPolicy = .never
460459

461460
open var allowedExternalEntityURLs: Set<URL>?
462461

@@ -954,7 +953,7 @@ extension XMLParser {
954953

955954
case uriFragmentError
956955

957-
case nodtdError
956+
case noDTDError
958957

959958
case delegateAbortedParseError
960959
}

TestFoundation/TestNSXMLDocument.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ class TestNSXMLDocument : XCTestCase {
9393
fooNode.addChild(bazNode)
9494
node.addChild(barNode)
9595

96-
XCTAssert(doc.nextNode === node)
97-
XCTAssert(doc.nextNode?.nextNode === fooNode)
98-
XCTAssert(doc.nextNode?.nextNode?.nextNode === bazNode)
99-
XCTAssert(doc.nextNode?.nextNode?.nextNode?.nextNode === barNode)
100-
101-
XCTAssert(barNode.previousNode === bazNode)
102-
XCTAssert(barNode.previousNode?.previousNode === fooNode)
103-
XCTAssert(barNode.previousNode?.previousNode?.previousNode === node)
104-
XCTAssert(barNode.previousNode?.previousNode?.previousNode?.previousNode === doc)
96+
XCTAssert(doc.next === node)
97+
XCTAssert(doc.next?.next === fooNode)
98+
XCTAssert(doc.next?.next?.next === bazNode)
99+
XCTAssert(doc.next?.next?.next?.next === barNode)
100+
101+
XCTAssert(barNode.previous === bazNode)
102+
XCTAssert(barNode.previous?.previous === fooNode)
103+
XCTAssert(barNode.previous?.previous?.previous === node)
104+
XCTAssert(barNode.previous?.previous?.previous?.previous === doc)
105105
}
106106

107107
func test_xpath() {

0 commit comments

Comments
 (0)