2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "SoClassRegistry.h"
23 #include "SoObjCClass.h"
26 @implementation SoClassRegistry
28 // TODO: register for bundle-did-load notification !!
29 static SoClassRegistry *registry = nil; // THREAD
31 + (id)sharedClassRegistry {
33 registry = [[SoClassRegistry alloc] init];
37 if ((self = [super init])) {
38 self->objcToSoClass = [[NSMutableDictionary alloc] initWithCapacity:64];
39 self->extToSoClass = [[NSMutableDictionary alloc] initWithCapacity:32];
40 self->nameToSoClass = [[NSMutableDictionary alloc] initWithCapacity:32];
46 [self->extToSoClass release];
47 [self->nameToSoClass release];
48 [self->objcToSoClass release];
54 - (SoClass *)soClassWithName:(NSString *)_name {
57 if ([_name length] == 0) return nil;
59 if ((clazz = NSClassFromString(_name)))
60 return [self soClassForClass:clazz];
65 - (SoClass *)soClassForExtension:(NSString *)_ext {
68 if ((soClass = [self->extToSoClass objectForKey:_ext]) == nil)
73 - (NSException *)registerSoClass:(SoClass *)_clazz forExtension:(NSString *)_e{
76 NSAssert(_clazz, @"invalid class parameter !");
77 NSAssert(_e, @"invalid file extension parameter !");
79 if ((soClass = [self->extToSoClass objectForKey:_e])) {
80 if (soClass == _clazz)
81 /* already registered */
84 [self debugWithFormat:
85 @"overriding existing registration for extension '%@': %@",
89 [self->extToSoClass setObject:_clazz forKey:_e];
93 - (SoClass *)soClassForExactName:(NSString *)_name {
96 if ((soClass = [self->nameToSoClass objectForKey:_name]) == nil)
101 - (NSException *)registerSoClass:(SoClass *)_clazz forExactName:(NSString *)_n{
104 NSAssert(_clazz, @"invalid class parameter !");
105 NSAssert(_n, @"invalid file extension parameter !");
107 if ((soClass = [self->nameToSoClass objectForKey:_n])) {
108 if (soClass == _clazz)
109 /* already registered */
112 [self debugWithFormat:
113 @"overriding existing registration for name '%@': %@",
117 [self->nameToSoClass setObject:_clazz forKey:_n];
123 - (SoClass *)soClassForClass:(Class)_clazz {
124 SoObjCClass *soClass;
129 if ((soClass = [self->objcToSoClass objectForKey:_clazz]))
132 soSuper = [self soClassForClass:[_clazz superclass]];
133 soClass = [[SoObjCClass alloc] initWithSoSuperClass:soSuper class:_clazz];
135 if (soClass == nil) {
136 [self debugWithFormat:@"could not create SoClass for class %@ !", _clazz];
139 [self->objcToSoClass setObject:soClass forKey:_clazz];
140 [soClass rescanClass];
141 [self debugWithFormat:@"mapped class %@ to SoClass %@",
142 NSStringFromClass(_clazz), soClass];
143 return [soClass autorelease];
146 @end /* SoClassRegistry */
148 @implementation SoClassRegistry(Logging)
150 - (NSString *)loggingPrefix {
151 return @"[so-class-registry]";
153 - (BOOL)isDebuggingEnabled {
154 static int debugOn = -1;
156 debugOn = [[NSUserDefaults standardUserDefaults]
157 boolForKey:@"SoClassRegistryDebugEnabled"] ? 1 : 0;
159 return debugOn ? YES : NO;
162 @end /* SoClassRegistry(Logging) */