This repository was archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMPWTagHandler.m
More file actions
258 lines (205 loc) · 7.33 KB
/
MPWTagHandler.m
File metadata and controls
258 lines (205 loc) · 7.33 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
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
//
// MPWTagHandler.m
// MPWXmlKit
//
// Created by Marcel Weiher on 2/19/08.
// Copyright 2008 Marcel Weiher. All rights reserved.
//
#import "MPWTagHandler.h"
#import "MPWTagAction.h"
#import "mpwfoundation_imports.h"
#import <objc/objc.h>
@implementation MPWTagHandler
objectAccessor( NSDictionary , exceptionMap, setExceptionMap )
idAccessor( attributeMap, setAttributeMap )
idAccessor( namespaceString, setNamespaceString )
objectAccessor(NSMutableDictionary, tagDict, setTagDict)
objectAccessor(MPWSmallStringTable, tagTable, setTagTable)
objectAccessor(MPWTagAction, undeclared, setUndeclared )
boolAccessor(isCaseInsensitive, setIsCaseInsensitive)
-init
{
self=[super init];
[self setTagDict:[NSMutableDictionary dictionaryWithCapacity:5]];
return self;
}
+tagHandler
{
return [[[self alloc] init] autorelease];
}
-(void)addTag:(NSString*)tag
{
if ( ![[self tagDict] objectForKey:tag]) {
MPWTagAction *action=[[[MPWTagAction alloc] initWithTagName:tag] autorelease];
[action setMappedName:[[self exceptionMap] objectForKey:tag]];
[[self tagDict] setObject:action forKey:tag];
}
}
-(MPWTagAction*)tagForKey:(NSString*)tag
{
return [[self tagDict] objectForKey:tag];
}
-(void)setUndeclaredElementHandler:handler backup:backup
{
MPWTagAction *u = [MPWTagAction undeclaredElementAction];
[u setElementInvocationForTarget:handler backup:backup];
[[self tagDict] setObject:u forKey:[u tagName]];
[u setTagName:nil];
[self setUndeclared:u];
}
-(void)declareAttributes:(NSArray*)attributes
{
[self setAttributeMap:[[[MPWSmallStringTable alloc] initWithKeys:attributes values:attributes] autorelease]];
}
-description
{
return [NSString stringWithFormat:@"<%@/%p: %@>",[self class],self,namespaceString];
}
-(void)dealloc
{
// NSLog(@"tag handler dealloc");
[exceptionMap release];
[attributeMap release];
[super dealloc];
// NSLog(@"tag handler did dealloc");
}
-actionForCString:(const char*)aCstring length:(long)len
{
if (!tagTable) {
[self buildLookupTables];
}
return OBJECTFORSTRINGLENGTH(tagTable, (char*)aCstring, (int)len);
}
-actionForCString:(const char*)aCstring
{
return [self actionForCString:aCstring length:strlen(aCstring)];
}
-getTagForCString1:(const char*)cstr length:(int)len
{
return [[self actionForCString:cstr length:len] tagName];
}
-elementHandlerInvocationForCString1:(const char*)cstr length:(int)len
{
return [[self actionForCString:cstr length:len] elementAction];
}
-tagHandlerInvocationForCString1:(const char*)cstr length:(int)len
{
return [[self actionForCString:cstr length:len] tagAction];
}
-(void)buildLookupTables
{
Class tableClass= isCaseInsensitive ? [MPWCaseInsensitiveSmallStringTable class] : [MPWSmallStringTable class];
NSArray *keys=[[self tagDict] allKeys];
NSArray *values=[[self tagDict] objectsForKeys:keys notFoundMarker:@""]; //[[[[self tagDict] collect] objectForKey:[keys each]]];
MPWSmallStringTable *stringTable = [[[tableClass alloc] initWithKeys:keys values:values] autorelease];
[stringTable setDefaultValue:[self undeclared]];
[self setTagTable:stringTable];
}
-(void)initializeElementActionMapWithTags:(NSArray*)keys target:actionTarget prefix:prefix
{
for ( NSString *key in keys ){
[self addTag:key];
MPWTagAction *action=[self tagForKey:key];
[action setNamespacePrefix:prefix];
[action setElementInvocationForTarget:actionTarget backup:nil];
}
}
-(void)initializeTagActionMapWithTags:(NSArray*)keys target:actionTarget prefix:prefix
{
for ( NSString *key in keys ){
[self addTag:key];
MPWTagAction *action=[self tagForKey:key];
[action setTagInvocationForTarget:actionTarget];
}
}
-(void)setInvocation:anInvocation forElement:(NSString*)tagName
{
MPWTagAction* action = [[self tagDict] objectForKey:tagName];
[action setElementAction:anInvocation];
}
@end
@interface MPWTagHandlerTesting : NSObject {}
@end
@implementation MPWTagHandlerTesting
+dummyElement:children attributes:attrs parser:paser
{
return @"53";
}
+dummyNamespaceElement:children attributes:attrs parser:paser
{
return @"62";
}
+dummyTag:tag parser:paser
{
return @"";
}
+(void)testElementHandlerForCString
{
id handler=[[[MPWTagHandler alloc] init] autorelease];
id invocation;
[handler initializeElementActionMapWithTags:[NSArray arrayWithObjects:@"dummy",nil] target:self prefix:@""];
invocation = [[handler actionForCString:"dummy" length:5] elementAction];
EXPECTNOTNIL(invocation, @"invocation");
IDEXPECT( [invocation resultOfInvoking], @"53", @"result of invoking");
}
+(void)testElementHandlerForCStringWithPrefix
{
id handler=[[[MPWTagHandler alloc] init] autorelease];
id invocation;
[handler initializeElementActionMapWithTags:[NSArray arrayWithObjects:@"dummy",nil] target:self prefix:@"Namespace"];
invocation = [[handler actionForCString:"dummy" length:5] elementAction];
EXPECTNOTNIL(invocation, @"invocation");
IDEXPECT( [invocation resultOfInvoking], @"62", @"result of invoking");
}
+(void)testTagHandlerCreatesActions
{
id handler=[MPWTagHandler tagHandler];
[handler addTag:@"hello"];
EXPECTNOTNIL([handler tagForKey:@"hello"], @"hello retrieved");
}
+(void)testCreateCstringLookupFromActionsDict
{
id handler=[MPWTagHandler tagHandler];
[handler addTag:@"hello"];
MPWTagAction *action=[handler tagForKey:@"hello"];
EXPECTNOTNIL(action, @"hello retrieved");
MPWTagAction *action1=[handler actionForCString:"hello"];
EXPECTNOTNIL(action1, @"after setup yet");
IDEXPECT(action, action1, @"retrieved via base dict and small string table");
EXPECTNIL([handler actionForCString:"hello1"], @"hello1 should not be in table");
}
+(void)testMakeElementActions
{
id handler=[MPWTagHandler tagHandler];
[handler initializeElementActionMapWithTags:[NSArray arrayWithObjects:@"dummy",nil] target:self prefix:@""];
MPWTagAction *dummyAction=[handler tagForKey:@"dummy"];
EXPECTNOTNIL(dummyAction, @"dummy action retrieved from nsdict");
IDEXPECT([dummyAction tagName], @"dummy", @"retrieved tag name");
MPWFastInvocation *elementInvocation=[dummyAction elementAction];
EXPECTNOTNIL(elementInvocation, @"element action");
IDEXPECT(NSStringFromSelector([elementInvocation selector]),@"dummyElement:attributes:parser:",@"selector");
IDEXPECT([elementInvocation target],self,@"target")
MPWTagAction *action1=[handler actionForCString:"dummy"];
MPWFastInvocation *invocation1=[action1 elementAction];
IDEXPECT(NSStringFromSelector([invocation1 selector]),@"dummyElement:attributes:parser:",@"selector");
}
+(void)testMakeTagActions
{
id handler=[MPWTagHandler tagHandler];
[handler initializeTagActionMapWithTags:[NSArray arrayWithObjects:@"dummy",nil] target:self prefix:@""];
MPWTagAction *action1=[handler actionForCString:"dummy"];
MPWFastInvocation *invocation1=[action1 tagAction];
IDEXPECT(NSStringFromSelector([invocation1 selector]),@"dummyTag:parser:",@"selector");
}
+(NSArray*)testSelectors
{
return [NSArray arrayWithObjects:
@"testElementHandlerForCString",
@"testElementHandlerForCStringWithPrefix",
@"testTagHandlerCreatesActions",
@"testCreateCstringLookupFromActionsDict",
@"testMakeElementActions",
@"testMakeTagActions",
nil];
}
@end