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 "WOHyperlink.h"
26 [WOHTMLDynamicElement]
32 _WODirectActionHyperlink
35 @interface _WOComplexHyperlink : WOHyperlink
37 /* superclass of most hyperlink classes */
39 WOAssociation *fragmentIdentifier;
40 WOAssociation *string;
41 WOAssociation *target;
42 WOAssociation *disabled;
46 WOAssociation *queryDictionary;
47 NSDictionary *queryParameters; /* associations beginning with ? */
49 /* non WO, image stuff */
50 WOAssociation *filename; /* path relative to WebServerResources */
51 WOAssociation *framework;
52 WOAssociation *src; /* absolute URL */
53 WOAssociation *disabledFilename; /* icon for 'disabled' state */
56 - (NSString *)associationDescription;
60 @interface _WOHrefHyperlink : _WOComplexHyperlink
66 @interface _WOActionHyperlink : _WOComplexHyperlink
68 WOAssociation *action;
72 @interface _WOPageHyperlink : _WOComplexHyperlink
74 WOAssociation *pageName;
78 @interface _WODirectActionHyperlink : _WOComplexHyperlink
80 WOAssociation *actionClass;
81 WOAssociation *directActionName;
82 BOOL sidInUrl; /* include session-id in wa URL ? */
86 #include "WOElement+private.h"
87 #include "WOHyperlinkInfo.h"
88 #include "WOCompoundElement.h"
89 #include <NGObjWeb/WOApplication.h>
90 #include <NGObjWeb/WOResourceManager.h>
91 #include <NGExtensions/NSString+Ext.h>
94 static Class NSURLClass = Nil;
96 @implementation _WOComplexHyperlink
99 return [super version] /* v4 */;
102 NSAssert2([super version] == 4,
103 @"invalid superclass (%@) version %i !",
104 NSStringFromClass([self superclass]), [super version]);
105 if (NSURLClass == Nil)
106 NSURLClass = [NSURL class];
109 - (id)initWithName:(NSString *)_name
110 hyperlinkInfo:(WOHyperlinkInfo *)_info
111 template:(WOElement *)_t
113 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
114 self->template = [_t retain];
116 self->fragmentIdentifier = _info->fragmentIdentifier;
117 self->string = _info->string;
118 self->target = _info->target;
119 self->disabled = _info->disabled;
120 self->queryDictionary = _info->queryDictionary;
121 self->queryParameters = _info->queryParameters;
124 self->filename = _info->filename;
125 self->framework = _info->framework;
126 self->src = _info->src;
127 self->disabledFilename = _info->disabledFilename;
129 self->containsForm = self->queryParameters ? YES : NO;
135 [self->template release];
136 [self->queryDictionary release];
137 [self->queryParameters release];
138 [self->disabledFilename release];
139 [self->filename release];
140 [self->framework release];
142 [self->fragmentIdentifier release];
143 [self->string release];
144 [self->target release];
145 [self->disabled release];
152 return self->template;
155 /* handle requests */
157 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
158 /* links can take form values !!!! (for query-parameters) */
160 if (self->queryParameters != nil) {
161 /* apply values to ?style parameters */
162 WOComponent *sComponent = [_ctx component];
166 keys = [self->queryParameters keyEnumerator];
167 while ((key = [keys nextObject]) != nil) {
170 assoc = [self->queryParameters objectForKey:key];
172 if ([assoc isValueSettable]) {
173 value = [_rq formValueForKey:key];
174 [assoc setValue:value inComponent:sComponent];
179 [self->template takeValuesFromRequest:_rq inContext:_ctx];
182 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
183 if (self->disabled != nil) {
184 if ([self->disabled boolValueInComponent:[_ctx component]])
188 if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
189 /* link is not the active element */
190 return [self->template invokeActionForRequest:_rq inContext:_ctx];
193 [[_ctx session] logWithFormat:@"%@[0x%08X]: no action/page set !",
194 NSStringFromClass([self class]), self];
198 - (BOOL)_appendHrefToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
199 [self subclassResponsibility:_cmd];
203 - (void)_addImageToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
204 WOComponent *sComponent = [_ctx component];
209 uUri = [[self->src valueInContext:_ctx] stringValue];
211 if ([self->disabled boolValueInComponent:sComponent]) {
212 uFi = [self->disabledFilename stringValueInComponent:sComponent];
214 uFi = [self->filename stringValueInComponent:sComponent];
217 uFi = [self->filename stringValueInComponent:sComponent];
219 if (!((uFi != nil) || (uUri != nil)))
222 languages = [_ctx resourceLookupLanguages];
224 WOResponse_AddCString(_resp, "<img src=\"");
227 WOResourceManager *rm;
229 if ((rm = [[_ctx component] resourceManager]) == nil)
230 rm = [[_ctx application] resourceManager];
232 uFi = [rm urlForResourceNamed:uFi
234 [self->framework stringValueInComponent:sComponent]
236 request:[_ctx request]];
238 NSLog(@"%@: did not find resource %@", sComponent,
239 [self->filename stringValueInComponent:sComponent]);
242 [_resp appendContentHTMLAttributeValue:uFi];
245 [_resp appendContentHTMLAttributeValue:uUri];
247 WOResponse_AddChar(_resp, '"');
249 [self appendExtraAttributesToResponse:_resp inContext:_ctx];
251 WOResponse_AddEmptyCloseParens(_resp, _ctx);
254 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
255 WOComponent *sComponent = [_ctx component];
259 if ([[_ctx request] isFromClientComponent])
262 content = [self->string valueInContext:_ctx];
263 doNotDisplay = [self->disabled boolValueInComponent:sComponent];
266 NSString *targetView;
267 NSString *queryString = nil;
269 targetView = [self->target stringValueInComponent:sComponent];
271 WOResponse_AddCString(_response, "<a href=\"");
273 if ([self _appendHrefToResponse:_response inContext:_ctx]) {
274 queryString = [self queryStringForQueryDictionary:
275 [self->queryDictionary valueInComponent:sComponent]
276 andQueryParameters:self->queryParameters
280 if (self->fragmentIdentifier) {
281 [_response appendContentCharacter:'#'];
282 WOResponse_AddString(_response,
283 [self->fragmentIdentifier stringValueInComponent:sComponent]);
286 [_response appendContentCharacter:'?'];
287 WOResponse_AddString(_response, queryString);
289 [_response appendContentCharacter:'"'];
292 WOResponse_AddCString(_response, " target=\"");
293 WOResponse_AddString(_response, targetView);
294 [_response appendContentCharacter:'"'];
297 [self appendExtraAttributesToResponse:_response inContext:_ctx];
299 if (self->otherTagString) {
300 WOResponse_AddChar(_response, ' ');
301 WOResponse_AddString(_response,
302 [self->otherTagString stringValueInComponent:
305 [_response appendContentCharacter:'>'];
309 [self->template appendToResponse:_response inContext:_ctx];
310 if (content) [_response appendContentHTMLString:content];
313 if ((self->src != nil) || (self->filename != nil))
314 [self _addImageToResponse:_response inContext:_ctx];
318 WOResponse_AddCString(_response, "</a>");
324 - (NSString *)associationDescription {
325 NSMutableString *str = [NSMutableString stringWithCapacity:256];
327 if (self->fragmentIdentifier)
328 [str appendFormat:@" fragment=%@", self->fragmentIdentifier];
329 if (self->string) [str appendFormat:@" string=%@", self->string];
330 if (self->target) [str appendFormat:@" target=%@", self->target];
331 if (self->disabled) [str appendFormat:@" disabled=%@", self->disabled];
334 if (self->filename) [str appendFormat:@" filename=%@", self->filename];
335 if (self->framework) [str appendFormat:@" framework=%@", self->framework];
336 if (self->src) [str appendFormat:@" src=%@", self->src];
341 @end /* _WOComplexHyperlink */
344 @implementation _WOHrefHyperlink
346 static BOOL debugStaticLinks = NO;
349 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
351 debugStaticLinks = [ud boolForKey:@"WODebugStaticLinkProcessing"];
354 - (id)initWithName:(NSString *)_name
355 hyperlinkInfo:(WOHyperlinkInfo *)_info
356 template:(WOElement *)_t
358 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
359 self->href = _info->href;
365 [self->href release];
371 - (BOOL)shouldRewriteURLString:(NSString *)_s inContext:(WOContext *)_ctx {
372 // TODO: we need a binding to disable rewriting!
373 if ([_s hasPrefix:@"mailto:"])
375 if ([_s hasPrefix:@"javascript:"])
380 - (BOOL)_appendHrefToResponse:(WOResponse *)_r inContext:(WOContext *)_ctx {
385 base = [_ctx baseURL];
386 hrefValue = [self->href valueInContext:_ctx];
389 if (hrefValue == nil)
392 if ([hrefValue isKindOfClass:NSURLClass]) {
393 s = [hrefValue stringValueRelativeToURL:base];
396 s = [hrefValue stringValue];
398 if ([self shouldRewriteURLString:s inContext:_ctx]) {
399 if ([s isAbsoluteURL]) {
400 // TODO: why are we doing this? we could just pass through the string?
401 // => probably to generate relative links
402 url = [NSURLClass URLWithString:s];
404 else if (base != nil) {
405 /* avoid creating a new URL for ".", just return the base */
406 url = [s isEqualToString:@"."]
408 : [NSURLClass URLWithString:s relativeToURL:base];
411 [self logWithFormat:@"WARNING: missing base URL in context ..."];
412 WOResponse_AddString(_r, s);
418 @"couldn't construct URL from 'href' string '%@' (base=%@)",
423 s = [url stringValueRelativeToURL:base];
429 if (debugStaticLinks) {
430 [self logWithFormat:@"static links based on 'href': '%@'", hrefValue];
431 [self logWithFormat:@" base %@", base];
432 [self logWithFormat:@" base-abs %@", [base absoluteString]];
433 [self logWithFormat:@" url %@", url];
434 [self logWithFormat:@" url-abs %@", [url absoluteString]];
435 [self logWithFormat:@" string %@", s];
437 WOResponse_AddString(_r, s);
443 - (NSString *)associationDescription {
444 NSMutableString *str = [NSMutableString stringWithCapacity:256];
446 [str appendFormat:@" href=%@", self->href];
447 [str appendString:[super associationDescription]];
452 @end /* _WOHrefHyperlink */
455 @implementation _WOActionHyperlink
457 - (id)initWithName:(NSString *)_name
458 hyperlinkInfo:(WOHyperlinkInfo *)_info
459 template:(WOElement *)_t
461 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
462 self->action = _info->action;
468 [self->action release];
472 /* dynamic invocation */
474 - (id)invokeActionForRequest:(WORequest *)_rq
475 inContext:(WOContext *)_ctx
477 if (self->disabled) {
478 if ([self->disabled boolValueInComponent:[_ctx component]])
482 if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
483 /* link is not the active element */
484 return [self->template invokeActionForRequest:_rq inContext:_ctx];
487 return [self executeAction:self->action inContext:_ctx];
490 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
491 inContext:(WOContext *)_ctx
493 WOResponse_AddString(_response, [_ctx componentActionURL]);
499 - (NSString *)associationDescription {
500 NSMutableString *str = [NSMutableString stringWithCapacity:256];
502 [str appendFormat:@" action=%@", self->action];
503 [str appendString:[super associationDescription]];
507 @end /* _WOActionHyperlink */
510 @implementation _WOPageHyperlink
512 - (id)initWithName:(NSString *)_name
513 hyperlinkInfo:(WOHyperlinkInfo *)_info
514 template:(WOElement *)_t
516 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
517 self->pageName = _info->pageName;
523 [self->pageName release];
529 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
533 if (self->disabled) {
534 if ([self->disabled boolValueInComponent:[_ctx component]])
538 if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
539 /* link is not the active element */
540 return [self->template invokeActionForRequest:_rq inContext:_ctx];
542 /* link is the active element */
544 name = [self->pageName stringValueInComponent:[_ctx component]];
545 page = [[_ctx application] pageWithName:name inContext:_ctx];
548 [[_ctx session] logWithFormat:
549 @"%@[0x%08X]: did not find page with name %@ !",
550 NSStringFromClass([self class]), self, name];
555 /* generate response */
557 - (BOOL)_appendHrefToResponse:(WOResponse *)_r inContext:(WOContext *)_ctx {
560 87% -componentActionURL
561 13% NSString dataUsingEncoding(appendContentString!)
562 TODO(prof): use addcstring
564 WOResponse_AddString(_r, [_ctx componentActionURL]);
570 - (NSString *)associationDescription {
571 NSMutableString *str = [NSMutableString stringWithCapacity:256];
573 [str appendFormat:@" pageName=%@", self->pageName];
574 [str appendString:[super associationDescription]];
578 @end /* _WOPageHyperlink */
581 @implementation _WODirectActionHyperlink
583 - (id)initWithName:(NSString *)_name
584 hyperlinkInfo:(WOHyperlinkInfo *)_info
585 template:(WOElement *)_t
587 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
588 self->actionClass = _info->actionClass;
589 self->directActionName = _info->directActionName;
590 self->sidInUrl = _info->sidInUrl;
592 self->containsForm = NO; /* direct actions are never form stuff ... */
598 [self->actionClass release];
599 [self->directActionName release];
603 /* handle requests */
605 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
606 /* DA links can *never* take form values !!!! */
607 [self->template takeValuesFromRequest:_req inContext:_ctx];
610 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
611 /* DA links can *never* invoke an action !!!! */
612 return [self->template invokeActionForRequest:_rq inContext:_ctx];
615 /* generate response */
617 - (BOOL)_appendHrefToResponse:(WOResponse *)_r
618 inContext:(WOContext *)_ctx
620 WOComponent *sComponent;
623 NSMutableDictionary *qd;
626 sComponent = [_ctx component];
627 daClass = [self->actionClass stringValueInComponent:sComponent];
628 daName = [self->directActionName stringValueInComponent:sComponent];
632 if (![daClass isEqualToString:@"DirectAction"])
633 daName = [NSString stringWithFormat:@"%@/%@", daClass, daName];
639 qd = [NSMutableDictionary dictionaryWithCapacity:16];
641 /* add query dictionary */
643 if (self->queryDictionary) {
644 if ((tmp = [self->queryDictionary valueInComponent:sComponent]))
645 [qd addEntriesFromDictionary:tmp];
648 /* add ?style parameters */
650 if (self->queryParameters) {
654 keys = [self->queryParameters keyEnumerator];
655 while ((key = [keys nextObject])) {
658 assoc = [self->queryParameters objectForKey:key];
659 value = [assoc stringValueInComponent:sComponent];
661 [qd setObject:(value ? value : @"") forKey:key];
667 if (self->sidInUrl) {
668 if ([_ctx hasSession]) {
672 [qd setObject:[sn sessionID] forKey:WORequestValueSessionID];
674 if (![sn isDistributionEnabled]) {
675 [qd setObject:[[WOApplication application] number]
676 forKey:WORequestValueInstance];
681 WOResponse_AddString(_r,
682 [_ctx directActionURLForActionNamed:daName
683 queryDictionary:qd]);
689 - (NSString *)associationDescription {
690 NSMutableString *str = [NSMutableString stringWithCapacity:256];
692 if (self->actionClass)
693 [str appendFormat:@" actionClass=%@", self->actionClass];
694 if (self->directActionName)
695 [str appendFormat:@" directAction=%@", self->directActionName];
697 [str appendString:[super associationDescription]];
701 @end /* _WODirectActionHyperlink */