]> err.no Git - sope/blob - sope-appserver/NGObjWeb/WOElement.m
Bugfix, bumped framework version
[sope] / sope-appserver / NGObjWeb / WOElement.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 // $Id$
22
23 #include <NGObjWeb/WOElement.h>
24 #include "WOElement+private.h"
25 #include <NGObjWeb/WOComponent.h>
26 #include "common.h"
27
28 @implementation WOElement
29
30 + (int)version {
31   return 2;
32 }
33
34 static id numStrings[100];
35
36 + (void)initialize {
37   static BOOL didInitialize = NO;
38
39   if (!didInitialize) {
40     int cnt;
41     
42     didInitialize = YES;
43     
44     for (cnt = 0; cnt < 100; cnt++) {
45       char buf[8];
46
47       sprintf(buf, "%i", cnt);
48       numStrings[cnt] = [[NSString alloc] initWithCString:buf];
49     }
50   }
51 }
52
53 - (id)init {
54   if ((self = [super init])) {
55 #if !NO_METHOD_CACHING
56     self->takeValues = (OWTakeValuesMethod)
57       [self methodForSelector:@selector(takeValuesFromRequest:inContext:)];
58     self->appendResponse = (OWAppendResponseMethod)
59       [self methodForSelector:@selector(appendToResponse:inContext:)];
60 #else
61 #  warning methods are not cached !
62 #endif
63   }
64   return self;
65 }
66
67 /* element IDs */
68
69 - (NSString *)stringForInt:(int)_i {
70   NSString *s = nil;
71   
72   if ((_i < 100) && (_i >= 0)) {
73     // MT flaw, should be locked
74     s = numStrings[_i];
75     if (s == nil) {
76       char buf[16];
77       sprintf(buf, "%i", _i);
78       s = [NSString stringWithCString:buf];
79       numStrings[_i] = RETAIN(s);
80     }
81   }
82   else {
83     char buf[16];
84     sprintf(buf, "%i", _i);
85     s = [NSString stringWithCString:buf];
86   }
87   return s;
88 }
89
90 - (NSString *)elementID {
91   return nil;
92 }
93
94 /* OWResponder */
95
96 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
97 }
98
99 - (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
100   return nil;
101 }
102
103 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
104 }
105
106 /* forms */
107
108 + (BOOL)isDynamicElement {
109   return NO;
110 }
111
112 // description
113
114 - (NSString *)indentString:(int)_indent {
115   switch (_indent) {
116     case  0: return @"";
117     case  2: return @"  ";
118     case  4: return @"    ";
119     case  6: return @"      ";
120     case  8: return @"        ";
121     case 10: return @"          ";
122     case 12: return @"            ";
123     case 14: return @"              ";
124
125     default: {
126       int cnt;
127       NSMutableString *str = [[NSMutableString alloc] init];
128       for (cnt = 0; cnt < _indent; cnt++)
129         [str appendString:@" "];
130       return AUTORELEASE(str);
131     }
132   }
133 }
134
135 - (NSString *)elementTreeWithIndent:(int)_indent {
136   NSMutableString *str = [[NSMutableString alloc] init];
137
138   [str appendString:[self indentString:_indent]];
139   [str appendString:[self description]];
140   [str appendString:@"\n"];
141
142   return AUTORELEASE(str);
143 }
144
145 - (NSString *)elementTree {
146   return [self elementTreeWithIndent:2];
147 }
148
149 - (NSString *)description {
150   return [NSString stringWithFormat:@"<%@[0x%08X]>",
151                      NSStringFromClass([self class]), self];
152 }
153
154 @end /* WOElement */
155
156 @implementation WOElement(QueryString)
157
158 - (NSString *)queryStringForQueryDictionary:(NSDictionary *)_queryDict
159   andQueryParameters:(NSDictionary *)_paras
160   inContext:(WOContext *)_ctx
161 {
162   NSMutableString *str;
163   NSEnumerator    *keys;
164   NSString        *key;
165   NSString        *value;
166   BOOL            isFirst;
167   WOComponent     *sComponent;
168
169   if ((_queryDict == nil) && (_paras == nil))
170     return nil;
171
172   str = [NSMutableString stringWithCapacity:128];
173   sComponent = [_ctx component];
174   
175   keys = [_queryDict keyEnumerator];
176   isFirst = YES;
177   while ((key = [keys nextObject])) {
178     value = [[_queryDict objectForKey:key] stringValue];
179     value = value ? [value stringByEscapingURL] : @"";
180     key   = key   ? [key   stringByEscapingURL] : @"";
181
182     if (isFirst) isFirst = NO;
183     else [str appendString:@"&"];
184
185     [str appendString:key];
186     [str appendString:@"="];
187     [str appendString:value];
188   }
189
190   /* ?style parameters */
191   
192   keys = [_paras keyEnumerator];
193   while ((key = [keys nextObject])) {
194     value = [[_paras objectForKey:key] stringValueInComponent:sComponent];
195     value = value ? [value stringByEscapingURL] : @"";
196     key   = key   ? [key   stringByEscapingURL] : @"";
197
198     if (isFirst) isFirst = NO;
199     else [str appendString:@"&"];
200
201     [str appendString:key];
202     [str appendString:@"="];
203     [str appendString:value];
204   }
205   
206   return [str length] > 0 ? str : nil;
207 }
208
209 @end /* WOElement(QueryString) */
210
211 NGObjWeb_DECLARE id OWGetProperty(NSDictionary *_set, NSString *_name) {
212   id propValue = [_set objectForKey:_name];
213
214   if (propValue) {
215     propValue = RETAIN(propValue);
216     [(id)_set removeObjectForKey:_name];
217   }
218   return propValue;
219 }