2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
22 #include "WOComponentReference.h"
23 #include "WOElement+private.h"
24 #include "WOContext+private.h"
25 #include "WOComponent+private.h"
28 @interface WOContext(ComponentStackCount)
29 - (unsigned)componentStackCount;
32 @implementation WOComponentReference
34 static int profileComponents = -1;
35 static BOOL coreOnRecursion = NO;
36 static BOOL debugOn = NO;
37 static Class NSDateClass = Nil;
40 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
42 profileComponents = [[ud objectForKey:@"WOProfileComponents"]
44 coreOnRecursion = [ud boolForKey:@"WOCoreOnRecursiveSubcomponents"];
45 NSDateClass = [NSDate class];
48 - (id)initWithName:(NSString *)_name
49 associations:(NSDictionary *)_config
50 template:(WOElement *)_c
52 if ((self = [super initWithName:_name associations:_config template:_c])) {
53 self->containsForm = YES;
54 self->activeComponent = OWGetProperty(_config, @"component");
55 self->bindings = [_config copyWithZone:[self zone]];
56 [(NSMutableDictionary *)_config removeAllObjects];
58 self->template = [_c retain];
64 [self->template release];
65 [self->bindings release];
66 [self->activeComponent release];
67 [self->child release];
73 - (WOComponent *)childComponent {
78 _updateComponent(WOComponentReference *self, WOContext *_ctx)
80 if (self->activeComponent) {
81 WOComponent *newChild;
83 newChild = [self->activeComponent valueInComponent:[_ctx component]];
86 if (newChild == nil) {
87 [[_ctx component] logWithFormat:
88 @"missing child (got nil) "
89 @"(element=%@, association=%@, component=%@).",
90 self, self->activeComponent,
91 [[_ctx component] name]];
95 if (newChild != self->child) {
96 //NSLog(@"switched component %@ => %@ ...",
97 // [self->child name], [newChild name]);
98 ASSIGN(self->child, newChild);
99 [newChild setParent:[_ctx component]];
100 [newChild setBindings:self->bindings];
105 // ******************** responder ********************
107 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
110 parent = [_ctx component];
112 _updateComponent(self, _ctx);
115 [_ctx enterComponent:self->child content:self->template];
116 [self->child takeValuesFromRequest:_req inContext:_ctx];
117 [_ctx leaveComponent:self->child];
121 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
125 parent = [_ctx component];
127 _updateComponent(self, _ctx);
130 [_ctx enterComponent:self->child content:self->template];
131 result = [self->child invokeActionForRequest:_req inContext:_ctx];
132 [_ctx leaveComponent:self->child];
138 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
141 parent = [_ctx component];
143 _updateComponent(self, _ctx);
145 if (self->child == parent) {
146 [self debugWithFormat:@"WARNING: recursive call of component: %@", parent];
152 NSTimeInterval st = 0.0;
154 if (profileComponents)
155 st = [[NSDateClass date] timeIntervalSince1970];
157 [_ctx enterComponent:self->child content:self->template];
158 [self->child appendToResponse:_response inContext:_ctx];
159 [_ctx leaveComponent:self->child];
161 if (profileComponents) {
164 diff = [[NSDateClass date] timeIntervalSince1970] - st;
165 for (i = [_ctx componentStackCount]; i >= 0; i--)
167 printf("[%s %s]: %0.3fs\n",
168 [[child name] cString],
169 #if APPLE_RUNTIME || NeXT_RUNTIME
178 [self debugWithFormat:@"missing component for reference: %@",
179 self->activeComponent];
180 [_response appendContentHTMLString:@"[missing component for reference: "];
181 [_response appendContentHTMLString:[self->activeComponent description]];
182 [_response appendContentHTMLString:@"]"];
188 - (NSString *)description {
189 NSMutableString *desc = [NSMutableString stringWithCapacity:64];
191 [desc appendFormat:@"<%@[0x%08X]:", NSStringFromClass([self class]), self];
194 [desc appendFormat:@" child=%@[0x%08X] childName=%@",
195 NSStringFromClass([self->child class]),
196 self->child, [self->child name]];
199 [desc appendString:@">"];
203 @end /* WOComponentReference */