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 request] isFromClientComponent])
190 sComponent = [_ctx component];
192 if ([self _appendHrefToResponse:_response inContext:_ctx]) {
193 queryString = [self queryStringForQueryDictionary:
194 [self->queryDictionary valueInComponent:sComponent]
195 andQueryParameters:self->queryParameters
199 if (self->fragmentIdentifier != nil) {
200 [_response appendContentCharacter:'#'];
201 WOResponse_AddString(_response,
202 [self->fragmentIdentifier stringValueInComponent:sComponent]);
204 if (queryString != nil) {
205 [_response appendContentCharacter:'?'];
206 WOResponse_AddString(_response, queryString);
210 [self->template appendToResponse:_response inContext:_ctx];
215 - (NSString *)associationDescription {
216 NSMutableString *str = [NSMutableString stringWithCapacity:256];
218 if (self->fragmentIdentifier)
219 [str appendFormat:@" fragment=%@", self->fragmentIdentifier];
224 @end /* WOActionURL */
226 @implementation _WOActionActionURL
228 - (id)initWithName:(NSString *)_name
229 associations:(NSDictionary *)_config
230 template:(WOElement *)_t
232 if ((self = [super _initWithName:_name associations:_config template:_t])) {
233 self->action = OWGetProperty(_config, @"action");
235 if (self->action == nil) {
236 NSLog(@"missing action association for WOActionURL ..");
242 if ([_config objectForKey:@"pageName"] ||
243 [_config objectForKey:@"href"] ||
244 [_config objectForKey:@"directActionName"] ||
245 [_config objectForKey:@"actionClass"]) {
246 NSLog(@"WARNING: inconsistent association settings in WOActionURL !"
247 @" (assign only one of pageName, href, "
248 @"directActionName or action)");
256 [self->action release];
260 /* dynamic invocation */
262 - (id)invokeActionForRequest:(WORequest *)_request
263 inContext:(WOContext *)_ctx
266 return [self executeAction:self->action inContext:_ctx];
269 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
270 inContext:(WOContext *)_ctx
272 WOResponse_AddString(_response, [_ctx componentActionURL]);
278 - (NSString *)associationDescription {
279 NSMutableString *str = [NSMutableString stringWithCapacity:256];
281 [str appendFormat:@" action=%@", self->action];
282 [str appendString:[super associationDescription]];
286 @end /* _WOActionActionURL */
288 @implementation _WOPageActionURL
290 - (id)initWithName:(NSString *)_name
291 associations:(NSDictionary *)_config
292 template:(WOElement *)_t
294 if ((self = [super _initWithName:_name associations:_config template:_t])) {
295 self->pageName = OWGetProperty(_config, @"pageName");
297 if (self->pageName == nil) {
298 NSLog(@"missing pageName association for WOActionURL ..");
304 if ([_config objectForKey:@"action"] ||
305 [_config objectForKey:@"href"] ||
306 [_config objectForKey:@"directActionName"] ||
307 [_config objectForKey:@"actionClass"]) {
308 NSLog(@"WARNING: inconsistent association settings in WOActionURL !"
309 @" (assign only one of pageName, href, "
310 @"directActionName or action)");
318 [self->pageName release];
324 - (id)invokeActionForRequest:(WORequest *)_request
325 inContext:(WOContext *)_ctx
327 WOComponent *page = nil;
328 NSString *name = nil;
330 name = [self->pageName stringValueInComponent:[_ctx component]];
331 page = [[_ctx application] pageWithName:name inContext:_ctx];
334 [[_ctx session] logWithFormat:
335 @"%@[0x%p]: did not find page with name %@ !",
336 NSStringFromClass([self class]), self, name];
341 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
342 inContext:(WOContext *)_ctx
344 WOResponse_AddString(_response, [_ctx componentActionURL]);
350 - (NSString *)associationDescription {
351 NSMutableString *str = [NSMutableString stringWithCapacity:256];
353 [str appendFormat:@" pageName=%@", self->pageName];
354 [str appendString:[super associationDescription]];
358 @end /* _WOPageActionURL */
360 @implementation _WODirectActionActionURL
362 - (id)initWithName:(NSString *)_name
363 associations:(NSDictionary *)_config
364 template:(WOElement *)_t
366 if ((self = [super _initWithName:_name associations:_config template:_t])) {
367 WOAssociation *sidInUrlAssoc;
369 sidInUrlAssoc = OWGetProperty(_config, @"?wosid");
370 self->actionClass = OWGetProperty(_config, @"actionClass");
371 self->directActionName = OWGetProperty(_config, @"directActionName");
373 self->sidInUrl = (sidInUrlAssoc)
374 ? [sidInUrlAssoc boolValueInComponent:nil]
378 if ([_config objectForKey:@"action"] ||
379 [_config objectForKey:@"href"] ||
380 [_config objectForKey:@"pageName"]) {
381 NSLog(@"WARNING: inconsistent association settings in WOActionURL !"
382 @" (assign only one of pageName, href, "
383 @"directActionName or action)");
391 [self->actionClass release];
392 [self->directActionName release];
398 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
399 inContext:(WOContext *)_ctx
401 WOComponent *sComponent;
404 NSMutableDictionary *qd;
407 sComponent = [_ctx component];
408 daClass = [self->actionClass stringValueInComponent:sComponent];
409 daName = [self->directActionName stringValueInComponent:sComponent];
413 if (![daClass isEqualToString:@"DirectAction"])
414 daName = [NSString stringWithFormat:@"%@/%@", daClass, daName];
420 qd = [NSMutableDictionary dictionaryWithCapacity:16];
422 /* add query dictionary */
424 if (self->queryDictionary) {
425 if ((tmp = [self->queryDictionary valueInComponent:sComponent]))
426 [qd addEntriesFromDictionary:tmp];
429 /* add ?style parameters */
431 if (self->queryParameters) {
435 keys = [self->queryParameters keyEnumerator];
436 while ((key = [keys nextObject]) != nil) {
439 assoc = [self->queryParameters objectForKey:key];
440 value = [assoc stringValueInComponent:sComponent];
442 [qd setObject:(value != nil ? value : (id)@"") forKey:key];
448 if (self->sidInUrl) {
449 if ([_ctx hasSession]) {
450 WOSession *sn = [_ctx session];
452 [qd setObject:[sn sessionID] forKey:WORequestValueSessionID];
454 if (![sn isDistributionEnabled]) {
455 [qd setObject:[[WOApplication application] number]
456 forKey:WORequestValueInstance];
461 WOResponse_AddString(_response,
462 [_ctx directActionURLForActionNamed:daName
463 queryDictionary:qd]);
469 - (NSString *)associationDescription {
470 NSMutableString *str = [NSMutableString stringWithCapacity:256];
472 if (self->actionClass != nil)
473 [str appendFormat:@" actionClass=%@", self->actionClass];
474 if (self->directActionName != nil)
475 [str appendFormat:@" directAction=%@", self->directActionName];
477 [str appendString:[super associationDescription]];
481 @end /* _WODirectActionActionURL */