]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOConditional.m
renamed common.h file in DynamicElements
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOConditional.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 WOConditional : WODynamicElement
25 {
26 @protected
27   // WODynamicElement: extraAttributes
28   // WODynamicElement: otherTagString
29   
30   WOAssociation *condition;
31   WOAssociation *negate;
32   WOElement     *template;
33
34   // non-WO
35   WOAssociation *value; // compare the condition with value
36
37 #if DEBUG
38   NSString *condName;
39 #endif
40 }
41
42 @end /* WOConditional */
43
44 #include <DOM/EDOM.h>
45 #include <NGObjWeb/WOxElemBuilder.h>
46 #include "decommon.h"
47 #include "WOElement+private.h"
48
49 // TODO: make that a class cluster for improved performance
50
51 @implementation WOConditional
52
53 static int descriptiveIDs = -1;
54
55 + (void)initialize {
56   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
57   
58   descriptiveIDs = [ud boolForKey:@"WODescriptiveElementIDs"] ? 1 : 0;
59 }
60
61 - (id)initWithName:(NSString *)_name
62   associations:(NSDictionary *)_config
63   template:(WOElement *)_c
64 {
65 #if DEBUG
66   self->condName = _name ? [_name copy] : @"condYES";
67 #endif
68   
69   if ((self = [super initWithName:_name associations:_config template:_c])) {
70     self->condition = OWGetProperty(_config, @"condition");
71     self->negate    = OWGetProperty(_config, @"negate");
72     self->value     = OWGetProperty(_config, @"value");
73     self->template  = [_c retain];
74     
75     if (self->condition == nil) {
76       [self logWithFormat:
77               @"WARNING: missing 'condition' association in element: '%@'",
78               _name];
79     }
80   }
81   return self;
82 }
83
84 - (id)initWithNegateElement:(id<DOMElement>)_element
85   templateBuilder:(WOxElemBuilder *)_builder
86 {
87   /* need an own -init so that we can patch the 'negate' association */
88   NSString            *name;
89   NSMutableDictionary *assocs;
90   NSArray             *children;
91   id<NSObject,DOMNamedNodeMap> attrs;
92   unsigned count;
93   
94   name = [_element tagName];
95   
96   /* construct associations */
97   
98   assocs = nil;
99   attrs = [_element attributes];
100   if ((count = [attrs length]) > 0)
101     assocs = [_builder associationsForAttributes:attrs];
102
103   if ([assocs objectForKey:@"negate"] != nil) {
104     // TODO: implement
105     [self logWithFormat:@"TODO: if-not with 'negate' binding not supported!"];
106     [self release];
107     return nil;
108   }
109   else {
110     static WOAssociation *yesAssoc = nil;
111     if (yesAssoc == nil) {
112       yesAssoc = [[WOAssociation associationWithValue:
113                                    [NSNumber numberWithBool:YES]] retain];
114     }
115     [assocs setObject:yesAssoc forKey:@"negate"];
116   }
117   
118   /* construct child elements */
119   
120   if ([_element hasChildNodes]) {
121     /* look for var:binding tags ... */
122     
123     children = [_builder buildNodes:[_element childNodes]
124                          templateBuilder:_builder];
125   }
126   else
127     children = nil;
128   
129   /* construct self ... */
130   return [self initWithName:name associations:assocs contentElements:children];
131 }
132
133 - (id)initWithElement:(id<DOMElement>)_element
134   templateBuilder:(WOxElemBuilder *)_builder
135 {
136   NSString *tag;
137   
138   tag = [_element tagName];
139   if ([tag isEqualToString:@"if-not"] || [tag isEqualToString:@"ifnot"])
140     return [self initWithNegateElement:_element templateBuilder:_builder];
141   
142   return [super initWithElement:_element templateBuilder:_builder];
143 }
144
145 - (void)dealloc {
146   [self->template  release];
147   [self->value     release];
148   [self->condition release];
149   [self->negate    release];
150 #if DEBUG
151   [self->condName release];
152 #endif
153   [super dealloc];
154 }
155
156 /* accessors */
157
158 - (WOElement *)template {
159   return self->template;
160 }
161
162 /* state */
163
164 static inline BOOL _doShow(WOConditional *self, WOContext *_ctx) {
165   WOComponent *cmp = [_ctx component];
166   BOOL doShow   = NO;
167   BOOL doNegate = [self->negate boolValueInComponent:cmp];
168
169   if (self->value) {
170     id v  = [self->value     valueInComponent:cmp];
171     id cv = [self->condition valueInComponent:cmp];
172     
173     doShow = [cv isEqual:v];
174   }
175   else
176     doShow = [self->condition boolValueInComponent:cmp];
177   
178   return doNegate ? !doShow : doShow;
179 }
180
181 /* processing requests */
182
183 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
184   if (!_doShow(self, _ctx)) {
185 #if 0
186     NSLog(@"didn't take value from request: %@\n  doShow=%@\n  doNegate=%@",
187           [self elementID],
188           self->condition, self->negate);
189 #endif
190     return;
191   }
192   
193 #if DEBUG
194   [_ctx appendElementIDComponent:descriptiveIDs ? self->condName : @"1"];
195 #else
196   [_ctx appendElementIDComponent:@"1"];
197 #endif
198   [self->template takeValuesFromRequest:_rq inContext:_ctx];
199   [_ctx deleteLastElementIDComponent];
200 }
201
202 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
203   NSString *state;
204   NSString *key;
205   id result;
206
207   state = [[_ctx currentElementID] stringValue];
208   
209   if (!state) 
210     return nil;
211     
212   [_ctx consumeElementID]; // consume state-id (on or off)
213     
214 #if DEBUG
215   key = descriptiveIDs ? self->condName : @"1";
216 #else
217   key = @"1";
218 #endif
219     
220   if (![state isEqualToString:key])
221     return nil;
222       
223   [_ctx appendElementIDComponent:state];
224   result = [self->template invokeActionForRequest:_rq inContext:_ctx];
225   [_ctx deleteLastElementIDComponent];
226   return result;
227 }
228
229 /* generating response */
230
231 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
232   if (!_doShow(self, _ctx))
233     return;
234 #if DEBUG
235   [_ctx appendElementIDComponent:descriptiveIDs ? self->condName : @"1"];
236 #else
237   [_ctx appendElementIDComponent:@"1"];
238 #endif
239     
240   [self->template appendToResponse:_response inContext:_ctx];
241   [_ctx deleteLastElementIDComponent];
242 }
243
244 /* description */
245
246 - (NSString *)associationDescription {
247   NSMutableString *str;
248
249   str = [NSMutableString stringWithCapacity:64];
250   if (self->condition) [str appendFormat:@" condition=%@", self->condition];
251   if (self->negate)    [str appendFormat:@" negate=%@",    self->negate];
252   if (self->template)  [str appendFormat:@" template=%@",  self->template];
253   return str;
254 }
255
256 @end /* WOConditional */