-
Notifications
You must be signed in to change notification settings - Fork 3
/
BPlugin.m
357 lines (282 loc) · 10.7 KB
/
BPlugin.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
// BPlugin.m
// Blocks
//
// Created by Jesse Grosjean on 8/21/07.
// Copyright 2007 Blocks. All rights reserved.
//
#import "BPlugin.h"
#import "BExtensionRegistry.h"
#import "BRequirement.h"
#import "BExtensionPoint.h"
#import "BExtension.h"
#import "BConfigurationElement.h"
#import "BLog.h"
#import "BLocks.h"
@interface BPlugin (BPluginPrivate)
- (BOOL)loadPluginXML;
@end
@interface BRequirement (BPluginPrivate)
- (id)initWithPlugin:(BPlugin *)aPlugin xmlElement:(NSXMLElement *)xmlElement;
@end
@interface BExtensionPoint (BPluginPrivate)
- (id)initWithPlugin:(BPlugin *)aPlugin xmlElement:(NSXMLElement *)xmlElement;
@end
@interface BExtension (BPluginPrivate)
- (id)initWithPlugin:(BPlugin *)aPlugin xmlElement:(NSXMLElement *)xmlElement;
@end
@interface BConfigurationElement (BPluginPrivate)
- (id)initWithExtension:(BExtension *)anExtension parent:(BConfigurationElement *)aParent xmlElement:(NSXMLElement *)xmlElement;
@end
@implementation BPlugin
#pragma mark Class Methods
+ (BPlugin *)pluginForClass:(Class)aClass {
return [[BExtensionRegistry sharedInstance] pluginFor:[[NSBundle bundleForClass:aClass] bundleIdentifier]];
}
+ (NSXMLDTD *)pluginDTD {
static NSXMLDTD *pluginDTD = nil;
if (!pluginDTD) {
NSError *error = nil;
NSString *dtdPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"plugin" ofType:@"dtd"];
pluginDTD = [[NSXMLDTD alloc] initWithContentsOfURL:[NSURL fileURLWithPath:dtdPath] options:0 error:&error];
if (!pluginDTD) {
BLogError([NSString stringWithFormat:@"failed to load XML dtd with error %@", error]);
}
[pluginDTD setName:@"plugin"];
}
return pluginDTD;
}
#pragma mark Init
static NSInteger discoveryOrderCounter = 0;
- (id)initWithBundle:(NSBundle *)aBundle {
if (self = [super init]) {
discoveryOrder = discoveryOrderCounter++;
bundle = aBundle;
requirements = [[NSMutableArray alloc] init];
extensionPoints = [[NSMutableArray alloc] init];
extensions = [[NSMutableArray alloc] init];
if (![self loadPluginXML]) {
BLogError([NSString stringWithFormat:@"failed loadPluginXML for bundle %@", [bundle bundleIdentifier]]);
return nil;
}
}
return self;
}
#pragma mark Properties
@synthesize bundle;
@synthesize label;
@synthesize version;
@synthesize identifier;
@synthesize requirements;
@synthesize extensionPoints;
@synthesize extensions;
@synthesize documentation;
- (NSString *)documentation {
if (!documentation) {
documentation = [NSString stringWithContentsOfFile:[self.bundle pathForResource:self.identifier ofType:@"markdown"]];
if (!documentation) {
documentation = BLocalizedString(@"No documentation file could be found for this plugin", nil);
}
}
return documentation;
}
- (NSArray *)headerFilePaths {
NSMutableArray *headerFilePaths = [NSMutableArray array];
NSString *headersFolder = [NSString stringWithFormat:@"%@/Contents/Headers/", [bundle bundlePath]];
for (NSString *each in [[NSFileManager defaultManager] directoryContentsAtPath:headersFolder]) {
[headerFilePaths addObject:[NSString stringWithFormat:@"%@%@", headersFolder, each]];
}
return headerFilePaths;
}
#pragma mark Loading Bundle
- (BOOL)isLoaded {
return [self.bundle isLoaded];
}
- (BOOL)loadAndReturnError:(NSError **)error {
if (![self isLoaded]) {
for (BRequirement *each in self.requirements) {
// if (![each isLoaded]) {
if ([each loadAndReturnError:error]) {
BLogInfo(([NSString stringWithFormat:@"Loaded code for requirement %@ by plugin %@", each, self.identifier]));
} else {
if ([each optional]) {
BLogError(([NSString stringWithFormat:@"Failed to load code for optioinal requirement %@ by plugin %@", each, self.identifier]));
} else {
BLogError(([NSString stringWithFormat:@"Failed to load code for requirement %@ by plugin %@", [each requiredBundleIdentifier], self.identifier]));
BLogError(([NSString stringWithFormat:@"Failed to load code for plugin with id %@", self.identifier]));
return NO;
}
}
// }
}
if ([self.bundle loadAndReturnError:error]) {
BLogInfo([NSString stringWithFormat:@"Loaded plugin %@", self.identifier]);
} else {
BLogError(@"Failed to load bundle with id %@: %@", self.identifier, bundle);
return NO;
}
}
return YES;
}
#pragma mark Util
- (Class)classNamed:(NSString *)className {
NSError *error = nil;
Class result = nil;
if ([self loadAndReturnError:&error]) {
result = [self.bundle classNamed:className];
}
if (!result) {
result = NSClassFromString(className);
}
return result;
}
/*
* First checks main bundle for image, then checks plugin bundle if no image was found.
*/
- (NSImage *)imageNamed:(NSString *)imageName {
NSString *imageNamePluginUniqueName = [NSString stringWithFormat:@"%@.%@", self.identifier, imageName];
NSImage *image = [NSImage imageNamed:imageNamePluginUniqueName];
if (!image) {
image = [[NSImage alloc] initWithContentsOfFile:[self.bundle pathForImageResource:imageName]];
[image setName:imageNamePluginUniqueName];
}
return image;
}
/*
* Allows [NSBundle main] to override localizations in plugins PluginXml.strings file with its own.
*/
- (NSString *)localizedPluginXmlString:(NSString *)string {
if ([string length] > 0 && [string characterAtIndex:0] == '%') {
string = [string substringFromIndex:1];
NSString *keyNotFoundMarker = @"BKeyNotFound";
NSString *localizedString = [[NSBundle mainBundle] localizedStringForKey:string value:keyNotFoundMarker table:@"PluginXml"];
if ([keyNotFoundMarker isEqualToString:localizedString]) {
string = [self.bundle localizedStringForKey:string value:@"" table:@"PluginXml"];
} else {
string = localizedString;
}
}
return string;
}
@end
@implementation BPlugin (BPluginPrivate)
- (BOOL)loadPluginXML {
NSError *error = nil;
NSString *xmlPath = [bundle pathForResource:@"Plugin" ofType:@"xml"];
NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
NSXMLDocument *document = xmlData != nil ?[[NSXMLDocument alloc] initWithData:xmlData options:0 error:&error] : nil;
if (!document) {
BLogError([NSString stringWithFormat:@"failed to load Plugin.xml resource for bundle %@ with error %@", bundle, error]);
return NO;
}
// [document setDTD:[BPlugin pluginDTD]];
// if (![document validateAndReturnError:&error]) {
// BLogError([NSString stringWithFormat:@"failed to validate Plugin.xml resource for bundle %@ with error %@", bundle, error]);
// }
NSXMLElement *rootElement = [document rootElement];
label = [[rootElement attributeForName:@"label"] objectValue];
version = [[rootElement attributeForName:@"version"] objectValue];
identifier = [[rootElement attributeForName:@"id"] objectValue];
if (!version || [[version componentsSeparatedByString:@"."] count] != 3) {
BLogError([NSString stringWithFormat:@"failed to load Plugin.xml resource for bundle %@ because plugins version number isn't present or doesn't conform to #.#.#", bundle]);
return NO;
}
for (NSXMLElement *each in [rootElement elementsForName:@"requirement"]) {
BRequirement *requirement = [[BRequirement alloc] initWithPlugin:self xmlElement:each];
if (!requirement.version || [[requirement.version componentsSeparatedByString:@"."] count] != 3) {
BLogError([NSString stringWithFormat:@"failed to load Plugin.xml resource for bundle %@ because the version number declared by the %@ requirment isn't present or doesn't conform to #.#.#", bundle, requirement]);
return NO;
}
[requirements addObject:requirement];
}
for (NSXMLElement *each in [rootElement elementsForName:@"extension-point"]) {
[extensionPoints addObject:[[BExtensionPoint alloc] initWithPlugin:self xmlElement:each]];
}
for (NSXMLElement *each in [rootElement elementsForName:@"extension"]) {
[extensions addObject:[[BExtension alloc] initWithPlugin:self xmlElement:each]];
}
return YES;
}
@end
@implementation BRequirement (BPluginPrivate)
- (id)initWithPlugin:(BPlugin *)aPlugin xmlElement:(NSXMLElement *)xmlElement {
if (self = [super init]) {
plugin = aPlugin;
optional = [[[xmlElement attributeForName:@"optional"] objectValue] isEqualToString:@"true"];
version = [[xmlElement attributeForName:@"version"] objectValue];
requiredBundleIdentifier = [[xmlElement attributeForName:@"bundle"] objectValue];
}
return self;
}
@end
@implementation BExtensionPoint (BPluginPrivate)
- (id)initWithPlugin:(BPlugin *)aPlugin xmlElement:(NSXMLElement *)xmlElement {
if (self = [super init]) {
plugin = aPlugin;
identifier = [NSString stringWithFormat:@"%@.%@",[aPlugin identifier], [[xmlElement attributeForName:@"id"] objectValue]];
}
return self;
}
@end
@implementation BExtension (BPluginPrivate)
- (id)initWithPlugin:(BPlugin *)aPlugin xmlElement:(NSXMLElement *)xmlElement {
if (self = [super init]) {
plugin = aPlugin;
label = [[xmlElement attributeForName:@"label"] objectValue];
NSString *processOrderString = [[xmlElement attributeForName:@"processOrder"] stringValue];
if (processOrderString) {
processOrder = [processOrderString doubleValue];
}
extensionPointUniqueIdentifier = [[xmlElement attributeForName:@"point"] objectValue];
NSArray *elementChildren = [xmlElement children];
NSUInteger count = [elementChildren count];
if (count > 0) {
configurationElements = [[NSMutableArray alloc] initWithCapacity:count];
NSUInteger i = 0;
while (i < count) {
[configurationElements addObject:[[BConfigurationElement alloc] initWithExtension:self parent:nil xmlElement:[elementChildren objectAtIndex:i]]];
i++;
}
}
}
return self;
}
@end
@implementation BConfigurationElement (BPluginPrivate)
- (id)initWithExtension:(BExtension *)anExtension parent:(BConfigurationElement *)aParent xmlElement:(NSXMLElement *)anXmlElement {
if (self = [super init]) {
xmlElement = anXmlElement;
extension = anExtension;
parent = aParent;
name = [xmlElement name];
value = [xmlElement objectValue];
NSArray *xmlElementAttributes = [xmlElement attributes];
NSUInteger count = [xmlElementAttributes count];
if (count > 0) {
attributes = [[NSMutableDictionary alloc] initWithCapacity:count];
NSUInteger i = 0;
while (i < count) {
NSXMLNode *eachNode = [xmlElementAttributes objectAtIndex:i];
[attributes setObject:[eachNode objectValue] forKey:[eachNode name]];
i++;
}
}
NSArray *elementChildren = [xmlElement children];
count = [elementChildren count];
if (count > 0) {
children = [[NSMutableArray alloc] initWithCapacity:count];
NSUInteger i = 0;
while (i < count) {
[children addObject:[[BConfigurationElement alloc] initWithExtension:extension parent:self xmlElement:[elementChildren objectAtIndex:i]]];
i++;
}
}
}
return self;
}
@end
@implementation NSBundle (BPluginExtensions)
- (NSString *)version {
return [[self infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
}
@end