Skip to content

Commit e37bcf6

Browse files
committed
Merge pull request #236 from rothomp3/feature/NSXMLDocument
Add NSXMLDTD and NSXMLDTDNode support
2 parents e5bcb58 + 6790e80 commit e37bcf6

File tree

12 files changed

+1326
-90
lines changed

12 files changed

+1326
-90
lines changed

CoreFoundation/Parsing.subproj/CFXMLInterface.c

Lines changed: 646 additions & 14 deletions
Large diffs are not rendered by default.

CoreFoundation/Parsing.subproj/CFXMLInterface.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,42 @@ extern CFIndex _kCFXMLInterfaceHuge;
4848
extern CFIndex _kCFXMLInterfaceOldsax;
4949
extern CFIndex _kCFXMLInterfaceIgnoreEnc;
5050
extern CFIndex _kCFXMLInterfaceBigLines;
51+
52+
extern CFIndex _kCFXMLTypeInvalid;
5153
extern CFIndex _kCFXMLTypeDocument;
5254
extern CFIndex _kCFXMLTypeElement;
5355
extern CFIndex _kCFXMLTypeAttribute;
5456
extern CFIndex _kCFXMLTypeDTD;
5557
extern CFIndex _kCFXMLDocTypeHTML;
58+
5659
extern CFIndex _kCFXMLDTDNodeTypeEntity;
5760
extern CFIndex _kCFXMLDTDNodeTypeAttribute;
5861
extern CFIndex _kCFXMLDTDNodeTypeElement;
5962
extern CFIndex _kCFXMLDTDNodeTypeNotation;
6063

64+
extern CFIndex _kCFXMLDTDNodeElementTypeUndefined;
65+
extern CFIndex _kCFXMLDTDNodeElementTypeEmpty;
66+
extern CFIndex _kCFXMLDTDNodeElementTypeAny;
67+
extern CFIndex _kCFXMLDTDNodeElementTypeMixed;
68+
extern CFIndex _kCFXMLDTDNodeElementTypeElement;
69+
70+
extern CFIndex _kCFXMLDTDNodeEntityTypeInternalGeneral;
71+
extern CFIndex _kCFXMLDTDNodeEntityTypeExternalGeneralParsed;
72+
extern CFIndex _kCFXMLDTDNodeEntityTypeExternalGeneralUnparsed;
73+
extern CFIndex _kCFXMLDTDNodeEntityTypeInternalParameter;
74+
extern CFIndex _kCFXMLDTDNodeEntityTypeExternalParameter;
75+
extern CFIndex _kCFXMLDTDNodeEntityTypeInternalPredefined;
76+
77+
extern CFIndex _kCFXMLDTDNodeAttributeTypeCData;
78+
extern CFIndex _kCFXMLDTDNodeAttributeTypeID;
79+
extern CFIndex _kCFXMLDTDNodeAttributeTypeIDRef;
80+
extern CFIndex _kCFXMLDTDNodeAttributeTypeIDRefs;
81+
extern CFIndex _kCFXMLDTDNodeAttributeTypeEntity;
82+
extern CFIndex _kCFXMLDTDNodeAttributeTypeEntities;
83+
extern CFIndex _kCFXMLDTDNodeAttributeTypeNMToken;
84+
extern CFIndex _kCFXMLDTDNodeAttributeTypeNMTokens;
85+
extern CFIndex _kCFXMLDTDNodeAttributeTypeEnumeration;
86+
extern CFIndex _kCFXMLDTDNodeAttributeTypeNotation;
6187

6288
typedef struct _xmlParserInput *_CFXMLInterfaceParserInput;
6389
typedef struct _xmlParserCtxt *_CFXMLInterfaceParserContext;
@@ -107,6 +133,8 @@ typedef void* _CFXMLNodePtr;
107133
typedef void* _CFXMLDocPtr;
108134
typedef void* _CFXMLNamespacePtr;
109135
typedef void* _CFXMLEntityPtr;
136+
typedef void* _CFXMLDTDPtr;
137+
typedef void* _CFXMLDTDNodePtr;
110138

111139
_CFXMLNodePtr _CFXMLNewNode(_CFXMLNamespacePtr namespace, const char* name);
112140
_CFXMLNodePtr _CFXMLCopyNode(_CFXMLNodePtr node, bool recursive);
@@ -154,6 +182,8 @@ CF_RETURNS_RETAINED CFStringRef _Nullable _CFXMLDocVersion(_CFXMLDocPtr doc);
154182
void _CFXMLDocSetVersion(_CFXMLDocPtr doc, const unsigned char* version);
155183
int _CFXMLDocProperties(_CFXMLDocPtr doc);
156184
void _CFXMLDocSetProperties(_CFXMLDocPtr doc, int newProperties);
185+
_CFXMLDTDPtr _Nullable _CFXMLDocDTD(_CFXMLDocPtr doc);
186+
void _CFXMLDocSetDTD(_CFXMLDocPtr doc, _CFXMLDTDPtr _Nullable dtd);
157187

158188
CF_RETURNS_RETAINED CFStringRef _Nullable _CFXMLEncodeEntities(_CFXMLDocPtr doc, const unsigned char* string);
159189
_CFXMLEntityPtr _Nullable _CFXMLGetDocEntity(_CFXMLDocPtr doc, const char* entity);
@@ -173,8 +203,39 @@ _CFXMLDocPtr _CFXMLDocPtrFromDataWithOptions(CFDataRef data, int options);
173203
CF_RETURNS_RETAINED CFStringRef _Nullable _CFXMLNodeLocalName(_CFXMLNodePtr node);
174204
CF_RETURNS_RETAINED CFStringRef _Nullable _CFXMLNodePrefix(_CFXMLNodePtr node);
175205

206+
bool _CFXMLDocValidate(_CFXMLDocPtr doc, CFErrorRef _Nullable * error);
207+
208+
_CFXMLDTDPtr _CFXMLNewDTD(_CFXMLDocPtr doc, const unsigned char* name, const unsigned char* publicID, const unsigned char* systemID);
209+
_CFXMLDTDNodePtr _Nullable _CFXMLParseDTDNode(const unsigned char* xmlString);
210+
_CFXMLDTDPtr _Nullable _CFXMLParseDTD(const unsigned char* URL);
211+
_CFXMLDTDPtr _Nullable _CFXMLParseDTDFromData(CFDataRef data, CFErrorRef _Nullable * error);
212+
CF_RETURNS_RETAINED CFStringRef _Nullable _CFXMLDTDExternalID(_CFXMLDTDPtr dtd);
213+
void _CFXMLDTDSetExternalID(_CFXMLDTDPtr dtd, const unsigned char* externalID);
214+
CF_RETURNS_RETAINED CFStringRef _Nullable _CFXMLDTDSystemID(_CFXMLDTDPtr dtd);
215+
void _CFXMLDTDSetSystemID(_CFXMLDTDPtr dtd, const unsigned char* systemID);
216+
217+
_CFXMLDTDNodePtr _Nullable _CFXMLDTDGetElementDesc(_CFXMLDTDPtr dtd, const unsigned char* name);
218+
_CFXMLDTDNodePtr _Nullable _CFXMLDTDGetAttributeDesc(_CFXMLDTDPtr dtd, const unsigned char* elementName, const unsigned char* name);
219+
_CFXMLDTDNodePtr _Nullable _CFXMLDTDGetNotationDesc(_CFXMLDTDPtr dtd, const unsigned char* name);
220+
_CFXMLDTDNodePtr _Nullable _CFXMLDTDGetEntityDesc(_CFXMLDTDPtr dtd, const unsigned char* name);
221+
_CFXMLDTDNodePtr _Nullable _CFXMLDTDGetPredefinedEntity(const unsigned char* name);
222+
223+
_CFXMLDTDNodePtr _Nullable _CFXMLDTDNewElementDesc(_CFXMLDTDPtr dtd, const unsigned char* name);
224+
_CFXMLDTDNodePtr _Nullable _CFXMLDTDNewAttributeDesc(_CFXMLDTDPtr dtd, const unsigned char* name);
225+
226+
CFIndex _CFXMLDTDElementNodeGetType(_CFXMLDTDNodePtr node);
227+
CFIndex _CFXMLDTDEntityNodeGetType(_CFXMLDTDNodePtr node);
228+
CFIndex _CFXMLDTDAttributeNodeGetType(_CFXMLDTDNodePtr node);
229+
230+
CF_RETURNS_RETAINED CFStringRef _Nullable _CFXMLDTDNodeGetSystemID(_CFXMLDTDNodePtr node);
231+
void _CFXMLDTDNodeSetSystemID(_CFXMLDTDNodePtr node, const unsigned char* systemID);
232+
CF_RETURNS_RETAINED CFStringRef _Nullable _CFXMLDTDNodeGetPublicID(_CFXMLDTDNodePtr node);
233+
void _CFXMLDTDNodeSetPublicID(_CFXMLDTDNodePtr node, const unsigned char* publicID);
234+
176235
void _CFXMLFreeNode(_CFXMLNodePtr node);
177236
void _CFXMLFreeDocument(_CFXMLDocPtr doc);
237+
void _CFXMLFreeDTD(_CFXMLDTDPtr dtd);
238+
void _CFXMLFreeProperty(_CFXMLNodePtr prop);
178239

179240
CF_EXTERN_C_END
180241
CF_ASSUME_NONNULL_END

Foundation.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@
245245
61E011811C1B5998000037DD /* CFMessagePort.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88DC1BBC9AEC00234F36 /* CFMessagePort.c */; };
246246
61E011821C1B599A000037DD /* CFMachPort.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88D01BBC9AAC00234F36 /* CFMachPort.c */; };
247247
CE19A88C1C23AA2300B4CB6A /* NSStringTestData.txt in Resources */ = {isa = PBXBuildFile; fileRef = CE19A88B1C23AA2300B4CB6A /* NSStringTestData.txt */; };
248+
D834F9941C31C4060023812A /* TestNSOrderedSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = D834F9931C31C4060023812A /* TestNSOrderedSet.swift */; };
249+
DCDBB8331C1768AC00313299 /* TestNSData.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCDBB8321C1768AC00313299 /* TestNSData.swift */; };
250+
E19E17DC1C2225930023AF4D /* TestNSXMLDocument.swift in Sources */ = {isa = PBXBuildFile; fileRef = E19E17DB1C2225930023AF4D /* TestNSXMLDocument.swift */; };
251+
E1A03F361C4828650023AF4D /* PropertyList-1.0.dtd in Resources */ = {isa = PBXBuildFile; fileRef = E1A03F351C4828650023AF4D /* PropertyList-1.0.dtd */; };
252+
E1A03F381C482C730023AF4D /* NSXMLDTDTestData.xml in Resources */ = {isa = PBXBuildFile; fileRef = E1A03F371C482C730023AF4D /* NSXMLDTDTestData.xml */; };
248253
E1A3726F1C31EBFB0023AF4D /* NSXMLDocumentTestData.xml in Resources */ = {isa = PBXBuildFile; fileRef = E1A3726E1C31EBFB0023AF4D /* NSXMLDocumentTestData.xml */; };
249254
EA66F6361BEED03E00136161 /* TargetConditionals.h in Headers */ = {isa = PBXBuildFile; fileRef = EA66F6351BEED03E00136161 /* TargetConditionals.h */; settings = {ATTRIBUTES = (Public, ); }; };
250255
EA66F6481BF1619600136161 /* NSURLTestData.plist in Resources */ = {isa = PBXBuildFile; fileRef = EA66F63B1BF1619600136161 /* NSURLTestData.plist */; };
@@ -590,6 +595,9 @@
590595
CE19A88B1C23AA2300B4CB6A /* NSStringTestData.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = NSStringTestData.txt; sourceTree = "<group>"; };
591596
D834F9931C31C4060023812A /* TestNSOrderedSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSOrderedSet.swift; sourceTree = "<group>"; };
592597
DCDBB8321C1768AC00313299 /* TestNSData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSData.swift; sourceTree = "<group>"; };
598+
E19E17DB1C2225930023AF4D /* TestNSXMLDocument.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSXMLDocument.swift; sourceTree = "<group>"; };
599+
E1A03F351C4828650023AF4D /* PropertyList-1.0.dtd */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "PropertyList-1.0.dtd"; sourceTree = "<group>"; };
600+
E1A03F371C482C730023AF4D /* NSXMLDTDTestData.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = NSXMLDTDTestData.xml; sourceTree = "<group>"; };
593601
E1A3726E1C31EBFB0023AF4D /* NSXMLDocumentTestData.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = NSXMLDocumentTestData.xml; sourceTree = "<group>"; };
594602
E876A73D1C1180E000F279EC /* TestNSRange.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSRange.swift; sourceTree = "<group>"; };
595603
EA313DFC1BE7F2E90060A403 /* CFURLComponents_Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CFURLComponents_Internal.h; sourceTree = "<group>"; };
@@ -1079,6 +1087,8 @@
10791087
528776181BF27D9500CB0090 /* Test.plist */,
10801088
EA66F63B1BF1619600136161 /* NSURLTestData.plist */,
10811089
E1A3726E1C31EBFB0023AF4D /* NSXMLDocumentTestData.xml */,
1090+
E1A03F351C4828650023AF4D /* PropertyList-1.0.dtd */,
1091+
E1A03F371C482C730023AF4D /* NSXMLDTDTestData.xml */,
10821092
);
10831093
path = Resources;
10841094
sourceTree = "<group>";
@@ -1635,7 +1645,9 @@
16351645
528776191BF27D9500CB0090 /* Test.plist in Resources */,
16361646
EA66F6481BF1619600136161 /* NSURLTestData.plist in Resources */,
16371647
CE19A88C1C23AA2300B4CB6A /* NSStringTestData.txt in Resources */,
1648+
E1A03F361C4828650023AF4D /* PropertyList-1.0.dtd in Resources */,
16381649
E1A3726F1C31EBFB0023AF4D /* NSXMLDocumentTestData.xml in Resources */,
1650+
E1A03F381C482C730023AF4D /* NSXMLDTDTestData.xml in Resources */,
16391651
);
16401652
runOnlyForDeploymentPostprocessing = 0;
16411653
};

Foundation/NSSwiftRuntime.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public protocol Bridgeable {
387387
}
388388

389389
#if os(OSX) || os(iOS)
390-
internal typealias _DarwinCompatibleBoolean = DarwinBoolean
390+
internal typealias _DarwinCompatibleBoolean = DarwinBoolean
391391
#else
392-
internal typealias _DarwinCompatibleBoolean = Bool
393-
#endif
392+
internal typealias _DarwinCompatibleBoolean = Bool
393+
#endif

Foundation/NSXMLDTD.swift

Lines changed: 114 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,89 +13,187 @@ import CoreFoundation
1313
@abstract Defines the order, repetition, and allowable values for a document
1414
*/
1515
public class NSXMLDTD : NSXMLNode {
16+
17+
internal var _xmlDTD: _CFXMLDTDPtr {
18+
return _CFXMLDTDPtr(_xmlNode)
19+
}
1620

17-
public convenience init(contentsOfURL url: NSURL, options mask: Int) throws { NSUnimplemented() }
18-
public init(data: NSData, options mask: Int) throws { NSUnimplemented() } //primitive
21+
public convenience init(contentsOfURL url: NSURL, options mask: Int) throws {
22+
guard let urlString = url.absoluteString else {
23+
//TODO: throw an error
24+
fatalError("nil URL")
25+
}
26+
27+
let node = _CFXMLParseDTD(urlString)
28+
if node == nil {
29+
//TODO: throw error
30+
fatalError("parsing dtd string failed")
31+
}
32+
self.init(ptr: node)
33+
}
34+
35+
public convenience init(data: NSData, options mask: Int) throws {
36+
var unmanagedError: Unmanaged<CFErrorRef>? = nil
37+
let node = _CFXMLParseDTDFromData(data._cfObject, &unmanagedError)
38+
if node == nil {
39+
if let error = unmanagedError?.takeRetainedValue()._nsObject {
40+
throw error
41+
}
42+
}
43+
44+
self.init(ptr: node)
45+
} //primitive
1946

2047
/*!
2148
@method publicID
2249
@abstract Sets the public id. This identifier should be in the default catalog in /etc/xml/catalog or in a path specified by the environment variable XML_CATALOG_FILES. When the public id is set the system id must also be set.
2350
*/
24-
public var publicID: String? //primitive
51+
public var publicID: String? {
52+
get {
53+
return _CFXMLDTDExternalID(_xmlDTD)?._swiftObject
54+
}
55+
56+
set {
57+
if let value = newValue {
58+
_CFXMLDTDSetExternalID(_xmlDTD, value)
59+
} else {
60+
_CFXMLDTDSetExternalID(_xmlDTD, nil)
61+
}
62+
}
63+
}
2564

2665
/*!
2766
@method systemID
2867
@abstract Sets the system id. This should be a URL that points to a valid DTD.
2968
*/
30-
public var systemID: String? //primitive
31-
69+
public var systemID: String? {
70+
get {
71+
return _CFXMLDTDSystemID(_xmlDTD)?._swiftObject
72+
}
73+
74+
set {
75+
if let value = newValue {
76+
_CFXMLDTDSetSystemID(_xmlDTD, value)
77+
} else {
78+
_CFXMLDTDSetSystemID(_xmlDTD, nil)
79+
}
80+
}
81+
}
82+
3283
/*!
3384
@method insertChild:atIndex:
3485
@abstract Inserts a child at a particular index.
3586
*/
36-
public func insertChild(child: NSXMLNode, atIndex index: Int) { NSUnimplemented() } //primitive
87+
public func insertChild(child: NSXMLNode, atIndex index: Int) {
88+
_insertChild(child, atIndex: index)
89+
} //primitive
3790

3891
/*!
3992
@method insertChildren:atIndex:
4093
@abstract Insert several children at a particular index.
4194
*/
42-
public func insertChildren(children: [NSXMLNode], atIndex index: Int) { NSUnimplemented() }
95+
public func insertChildren(children: [NSXMLNode], atIndex index: Int) {
96+
_insertChildren(children, atIndex: index)
97+
}
4398

4499
/*!
45100
@method removeChildAtIndex:
46101
@abstract Removes a child at a particular index.
47102
*/
48-
public func removeChildAtIndex(index: Int) { NSUnimplemented() } //primitive
103+
public func removeChildAtIndex(index: Int) {
104+
_removeChildAtIndex(index)
105+
} //primitive
49106

50107
/*!
51108
@method setChildren:
52109
@abstract Removes all existing children and replaces them with the new children. Set children to nil to simply remove all children.
53110
*/
54-
public func setChildren(children: [NSXMLNode]?) { NSUnimplemented() } //primitive
111+
public func setChildren(children: [NSXMLNode]?) {
112+
_setChildren(children)
113+
} //primitive
55114

56115
/*!
57116
@method addChild:
58117
@abstract Adds a child to the end of the existing children.
59118
*/
60-
public func addChild(child: NSXMLNode) { NSUnimplemented() }
119+
public func addChild(child: NSXMLNode) {
120+
_addChild(child)
121+
}
61122

62123
/*!
63124
@method replaceChildAtIndex:withNode:
64125
@abstract Replaces a child at a particular index with another child.
65126
*/
66-
public func replaceChildAtIndex(index: Int, withNode node: NSXMLNode) { NSUnimplemented() }
127+
public func replaceChildAtIndex(index: Int, withNode node: NSXMLNode) {
128+
_replaceChildAtIndex(index, withNode: node)
129+
}
67130

68131
/*!
69132
@method entityDeclarationForName:
70133
@abstract Returns the entity declaration matching this name.
71134
*/
72-
public func entityDeclarationForName(name: String) -> NSXMLDTDNode? { NSUnimplemented() } //primitive
135+
public func entityDeclarationForName(name: String) -> NSXMLDTDNode? {
136+
let node = _CFXMLDTDGetEntityDesc(_xmlDTD, name)
137+
if node == nil {
138+
return nil
139+
}
140+
return NSXMLDTDNode._objectNodeForNode(node)
141+
} //primitive
73142

74143
/*!
75144
@method notationDeclarationForName:
76145
@abstract Returns the notation declaration matching this name.
77146
*/
78-
public func notationDeclarationForName(name: String) -> NSXMLDTDNode? { NSUnimplemented() } //primitive
147+
public func notationDeclarationForName(name: String) -> NSXMLDTDNode? {
148+
let node = _CFXMLDTDGetNotationDesc(_xmlDTD, name)
149+
150+
if node == nil {
151+
return nil
152+
}
153+
return NSXMLDTDNode._objectNodeForNode(node)
154+
} //primitive
79155

80156
/*!
81157
@method elementDeclarationForName:
82158
@abstract Returns the element declaration matching this name.
83159
*/
84-
public func elementDeclarationForName(name: String) -> NSXMLDTDNode? { NSUnimplemented() } //primitive
160+
public func elementDeclarationForName(name: String) -> NSXMLDTDNode? {
161+
let node = _CFXMLDTDGetElementDesc(_xmlDTD, name)
162+
163+
if node == nil {
164+
return nil
165+
}
166+
return NSXMLDTDNode._objectNodeForNode(node)
167+
} //primitive
85168

86169
/*!
87170
@method attributeDeclarationForName:
88171
@abstract Returns the attribute declaration matching this name.
89172
*/
90-
public func attributeDeclarationForName(name: String, elementName: String) -> NSXMLDTDNode? { NSUnimplemented() } //primitive
173+
public func attributeDeclarationForName(name: String, elementName: String) -> NSXMLDTDNode? {
174+
let node = _CFXMLDTDGetAttributeDesc(_xmlDTD, elementName, name)
175+
176+
if node == nil {
177+
return nil
178+
}
179+
return NSXMLDTDNode._objectNodeForNode(node)
180+
} //primitive
91181

92182
/*!
93183
@method predefinedEntityDeclarationForName:
94184
@abstract Returns the predefined entity declaration matching this name.
95185
@discussion The five predefined entities are
96186
<ul><li>&amp;lt; - &lt;</li><li>&amp;gt; - &gt;</li><li>&amp;amp; - &amp;</li><li>&amp;quot; - &quot;</li><li>&amp;apos; - &amp;</li></ul>
97187
*/
98-
public class func predefinedEntityDeclarationForName(name: String) -> NSXMLDTDNode? { NSUnimplemented() }
188+
public class func predefinedEntityDeclarationForName(name: String) -> NSXMLDTDNode? {
189+
let node = _CFXMLDTDGetPredefinedEntity(name)
190+
191+
if node == nil {
192+
return nil
193+
}
194+
195+
return NSXMLDTDNode._objectNodeForNode(node)
196+
}
99197

100198
internal override class func _objectNodeForNode(node: _CFXMLNodePtr) -> NSXMLDTD {
101199
precondition(_CFXMLNodeGetType(node) == _kCFXMLTypeDTD)

0 commit comments

Comments
 (0)