]> err.no Git - sope/blob - sope-appserver/WEExtensions/WEContextKey.m
added strict OSX bundle dependencies
[sope] / sope-appserver / WEExtensions / WEContextKey.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/WODynamicElement.h>
23
24 @interface WEContextKey : WODynamicElement
25 {
26   WOAssociation *key;
27   WOAssociation *value;
28   WOElement     *template;
29 }
30 @end
31
32 #include "common.h"
33
34 @implementation WEContextKey
35
36 - (id)initWithName:(NSString *)_name
37   associations:(NSDictionary *)_config
38   template:(WOElement *)_tmp
39 {
40   if ((self = [super initWithName:_name associations:_config template:_tmp])) {
41     self->key   = WOExtGetProperty(_config, @"key");
42     self->value = WOExtGetProperty(_config, @"value");
43
44     if (self->key == nil)
45       NSLog(@"Warning! WEContextKey no key set");
46     
47     if (self->value == nil) {
48       self->value = [WOAssociation associationWithValue:@"YES"];
49       RETAIN(self->value);
50     }
51     
52     ASSIGN(self->template, _tmp);
53   }
54   return self;
55 }
56
57 #if !LIB_FOUNDATION_BOEHM_GC
58 - (void)dealloc {
59   RELEASE(self->key);
60   RELEASE(self->value);
61   RELEASE(self->template);
62   
63   [super dealloc];
64 }
65 #endif
66
67 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
68   NSString *k  = nil;
69   id       v   = nil;
70   id       tmp = nil;
71
72   k = [self->key   stringValueInComponent:[_ctx component]];
73   v = [self->value valueInComponent:[_ctx component]];
74
75   if (k && v) {
76     tmp = [_ctx objectForKey:k]; // save old context value
77     [_ctx setObject:v forKey:k];
78   }
79   
80   [self->template takeValuesFromRequest:_req inContext:_ctx];
81
82   if (k && v)   [_ctx removeObjectForKey:k];
83   if (tmp && k) [_ctx setObject:tmp forKey:k]; // restore old context value
84 }
85
86 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
87   NSString *k     = nil;
88   id       v      = nil;
89   id       tmp    = nil;
90   id       result = nil;
91
92   k = [self->key   stringValueInComponent:[_ctx component]];
93   v = [self->value valueInComponent:[_ctx component]];
94
95   if (k && v) {
96     tmp = [_ctx objectForKey:k]; // save old context value
97     [_ctx setObject:v forKey:k];
98   }
99   
100   result = [self->template invokeActionForRequest:_req inContext:_ctx];
101   
102   if (k && v)   [_ctx removeObjectForKey:k];
103   if (tmp && k) [_ctx setObject:tmp forKey:k]; // restore old context value
104   
105   return result;
106 }
107
108 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
109   NSString *k  = nil;
110   id       v   = nil;
111   id       tmp = nil;
112   
113   k = [self->key   stringValueInComponent:[_ctx component]];
114   v = [self->value valueInComponent:[_ctx component]];
115
116   if (k && v) {
117     tmp = [_ctx objectForKey:k]; // save old context value
118     [_ctx setObject:v forKey:k];
119   }
120
121   [self->template appendToResponse:_response inContext:_ctx];
122   
123   if (k && v)   [_ctx removeObjectForKey:k];
124   if (tmp && k) [_ctx setObject:tmp forKey:k]; // restore old context value
125 }
126
127 @end /* WEContextKey */