forked from jessegrosjean/blocks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BConfigurationElement.m
224 lines (173 loc) · 7.52 KB
/
BConfigurationElement.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
//
// BConfigurationElement.m
// Blocks
//
// Created by Jesse Grosjean on 8/21/07.
// Copyright 2007 Blocks. All rights reserved.
//
#import "BConfigurationElement.h"
#import "BExecutableExtensionProxy.h"
#import "BPlugin.h"
#import "BExtension.h"
#import "BLog.h"
@interface BConfigurationElement (BConfigurationElementPrivate)
- (BExtension *)extension;
@end
@implementation BConfigurationElement
#pragma mark Init
- (id)init {
if (self = [super init]) {
}
return self;
}
#pragma mark Executable Extensions Factory
- (Class)executableExtensionClassFromAttribute:(NSString *)attributeName {
return [self createExecutableExtensionFromAttribute:attributeName conformingToClass:nil conformingToProtocol:nil respondingToSelectors:nil];
}
- (Class)executableExtensionClassFromAttribute:(NSString *)attributeName conformingToClass:(Class)aClass conformingToProtocol:(Protocol *)aProtocol respondingToSelectors:(NSArray *)selectors {
NSArray *executableExtensionClassNameAndSelector = [[self attributeForKey:attributeName] componentsSeparatedByString:@" "];
NSString *executableExtensionClassName = [executableExtensionClassNameAndSelector objectAtIndex:0];
Class executableExtensionClass = nil;
if (!executableExtensionClassName) {
BLogError(@"Failed to find executable extension class name attribute");
} else {
@try {
executableExtensionClass = [[[self declaringExtension] plugin] classNamed:executableExtensionClassName];
} @catch (NSException *e) {
BLogErrorWithException(e, [NSString stringWithFormat:@"Exception %@ while loading executable extension class %@", e, executableExtensionClassName]);
}
}
if (!executableExtensionClass) return nil;
if (aClass) {
if (![executableExtensionClass isSubclassOfClass:aClass]) {
BLogError([NSString stringWithFormat:@"Executable extension class %@ failed to conform to class %@", executableExtensionClass, aClass]);
return nil;
}
}
if (aProtocol) {
if (![executableExtensionClass conformsToProtocol:aProtocol]) {
BLogError([NSString stringWithFormat:@"Executable extension class %@ failed to conform to protocol %@", executableExtensionClass, aProtocol]);
return nil;
}
}
for (NSValue *eachValue in selectors) {
SEL eachSelector = [eachValue pointerValue];
if (![executableExtensionClass instancesRespondToSelector:eachSelector]) {
BLogError([NSString stringWithFormat:@"Executable extension class %@ failed to respond to required selector %@", executableExtensionClass, NSStringFromSelector(eachSelector)]);
return nil;
}
}
return executableExtensionClass;
}
- (id)createExecutableExtensionFromAttribute:(NSString *)attributeName {
return [self createExecutableExtensionFromAttribute:attributeName conformingToClass:nil conformingToProtocol:nil respondingToSelectors:nil];
}
- (id)createExecutableExtensionFromAttribute:(NSString *)attributeName conformingToClass:(Class)aClass conformingToProtocol:(Protocol *)aProtocol respondingToSelectors:(NSArray *)selectors {
Class executableExtensionClass = [self executableExtensionClassFromAttribute:attributeName conformingToClass:aClass conformingToProtocol:aProtocol respondingToSelectors:selectors];
if (executableExtensionClass) {
@try {
NSArray *executableExtensionClassNameAndSelector = [[self attributeForKey:attributeName] componentsSeparatedByString:@" "];
SEL instanceSelector = nil;
if ([executableExtensionClassNameAndSelector count] > 1) {
instanceSelector = NSSelectorFromString([executableExtensionClassNameAndSelector objectAtIndex:1]);
}
if (instanceSelector != nil) {
return [executableExtensionClass performSelector:instanceSelector];
} else {
id executableExtension = [executableExtensionClass alloc];
if ([executableExtension respondsToSelector:@selector(initWithConfigurationElement:)]) {
executableExtension = [executableExtension initWithConfigurationElement:self];
} else {
executableExtension = [executableExtension init];
}
return executableExtension;
}
} @catch (NSException *e) {
BLogErrorWithException(e, [NSString stringWithFormat:@"Exception %@ while loading instance of extension %@", e, self]);
}
}
return nil;
}
- (id)createExecutableExtensionProxyFromAttribute:(NSString *)attributeName {
return [self createExecutableExtensionProxyFromAttribute:attributeName conformingToClass:nil conformingToProtocol:nil respondingToSelectors:nil];
}
- (id)createExecutableExtensionProxyFromAttribute:(NSString *)attributeName conformingToClass:(Class)aClass conformingToProtocol:(Protocol *)aProtocol respondingToSelectors:(NSArray *)selectors {
if (aClass != nil || aProtocol != nil || selectors != nil) {
if (![self executableExtensionClassFromAttribute:attributeName conformingToClass:aClass conformingToProtocol:aProtocol respondingToSelectors:selectors]) {
BLogError(@"failed to create executableExtensionProxy because proxied object class didn't conform to constraints.");
return nil;
}
}
return [[BExecutableExtensionProxy alloc] initWithConfigurationElement:self attributeName:attributeName];
}
#pragma mark Properties
- (BPlugin *)plugin {
return [[self declaringExtension] plugin];
}
- (BExtension *)declaringExtension {
BExtension *result = [self extension];
if (!result) {
result = [[self parent] declaringExtension];
}
return result;
}
@synthesize parent;
@synthesize name;
@synthesize attributes;
@synthesize value;
- (NSString *)attributeForKey:(NSString *)attributeName {
return [attributes objectForKey:attributeName];
}
- (BOOL)booleanAttributeForKey:(NSString *)attributeName {
return [@"yes" isEqualToString:[self attributeForKey:attributeName]];
}
- (NSInteger)integerAttributeForKey:(NSString *)attributeName {
NSString *attribute = [self attributeForKey:attributeName];
return attribute == nil ? 0 : [attribute integerValue];
}
- (CGFloat)floatAttributeForKey:(NSString *)attributeName {
NSString *attribute = [self attributeForKey:attributeName];
return attribute == nil ? 0 : [attribute doubleValue];
}
- (SEL)selectorAttributeForKey:(NSString *)attributeName {
NSString *attribute = [self attributeForKey:attributeName];
return attribute == nil ? NULL : NSSelectorFromString(attribute);
}
- (NSString *)localizedAttributeForKey:(NSString *)attributeName {
return [[[self declaringExtension] plugin] localizedPluginXmlString:[self attributeForKey:attributeName]];
}
- (NSImage *)imageAttributeForKey:(NSString *)attributeName {
return [[[self declaringExtension] plugin] imageNamed:[self attributeForKey:attributeName]];
}
- (id)executableExtensionAttributeForKey:(NSString *)attributeName {
NSString *attribute = [self attributeForKey:attributeName];
return attribute == nil || [attribute isEqualToString:@"nil"] ? nil : [self createExecutableExtensionFromAttribute:attributeName];
}
#pragma mark Child Configuration Elements
@synthesize children;
- (NSArray *)childrenNamed:(NSString *)aName {
return [children filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name = %@", aName]];
}
#pragma mark Util
- (id)contentAsPropertyList {
return [[xmlElement XMLString] propertyList];
}
- (NSXMLElement *)contentAsXmlElement {
return xmlElement;
}
- (BOOL)assertKeysPresent:(NSArray *)keys {
NSDictionary *elementAttributes = [self attributes];
for (NSString *each in keys) {
if (![elementAttributes objectForKey:each]) {
BLogError([NSString stringWithFormat:@"Required key %@ not found in configuration element.", each]);
return NO;
}
}
return YES;
}
@end
@implementation BConfigurationElement (BConfigurationElementPrivate)
- (BExtension *)extension {
return extension;
}
@end