]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOConditional.m
use %p for pointer formats, fixed gcc 4.1 warnings, minor code improvs
[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] : (id)@"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 warnWithFormat:
77               @"missing 'condition' association in element: '%@'", _name];
78     }
79   }
80   return self;
81 }
82
83 - (id)initWithNegateElement:(id<DOMElement>)_element
84   templateBuilder:(WOxElemBuilder *)_builder
85 {
86   /* need an own -init so that we can patch the 'negate' association */
87   NSString            *name;
88   NSMutableDictionary *assocs;
89   NSArray             *children;
90   id<NSObject,DOMNamedNodeMap> attrs;
91   unsigned count;
92   
93   name = [_element tagName];
94   
95   /* construct associations */
96   
97   assocs = nil;
98   attrs = [_element attributes];
99   if ((count = [attrs length]) > 0)
100     assocs = [_builder associationsForAttributes:attrs];
101
102   if ([assocs objectForKey:@"negate"] != nil) {
103     // TODO: implement
104     [self logWithFormat:@"TODO: if-not with 'negate' binding not supported!"];
105     [self release];
106     return nil;
107   }
108   else {
109     static WOAssociation *yesAssoc = nil;
110     if (yesAssoc == nil) {
111       yesAssoc = [[WOAssociation associationWithValue:
112                                    [NSNumber numberWithBool:YES]] retain];
113     }
114     [assocs setObject:yesAssoc forKey:@"negate"];
115   }
116   
117   /* construct child elements */
118   
119   if ([_element hasChildNodes]) {
120     /* look for var:binding tags ... */
121     
122     children = [_builder buildNodes:[_element childNodes]
123                          templateBuilder:_builder];
124   }
125   else
126     children = nil;
127   
128   /* construct self ... */
129   return [self initWithName:name associations:assocs contentElements:children];
130 }
131
132 - (id)initWithElement:(id<DOMElement>)_element
133   templateBuilder:(WOxElemBuilder *)_builder
134 {
135   NSString *tag;
136   
137   tag = [_element tagName];
138   if ([tag isEqualToString:@"if-not"] || [tag isEqualToString:@"ifnot"])
139     return [self initWithNegateElement:_element templateBuilder:_builder];
140   
141   return [super initWithElement:_element templateBuilder:_builder];
142 }
143
144 - (void)dealloc {
145   [self->template  release];
146   [self->value     release];
147   [self->condition release];
148   [self->negate    release];
149 #if DEBUG
150   [self->condName release];
151 #endif
152   [super dealloc];
153 }
154
155 /* accessors */
156
157 - (id)template {
158   return self->template;
159 }
160
161 /* state */
162
163 static inline BOOL _doShow(WOConditional *self, WOContext *_ctx) {
164   WOComponent *cmp = [_ctx component];
165   BOOL doShow   = NO;
166   BOOL doNegate = [self->negate boolValueInComponent:cmp];
167
168   if (self->value) {
169     id v  = [self->value     valueInComponent:cmp];
170     id cv = [self->condition valueInComponent:cmp];
171     
172     doShow = [cv isEqual:v];
173   }
174   else
175     doShow = [self->condition boolValueInComponent:cmp];
176   
177   return doNegate ? !doShow : doShow;
178 }
179
180 /* processing requests */
181
182 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
183   if (!_doShow(self, _ctx)) {
184 #if 0
185     NSLog(@"didn't take value from request: %@\n  doShow=%@\n  doNegate=%@",
186           [self elementID],
187           self->condition, self->negate);
188 #endif
189     return;
190   }
191   
192 #if DEBUG
193   [_ctx appendElementIDComponent:
194           descriptiveIDs ? self->condName : (NSString *)@"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 : (NSString *)@"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:
236           descriptiveIDs ? self->condName : (NSString *)@"1"];
237 #else
238   [_ctx appendElementIDComponent:@"1"];
239 #endif
240     
241   [self->template appendToResponse:_response inContext:_ctx];
242   [_ctx deleteLastElementIDComponent];
243 }
244
245 /* description */
246
247 - (NSString *)associationDescription {
248   NSMutableString *str;
249
250   str = [NSMutableString stringWithCapacity:64];
251   if (self->condition) [str appendFormat:@" condition=%@", self->condition];
252   if (self->negate)    [str appendFormat:@" negate=%@",    self->negate];
253   if (self->template)  [str appendFormat:@" template=%@",  self->template];
254   return str;
255 }
256
257 @end /* WOConditional */