]> err.no Git - sope/blob - sope-appserver/NGObjWeb/SoObjects/SoObjCClass.m
fixed various warnings
[sope] / sope-appserver / NGObjWeb / SoObjects / SoObjCClass.m
1 /*
2   Copyright (C) 2002-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 // $Id: SoObjCClass.m 1 2004-08-20 10:08:27Z znek $
22
23 #include "SoObjCClass.h"
24 #include "SoSelectorInvocation.h"
25 #include <NGExtensions/NGObjCRuntime.h>
26 #include <NGExtensions/NSString+Ext.h>
27 #include "common.h"
28
29 @implementation SoObjCClass
30
31 - (id)initWithSoSuperClass:(SoClass *)_soClass class:(Class)_clazz {
32   NSAssert(_clazz, @"missing ObjC class parameter !");
33   if ((self = [super initWithSoSuperClass:_soClass])) {
34     self->clazz = _clazz;
35   }
36   return self;
37 }
38 - (id)initWithSoSuperClass:(SoClass *)_soClass {
39   return [self initWithSoSuperClass:_soClass class:nil];
40 }
41
42 - (void)rescanClass {
43   NSMutableDictionary *prefixMap;
44   NSEnumerator *e;
45   NSString *methodName;
46   
47   prefixMap = [[NSMutableDictionary alloc] initWithCapacity:32];
48   
49   [self debugWithFormat:@"scanning ObjC class %@ for SoObject methods ...",
50           NSStringFromClass(self->clazz)];
51   e = [self->clazz methodNameEnumerator];
52   while ((methodName = [e nextObject])) {
53     SoSelectorInvocation *invocation;
54     NSString *methodPrefix;
55     NSRange  r;
56     unsigned len;
57     
58     if ((len = [methodName length]) < 6)
59       continue;
60     
61     r = [methodName rangeOfString:@"Action"];
62     if (r.length == 0) continue;
63     
64     /* eg: doItAction:abc: => doItAction */
65     methodPrefix = [methodName substringToIndex:(r.location + r.length)];
66     
67     if (len > (r.location + r.length)) {
68       /* something is beyond the xxxAction, *must* be followed by a colon */
69       if ([methodName characterAtIndex:(r.location + r.length)] != ':')
70         continue;
71     }
72     
73     [self debugWithFormat:@"  found an action: %@", methodName];
74     
75     if ((invocation = [prefixMap objectForKey:methodPrefix]) == nil) {
76       invocation = [[SoSelectorInvocation alloc] init];
77       [prefixMap setObject:invocation forKey:methodPrefix];
78       [invocation release];
79     }
80     [invocation addSelectorNamed:methodName];
81   }
82   
83   e = [prefixMap keyEnumerator];
84   while ((methodName = [e nextObject])) {
85     SoSelectorInvocation *inv;
86     NSString *slotName;
87     
88     slotName = [methodName hasSuffix:@"Action"]
89       ? [methodName substringToIndex:([methodName length] - 6)]
90       : methodName;
91     inv = [prefixMap objectForKey:methodName];
92     [self setValue:inv forSlot:slotName];
93   }
94 }
95
96 /* factory */
97
98 - (id)instantiateObject {
99   return [[[self->clazz alloc] init] autorelease];
100 }
101
102 - (NSClassDescription *)soClassDescription {
103   return [NSClassDescription classDescriptionForClass:self->clazz];
104 }
105
106 - (NSString *)className {
107   return NSStringFromClass(self->clazz);
108 }
109 - (Class)objcClass {
110   return self->clazz;
111 }
112
113 /* description */
114
115 - (NSString *)description {
116   NSMutableString *ms;
117   
118   ms = [NSMutableString stringWithCapacity:64];
119   [ms appendFormat:@"<0x%08X[%@]:", self,
120         NSStringFromClass((Class)*(void**)self)];
121   
122   if (self->soSuperClass)
123     [ms appendFormat:@" super=0x%08X", self->soSuperClass];
124   else
125     [ms appendString:@" root"];
126
127   if (self->clazz)
128     [ms appendFormat:@" objc=%@", NSStringFromClass(self->clazz)];
129   else
130     [ms appendString:@" <no-objc-class>"];
131   
132   if ([self->slots count] > 0) {
133     [ms appendFormat:@" slots=%@", 
134           [[self->slots allKeys] componentsJoinedByString:@","]];
135   }
136   
137   [ms appendString:@">"];
138   return ms;
139 }
140
141 /* logging */
142
143 - (NSString *)loggingPrefix {
144   return [NSString stringWithFormat:@"[so-objc-class:%@]", 
145                      NSStringFromClass(self->clazz)];
146 }
147 - (BOOL)isDebuggingEnabled {
148   static int debugOn = -1;
149   if (debugOn == -1) {
150     debugOn = [[NSUserDefaults standardUserDefaults]
151                 boolForKey:@"SoObjCClassDebugEnabled"] ? 1 : 0;
152   }
153   return debugOn ? YES : NO;
154 }
155
156 @end /* SoObjCClass */