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 "WOHTMLDynamicElement.h"
23 #include "WOElement+private.h"
24 #include <NGObjWeb/WOApplication.h>
25 #include <NGObjWeb/WOResourceManager.h>
29 WOActionURL associations:
31 pageName | action | (directActionName & actionClass)
36 @interface WOActionURL : WOHTMLDynamicElement
38 // WODynamicElement: extraAttributes
39 // WODynamicElement: otherTagString
41 WOAssociation *fragmentIdentifier;
45 WOAssociation *queryDictionary;
46 NSDictionary *queryParameters; /* associations beginning with ? */
49 @end /* WOActionURL */
51 @interface _WOActionActionURL : WOActionURL
53 WOAssociation *action;
57 @interface _WOPageActionURL : WOActionURL
59 WOAssociation *pageName;
63 @interface _WODirectActionActionURL : WOActionURL
65 WOAssociation *actionClass;
66 WOAssociation *directActionName;
67 BOOL sidInUrl; /* include session-id in wa URL ? */
71 @interface WOActionURL(PrivateMethods)
73 - (id)_initWithName:(NSString *)_name
74 associations:(NSDictionary *)_config
75 template:(WOElement *)_t;
77 - (NSString *)associationDescription;
81 @implementation WOActionURL
87 NSAssert2([super version] == 2,
88 @"invalid superclass (%@) version %i !",
89 NSStringFromClass([self superclass]), [super version]);
92 - (id)initWithName:(NSString *)_name
93 associations:(NSDictionary *)_config
94 template:(WOElement *)_t
96 Class linkClass = Nil;
98 if ([_config objectForKey:@"action"])
99 linkClass = [_WOActionActionURL class];
100 else if ([_config objectForKey:@"pageName"])
101 linkClass = [_WOPageActionURL class];
103 linkClass = [_WODirectActionActionURL class];
107 [[linkClass alloc] initWithName:_name associations:_config template:_t];
110 - (id)_initWithName:(NSString *)_name
111 associations:(NSDictionary *)_config
112 template:(WOElement *)_t
114 if ((self = [super initWithName:_name associations:_config template:_t])) {
115 self->fragmentIdentifier = OWGetProperty(_config, @"fragmentIdentifier");
116 self->queryDictionary = OWGetProperty(_config, @"queryDictionary");
117 self->queryParameters = OWExtractQueryParameters(_config);
118 self->template = [_t retain];
119 self->containsForm = self->queryParameters ? YES : NO;
125 [self->template release];
126 [self->queryDictionary release];
127 [self->queryParameters release];
128 [self->fragmentIdentifier release];
135 return self->template;
138 /* handling requests */
140 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
141 /* links can take form values !!!! (for query-parameters) */
143 if (self->queryParameters) {
144 /* apply values to ?style parameters */
145 WOComponent *sComponent = [_ctx component];
149 keys = [self->queryParameters keyEnumerator];
150 while ((key = [keys nextObject])) {
153 assoc = [self->queryParameters objectForKey:key];
154 value = [_req formValueForKey:key];
156 [assoc setValue:value inComponent:sComponent];
160 [self->template takeValuesFromRequest:_req inContext:_ctx];
163 - (id)invokeActionForRequest:(WORequest *)_request
164 inContext:(WOContext *)_ctx
166 [[_ctx session] logWithFormat:@"%@[0x%p]: no action/page set !",
167 NSStringFromClass([self class]), self];
171 - (BOOL)_appendHrefToResponse:(WOResponse *)_r inContext:(WOContext *)_ctx {
172 #if APPLE_FOUNDATION_LIBRARY || NeXT_Foundation_LIBRARY || \
173 COCOA_Foundation_LIBRARY
174 NSLog(@"subclass responsibility ...");
176 [self subclassResponsibility:_cmd];
181 /* generate response */
183 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
184 WOComponent *sComponent;
185 NSString *queryString = nil;
187 if ([_ctx isRenderingDisabled] || [[_ctx request] isFromClientComponent]) {
188 [self->template appendToResponse:_response inContext:_ctx];
192 sComponent = [_ctx component];
194 if ([self _appendHrefToResponse:_response inContext:_ctx]) {
195 queryString = [self queryStringForQueryDictionary:
196 [self->queryDictionary valueInComponent:sComponent]
197 andQueryParameters:self->queryParameters
201 if (self->fragmentIdentifier != nil) {
202 [_response appendContentCharacter:'#'];
203 WOResponse_AddString(_response,
204 [self->fragmentIdentifier stringValueInComponent:sComponent]);
206 if (queryString != nil) {
207 [_response appendContentCharacter:'?'];
208 WOResponse_AddString(_response, queryString);
212 [self->template appendToResponse:_response inContext:_ctx];
217 - (NSString *)associationDescription {
218 NSMutableString *str = [NSMutableString stringWithCapacity:256];
220 if (self->fragmentIdentifier)
221 [str appendFormat:@" fragment=%@", self->fragmentIdentifier];
226 @end /* WOActionURL */
228 @implementation _WOActionActionURL
230 - (id)initWithName:(NSString *)_name
231 associations:(NSDictionary *)_config
232 template:(WOElement *)_t
234 if ((self = [super _initWithName:_name associations:_config template:_t])) {
235 self->action = OWGetProperty(_config, @"action");
237 if (self->action == nil) {
238 NSLog(@"missing action association for WOActionURL ..");
244 if ([_config objectForKey:@"pageName"] ||
245 [_config objectForKey:@"href"] ||
246 [_config objectForKey:@"directActionName"] ||
247 [_config objectForKey:@"actionClass"]) {
248 NSLog(@"WARNING: inconsistent association settings in WOActionURL !"
249 @" (assign only one of pageName, href, "
250 @"directActionName or action)");
258 [self->action release];
262 /* dynamic invocation */
264 - (id)invokeActionForRequest:(WORequest *)_request
265 inContext:(WOContext *)_ctx
268 return [self executeAction:self->action inContext:_ctx];
271 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
272 inContext:(WOContext *)_ctx
274 WOResponse_AddString(_response, [_ctx componentActionURL]);
280 - (NSString *)associationDescription {
281 NSMutableString *str = [NSMutableString stringWithCapacity:256];
283 [str appendFormat:@" action=%@", self->action];
284 [str appendString:[super associationDescription]];
288 @end /* _WOActionActionURL */
290 @implementation _WOPageActionURL
292 - (id)initWithName:(NSString *)_name
293 associations:(NSDictionary *)_config
294 template:(WOElement *)_t
296 if ((self = [super _initWithName:_name associations:_config template:_t])) {
297 self->pageName = OWGetProperty(_config, @"pageName");
299 if (self->pageName == nil) {
300 NSLog(@"missing pageName association for WOActionURL ..");
306 if ([_config objectForKey:@"action"] ||
307 [_config objectForKey:@"href"] ||
308 [_config objectForKey:@"directActionName"] ||
309 [_config objectForKey:@"actionClass"]) {
310 NSLog(@"WARNING: inconsistent association settings in WOActionURL !"
311 @" (assign only one of pageName, href, "
312 @"directActionName or action)");
320 [self->pageName release];
326 - (id)invokeActionForRequest:(WORequest *)_request
327 inContext:(WOContext *)_ctx
329 WOComponent *page = nil;
330 NSString *name = nil;
332 name = [self->pageName stringValueInComponent:[_ctx component]];
333 page = [[_ctx application] pageWithName:name inContext:_ctx];
336 [[_ctx session] logWithFormat:
337 @"%@[0x%p]: did not find page with name %@ !",
338 NSStringFromClass([self class]), self, name];
343 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
344 inContext:(WOContext *)_ctx
346 WOResponse_AddString(_response, [_ctx componentActionURL]);
352 - (NSString *)associationDescription {
353 NSMutableString *str = [NSMutableString stringWithCapacity:256];
355 [str appendFormat:@" pageName=%@", self->pageName];
356 [str appendString:[super associationDescription]];
360 @end /* _WOPageActionURL */
362 @implementation _WODirectActionActionURL
364 - (id)initWithName:(NSString *)_name
365 associations:(NSDictionary *)_config
366 template:(WOElement *)_t
368 if ((self = [super _initWithName:_name associations:_config template:_t])) {
369 WOAssociation *sidInUrlAssoc;
371 sidInUrlAssoc = OWGetProperty(_config, @"?wosid");
372 self->actionClass = OWGetProperty(_config, @"actionClass");
373 self->directActionName = OWGetProperty(_config, @"directActionName");
375 self->sidInUrl = (sidInUrlAssoc)
376 ? [sidInUrlAssoc boolValueInComponent:nil]
380 if ([_config objectForKey:@"action"] ||
381 [_config objectForKey:@"href"] ||
382 [_config objectForKey:@"pageName"]) {
383 NSLog(@"WARNING: inconsistent association settings in WOActionURL !"
384 @" (assign only one of pageName, href, "
385 @"directActionName or action)");
393 [self->actionClass release];
394 [self->directActionName release];
400 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
401 inContext:(WOContext *)_ctx
403 WOComponent *sComponent;
406 NSMutableDictionary *qd;
409 sComponent = [_ctx component];
410 daClass = [self->actionClass stringValueInComponent:sComponent];
411 daName = [self->directActionName stringValueInComponent:sComponent];
415 if (![daClass isEqualToString:@"DirectAction"])
416 daName = [NSString stringWithFormat:@"%@/%@", daClass, daName];
422 qd = [NSMutableDictionary dictionaryWithCapacity:16];
424 /* add query dictionary */
426 if (self->queryDictionary) {
427 if ((tmp = [self->queryDictionary valueInComponent:sComponent]))
428 [qd addEntriesFromDictionary:tmp];
431 /* add ?style parameters */
433 if (self->queryParameters) {
437 keys = [self->queryParameters keyEnumerator];
438 while ((key = [keys nextObject]) != nil) {
441 assoc = [self->queryParameters objectForKey:key];
442 value = [assoc stringValueInComponent:sComponent];
444 [qd setObject:(value != nil ? value : (id)@"") forKey:key];
450 if (self->sidInUrl) {
451 if ([_ctx hasSession]) {
452 WOSession *sn = [_ctx session];
454 [qd setObject:[sn sessionID] forKey:WORequestValueSessionID];
456 if (![sn isDistributionEnabled]) {
457 [qd setObject:[[WOApplication application] number]
458 forKey:WORequestValueInstance];
463 WOResponse_AddString(_response,
464 [_ctx directActionURLForActionNamed:daName
465 queryDictionary:qd]);
471 - (NSString *)associationDescription {
472 NSMutableString *str = [NSMutableString stringWithCapacity:256];
474 if (self->actionClass != nil)
475 [str appendFormat:@" actionClass=%@", self->actionClass];
476 if (self->directActionName != nil)
477 [str appendFormat:@" directAction=%@", self->directActionName];
479 [str appendString:[super associationDescription]];
483 @end /* _WODirectActionActionURL */