]> err.no Git - sope/blob - sope-appserver/NGObjWeb/SoObjects/SoClassRegistry.m
fixed various warnings
[sope] / sope-appserver / NGObjWeb / SoObjects / SoClassRegistry.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "SoClassRegistry.h"
23 #include "SoObjCClass.h"
24 #include "common.h"
25
26 @implementation SoClassRegistry
27
28 // TODO: register for bundle-did-load notification !!
29 static SoClassRegistry *registry = nil; // THREAD
30
31 + (id)sharedClassRegistry {
32   if (registry == nil)
33     registry = [[SoClassRegistry alloc] init];
34   return registry;
35 }
36 - (id)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];
41   }
42   return self;
43 }
44
45 - (void)dealloc {
46   [self->extToSoClass  release];
47   [self->nameToSoClass release];
48   [self->objcToSoClass release];
49   [super dealloc];
50 }
51
52 /* name registry */
53
54 - (SoClass *)soClassWithName:(NSString *)_name {
55   Class clazz;
56   
57   if ([_name length] == 0) return nil;
58   
59   if ((clazz = NSClassFromString(_name)))
60     return [self soClassForClass:clazz];
61   
62   return nil;
63 }
64
65 - (SoClass *)soClassForExtension:(NSString *)_ext {
66   SoClass *soClass;
67   
68   if ((soClass = [self->extToSoClass objectForKey:_ext]) == nil)
69     return nil;
70
71   return soClass;
72 }
73 - (NSException *)registerSoClass:(SoClass *)_clazz forExtension:(NSString *)_e{
74   SoClass *soClass;
75   
76   NSAssert(_clazz, @"invalid class parameter !");
77   NSAssert(_e,     @"invalid file extension parameter !");
78   
79   if ((soClass = [self->extToSoClass objectForKey:_e])) {
80     if (soClass == _clazz)
81       /* already registered */
82       return nil;
83     
84     [self debugWithFormat:
85             @"overriding existing registration for extension '%@': %@",
86             _e, soClass];
87   }
88   
89   [self->extToSoClass setObject:_clazz forKey:_e];
90   return nil;
91 }
92
93 - (SoClass *)soClassForExactName:(NSString *)_name {
94   SoClass *soClass;
95   
96   if ((soClass = [self->nameToSoClass objectForKey:_name]) == nil)
97     return nil;
98
99   return soClass;
100 }
101 - (NSException *)registerSoClass:(SoClass *)_clazz forExactName:(NSString *)_n{
102   SoClass *soClass;
103   
104   NSAssert(_clazz, @"invalid class parameter !");
105   NSAssert(_n,     @"invalid file extension parameter !");
106   
107   if ((soClass = [self->nameToSoClass objectForKey:_n])) {
108     if (soClass == _clazz)
109       /* already registered */
110       return nil;
111     
112     [self debugWithFormat:
113             @"overriding existing registration for name '%@': %@",
114             _n, soClass];
115   }
116   
117   [self->nameToSoClass setObject:_clazz forKey:_n];
118   return nil;
119 }
120
121 /* ObjC classes */
122
123 - (SoClass *)soClassForClass:(Class)_clazz {
124   SoObjCClass *soClass;
125   SoClass     *soSuper;
126   
127   if (_clazz == Nil)
128     return nil;
129   if ((soClass = [self->objcToSoClass objectForKey:_clazz]))
130     return soClass;
131   
132   soSuper = [self soClassForClass:[_clazz superclass]];
133   soClass = [[SoObjCClass alloc] initWithSoSuperClass:soSuper class:_clazz];
134   
135   if (soClass == nil) {
136     [self debugWithFormat:@"could not create SoClass for class %@ !", _clazz];
137     return nil;
138   }
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];
144 }
145
146 @end /* SoClassRegistry */
147
148 @implementation SoClassRegistry(Logging)
149
150 - (NSString *)loggingPrefix {
151   return @"[so-class-registry]";
152 }
153 - (BOOL)isDebuggingEnabled {
154   static int debugOn = -1;
155   if (debugOn == -1) {
156     debugOn = [[NSUserDefaults standardUserDefaults]
157                 boolForKey:@"SoClassRegistryDebugEnabled"] ? 1 : 0;
158   }
159   return debugOn ? YES : NO;
160 }
161
162 @end /* SoClassRegistry(Logging) */