1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/*
Copyright (C) 2000-2005 SKYRIX Software AG
This file is part of SOPE.
SOPE is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with SOPE; see the file COPYING. If not, write to the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#include <NGObjWeb/WOHTMLDynamicElement.h>
@class NSDictionary;
@class WOAssociation;
@interface WOSwitchComponent : WOHTMLDynamicElement
{
// WODynamicElement: extraAttributes
// WODynamicElement: otherTagString
@protected
WOAssociation *componentName; // WOComponentName attribute
NSDictionary *bindings;
WOElement *template;
}
@end
#include <NGObjWeb/WOComponent.h>
#include <NGObjWeb/WOAssociation.h>
#include <NGObjWeb/WOContext.h>
#include "WOElement+private.h"
#include "WOContext+private.h"
#include "WOComponent+private.h"
#include "decommon.h"
@implementation WOSwitchComponent
- (id)initWithName:(NSString *)_name
associations:(NSDictionary *)_config
template:(WOElement *)_c
{
if ((self = [super initWithName:_name associations:_config template:_c])) {
self->containsForm = YES;
self->componentName = OWGetProperty(_config, @"WOComponentName");
self->bindings = [_config copy];
[(NSMutableDictionary *)_config removeAllObjects];
self->template = [_c retain];
}
return self;
}
- (void)dealloc {
[self->template release];
[self->componentName release];
[self->bindings release];
[super dealloc];
}
/* component lookup */
- (WOComponent *)lookupComponent:(NSString *)cname
inContext:(WOContext *)_ctx
{
WOComponent *component;
if (cname == nil)
return nil;
if ((component = [[_ctx component] pageWithName:cname]) == nil) {
[[_ctx component] debugWithFormat:@"couldn't find component '%@'", cname];
return nil;
}
[component setParent:[_ctx component]];
[component setBindings:self->bindings];
return component;
}
/* handling requests */
- (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
WOComponent *c;
NSString *cname;
cname = [self->componentName stringValueInComponent:[_ctx component]];
if ((c = [self lookupComponent:cname inContext:_ctx]) == nil)
return;
[_ctx appendElementIDComponent:cname];
[_ctx enterComponent:c content:self->template];
[c takeValuesFromRequest:_req inContext:_ctx];
[_ctx leaveComponent:c];
[_ctx deleteLastElementIDComponent];
}
- (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
WOComponent *c;
id result;
NSString *cname, *reqname;
if ((reqname = [_ctx currentElementID]) == nil)
/* missing id in request */
return nil;
cname = [self->componentName stringValueInComponent:[_ctx component]];
if (![cname isEqualToString:reqname]) {
/* component mismatch */
[[_ctx component] logWithFormat:
@"WOSwitchComponent: component name mismatch"
@" (%@ vs %@), ignoring action.",
cname, reqname];
return nil;
}
if ((c = [self lookupComponent:cname inContext:_ctx]) == nil)
return nil;
[_ctx consumeElementID];
[_ctx appendElementIDComponent:cname];
[_ctx enterComponent:c content:self->template];
result = [c invokeActionForRequest:_req inContext:_ctx];
[_ctx leaveComponent:c];
[_ctx deleteLastElementIDComponent];
return result;
}
/* generate response */
- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
WOComponent *c;
NSString *cname;
cname = [self->componentName stringValueInComponent:[_ctx component]];
if ((c = [self lookupComponent:cname inContext:_ctx]) == nil)
return;
[_ctx appendElementIDComponent:cname];
[_ctx enterComponent:c content:self->template];
[c appendToResponse:_response inContext:_ctx];
[_ctx leaveComponent:c];
[_ctx deleteLastElementIDComponent];
}
@end /* WOSwitchComponent */
|