2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
23 WESwitch { selection | selections };
24 WECase { key | keys };
27 Warning: The DefaultCase must appear at the last position!!!
34 Switch: WESwitch { selection = selection; };
35 FirstCase: WECase { key = "first"; };
36 SecondCase: WECase { keys = ("second", "third"); };
37 DefaultCase: WEDefaultCase {};
41 <#FirstCase>content of first case</#FirstCase>
42 <#SecondCase>content of second case</#SecondCase>
43 <#DefaultCase>content of default case</#SecondCase>
47 #include <NGObjWeb/WODynamicElement.h>
51 @interface WESwitch : WODynamicElement
54 WOAssociation *selection; // string -> single switch
55 WOAssociation *selections; // array -> multi switch
60 @interface WECase : WODynamicElement
62 WOAssociation *key; // string -> unique identifier
63 WOAssociation *keys; // array of unique identifiers
65 WOAssociation *defaultCase; // emulates a WEDefaultCase DEPRECATED!!!
71 @interface WEDefaultCase : WODynamicElement
80 static NSString *WESwitch_DefaultCaseFound = @"WESwitch_DefaultCaseFound";
82 static NSString *WESwitch_CaseDidMatch = @"WESwitch_CaseDidMatch";
83 static NSString *WESwitchSelection = @"WESwitchSelection";
84 static NSString *WESwitchSelections = @"WESwitchSelections";
85 static NSString *WESwitchDict = @"WESwitchDict";
87 @implementation WESwitch
89 - (id)initWithName:(NSString *)_name
90 associations:(NSDictionary *)_config
91 template:(WOElement *)_subs
93 if ((self = [super initWithName:_name associations:_config template:_subs])) {
94 self->selection = WOExtGetProperty(_config, @"selection");
95 self->selections = WOExtGetProperty(_config, @"selections");
97 self->template = [_subs retain];
103 [self->template release];
104 [self->selection release];
105 [self->selections release];
109 /* processing requests */
111 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
112 WOComponent *cmp = nil;
113 NSArray *array = nil;
119 cmp = [_ctx component];
120 array = [self->selections valueInComponent:cmp];
121 k = [self->selection valueInComponent:cmp];
125 [self->template takeValuesFromRequest:_req inContext:_ctx];
128 [_ctx setObject:k forKey:WESwitchSelection];
129 [self->template takeValuesFromRequest:_req inContext:_ctx];
130 [_ctx removeObjectForKey:WESwitchSelection];
133 for (i = 0; i < cnt; i++) {
134 [_ctx setObject:[array objectAtIndex:i] forKey:WESwitchSelection];
135 [self->template takeValuesFromRequest:_req inContext:_ctx];
138 [_ctx setObject:array forKey:WESwitchSelections];
139 [self->template takeValuesFromRequest:_req inContext:_ctx];
140 [_ctx removeObjectForKey:WESwitchSelections];
142 [_ctx removeObjectForKey:WESwitchSelection];
145 NSLog(@"Warning(%s):This case is not implemented!!!", __PRETTY_FUNCTION__);
146 [self->template takeValuesFromRequest:_req inContext:_ctx];
152 @"Warning! WESwitch: Neither 'selection' nor 'selections' set!!!"];
156 [_ctx removeObjectForKey:WESwitch_CaseDidMatch];
159 - (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
160 return [self->template invokeActionForRequest:_request inContext:_ctx];
163 /* generating response */
165 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
166 WOComponent *cmp = nil;
167 NSArray *array = nil;
173 cmp = [_ctx component];
174 array = [self->selections valueInComponent:cmp];
175 k = [self->selection valueInComponent:cmp];
178 if (_response == nil) {
179 [self->template appendToResponse:_response inContext:_ctx];
182 [_ctx setObject:k forKey:WESwitchSelection];
183 [self->template appendToResponse:_response inContext:_ctx];
184 [_ctx removeObjectForKey:WESwitchSelection];
187 for (i=0; i<cnt; i++) {
188 [_ctx setObject:[array objectAtIndex:i] forKey:WESwitchSelection];
189 [self->template appendToResponse:_response inContext:_ctx];
192 [_ctx setObject:array forKey:WESwitchSelections];
193 [self->template appendToResponse:_response inContext:_ctx];
194 [_ctx removeObjectForKey:WESwitchSelections];
196 [_ctx removeObjectForKey:WESwitchSelection];
199 NSMutableDictionary *dict = nil;
201 // get subcontent of WECases
202 [_ctx setObject:array forKey:WESwitchSelections];
203 [self->template appendToResponse:_response inContext:_ctx];
205 dict = [_ctx objectForKey:WESwitchDict];
209 for (i=0; i<cnt; i++) {
210 NSString *k = [array objectAtIndex:i];
211 NSData *c = [dict objectForKey:k]; // subcontent of WECase
214 [_response appendContentData:c];
218 [_ctx removeObjectForKey:WESwitchDict];
219 [_ctx removeObjectForKey:WESwitchSelections];
225 @"Warning! WESwitch: Neither 'selection' nor 'selections' set!!!"];
229 [_ctx removeObjectForKey:WESwitch_CaseDidMatch];
234 @implementation WECase
236 - (id)initWithName:(NSString *)_name
237 associations:(NSDictionary *)_config
238 template:(WOElement *)_t
240 if ((self = [super initWithName:_name associations:_config template:_t])) {
241 self->key = WOExtGetProperty(_config, @"key");
242 self->keys = WOExtGetProperty(_config, @"keys");
245 self->defaultCase = WOExtGetProperty(_config, @"default");
247 self->template = [_t retain];
253 [self->template release];
255 [self->keys release];
256 [self->defaultCase release];
260 /* processing requests */
262 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
263 NSArray *selections = nil;
264 NSString *selection = nil;
268 k = [self->key stringValueInComponent:[_ctx component]];
269 ks = [self->keys valueInComponent:[_ctx component]];
271 selections = [_ctx objectForKey:WESwitchSelections];
272 selection = [_ctx objectForKey:WESwitchSelection];
274 if ([self->defaultCase boolValueInComponent:[_ctx component]]) {
275 if ([_ctx objectForKey:WESwitch_CaseDidMatch] == nil)
276 [self->template takeValuesFromRequest:_req inContext:_ctx];
280 if ((k == nil) && (ks == nil)) {
282 [[_ctx component] logWithFormat:
283 @"Warning! WECase: Neither 'key' nor 'keys' set!!!"];
287 if ((k != nil) && (ks != nil)) {
289 [[_ctx component] logWithFormat:
290 @"Warning! WECase: Both, 'key' and 'keys' are set!!!"];
296 [self->template takeValuesFromRequest:nil inContext:_ctx];
299 if (k && [k isEqualToString:selection]) {
300 [self->template takeValuesFromRequest:_req inContext:_ctx];
301 [_ctx setObject:@"YES" forKey:WESwitch_CaseDidMatch];
303 else if (ks && [ks containsObject:selection]) {
304 [self->template takeValuesFromRequest:_req inContext:_ctx];
305 [_ctx setObject:@"YES" forKey:WESwitch_CaseDidMatch];
308 else if (selections && [selections count] > 0) {
309 NSLog(@"Warning(%s): This case is not implemented!", __PRETTY_FUNCTION__);
310 [self->template takeValuesFromRequest:_req inContext:_ctx];
314 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
315 return [self->template invokeActionForRequest:_req inContext:_ctx];
318 /* generating response */
320 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
321 NSArray *selections = nil;
322 NSString *selection = nil;
327 k = [self->key stringValueInComponent:[_ctx component]];
328 ks = [self->keys valueInComponent:[_ctx component]];
330 selections = [_ctx objectForKey:WESwitchSelections];
331 selection = [_ctx objectForKey:WESwitchSelection];
333 if ([self->defaultCase boolValueInComponent:[_ctx component]]) {
334 if ([_ctx objectForKey:WESwitch_CaseDidMatch] == nil)
335 [self->template appendToResponse:_response inContext:_ctx];
339 if ((k == nil) && (ks == nil)) {
341 [[_ctx component] logWithFormat:
342 @"Warning! WECase: Neither 'key' nor 'keys' set!!!"];
346 if ((k != nil) && (ks != nil)) {
348 [[_ctx component] logWithFormat:
349 @"Warning! WECase: Both, 'key' and 'keys' are set!!!"];
354 if (_response == nil) {
355 [self->template appendToResponse:nil inContext:_ctx];
358 if (k && [k isEqualToString:selection]) {
359 [self->template appendToResponse:_response inContext:_ctx];
360 [_ctx setObject:@"YES" forKey:WESwitch_CaseDidMatch];
362 else if (ks && [ks containsObject:selection]) {
363 [self->template appendToResponse:_response inContext:_ctx];
364 [_ctx setObject:@"YES" forKey:WESwitch_CaseDidMatch];
367 else if (selections && [selections count] > 0) {
368 if ([selections containsObject:k]) {
369 static NSData *emptyData = nil;
370 NSMutableDictionary *dict = nil;
371 NSData *oldContent = nil;
373 if (emptyData == nil)
374 emptyData = [[NSData alloc] init];
376 // get subcontent dictionary
377 dict = [_ctx objectForKey:WESwitchDict];
379 dict = [NSMutableDictionary dictionaryWithCapacity:[selections count]];
382 oldContent = [_response content];
384 [_response setContent:emptyData];
386 // append template to new content
387 [self->template appendToResponse:_response inContext:_ctx];
389 // save new content in dict
390 if ([_response content])
391 [dict setObject:[_response content] forKey:k];
392 [_ctx setObject:dict forKey:WESwitchDict];
394 // restore old content
395 [_response setContent:oldContent];
396 [oldContent release]; oldContent = nil;
398 // TODO: use NSNumber here?
399 [_ctx setObject:@"YES" forKey:WESwitch_CaseDidMatch];
406 @implementation WEDefaultCase
408 - (id)initWithName:(NSString *)_name
409 associations:(NSDictionary *)_config
410 template:(WOElement *)_t
412 if ((self = [super initWithName:_name associations:_config template:_t])) {
413 self->template = [_t retain];
419 [self->template release];
423 /* processing requests */
425 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
426 if (([_ctx objectForKey:WESwitch_CaseDidMatch] == nil))
427 [self->template takeValuesFromRequest:_req inContext:_ctx];
430 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
431 return [self->template invokeActionForRequest:_req inContext:_ctx];
434 /* generating response */
436 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
437 if (([_ctx objectForKey:WESwitch_CaseDidMatch] == nil))
438 [self->template appendToResponse:_response inContext:_ctx];
441 [_ctx setObject:@"Yes" forKey:WESwitch_DefaultCaseFound];
445 @end /* WEDefaultCase */