]> err.no Git - sope/blob - sope-appserver/NGObjWeb/WOChildComponentReference.m
Add libxml2-dev to libsope-xml4.7-dev deps
[sope] / sope-appserver / NGObjWeb / WOChildComponentReference.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 "WOChildComponentReference.h"
23 #include "WOComponent+private.h"
24 #include "WOContext+private.h"
25 #include <NGObjWeb/WOSession.h>
26 #include <NGObjWeb/WOResponse.h>
27 #include "common.h"
28
29 @interface WOContext(ComponentStackCount)
30 - (unsigned)componentStackCount;
31 @end
32
33 @implementation WOChildComponentReference
34
35 static int profileComponents = -1;
36 static Class NSDateClass = Nil;
37
38 + (int)version {
39   return 2;
40 }
41 + (void)initialize {
42   NSAssert2([super version] == 2,
43             @"invalid superclass (%@) version %i !",
44             NSStringFromClass([self superclass]), [super version]);
45
46   if (profileComponents == -1) {
47     profileComponents = [[[NSUserDefaults standardUserDefaults]
48                                           objectForKey:@"WOProfileComponents"]
49                                           boolValue] ? 1 : 0;
50   }
51   if (NSDateClass == Nil)
52     NSDateClass = [NSDate class];
53 }
54
55 - (id)initWithName:(NSString *)_name
56   associations:(NSDictionary *)_associations
57   template:(WOElement *)_template
58 {
59   self = [super initWithName:_name associations:nil template:_template];
60   if (self) {
61     self->childName = [_name copyWithZone:[self zone]];
62     self->template  = RETAIN(_template);
63   }
64   return self;
65 }
66
67 #if !LIB_FOUNDATION_BOEHM_GC
68 - (void)dealloc {
69   RELEASE(self->childName);
70   RELEASE(self->template);
71   [super dealloc];
72 }
73 #endif
74
75 /* accessors */
76
77 - (NSString *)childName {
78   return self->childName;
79 }
80
81 - (id)template {
82   return self->template;
83 }
84
85 /* responder */
86
87 - (void)takeValuesFromRequest:(WORequest *)_request
88   inContext:(WOContext *)_ctx
89 {
90   WOComponent *parent, *child;
91   NSTimeInterval st = 0.0;
92   
93   if ((parent = [_ctx component]) == nil) {
94     [self warnWithFormat:
95             @"%s: did not find parent component of child %@",
96             __PRETTY_FUNCTION__, self->childName];
97     return;
98   }
99   if ((child = [parent childComponentWithName:self->childName]) == nil) {
100     [self warnWithFormat:
101             @"did not find child component %@ of parent %@",
102             self->childName, [parent name]];
103     return;
104   }
105   
106   if (profileComponents)
107     st = [[NSDateClass date] timeIntervalSince1970];
108
109   WOContext_enterComponent(_ctx, child, self->template);
110   [child takeValuesFromRequest:_request inContext:_ctx];
111   WOContext_leaveComponent(_ctx, child);
112   
113   if (profileComponents) {
114     NSTimeInterval diff;
115     int i;
116     diff = [[NSDateClass date] timeIntervalSince1970] - st;
117     for (i = [_ctx componentStackCount]; i >= 0; i--)
118       printf("  ");
119     printf("[%s %s]: %0.3fs\n",
120            [[child name] cString], sel_get_name(_cmd), diff);
121   }
122 }
123
124 - (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
125   WOComponent    *parent, *child;
126   id             result = nil;
127   NSTimeInterval st = 0.0;
128
129   if ((parent = [_ctx component]) == nil) {
130     [[_ctx session]
131            warnWithFormat:@"did not find parent component of child %@",
132              self->childName];
133     return nil;
134   }
135   if ((child = [parent childComponentWithName:self->childName]) == nil) {
136     [[_ctx session]
137            warnWithFormat:
138              @"did not find child component %@ of parent %@",
139              self->childName, [parent name]];
140     return nil;
141   }
142   
143   if (profileComponents)
144     st = [[NSDateClass date] timeIntervalSince1970];
145   
146   WOContext_enterComponent(_ctx, child, self->template);
147   result = [child invokeActionForRequest:_request inContext:_ctx];
148   WOContext_leaveComponent(_ctx, child);
149
150   if (profileComponents) {
151     NSTimeInterval diff;
152     int i;
153     diff = [[NSDateClass date] timeIntervalSince1970] - st;
154     for (i = [_ctx componentStackCount]; i >= 0; i--)
155       printf("  ");
156     printf("[%s %s]: %0.3fs\n",
157            [[child name] cString], sel_get_name(_cmd), diff);
158   }
159
160   return result;
161 }
162
163 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
164   WOComponent *parent, *child;
165   NSTimeInterval st = 0.0;
166   
167   if ((parent = [_ctx component]) == nil) {
168     [self warnWithFormat:
169             @"%s: did not find parent component of child %@",
170             __PRETTY_FUNCTION__, self->childName];
171     return;
172   }
173   if ((child = [parent childComponentWithName:self->childName]) == nil) {
174     [self warnWithFormat:
175             @"did not find child component %@ of parent %@",
176             self->childName, [parent name]];
177     if ([_ctx isRenderingDisabled]) return;
178     [_response appendContentString:@"<pre>[missing component: "];
179     [_response appendContentHTMLString:self->childName];
180     [_response appendContentString:@"]</pre>"];
181     return;
182   }
183   
184   if (profileComponents)
185     st = [[NSDateClass date] timeIntervalSince1970];
186   
187   WOContext_enterComponent(_ctx, child, self->template);
188   [child appendToResponse:_response inContext:_ctx];
189   WOContext_leaveComponent(_ctx, child);
190   
191   if (profileComponents) {
192     NSTimeInterval diff;
193     int i;
194     diff = [[NSDateClass date] timeIntervalSince1970] - st;
195     for (i = [_ctx componentStackCount]; i >= 0; i--)
196       printf("  ");
197     printf("[%s %s]: %0.3fs\n",
198            [[child name] cString], sel_get_name(_cmd), diff);
199   }
200 }
201
202 @end /* WOChildComponentReference */