forked from SVGKit/SVGKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNamedNodeMap.m
More file actions
173 lines (130 loc) · 5.16 KB
/
Copy pathNamedNodeMap.m
File metadata and controls
173 lines (130 loc) · 5.16 KB
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
//
// NamedNodeMap.m
// SVGKit
//
// Created by adam on 22/05/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "NamedNodeMap.h"
@interface NamedNodeMap()
@property(nonatomic,retain) NSMutableDictionary* internalDictionary;
@property(nonatomic,retain) NSMutableDictionary* internalDictionaryOfNamespaces;
@end
@implementation NamedNodeMap
@synthesize internalDictionary;
@synthesize internalDictionaryOfNamespaces;
- (id)init {
self = [super init];
if (self) {
self.internalDictionary = [NSMutableDictionary dictionary];
self.internalDictionaryOfNamespaces = [NSMutableDictionary dictionary];
}
return self;
}
-(Node*) getNamedItem:(NSString*) name
{
Node* simpleResult = [self.internalDictionary objectForKey:name];
if( simpleResult == nil )
{
/**
Check the namespaces in turn, to see if we can find this node in one of them
NB: according to spec, this behaviour is:
"The result depends on the implementation"
I've chosen to implement it the most user-friendly way possible. It is NOT the best
solution IMHO - the spec authors should have defined the outcome!
*/
for( NSString* key in [self.internalDictionaryOfNamespaces allKeys] )
{
simpleResult = [self getNamedItemNS:key localName:name];
if( simpleResult != nil )
break;
}
}
return simpleResult;
}
-(Node*) setNamedItem:(Node*) arg
{
NSAssert( [[self.internalDictionaryOfNamespaces allKeys] count] < 1, @"WARNING: you are using namespaced attributes in parallel with non-namespaced. According to the DOM Spec, this leads to UNDEFINED behaviour. This is insane - you do NOT want to be doing this! Crashing deliberately...." );
Node* oldNode = [self.internalDictionary objectForKey:arg.localName];
[self.internalDictionary setObject:arg forKey:arg.localName];
return oldNode;
}
-(Node*) removeNamedItem:(NSString*) name
{
NSAssert( [[self.internalDictionaryOfNamespaces allKeys] count] < 1, @"WARNING: you are using namespaced attributes in parallel with non-namespaced. According to the DOM Spec, this leads to UNDEFINED behaviour. This is insane - you do NOT want to be doing this! Crashing deliberately...." );
Node* oldNode = [self.internalDictionary objectForKey:name];
[self.internalDictionary removeObjectForKey:name];
return oldNode;
}
-(unsigned long)length
{
int count = [self.internalDictionary count];
for( NSDictionary* namespaceDict in self.internalDictionaryOfNamespaces )
{
count += [namespaceDict count];
}
return count;
}
-(Node*) item:(unsigned long) index
{
if( index < [self.internalDictionary count] )
return [self.internalDictionary.allValues objectAtIndex:index];
else
{
index -= self.internalDictionary.count;
for( NSDictionary* namespaceDict in self.internalDictionaryOfNamespaces )
{
if( index < [namespaceDict count] )
return [namespaceDict.allValues objectAtIndex:index];
else
index -= [namespaceDict count];
}
}
return nil;
}
// Introduced in DOM Level 2:
-(Node*) getNamedItemNS:(NSString*) namespaceURI localName:(NSString*) localName
{
NSMutableDictionary* namespaceDict = [self.internalDictionaryOfNamespaces objectForKey:namespaceURI];
return [namespaceDict objectForKey:localName];
}
// Introduced in DOM Level 2:
-(Node*) setNamedItemNS:(Node*) arg
{
return [self setNamedItemNS:arg inNodeNamespace:nil];
}
// Introduced in DOM Level 2:
-(Node*) removeNamedItemNS:(NSString*) namespaceURI localName:(NSString*) localName
{
NSMutableDictionary* namespaceDict = [self.internalDictionaryOfNamespaces objectForKey:namespaceURI];
Node* oldNode = [namespaceDict objectForKey:localName];
[namespaceDict removeObjectForKey:localName];
return oldNode;
}
#pragma mark - MISSING METHOD FROM SVG Spec, without which you cannot parse documents (don't understand how they intended you to fulfil the spec without this method)
-(Node*) setNamedItemNS:(Node*) arg inNodeNamespace:(NSString*) nodesNamespace
{
NSString* effectiveNamespace = arg.namespaceURI != nil ? arg.namespaceURI : nodesNamespace;
if( effectiveNamespace == nil )
{
return [self setNamedItem:arg]; // this should never happen, but there's a lot of malformed SVG files out in the wild
}
NSMutableDictionary* namespaceDict = [self.internalDictionaryOfNamespaces objectForKey:effectiveNamespace];
if( namespaceDict == nil )
{
namespaceDict = [NSMutableDictionary dictionary];
[self.internalDictionaryOfNamespaces setObject:namespaceDict forKey:effectiveNamespace];
}
Node* oldNode = [namespaceDict objectForKey:arg.localName];
[namespaceDict setObject:arg forKey:arg.localName];
return oldNode;
}
#pragma mark - ADDITIONAL to SVG Spec: useful debug / output / description methods
-(NSString *)description
{
/** test (and output) both the "DOM 1" and "DOM 2" dictionaries, if they're non-empty */
NSString* dom1 = self.internalDictionary.count > 0 ? [NSString stringWithFormat:@"DOM-v1(%@)", self.internalDictionary] : nil;
NSString* dom2 = self.internalDictionaryOfNamespaces.count > 0 ? [NSString stringWithFormat:@"DOM-v2(%@)", self.internalDictionaryOfNamespaces] : nil;
return [NSString stringWithFormat:@"NamedNodeMap: %@%@%@", dom1, dom1 != nil && dom2 != nil ? @"\n" : @"", dom2 ];
}
@end