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