Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c080014c15
Fetching contributors…

Cannot retrieve contributors at this time

file 279 lines (242 sloc) 6.862 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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
//
// BridgeSupportController.m
// JSCocoa
//
// Created by Patrick Geiller on 08/07/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//


#import "BridgeSupportController.h"


@implementation BridgeSupportController


+ (id)sharedController
{
static id singleton;
@synchronized(self)
{
if (!singleton)
singleton = [[BridgeSupportController alloc] init];
return singleton;
}
return singleton;
}

- (id)init
{
self = [super init];

paths = [[NSMutableArray alloc] init];
xmlDocuments = [[NSMutableArray alloc] init];
hash = [[NSMutableDictionary alloc] init];
variadicSelectors = [[NSMutableDictionary alloc] init];
variadicFunctions = [[NSMutableDictionary alloc] init];

return self;
}

- (void)dealloc
{
[variadicFunctions release];
[variadicSelectors release];
[hash release];
[paths release];
[xmlDocuments release];

[super dealloc];
}

//
// Load a bridgeSupport file into a hash as { name : xmlTagString }
//
- (BOOL)loadBridgeSupport:(NSString*)path
{
NSError* error = nil;
/*
Adhoc parser
NSXMLDocument is too slow
loading xml document as string then querying on-demand is too slow
can't get CFXMLParserRef to work
don't wan't to delve into expat
-> ad hoc : load file, build a hash of { name : xmlTagString }
*/
NSString* xmlDocument = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error) return NSLog(@"loadBridgeSupport : %@", error), NO;

char* c = (char*)[xmlDocument UTF8String];
#ifdef __OBJC_GC__
char* originalC = c;
[[NSGarbageCollector defaultCollector] disableCollectorForPointer:originalC];
#endif

// double t0 = CFAbsoluteTimeGetCurrent();
// Start parsing
for (; *c; c++)
{
if (*c == '<')
{
char startTagChar = c[1];
if (startTagChar == 0) return NO;

// 'co' constant
// 'cl' class
// 'e' enum
// 'fu' function
// 'st' struct
if ((c[1] == 'c' && (c[2] == 'o' || c[2] == 'l')) || c[1] == 'e' || (c[1] == 'f' && c[2] == 'u') || (c[1] == 's' && c[2] == 't'))
{
char* tagStart;
char* cN; // cN = char starting attribute's name, cV = char starting attribute's value
char* cV;
BOOL hasName = NO;
id name = nil;

tagStart = c;

// Skip XML tag name by skipping until the first space
for (; *c && *c != ' '; c++);
// Skip space
c++;

// Scour attributes until we find the name
while (!hasName && *c) {
// Skip spaces to attribute name
for (; *c && *c == ' '; c++);
cN = c;
// Skip attribute name until '
for (; *c && *c != '\''; c++);
c++;
cV = c;
// Skip to the end of the attribute value
for (; *c && *c != '\''; c++);

if (cN[0] == 'n' && cN[1] == 'a') {
hasName = YES;
name = [[NSString alloc] initWithBytes:cV length:c-cV encoding:NSUTF8StringEncoding];
}
else
// Skip attribute closing '
c++;
}
if (!hasName)
return NSLog(@"(loadBridgeSupport) Parsing failed to get name in file %@", path), NO;

// Move to tag end
BOOL foundEndTag = NO;
BOOL foundOpenTag = NO;
c++;
for (; *c && !foundEndTag; c++)
{
if (*c == '<') foundOpenTag = YES;
else
if (*c == '/')
{
if (!foundOpenTag)
{
if(c[1] == '>') foundEndTag = YES, c++;
}
else
{
if (startTagChar == c[1])
{
foundEndTag = YES;
// Skip to end of tag
for (; *c && *c != '>'; c++);
}
}
}
else
// Variadic parsing
if (c[0] == 'v' && c[1] == 'a' && c[2] == 'r')
{
if (strncmp(c, "variadic", 8) == 0)
{
// Skip back to tag start
cV = c;
for (; *cV != '<'; cV--);

// Tag name starts with 'm' : variadic method
// <method variadic='true' selector='alertWithMessageText:defaultButton:alternateButton:otherButton:informativeTextWithFormat:' class_method='true'>
if (cV[1] == 'm')
{
c = cV;
id variadicMethodName = nil;
// Extract selector name
for (; *c != '>'; c++)
{
if (c[0] == ' ' && c[1] == 's' && c[2] == 'e' && c[3] == 'l')
{
for (; *c && *c != '\''; c++);
c++;
cV = c;
for (; *c && *c != '\''; c++);
variadicMethodName = [[[NSString alloc] initWithBytes:cV length:c-cV encoding:NSUTF8StringEncoding] autorelease];
}
}
[variadicSelectors setValue:@"true" forKey:variadicMethodName];
// NSLog(@"SELECTOR %@", name);
}
else
// Variadic function
// <function name='NSBeginAlertSheet' variadic='true'>
{
[variadicFunctions setValue:@"true" forKey:name];
// NSLog(@"function %@", name);
}
}
}
}

cV = tagStart;
id value = [[NSString alloc] initWithBytes:cV length:c-cV encoding:NSUTF8StringEncoding];

[hash setValue:value forKey:name];
[value release];
[name release];
}
}
}
// double t1 = CFAbsoluteTimeGetCurrent();
// NSLog(@"BridgeSupport %@ parsed in %f", [[path lastPathComponent] stringByDeletingPathExtension], t1-t0);
#ifdef __OBJC_GC__
[[NSGarbageCollector defaultCollector] enableCollectorForPointer:originalC];
#endif
[paths addObject:path];
[xmlDocuments addObject:xmlDocument];

return YES;
}


- (BOOL)isBridgeSupportLoaded:(NSString*)path
{
NSUInteger idx = [self bridgeSupportIndexForString:path];
return idx == NSNotFound ? NO : YES;
}

//
// bridgeSupportIndexForString
// given 'AppKit', return index of '/System/Library/Frameworks/AppKit.framework/Versions/C/Resources/BridgeSupport/AppKitFull.bridgesupport'
//
- (NSUInteger)bridgeSupportIndexForString:(NSString*)string
{
NSUInteger i, l = [paths count];
for (i=0; i<l; i++)
{
NSString* path = [paths objectAtIndex:i];
NSRange range = [path rangeOfString:string];

if (range.location != NSNotFound) return range.location;
}
return NSNotFound;
}

- (NSMutableDictionary*)variadicSelectors
{
return variadicSelectors;
}

- (NSMutableDictionary*)variadicFunctions
{
return variadicFunctions;
}

- (NSArray*)keys
{
[hash removeObjectForKey:@"NSProxy"];
[hash removeObjectForKey:@"NSProtocolChecker"];
[hash removeObjectForKey:@"NSDistantObject"];

return [hash allKeys];
}


- (NSString*)queryName:(NSString*)name
{
return [hash valueForKey:name];
}
- (NSString*)queryName:(NSString*)name type:(NSString*)type
{
id v = [self queryName:name];
if (!v) return nil;

char* c = (char*)[v UTF8String];
// Skip tag start
c++;
char* c0 = c;
for (; *c && *c != ' '; c++);
id extractedType = [[NSString alloc] initWithBytes:c0 length:c-c0 encoding:NSUTF8StringEncoding];
[extractedType autorelease];
// NSLog(@"extractedType=%@", extractedType);

if (![extractedType isEqualToString:type]) return nil;
return v;
}

@end
Something went wrong with that request. Please try again.