-
Notifications
You must be signed in to change notification settings - Fork 0
/
CXMLElement.m
executable file
·286 lines (248 loc) · 8.75 KB
/
CXMLElement.m
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//
// CXMLElement.m
// TouchCode
//
// Created by Jonathan Wight on 03/07/08.
// Copyright 2008 toxicsoftware.com. All rights reserved.
//
// 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 "CXMLElement.h"
#import "CXMLNode_PrivateExtensions.h"
#import "CXMLNode_CreationExtensions.h"
#import "CXMLNamespaceNode.h"
@implementation CXMLElement
- (NSArray *)elementsForName:(NSString *)name
{
NSMutableArray *theElements = [NSMutableArray array];
// TODO -- native xml api?
const xmlChar *theName = (const xmlChar *)[name UTF8String];
xmlNodePtr theCurrentNode = _node->children;
while (theCurrentNode != NULL)
{
if (theCurrentNode->type == XML_ELEMENT_NODE && xmlStrcmp(theName, theCurrentNode->name) == 0)
{
CXMLNode *theNode = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode freeOnDealloc:NO];
[theElements addObject:theNode];
}
theCurrentNode = theCurrentNode->next;
}
return(theElements);
}
- (NSArray *)elementsForLocalName:(NSString *)localName URI:(NSString *)URI
{
if (URI == nil || [URI length] == 0)
return [self elementsForName:localName];
NSMutableArray *theElements = [NSMutableArray array];
const xmlChar *theLocalName = (const xmlChar *)[localName UTF8String];
const xmlChar *theNamespaceName = (const xmlChar *)[URI UTF8String];
xmlNodePtr theCurrentNode = _node->children;
while (theCurrentNode != NULL)
{
if (theCurrentNode->type == XML_ELEMENT_NODE
&& xmlStrcmp(theLocalName, theCurrentNode->name) == 0
&& theCurrentNode->ns
&& xmlStrcmp(theNamespaceName, theCurrentNode->ns->href) == 0)
{
CXMLNode *theNode = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode freeOnDealloc:NO];
[theElements addObject:theNode];
}
theCurrentNode = theCurrentNode->next;
}
return theElements;
}
- (NSArray *)attributes
{
NSMutableArray *theAttributes = [NSMutableArray array];
xmlAttrPtr theCurrentNode = _node->properties;
while (theCurrentNode != NULL)
{
CXMLNode *theAttribute = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode freeOnDealloc:NO];
[theAttributes addObject:theAttribute];
theCurrentNode = theCurrentNode->next;
}
return(theAttributes);
}
- (CXMLNode *)attributeForName:(NSString *)name
{
// TODO -- look for native libxml2 function for finding a named attribute (like xmlGetProp)
NSRange split = [name rangeOfString:@":"];
xmlChar *theLocalName = NULL;
xmlChar *thePrefix = NULL;
if (split.length > 0)
{
theLocalName = (xmlChar *)[[name substringFromIndex:split.location + 1] UTF8String];
thePrefix = (xmlChar *)[[name substringToIndex:split.location] UTF8String];
}
else
{
theLocalName = (xmlChar *)[name UTF8String];
}
xmlAttrPtr theCurrentNode = _node->properties;
while (theCurrentNode != NULL)
{
if (xmlStrcmp(theLocalName, theCurrentNode->name) == 0)
{
if (thePrefix == NULL || (theCurrentNode->ns
&& theCurrentNode->ns->prefix
&& xmlStrcmp(thePrefix, theCurrentNode->ns->prefix) == 0))
{
CXMLNode *theAttribute = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode freeOnDealloc:NO];
return(theAttribute);
}
}
theCurrentNode = theCurrentNode->next;
}
return(NULL);
}
- (CXMLNode *)attributeForLocalName:(NSString *)localName URI:(NSString *)URI
{
if (URI == nil)
return [self attributeForName:localName];
// TODO -- look for native libxml2 function for finding a named attribute (like xmlGetProp)
const xmlChar *theLocalName = (const xmlChar *)[localName UTF8String];
const xmlChar *theNamespaceName = (const xmlChar *)[URI UTF8String];
xmlAttrPtr theCurrentNode = _node->properties;
while (theCurrentNode != NULL)
{
if (theCurrentNode->ns && theCurrentNode->ns->href &&
xmlStrcmp(theLocalName, theCurrentNode->name) == 0 &&
xmlStrcmp(theNamespaceName, theCurrentNode->ns->href) == 0)
{
CXMLNode *theAttribute = [CXMLNode nodeWithLibXMLNode:(xmlNodePtr)theCurrentNode freeOnDealloc:NO];
return(theAttribute);
}
theCurrentNode = theCurrentNode->next;
}
return(NULL);
}
- (NSArray *)namespaces
{
NSMutableArray *theNamespaces = [[[NSMutableArray alloc] init] autorelease];
xmlNsPtr theCurrentNamespace = _node->nsDef;
while (theCurrentNamespace != NULL)
{
NSString *thePrefix = theCurrentNamespace->prefix ? [NSString stringWithUTF8String:(const char *)theCurrentNamespace->prefix] : @"";
NSString *theURI = [NSString stringWithUTF8String:(const char *)theCurrentNamespace->href];
CXMLNamespaceNode *theNode = [[CXMLNamespaceNode alloc] initWithPrefix:thePrefix URI:theURI parentElement:self];
[theNamespaces addObject:theNode];
[theNode release];
theCurrentNamespace = theCurrentNamespace->next;
}
return theNamespaces;
}
- (CXMLNode *)namespaceForPrefix:(NSString *)name
{
const xmlChar *thePrefix = (const xmlChar *)[name UTF8String];
xmlNsPtr theCurrentNamespace = _node->nsDef;
while (theCurrentNamespace != NULL)
{
if (xmlStrcmp(theCurrentNamespace->prefix, thePrefix) == 0)
{
NSString *thePrefix = theCurrentNamespace->prefix ? [NSString stringWithUTF8String:(const char *)theCurrentNamespace->prefix] : @"";
NSString *theURI = [NSString stringWithUTF8String:(const char *)theCurrentNamespace->href];
return [[[CXMLNamespaceNode alloc] initWithPrefix:thePrefix URI:theURI parentElement:self] autorelease];
}
theCurrentNamespace = theCurrentNamespace->next;
}
return nil;
}
- (CXMLNode *)resolveNamespaceForName:(NSString *)name
{
NSRange split = [name rangeOfString:@":"];
if (split.length > 0)
return [self namespaceForPrefix:[name substringToIndex:split.location]];
xmlNsPtr theCurrentNamespace = _node->nsDef;
while (theCurrentNamespace != NULL)
{
if (theCurrentNamespace->prefix == 0
|| (theCurrentNamespace->prefix)[0] == 0)
{
NSString *thePrefix = theCurrentNamespace->prefix ? [NSString stringWithUTF8String:(const char *)theCurrentNamespace->prefix] : @"";
NSString *theURI = [NSString stringWithUTF8String:(const char *)theCurrentNamespace->href];
return [[[CXMLNamespaceNode alloc] initWithPrefix:thePrefix URI:theURI parentElement:self] autorelease];
}
theCurrentNamespace = theCurrentNamespace->next;
}
return nil;
}
- (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI
{
const xmlChar *theXMLURI = (const xmlChar *)[namespaceURI UTF8String];
xmlNsPtr theCurrentNamespace = _node->nsDef;
while (theCurrentNamespace != NULL)
{
if (xmlStrcmp(theCurrentNamespace->href, theXMLURI) == 0)
{
if(theCurrentNamespace->prefix)
return [NSString stringWithUTF8String:(const char *)theCurrentNamespace->prefix];
return @"";
}
theCurrentNamespace = theCurrentNamespace->next;
}
return nil;
}
//- (NSString*)_XMLStringWithOptions:(NSUInteger)options appendingToString:(NSMutableString*)str
//{
//NSString* name = [self name];
//[str appendString:[NSString stringWithFormat:@"<%@", name]];
//
//for (id attribute in [self attributes] )
// {
// [attribute _XMLStringWithOptions:options appendingToString:str];
// }
//
//if ( ! _node->children )
// {
// bool isEmpty = NO;
// NSArray *emptyTags = [NSArray arrayWithObjects: @"br", @"area", @"link", @"img", @"param", @"hr", @"input", @"col", @"base", @"meta", nil ];
// for (id s in emptyTags)
// {
// if ( [s isEqualToString:@"base"] )
// {
// isEmpty = YES;
// break;
// }
// }
// if ( isEmpty )
// {
// [str appendString:@"/>"];
// return str;
// }
// }
//
//[str appendString:@">"];
//
//if ( _node->children )
// {
// for (id child in [self children])
// [child _XMLStringWithOptions:options appendingToString:str];
// }
//[str appendString:[NSString stringWithFormat:@"</%@>", name]];
//return str;
//}
- (NSString *)description
{
NSAssert(_node != NULL, @"TODO");
return([NSString stringWithFormat:@"<%@ %p [%p] %@ %@>", NSStringFromClass([self class]), self, self->_node, [self name], [self XMLStringWithOptions:0]]);
}
@end