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];
151 - (WOElement *)template {
152 return self->template;
155 // ******************** responder ********************
157 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
158 /* links can take form values !!!! (for query-parameters) */
160 if (self->queryParameters) {
161 /* apply values to ?style parameters */
162 WOComponent *sComponent = [_ctx component];
166 keys = [self->queryParameters keyEnumerator];
167 while ((key = [keys nextObject])) {
170 assoc = [self->queryParameters objectForKey:key];
172 if ([assoc isValueSettable]) {
173 value = [_req formValueForKey:key];
174 [assoc setValue:value inComponent:sComponent];
179 [self->template takeValuesFromRequest:_req inContext:_ctx];
182 - (id)invokeActionForRequest:(WORequest *)_request
183 inContext:(WOContext *)_ctx
185 if (self->disabled) {
186 if ([self->disabled boolValueInComponent:[_ctx component]])
190 if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
191 /* link is not the active element */
192 return [self->template invokeActionForRequest:_request inContext:_ctx];
195 [[_ctx session] logWithFormat:@"%@[0x%08X]: no action/page set !",
196 NSStringFromClass([self class]), self];
200 - (BOOL)_appendHrefToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
201 [self subclassResponsibility:_cmd];
205 - (void)_addImageToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
206 WOComponent *sComponent = [_ctx component];
211 uUri = [[self->src valueInContext:_ctx] stringValue];
213 if ([self->disabled boolValueInComponent:sComponent]) {
214 uFi = [self->disabledFilename stringValueInComponent:sComponent];
216 uFi = [self->filename stringValueInComponent:sComponent];
219 uFi = [self->filename stringValueInComponent:sComponent];
221 if (!((uFi != nil) || (uUri != nil)))
224 languages = [_ctx resourceLookupLanguages];
226 WOResponse_AddCString(_resp, "<img src=\"");
229 WOResourceManager *rm;
231 if ((rm = [[_ctx component] resourceManager]) == nil)
232 rm = [[_ctx application] resourceManager];
234 uFi = [rm urlForResourceNamed:uFi
236 [self->framework stringValueInComponent:sComponent]
238 request:[_ctx request]];
240 NSLog(@"%@: did not find resource %@", sComponent,
241 [self->filename stringValueInComponent:sComponent]);
244 [_resp appendContentHTMLAttributeValue:uFi];
247 [_resp appendContentHTMLAttributeValue:uUri];
249 WOResponse_AddChar(_resp, '"');
251 [self appendExtraAttributesToResponse:_resp inContext:_ctx];
253 WOResponse_AddEmptyCloseParens(_resp, _ctx);
256 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
257 WOComponent *sComponent = [_ctx component];
259 NSString *targetView;
260 NSString *queryString = nil;
262 if ([[_ctx request] isFromClientComponent])
265 content = [self->string valueInContext:_ctx];
266 targetView = [self->target stringValueInComponent:sComponent];
268 WOResponse_AddCString(_response, "<a href=\"");
270 if ([self _appendHrefToResponse:_response inContext:_ctx]) {
271 queryString = [self queryStringForQueryDictionary:
272 [self->queryDictionary valueInComponent:sComponent]
273 andQueryParameters:self->queryParameters
277 if (self->fragmentIdentifier) {
278 [_response appendContentCharacter:'#'];
279 WOResponse_AddString(_response,
280 [self->fragmentIdentifier stringValueInComponent:sComponent]);
283 [_response appendContentCharacter:'?'];
284 WOResponse_AddString(_response, queryString);
286 [_response appendContentCharacter:'"'];
289 WOResponse_AddCString(_response, " target=\"");
290 WOResponse_AddString(_response, targetView);
291 [_response appendContentCharacter:'"'];
294 [self appendExtraAttributesToResponse:_response inContext:_ctx];
296 if (self->otherTagString) {
297 WOResponse_AddChar(_response, ' ');
298 WOResponse_AddString(_response,
299 [self->otherTagString stringValueInComponent:
302 [_response appendContentCharacter:'>'];
305 [self->template appendToResponse:_response inContext:_ctx];
306 if (content) [_response appendContentHTMLString:content];
309 if ((self->src != nil) || (self->filename != nil))
310 [self _addImageToResponse:_response inContext:_ctx];
313 WOResponse_AddCString(_response, "</a>");
318 - (NSString *)associationDescription {
319 NSMutableString *str = [NSMutableString stringWithCapacity:256];
321 if (self->fragmentIdentifier)
322 [str appendFormat:@" fragment=%@", self->fragmentIdentifier];
323 if (self->string) [str appendFormat:@" string=%@", self->string];
324 if (self->target) [str appendFormat:@" target=%@", self->target];
325 if (self->disabled) [str appendFormat:@" disabled=%@", self->disabled];
328 if (self->filename) [str appendFormat:@" filename=%@", self->filename];
329 if (self->framework) [str appendFormat:@" framework=%@", self->framework];
330 if (self->src) [str appendFormat:@" src=%@", self->src];
335 @end /* _WOComplexHyperlink */
337 @implementation _WOHrefHyperlink
339 static BOOL debugStaticLinks = NO;
342 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
344 debugStaticLinks = [ud boolForKey:@"WODebugStaticLinkProcessing"];
347 - (id)initWithName:(NSString *)_name
348 hyperlinkInfo:(WOHyperlinkInfo *)_info
349 template:(WOElement *)_t
351 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
352 self->href = _info->href;
358 [self->href release];
364 - (BOOL)shouldRewriteURLString:(NSString *)_s inContext:(WOContext *)_ctx {
365 // TODO: we need a binding to disable rewriting!
366 if ([_s hasPrefix:@"mailto:"])
368 if ([_s hasPrefix:@"javascript:"])
373 - (BOOL)_appendHrefToResponse:(WOResponse *)_r inContext:(WOContext *)_ctx {
378 base = [_ctx baseURL];
379 hrefValue = [self->href valueInContext:_ctx];
382 if (hrefValue == nil)
385 if ([hrefValue isKindOfClass:NSURLClass]) {
386 s = [hrefValue stringValueRelativeToURL:base];
389 s = [hrefValue stringValue];
391 if ([self shouldRewriteURLString:s inContext:_ctx]) {
392 if ([s isAbsoluteURL]) {
393 // TODO: why are we doing this? we could just pass through the string?
394 // => probably to generate relative links
395 url = [NSURLClass URLWithString:s];
397 else if (base != nil) {
398 /* avoid creating a new URL for ".", just return the base */
399 url = [s isEqualToString:@"."]
401 : [NSURLClass URLWithString:s relativeToURL:base];
404 [self logWithFormat:@"WARNING: missing base URL in context ..."];
405 WOResponse_AddString(_r, s);
411 @"couldn't construct URL from 'href' string '%@' (base=%@)",
416 s = [url stringValueRelativeToURL:base];
422 if (debugStaticLinks) {
423 [self logWithFormat:@"static links based on 'href': '%@'", hrefValue];
424 [self logWithFormat:@" base %@", base];
425 [self logWithFormat:@" base-abs %@", [base absoluteString]];
426 [self logWithFormat:@" url %@", url];
427 [self logWithFormat:@" url-abs %@", [url absoluteString]];
428 [self logWithFormat:@" string %@", s];
430 WOResponse_AddString(_r, s);
436 - (NSString *)associationDescription {
437 NSMutableString *str = [NSMutableString stringWithCapacity:256];
439 [str appendFormat:@" href=%@", self->href];
440 [str appendString:[super associationDescription]];
445 @end /* _WOHrefHyperlink */
447 @implementation _WOActionHyperlink
449 - (id)initWithName:(NSString *)_name
450 hyperlinkInfo:(WOHyperlinkInfo *)_info
451 template:(WOElement *)_t
453 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
454 self->action = _info->action;
460 [self->action release];
464 /* dynamic invocation */
466 - (id)invokeActionForRequest:(WORequest *)_request
467 inContext:(WOContext *)_ctx
469 if (self->disabled) {
470 if ([self->disabled boolValueInComponent:[_ctx component]])
474 if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
475 /* link is not the active element */
476 return [self->template invokeActionForRequest:_request inContext:_ctx];
479 return [self executeAction:self->action inContext:_ctx];
482 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
483 inContext:(WOContext *)_ctx
485 WOResponse_AddString(_response, [_ctx componentActionURL]);
491 - (NSString *)associationDescription {
492 NSMutableString *str = [NSMutableString stringWithCapacity:256];
494 [str appendFormat:@" action=%@", self->action];
495 [str appendString:[super associationDescription]];
499 @end /* _WOActionHyperlink */
501 @implementation _WOPageHyperlink
503 - (id)initWithName:(NSString *)_name
504 hyperlinkInfo:(WOHyperlinkInfo *)_info
505 template:(WOElement *)_t
507 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
508 self->pageName = _info->pageName;
514 [self->pageName release];
520 - (id)invokeActionForRequest:(WORequest *)_request
521 inContext:(WOContext *)_ctx
526 if (self->disabled) {
527 if ([self->disabled boolValueInComponent:[_ctx component]])
531 if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
532 /* link is not the active element */
533 return [self->template invokeActionForRequest:_request inContext:_ctx];
535 /* link is the active element */
537 name = [self->pageName stringValueInComponent:[_ctx component]];
538 page = [[_ctx application] pageWithName:name inContext:_ctx];
541 [[_ctx session] logWithFormat:
542 @"%@[0x%08X]: did not find page with name %@ !",
543 NSStringFromClass([self class]), self, name];
548 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
549 inContext:(WOContext *)_ctx
553 87% -componentActionURL
554 13% NSString dataUsingEncoding(appendContentString!)
555 TODO(prof): use addcstring
557 WOResponse_AddString(_response, [_ctx componentActionURL]);
563 - (NSString *)associationDescription {
564 NSMutableString *str = [NSMutableString stringWithCapacity:256];
566 [str appendFormat:@" pageName=%@", self->pageName];
567 [str appendString:[super associationDescription]];
571 @end /* _WOPageHyperlink */
573 @implementation _WODirectActionHyperlink
575 - (id)initWithName:(NSString *)_name
576 hyperlinkInfo:(WOHyperlinkInfo *)_info
577 template:(WOElement *)_t
579 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
580 self->actionClass = _info->actionClass;
581 self->directActionName = _info->directActionName;
582 self->sidInUrl = _info->sidInUrl;
584 self->containsForm = NO; /* direct actions are never form stuff ... */
590 [self->actionClass release];
591 [self->directActionName release];
597 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
598 /* DA links can *never* take form values !!!! */
599 [self->template takeValuesFromRequest:_req inContext:_ctx];
602 - (id)invokeActionForRequest:(WORequest *)_request
603 inContext:(WOContext *)_ctx
605 /* DA links can *never* invoke an action !!!! */
606 return [self->template invokeActionForRequest:_request inContext:_ctx];
609 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
610 inContext:(WOContext *)_ctx
612 WOComponent *sComponent;
615 NSMutableDictionary *qd;
618 sComponent = [_ctx component];
619 daClass = [self->actionClass stringValueInComponent:sComponent];
620 daName = [self->directActionName stringValueInComponent:sComponent];
624 if (![daClass isEqualToString:@"DirectAction"])
625 daName = [NSString stringWithFormat:@"%@/%@", daClass, daName];
631 qd = [NSMutableDictionary dictionaryWithCapacity:16];
633 /* add query dictionary */
635 if (self->queryDictionary) {
636 if ((tmp = [self->queryDictionary valueInComponent:sComponent]))
637 [qd addEntriesFromDictionary:tmp];
640 /* add ?style parameters */
642 if (self->queryParameters) {
646 keys = [self->queryParameters keyEnumerator];
647 while ((key = [keys nextObject])) {
650 assoc = [self->queryParameters objectForKey:key];
651 value = [assoc stringValueInComponent:sComponent];
653 [qd setObject:(value ? value : @"") forKey:key];
659 if (self->sidInUrl) {
660 if ([_ctx hasSession]) {
664 [qd setObject:[sn sessionID] forKey:WORequestValueSessionID];
666 if (![sn isDistributionEnabled]) {
667 [qd setObject:[[WOApplication application] number]
668 forKey:WORequestValueInstance];
673 WOResponse_AddString(_response,
674 [_ctx directActionURLForActionNamed:daName
675 queryDictionary:qd]);
681 - (NSString *)associationDescription {
682 NSMutableString *str = [NSMutableString stringWithCapacity:256];
684 if (self->actionClass)
685 [str appendFormat:@" actionClass=%@", self->actionClass];
686 if (self->directActionName)
687 [str appendFormat:@" directAction=%@", self->directActionName];
689 [str appendString:[super associationDescription]];
693 @end /* _WODirectActionHyperlink */