]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOSwitchComponent.m
fixed bundle resource lookup on MacOSX, changed resource lookup in
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOSwitchComponent.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 "WOHTMLDynamicElement.h"
23
24 @class NSDictionary;
25 @class WOAssociation;
26
27 @interface WOSwitchComponent : WOHTMLDynamicElement
28 {
29   // WODynamicElement: extraAttributes
30   // WODynamicElement: otherTagString
31 @protected
32   WOAssociation *componentName; // WOComponentName attribute
33   NSDictionary  *bindings;
34   WOElement     *template;
35 }
36
37 @end
38
39 #include <NGObjWeb/WOComponent.h>
40 #include <NGObjWeb/WOAssociation.h>
41 #include <NGObjWeb/WOContext.h>
42 #import "WOElement+private.h"
43 #import "WOContext+private.h"
44 #import "WOComponent+private.h"
45 #include "common.h"
46
47 @implementation WOSwitchComponent
48
49 - (id)initWithName:(NSString *)_name
50   associations:(NSDictionary *)_config
51   template:(WOElement *)_c
52 {
53   if ((self = [super initWithName:_name associations:_config template:_c])) {
54     self->containsForm  = YES;
55     self->componentName = OWGetProperty(_config, @"WOComponentName");
56     self->bindings      = [_config copyWithZone:[self zone]];
57     [(NSMutableDictionary *)_config removeAllObjects];
58
59     self->template = RETAIN(_c);
60   }
61   return self;
62 }
63
64 #if !LIB_FOUNDATION_BOEHM_GC
65 - (void)dealloc {
66   RELEASE(self->template);
67   RELEASE(self->componentName);
68   RELEASE(self->bindings);
69   [super dealloc];
70 }
71 #endif
72
73 - (WOComponent *)lookupComponent:(NSString *)cname inContext:(WOContext *)_ctx {
74   WOComponent *component;
75   
76   if (cname == nil)
77     return nil;
78
79   if ((component = [[_ctx component] pageWithName:cname]) == nil) {
80     [[_ctx component] debugWithFormat:@"couldn't find component '%@'", cname];
81     return nil;
82   }
83   
84   [component setParent:[_ctx component]];
85   [component setBindings:self->bindings];
86   
87   return component;
88 }
89
90 // responder
91
92 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
93   WOComponent *c;
94   NSString    *cname;
95   
96   cname = [self->componentName stringValueInComponent:[_ctx component]];
97   
98   if ((c = [self lookupComponent:cname inContext:_ctx]) == nil)
99     return;
100   
101   [_ctx appendElementIDComponent:cname];
102   [_ctx enterComponent:c content:self->template];
103   [c takeValuesFromRequest:_req inContext:_ctx];
104   [_ctx leaveComponent:c];
105   [_ctx deleteLastElementIDComponent];
106 }
107
108 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
109   WOComponent *c;
110   id       result;
111   NSString *cname, *reqname;
112
113   if ((reqname = [_ctx currentElementID]) == nil)
114     /* missing id in request */
115     return nil;
116   
117   cname = [self->componentName stringValueInComponent:[_ctx component]];
118
119   if (![cname isEqualToString:reqname]) {
120     /* component mismatch */
121     [[_ctx component] logWithFormat:
122                         @"WOSwitchComponent: component name mismatch (%@ vs %@),"
123                         @"ignoring action.",
124                         cname, reqname];
125     return nil;
126   }
127   
128   if ((c = [self lookupComponent:cname inContext:_ctx]) == nil)
129     return nil;
130
131   [_ctx appendElementIDComponent:cname];
132   [_ctx enterComponent:c content:self->template];
133   result = [c invokeActionForRequest:_req inContext:_ctx];
134   [_ctx leaveComponent:c];
135   [_ctx deleteLastElementIDComponent];
136   
137   return result;
138 }
139
140 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
141   WOComponent *c;
142   NSString    *cname;
143   
144   cname = [self->componentName stringValueInComponent:[_ctx component]];
145   
146   if ((c = [self lookupComponent:cname inContext:_ctx]) == nil)
147     return;
148   
149   [_ctx appendElementIDComponent:cname];
150   [_ctx enterComponent:c content:self->template];
151   [c appendToResponse:_response inContext:_ctx];
152   [_ctx leaveComponent:c];
153   [_ctx deleteLastElementIDComponent];
154 }
155
156 @end /* WOSwitchComponent */