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];
252 WOResponse_AddCString(_resp, " />");
255 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
256 WOComponent *sComponent = [_ctx component];
258 NSString *targetView;
259 NSString *queryString = nil;
261 if ([[_ctx request] isFromClientComponent])
264 content = [self->string valueInContext:_ctx];
265 targetView = [self->target stringValueInComponent:sComponent];
267 WOResponse_AddCString(_response, "<a href=\"");
269 if ([self _appendHrefToResponse:_response inContext:_ctx]) {
270 queryString = [self queryStringForQueryDictionary:
271 [self->queryDictionary valueInComponent:sComponent]
272 andQueryParameters:self->queryParameters
276 if (self->fragmentIdentifier) {
277 [_response appendContentCharacter:'#'];
278 WOResponse_AddString(_response,
279 [self->fragmentIdentifier stringValueInComponent:sComponent]);
282 [_response appendContentCharacter:'?'];
283 WOResponse_AddString(_response, queryString);
285 [_response appendContentCharacter:'"'];
288 WOResponse_AddCString(_response, " target=\"");
289 WOResponse_AddString(_response, targetView);
290 [_response appendContentCharacter:'"'];
293 [self appendExtraAttributesToResponse:_response inContext:_ctx];
295 if (self->otherTagString) {
296 WOResponse_AddChar(_response, ' ');
297 WOResponse_AddString(_response,
298 [self->otherTagString stringValueInComponent:
301 [_response appendContentCharacter:'>'];
304 [self->template appendToResponse:_response inContext:_ctx];
305 if (content) [_response appendContentHTMLString:content];
308 if ((self->src != nil) || (self->filename != nil))
309 [self _addImageToResponse:_response inContext:_ctx];
312 WOResponse_AddCString(_response, "</a>");
317 - (NSString *)associationDescription {
318 NSMutableString *str = [NSMutableString stringWithCapacity:256];
320 if (self->fragmentIdentifier)
321 [str appendFormat:@" fragment=%@", self->fragmentIdentifier];
322 if (self->string) [str appendFormat:@" string=%@", self->string];
323 if (self->target) [str appendFormat:@" target=%@", self->target];
324 if (self->disabled) [str appendFormat:@" disabled=%@", self->disabled];
327 if (self->filename) [str appendFormat:@" filename=%@", self->filename];
328 if (self->framework) [str appendFormat:@" framework=%@", self->framework];
329 if (self->src) [str appendFormat:@" src=%@", self->src];
334 @end /* _WOComplexHyperlink */
336 @implementation _WOHrefHyperlink
338 static BOOL debugStaticLinks = NO;
341 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
343 debugStaticLinks = [ud boolForKey:@"WODebugStaticLinkProcessing"];
346 - (id)initWithName:(NSString *)_name
347 hyperlinkInfo:(WOHyperlinkInfo *)_info
348 template:(WOElement *)_t
350 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
351 self->href = _info->href;
357 [self->href release];
363 - (BOOL)shouldRewriteURLString:(NSString *)_s inContext:(WOContext *)_ctx {
364 // TODO: we need a binding to disable rewriting!
365 if ([_s hasPrefix:@"mailto:"])
367 if ([_s hasPrefix:@"javascript:"])
372 - (BOOL)_appendHrefToResponse:(WOResponse *)_r inContext:(WOContext *)_ctx {
377 base = [_ctx baseURL];
378 hrefValue = [self->href valueInContext:_ctx];
381 if (hrefValue == nil)
384 if ([hrefValue isKindOfClass:NSURLClass]) {
385 s = [hrefValue stringValueRelativeToURL:base];
388 s = [hrefValue stringValue];
390 if ([self shouldRewriteURLString:s inContext:_ctx]) {
391 if ([s isAbsoluteURL]) {
392 // TODO: why are we doing this? we could just pass through the string?
393 // => probably to generate relative links
394 url = [NSURLClass URLWithString:s];
396 else if (base != nil) {
397 /* avoid creating a new URL for ".", just return the base */
398 url = [s isEqualToString:@"."]
400 : [NSURLClass URLWithString:s relativeToURL:base];
403 [self logWithFormat:@"WARNING: missing base URL in context ..."];
404 WOResponse_AddString(_r, s);
410 @"couldn't construct URL from 'href' string '%@' (base=%@)",
415 s = [url stringValueRelativeToURL:base];
421 if (debugStaticLinks) {
422 [self logWithFormat:@"static links based on 'href': '%@'", hrefValue];
423 [self logWithFormat:@" base %@", base];
424 [self logWithFormat:@" base-abs %@", [base absoluteString]];
425 [self logWithFormat:@" url %@", url];
426 [self logWithFormat:@" url-abs %@", [url absoluteString]];
427 [self logWithFormat:@" string %@", s];
429 WOResponse_AddString(_r, s);
435 - (NSString *)associationDescription {
436 NSMutableString *str = [NSMutableString stringWithCapacity:256];
438 [str appendFormat:@" href=%@", self->href];
439 [str appendString:[super associationDescription]];
444 @end /* _WOHrefHyperlink */
446 @implementation _WOActionHyperlink
448 - (id)initWithName:(NSString *)_name
449 hyperlinkInfo:(WOHyperlinkInfo *)_info
450 template:(WOElement *)_t
452 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
453 self->action = _info->action;
459 [self->action release];
463 /* dynamic invocation */
465 - (id)invokeActionForRequest:(WORequest *)_request
466 inContext:(WOContext *)_ctx
468 if (self->disabled) {
469 if ([self->disabled boolValueInComponent:[_ctx component]])
473 if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
474 /* link is not the active element */
475 return [self->template invokeActionForRequest:_request inContext:_ctx];
478 return [self executeAction:self->action inContext:_ctx];
481 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
482 inContext:(WOContext *)_ctx
484 WOResponse_AddString(_response, [_ctx componentActionURL]);
490 - (NSString *)associationDescription {
491 NSMutableString *str = [NSMutableString stringWithCapacity:256];
493 [str appendFormat:@" action=%@", self->action];
494 [str appendString:[super associationDescription]];
498 @end /* _WOActionHyperlink */
500 @implementation _WOPageHyperlink
502 - (id)initWithName:(NSString *)_name
503 hyperlinkInfo:(WOHyperlinkInfo *)_info
504 template:(WOElement *)_t
506 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
507 self->pageName = _info->pageName;
513 [self->pageName release];
519 - (id)invokeActionForRequest:(WORequest *)_request
520 inContext:(WOContext *)_ctx
525 if (self->disabled) {
526 if ([self->disabled boolValueInComponent:[_ctx component]])
530 if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
531 /* link is not the active element */
532 return [self->template invokeActionForRequest:_request inContext:_ctx];
534 /* link is the active element */
536 name = [self->pageName stringValueInComponent:[_ctx component]];
537 page = [[_ctx application] pageWithName:name inContext:_ctx];
540 [[_ctx session] logWithFormat:
541 @"%@[0x%08X]: did not find page with name %@ !",
542 NSStringFromClass([self class]), self, name];
547 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
548 inContext:(WOContext *)_ctx
552 87% -componentActionURL
553 13% NSString dataUsingEncoding(appendContentString!)
554 TODO(prof): use addcstring
556 WOResponse_AddString(_response, [_ctx componentActionURL]);
562 - (NSString *)associationDescription {
563 NSMutableString *str = [NSMutableString stringWithCapacity:256];
565 [str appendFormat:@" pageName=%@", self->pageName];
566 [str appendString:[super associationDescription]];
570 @end /* _WOPageHyperlink */
572 @implementation _WODirectActionHyperlink
574 - (id)initWithName:(NSString *)_name
575 hyperlinkInfo:(WOHyperlinkInfo *)_info
576 template:(WOElement *)_t
578 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
579 self->actionClass = _info->actionClass;
580 self->directActionName = _info->directActionName;
581 self->sidInUrl = _info->sidInUrl;
583 self->containsForm = NO; /* direct actions are never form stuff ... */
589 [self->actionClass release];
590 [self->directActionName release];
596 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
597 /* DA links can *never* take form values !!!! */
598 [self->template takeValuesFromRequest:_req inContext:_ctx];
601 - (id)invokeActionForRequest:(WORequest *)_request
602 inContext:(WOContext *)_ctx
604 /* DA links can *never* invoke an action !!!! */
605 return [self->template invokeActionForRequest:_request inContext:_ctx];
608 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
609 inContext:(WOContext *)_ctx
611 WOComponent *sComponent;
614 NSMutableDictionary *qd;
617 sComponent = [_ctx component];
618 daClass = [self->actionClass stringValueInComponent:sComponent];
619 daName = [self->directActionName stringValueInComponent:sComponent];
623 if (![daClass isEqualToString:@"DirectAction"])
624 daName = [NSString stringWithFormat:@"%@/%@", daClass, daName];
630 qd = [NSMutableDictionary dictionaryWithCapacity:16];
632 /* add query dictionary */
634 if (self->queryDictionary) {
635 if ((tmp = [self->queryDictionary valueInComponent:sComponent]))
636 [qd addEntriesFromDictionary:tmp];
639 /* add ?style parameters */
641 if (self->queryParameters) {
645 keys = [self->queryParameters keyEnumerator];
646 while ((key = [keys nextObject])) {
649 assoc = [self->queryParameters objectForKey:key];
650 value = [assoc stringValueInComponent:sComponent];
652 [qd setObject:(value ? value : @"") forKey:key];
658 if (self->sidInUrl) {
659 if ([_ctx hasSession]) {
663 [qd setObject:[sn sessionID] forKey:WORequestValueSessionID];
665 if (![sn isDistributionEnabled]) {
666 [qd setObject:[[WOApplication application] number]
667 forKey:WORequestValueInstance];
672 WOResponse_AddString(_response,
673 [_ctx directActionURLForActionNamed:daName
674 queryDictionary:qd]);
680 - (NSString *)associationDescription {
681 NSMutableString *str = [NSMutableString stringWithCapacity:256];
683 if (self->actionClass)
684 [str appendFormat:@" actionClass=%@", self->actionClass];
685 if (self->directActionName)
686 [str appendFormat:@" directAction=%@", self->directActionName];
688 [str appendString:[super associationDescription]];
692 @end /* _WODirectActionHyperlink */