forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFXMLTree.c
249 lines (230 loc) · 11.4 KB
/
CFXMLTree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
/* CFXMLTree.c
Copyright (c) 1999 - 2015 Apple Inc. and the Swift project authors
Responsibility: David Smith
*/
#include "CFInternal.h"
#include <CoreFoundation/CFXMLParser.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
/*************/
/* CFXMLTree */
/*************/
/* Creates a childless node from desc */
CFXMLTreeRef CFXMLTreeCreateWithNode(CFAllocatorRef allocator, CFXMLNodeRef node) {
CFTreeContext treeCtxt;
treeCtxt.version = 0;
treeCtxt.info = (void *)node;
treeCtxt.retain = CFRetain;
treeCtxt.release = CFRelease;
treeCtxt.copyDescription = CFCopyDescription;
return CFTreeCreate(allocator, &treeCtxt);
}
CFXMLNodeRef CFXMLTreeGetNode(CFXMLTreeRef xmlNode) {
CFTreeContext treeContext;
treeContext.version = 0;
CFTreeGetContext(xmlNode, &treeContext);
return (CFXMLNodeRef)treeContext.info;
}
// We will probably ultimately want to export this under some public API name
CF_PRIVATE Boolean CFXMLTreeEqual(CFXMLTreeRef xmlTree1, CFXMLTreeRef xmlTree2) {
CFXMLNodeRef node1, node2;
CFXMLTreeRef child1, child2;
if (CFTreeGetChildCount(xmlTree1) != CFTreeGetChildCount(xmlTree2)) return false;
node1 = CFXMLTreeGetNode(xmlTree1);
node2 = CFXMLTreeGetNode(xmlTree2);
if (!CFEqual(node1, node2)) return false;
for (child1 = CFTreeGetFirstChild(xmlTree1), child2 = CFTreeGetFirstChild(xmlTree2); child1 && child2; child1 = CFTreeGetNextSibling(child1), child2 = CFTreeGetNextSibling(child2)) {
if (!CFXMLTreeEqual(child1, child2)) return false;
}
return true;
}
static void _CFAppendXML(CFMutableStringRef str, CFXMLTreeRef tree);
static void _CFAppendXMLProlog(CFMutableStringRef str, const CFXMLTreeRef node);
static void _CFAppendXMLEpilog(CFMutableStringRef str, const CFXMLTreeRef node);
CFDataRef CFXMLTreeCreateXMLData(CFAllocatorRef allocator, CFXMLTreeRef xmlTree) {
CFMutableStringRef xmlStr;
CFDataRef result;
CFStringEncoding encoding;
__CFGenericValidateType(xmlTree, CFTreeGetTypeID());
xmlStr = CFStringCreateMutable(allocator, 0);
_CFAppendXML(xmlStr, xmlTree);
if (CFXMLNodeGetTypeCode(CFXMLTreeGetNode(xmlTree)) == kCFXMLNodeTypeDocument) {
const CFXMLDocumentInfo *docData = (CFXMLDocumentInfo *)CFXMLNodeGetInfoPtr(CFXMLTreeGetNode(xmlTree));
encoding = docData ? docData->encoding : kCFStringEncodingUTF8;
} else {
encoding = kCFStringEncodingUTF8;
}
result = CFStringCreateExternalRepresentation(allocator, xmlStr, encoding, 0);
CFRelease(xmlStr);
return result;
}
static void _CFAppendXML(CFMutableStringRef str, CFXMLTreeRef tree) {
CFXMLTreeRef child;
_CFAppendXMLProlog(str, tree);
for (child = CFTreeGetFirstChild(tree); child; child = CFTreeGetNextSibling(child)) {
_CFAppendXML(str, child);
}
_CFAppendXMLEpilog(str, tree);
}
CF_PRIVATE void appendQuotedString(CFMutableStringRef str, CFStringRef strToQuote) {
char quoteChar = CFStringFindWithOptions(strToQuote, CFSTR("\""), CFRangeMake(0, CFStringGetLength(strToQuote)), 0, NULL) ? '\'' : '\"';
CFStringAppendFormat(str, NULL, CFSTR("%c%@%c"), quoteChar, strToQuote, quoteChar);
}
static void appendExternalID(CFMutableStringRef str, CFXMLExternalID *extID) {
if (extID->publicID) {
CFStringAppendCString(str, " PUBLIC ", kCFStringEncodingASCII);
appendQuotedString(str, extID->publicID);
if (extID->systemID) {
// Technically, for externalIDs, systemID must not be NULL. However, by testing for a NULL systemID, we can use this to emit publicIDs, too. REW, 2/15/2000
CFStringAppendCString(str, " ", kCFStringEncodingASCII);
appendQuotedString(str, CFURLGetString(extID->systemID));
}
} else if (extID->systemID) {
CFStringAppendCString(str, " SYSTEM ", kCFStringEncodingASCII);
appendQuotedString(str, CFURLGetString(extID->systemID));
} else {
// Should never get here
}
}
static void appendElementProlog(CFMutableStringRef str, CFXMLTreeRef tree) {
const CFXMLElementInfo *data = (CFXMLElementInfo *)CFXMLNodeGetInfoPtr(CFXMLTreeGetNode(tree));
CFStringAppendFormat(str, NULL, CFSTR("<%@"), CFXMLNodeGetString(CFXMLTreeGetNode(tree)));
if (data->attributeOrder) {
CFIndex i, c = CFArrayGetCount(data->attributeOrder);
for (i = 0; i < c; i ++) {
CFStringRef attr = (CFStringRef)CFArrayGetValueAtIndex(data->attributeOrder, i);
CFStringRef value = (CFStringRef)CFDictionaryGetValue(data->attributes, attr);
CFStringAppendFormat(str, NULL, CFSTR(" %@="), attr);
appendQuotedString(str, value);
}
}
if (data->isEmpty) {
CFStringAppendCString(str, "/>", kCFStringEncodingASCII);
} else {
CFStringAppendCString(str, ">", kCFStringEncodingASCII);
}
}
/* Although named "prolog", for leafs of the tree, this is the only XML generation function called. This is why Comments, Processing Instructions, etc. generate their XML during this function. REW, 2/11/2000 */
static void _CFAppendXMLProlog(CFMutableStringRef str, const CFXMLTreeRef tree) {
switch (CFXMLNodeGetTypeCode(CFXMLTreeGetNode(tree))) {
case kCFXMLNodeTypeDocument:
break;
case kCFXMLNodeTypeElement:
appendElementProlog(str, tree);
break;
case kCFXMLNodeTypeAttribute:
// Should never be encountered
break;
case kCFXMLNodeTypeProcessingInstruction: {
CFXMLProcessingInstructionInfo *data = (CFXMLProcessingInstructionInfo *)CFXMLNodeGetInfoPtr(CFXMLTreeGetNode(tree));
if (data->dataString) {
CFStringAppendFormat(str, NULL, CFSTR("<?%@ %@?>"), CFXMLNodeGetString(CFXMLTreeGetNode(tree)), data->dataString);
} else {
CFStringAppendFormat(str, NULL, CFSTR("<?%@?>"), CFXMLNodeGetString(CFXMLTreeGetNode(tree)));
}
break;
}
case kCFXMLNodeTypeComment:
CFStringAppendFormat(str, NULL, CFSTR("<!--%@-->"), CFXMLNodeGetString(CFXMLTreeGetNode(tree)));
break;
case kCFXMLNodeTypeText:
CFStringAppend(str, CFXMLNodeGetString(CFXMLTreeGetNode(tree)));
break;
case kCFXMLNodeTypeCDATASection:
CFStringAppendFormat(str, NULL, CFSTR("<![CDATA[%@]]>"), CFXMLNodeGetString(CFXMLTreeGetNode(tree)));
break;
case kCFXMLNodeTypeDocumentFragment:
break;
case kCFXMLNodeTypeEntity: {
CFXMLEntityInfo *data = (CFXMLEntityInfo *)CFXMLNodeGetInfoPtr(CFXMLTreeGetNode(tree));
CFStringAppendCString(str, "<!ENTITY ", kCFStringEncodingASCII);
if (data->entityType == kCFXMLEntityTypeParameter) {
CFStringAppend(str, CFSTR("% "));
}
CFStringAppend(str, CFXMLNodeGetString(CFXMLTreeGetNode(tree)));
CFStringAppend(str, CFSTR(" "));
if (data->replacementText) {
appendQuotedString(str, data->replacementText);
CFStringAppendCString(str, ">", kCFStringEncodingASCII);
} else {
appendExternalID(str, &(data->entityID));
if (data->notationName) {
CFStringAppendFormat(str, NULL, CFSTR(" NDATA %@"), data->notationName);
}
CFStringAppendCString(str, ">", kCFStringEncodingASCII);
}
break;
}
case kCFXMLNodeTypeEntityReference:
{
CFXMLEntityTypeCode entityType = ((CFXMLEntityReferenceInfo *)CFXMLNodeGetInfoPtr(CFXMLTreeGetNode(tree)))->entityType;
if (entityType == kCFXMLEntityTypeParameter) {
CFStringAppendFormat(str, NULL, CFSTR("%%%@;"), CFXMLNodeGetString(CFXMLTreeGetNode(tree)));
} else {
CFStringAppendFormat(str, NULL, CFSTR("&%@;"), CFXMLNodeGetString(CFXMLTreeGetNode(tree)));
}
break;
}
case kCFXMLNodeTypeDocumentType:
CFStringAppendCString(str, "<!DOCTYPE ", kCFStringEncodingASCII);
CFStringAppend(str, CFXMLNodeGetString(CFXMLTreeGetNode(tree)));
if (CFXMLNodeGetInfoPtr(CFXMLTreeGetNode(tree))) {
CFXMLExternalID *extID = &((CFXMLDocumentTypeInfo *)CFXMLNodeGetInfoPtr(CFXMLTreeGetNode(tree)))->externalID;
appendExternalID(str, extID);
}
CFStringAppendCString(str, " [", kCFStringEncodingASCII);
break;
case kCFXMLNodeTypeWhitespace:
CFStringAppend(str, CFXMLNodeGetString(CFXMLTreeGetNode(tree)));
break;
case kCFXMLNodeTypeNotation: {
CFXMLNotationInfo *data = (CFXMLNotationInfo *)CFXMLNodeGetInfoPtr(CFXMLTreeGetNode(tree));
CFStringAppendFormat(str, NULL, CFSTR("<!NOTATION %@ "), CFXMLNodeGetString(CFXMLTreeGetNode(tree)));
appendExternalID(str, &(data->externalID));
CFStringAppendCString(str, ">", kCFStringEncodingASCII);
break;
}
case kCFXMLNodeTypeElementTypeDeclaration:
CFStringAppendFormat(str, NULL, CFSTR("<!ELEMENT %@ %@>"), CFXMLNodeGetString(CFXMLTreeGetNode(tree)), ((CFXMLElementTypeDeclarationInfo *)CFXMLNodeGetInfoPtr(CFXMLTreeGetNode(tree)))->contentDescription);
break;
case kCFXMLNodeTypeAttributeListDeclaration: {
CFXMLAttributeListDeclarationInfo *attListData = (CFXMLAttributeListDeclarationInfo *)CFXMLNodeGetInfoPtr(CFXMLTreeGetNode(tree));
CFIndex idx;
CFStringAppendCString(str, "<!ATTLIST ", kCFStringEncodingASCII);
CFStringAppend(str, CFXMLNodeGetString(CFXMLTreeGetNode(tree)));
for (idx = 0; idx < attListData->numberOfAttributes; idx ++) {
CFXMLAttributeDeclarationInfo *attr = &(attListData->attributes[idx]);
CFStringAppendFormat(str, NULL, CFSTR("\n\t%@ %@ %@"), attr->attributeName, attr->typeString, attr->defaultString);
}
CFStringAppendCString(str, ">", kCFStringEncodingASCII);
break;
}
default:
CFAssert(false, __kCFLogAssertion, "Encountered unexpected XMLDataTypeID %d", CFXMLNodeGetTypeCode(CFXMLTreeGetNode(tree)));
}
}
static void _CFAppendXMLEpilog(CFMutableStringRef str, CFXMLTreeRef tree) {
CFXMLNodeTypeCode typeID = CFXMLNodeGetTypeCode(CFXMLTreeGetNode(tree));
if (typeID == kCFXMLNodeTypeElement) {
if (((CFXMLElementInfo *)CFXMLNodeGetInfoPtr(CFXMLTreeGetNode(tree)))->isEmpty) return;
CFStringAppendFormat(str, NULL, CFSTR("</%@>"), CFXMLNodeGetString(CFXMLTreeGetNode(tree)));
} else if (typeID == kCFXMLNodeTypeDocumentType) {
CFIndex len = CFStringGetLength(str);
if (CFStringHasSuffix(str, CFSTR(" ["))) {
// There were no in-line DTD elements
CFStringDelete(str, CFRangeMake(len-2, 2));
} else {
CFStringAppendCString(str, "]", kCFStringEncodingASCII);
}
CFStringAppendCString(str, ">", kCFStringEncodingASCII);
}
}
#pragma GCC diagnostic pop