]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/_WOComplexHyperlink.m
61c4ce765a7fc4742455ca7edc3d8075e6bf1842
[sope] / sope-appserver / NGObjWeb / DynamicElements / _WOComplexHyperlink.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "WOHyperlink.h"
23
24 /*
25   Class Hierachy
26     [WOHTMLDynamicElement]
27       [WOHyperlink]
28         _WOComplexHyperlink
29           _WOHrefHyperlink
30           _WOActionHyperlink
31           _WOPageHyperlink
32           _WODirectActionHyperlink
33 */
34
35 @interface _WOComplexHyperlink : WOHyperlink
36 {
37   /* superclass of most hyperlink classes */
38 @protected
39   WOAssociation *fragmentIdentifier;
40   WOAssociation *string;
41   WOAssociation *target;
42   WOAssociation *disabled;
43   WOElement     *template;
44   
45   /* new in WO4: */
46   WOAssociation *queryDictionary;
47   NSDictionary  *queryParameters;  /* associations beginning with ? */
48
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 */
54 }
55
56 - (NSString *)associationDescription;
57
58 @end
59
60 @interface _WOHrefHyperlink : _WOComplexHyperlink
61 {
62   WOAssociation *href;
63 }
64 @end
65
66 @interface _WOActionHyperlink : _WOComplexHyperlink
67 {
68   WOAssociation *action;
69 }
70 @end
71
72 @interface _WOPageHyperlink : _WOComplexHyperlink
73 {
74   WOAssociation *pageName;
75 }
76 @end
77
78 @interface _WODirectActionHyperlink : _WOComplexHyperlink
79 {
80   WOAssociation *actionClass;
81   WOAssociation *directActionName;
82   BOOL          sidInUrl;          /* include session-id in wa URL ? */
83 }
84 @end
85
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>
92 #include "decommon.h"
93
94 static Class NSURLClass = Nil;
95
96 @implementation _WOComplexHyperlink
97
98 + (int)version {
99   return [super version] /* v4 */;
100 }
101 + (void)initialize {
102   NSAssert2([super version] == 4,
103             @"invalid superclass (%@) version %i !",
104             NSStringFromClass([self superclass]), [super version]);
105   if (NSURLClass == Nil)
106     NSURLClass = [NSURL class];
107 }
108
109 - (id)initWithName:(NSString *)_name
110   hyperlinkInfo:(WOHyperlinkInfo *)_info
111   template:(WOElement *)_t
112 {
113   if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
114     self->template = [_t retain];
115     
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;
122     
123     /* image */
124     self->filename         = _info->filename;
125     self->framework        = _info->framework;
126     self->src              = _info->src;
127     self->disabledFilename = _info->disabledFilename;
128     
129     self->containsForm = self->queryParameters ? YES : NO;
130   }
131   return self;
132 }
133
134 - (void)dealloc {
135   [self->template           release];
136   [self->queryDictionary    release];
137   [self->queryParameters    release];
138   [self->disabledFilename   release];
139   [self->filename           release];
140   [self->framework          release];
141   [self->src                release];
142   [self->fragmentIdentifier release];
143   [self->string             release];
144   [self->target             release];
145   [self->disabled           release];
146   [super dealloc];
147 }
148
149 /* accessors */
150
151 - (id)template {
152   return self->template;
153 }
154
155 /* handle requests */
156
157 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
158   /* links can take form values !!!! (for query-parameters) */
159   
160   if (self->queryParameters) {
161     /* apply values to ?style parameters */
162     WOComponent  *sComponent = [_ctx component];
163     NSEnumerator *keys;
164     NSString     *key;
165
166     keys = [self->queryParameters keyEnumerator];
167     while ((key = [keys nextObject])) {
168       id assoc, value;
169
170       assoc = [self->queryParameters objectForKey:key];
171       
172       if ([assoc isValueSettable]) {
173         value = [_rq formValueForKey:key];
174         [assoc setValue:value inComponent:sComponent];
175       }
176     }
177   }
178   
179   [self->template takeValuesFromRequest:_rq inContext:_ctx];
180 }
181
182 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
183   if (self->disabled != nil) {
184     if ([self->disabled boolValueInComponent:[_ctx component]])
185       return nil;
186   }
187   
188   if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
189     /* link is not the active element */
190     return [self->template invokeActionForRequest:_rq inContext:_ctx];
191   
192   /* link is active */
193   [[_ctx session] logWithFormat:@"%@[0x%08X]: no action/page set !",
194                   NSStringFromClass([self class]), self];
195   return nil;
196 }
197
198 - (BOOL)_appendHrefToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
199   [self subclassResponsibility:_cmd];
200   return NO;
201 }
202
203 - (void)_addImageToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
204   WOComponent *sComponent = [_ctx component];
205   NSString *uUri;
206   NSString *uFi  = nil;
207   NSArray *languages;
208   
209   uUri = [[self->src valueInContext:_ctx] stringValue];
210       
211   if ([self->disabled boolValueInComponent:sComponent]) {
212     uFi =  [self->disabledFilename stringValueInComponent:sComponent];
213     if (uFi == nil)
214       uFi = [self->filename stringValueInComponent:sComponent];
215   }
216   else
217     uFi = [self->filename stringValueInComponent:sComponent];
218   
219   if (!((uFi != nil) || (uUri != nil))) 
220     return;
221
222   languages = [_ctx resourceLookupLanguages];
223         
224   WOResponse_AddCString(_resp, "<img src=\"");
225   
226   if (uFi) {
227     WOResourceManager *rm;
228           
229     if ((rm = [[_ctx component] resourceManager]) == nil)
230       rm = [[_ctx application] resourceManager];
231           
232     uFi = [rm urlForResourceNamed:uFi
233               inFramework:
234                 [self->framework stringValueInComponent:sComponent]
235               languages:languages
236               request:[_ctx request]];
237     if (uFi == nil) {
238       NSLog(@"%@: did not find resource %@", sComponent,
239             [self->filename stringValueInComponent:sComponent]);
240       uFi = uUri;
241     }
242     [_resp appendContentHTMLAttributeValue:uFi];
243   }
244   else {
245     [_resp appendContentHTMLAttributeValue:uUri];
246   }
247   WOResponse_AddChar(_resp, '"');
248   
249   [self appendExtraAttributesToResponse:_resp inContext:_ctx];
250   
251   WOResponse_AddEmptyCloseParens(_resp, _ctx);
252 }
253
254 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
255   WOComponent *sComponent = [_ctx component];
256   NSString *content;
257   NSString *targetView;
258   NSString *queryString = nil;
259
260   if ([[_ctx request] isFromClientComponent])
261     return;
262   
263   content    = [self->string valueInContext:_ctx];
264   targetView = [self->target stringValueInComponent:sComponent];
265     
266   WOResponse_AddCString(_response, "<a href=\"");
267     
268   if ([self _appendHrefToResponse:_response inContext:_ctx]) {
269     queryString = [self queryStringForQueryDictionary:
270                           [self->queryDictionary valueInComponent:sComponent]
271                         andQueryParameters:self->queryParameters
272                         inContext:_ctx];
273   }
274   
275   if (self->fragmentIdentifier) {
276       [_response appendContentCharacter:'#'];
277       WOResponse_AddString(_response,
278          [self->fragmentIdentifier stringValueInComponent:sComponent]);
279   }
280   if (queryString) {
281     [_response appendContentCharacter:'?'];
282     WOResponse_AddString(_response, queryString);
283   }
284   [_response appendContentCharacter:'"'];
285     
286   if (targetView) {
287     WOResponse_AddCString(_response, " target=\"");
288     WOResponse_AddString(_response, targetView);
289     [_response appendContentCharacter:'"'];
290   }
291     
292   [self appendExtraAttributesToResponse:_response inContext:_ctx];
293     
294   if (self->otherTagString) {
295     WOResponse_AddChar(_response, ' ');
296     WOResponse_AddString(_response,
297                          [self->otherTagString stringValueInComponent:
298                            [_ctx component]]);
299   }
300   [_response appendContentCharacter:'>'];
301     
302   /* content */
303   [self->template appendToResponse:_response inContext:_ctx];
304   if (content) [_response appendContentHTMLString:content];
305   
306   /* image content */
307   if ((self->src != nil) || (self->filename != nil))
308     [self _addImageToResponse:_response inContext:_ctx];
309   
310   /* closing tag */
311   WOResponse_AddCString(_response, "</a>");
312 }
313
314 /* description */
315
316 - (NSString *)associationDescription {
317   NSMutableString *str = [NSMutableString stringWithCapacity:256];
318
319   if (self->fragmentIdentifier)
320     [str appendFormat:@" fragment=%@", self->fragmentIdentifier];
321   if (self->string)   [str appendFormat:@" string=%@",   self->string];
322   if (self->target)   [str appendFormat:@" target=%@",   self->target];
323   if (self->disabled) [str appendFormat:@" disabled=%@", self->disabled];
324
325   /* image .. */
326   if (self->filename)  [str appendFormat:@" filename=%@",  self->filename];
327   if (self->framework) [str appendFormat:@" framework=%@", self->framework];
328   if (self->src)       [str appendFormat:@" src=%@",       self->src];
329
330   return str;
331 }
332
333 @end /* _WOComplexHyperlink */
334
335
336 @implementation _WOHrefHyperlink
337
338 static BOOL debugStaticLinks = NO;
339
340 + (void)initialize {
341   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
342   
343   debugStaticLinks = [ud boolForKey:@"WODebugStaticLinkProcessing"];
344 }
345
346 - (id)initWithName:(NSString *)_name
347   hyperlinkInfo:(WOHyperlinkInfo *)_info
348   template:(WOElement *)_t
349 {
350   if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
351     self->href = _info->href;
352   }
353   return self;
354 }
355
356 - (void)dealloc {
357   [self->href release];
358   [super dealloc];
359 }
360
361 /* URI generation */
362
363 - (BOOL)shouldRewriteURLString:(NSString *)_s inContext:(WOContext *)_ctx {
364   // TODO: we need a binding to disable rewriting!
365   if ([_s hasPrefix:@"mailto:"])
366     return NO;
367   if ([_s hasPrefix:@"javascript:"])
368     return NO;
369   return YES;
370 }
371
372 - (BOOL)_appendHrefToResponse:(WOResponse *)_r inContext:(WOContext *)_ctx {
373   NSString *s;
374   id    hrefValue;
375   NSURL *url, *base;
376   
377   base      = [_ctx baseURL];
378   hrefValue = [self->href valueInContext:_ctx];
379   url       = hrefValue;
380   
381   if (hrefValue == nil)
382     return NO;
383   
384   if ([hrefValue isKindOfClass:NSURLClass]) {
385     s = [hrefValue stringValueRelativeToURL:base];
386   }
387   else {
388     s = [hrefValue stringValue];
389     
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];
395       }
396       else if (base != nil) {
397         /* avoid creating a new URL for ".", just return the base */
398         url = [s isEqualToString:@"."]
399           ? base
400           : [NSURLClass URLWithString:s relativeToURL:base];
401       }
402       else {
403         [self logWithFormat:@"WARNING: missing base URL in context ..."];
404         WOResponse_AddString(_r, s);
405         return YES;
406       }
407       
408       if (url == nil) {
409         [self logWithFormat:
410                 @"couldn't construct URL from 'href' string '%@' (base=%@)",
411                 s, base];
412         return NO;
413       }
414       
415       s = [url stringValueRelativeToURL:base];
416     }
417   }
418   
419   /* generate URL */
420   
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];
428   }
429   WOResponse_AddString(_r, s);
430   return YES;
431 }
432
433 /* description */
434
435 - (NSString *)associationDescription {
436   NSMutableString *str = [NSMutableString stringWithCapacity:256];
437
438   [str appendFormat:@" href=%@", self->href];
439   [str appendString:[super associationDescription]];
440   
441   return str;
442 }
443
444 @end /* _WOHrefHyperlink */
445
446
447 @implementation _WOActionHyperlink
448
449 - (id)initWithName:(NSString *)_name
450   hyperlinkInfo:(WOHyperlinkInfo *)_info
451   template:(WOElement *)_t
452 {
453   if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
454     self->action = _info->action;
455   }
456   return self;
457 }
458
459 - (void)dealloc {
460   [self->action release];
461   [super dealloc];
462 }
463
464 /* dynamic invocation */
465
466 - (id)invokeActionForRequest:(WORequest *)_rq
467   inContext:(WOContext *)_ctx
468 {
469   if (self->disabled) {
470     if ([self->disabled boolValueInComponent:[_ctx component]])
471       return nil;
472   }
473
474   if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
475     /* link is not the active element */
476     return [self->template invokeActionForRequest:_rq inContext:_ctx];
477   
478   /* link is active */
479   return [self executeAction:self->action inContext:_ctx];
480 }
481
482 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
483   inContext:(WOContext *)_ctx
484 {
485   WOResponse_AddString(_response, [_ctx componentActionURL]);
486   return YES;
487 }
488
489 /* description */
490
491 - (NSString *)associationDescription {
492   NSMutableString *str = [NSMutableString stringWithCapacity:256];
493
494   [str appendFormat:@" action=%@", self->action];
495   [str appendString:[super associationDescription]];
496   return str;
497 }
498
499 @end /* _WOActionHyperlink */
500
501
502 @implementation _WOPageHyperlink
503
504 - (id)initWithName:(NSString *)_name
505   hyperlinkInfo:(WOHyperlinkInfo *)_info
506   template:(WOElement *)_t
507 {
508   if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
509     self->pageName = _info->pageName;
510   }
511   return self;
512 }
513
514 - (void)dealloc {
515   [self->pageName release];
516   [super dealloc];
517 }
518
519 /* handle request */
520
521 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
522   WOComponent *page;
523   NSString    *name;
524
525   if (self->disabled) {
526     if ([self->disabled boolValueInComponent:[_ctx component]])
527       return nil;
528   }
529   
530   if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
531     /* link is not the active element */
532     return [self->template invokeActionForRequest:_rq inContext:_ctx];
533   
534   /* link is the active element */
535   
536   name = [self->pageName stringValueInComponent:[_ctx component]];
537   page = [[_ctx application] pageWithName:name inContext:_ctx];
538
539   if (page == nil) {
540     [[_ctx session] logWithFormat:
541                       @"%@[0x%08X]: did not find page with name %@ !",
542                       NSStringFromClass([self class]), self, name];
543   }
544   return page;
545 }
546
547 /* generate response */
548
549 - (BOOL)_appendHrefToResponse:(WOResponse *)_r inContext:(WOContext *)_ctx {
550   /*
551     Profiling:
552       87% -componentActionURL
553       13% NSString dataUsingEncoding(appendContentString!)
554     TODO(prof): use addcstring
555   */
556   WOResponse_AddString(_r, [_ctx componentActionURL]);
557   return YES;
558 }
559
560 /* description */
561
562 - (NSString *)associationDescription {
563   NSMutableString *str = [NSMutableString stringWithCapacity:256];
564
565   [str appendFormat:@" pageName=%@", self->pageName];
566   [str appendString:[super associationDescription]];
567   return str;
568 }
569
570 @end /* _WOPageHyperlink */
571
572
573 @implementation _WODirectActionHyperlink
574
575 - (id)initWithName:(NSString *)_name
576   hyperlinkInfo:(WOHyperlinkInfo *)_info
577   template:(WOElement *)_t
578 {
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;
583
584     self->containsForm = NO; /* direct actions are never form stuff ... */
585   }
586   return self;
587 }
588
589 - (void)dealloc {
590   [self->actionClass      release];
591   [self->directActionName release];
592   [super dealloc];
593 }
594
595 /* handle requests */
596
597 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
598   /* DA links can *never* take form values !!!! */
599   [self->template takeValuesFromRequest:_req inContext:_ctx];
600 }
601
602 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
603   /* DA links can *never* invoke an action !!!! */
604   return [self->template invokeActionForRequest:_rq inContext:_ctx];
605 }
606
607 /* generate response */
608
609 - (BOOL)_appendHrefToResponse:(WOResponse *)_r
610   inContext:(WOContext *)_ctx
611 {
612   WOComponent         *sComponent;
613   NSString            *daClass;
614   NSString            *daName;
615   NSMutableDictionary *qd;
616   NSDictionary        *tmp;
617
618   sComponent = [_ctx component];
619   daClass = [self->actionClass stringValueInComponent:sComponent];
620   daName  = [self->directActionName stringValueInComponent:sComponent];
621
622   if (daClass) {
623     if (daName) {
624       if (![daClass isEqualToString:@"DirectAction"])
625         daName = [NSString stringWithFormat:@"%@/%@", daClass, daName];
626     }
627     else
628       daName = daClass;
629   }
630   
631   qd = [NSMutableDictionary dictionaryWithCapacity:16];
632   
633   /* add query dictionary */
634   
635   if (self->queryDictionary) {
636     if ((tmp = [self->queryDictionary valueInComponent:sComponent]))
637       [qd addEntriesFromDictionary:tmp];
638   }
639   
640   /* add ?style parameters */
641
642   if (self->queryParameters) {
643     NSEnumerator *keys;
644     NSString     *key;
645
646     keys = [self->queryParameters keyEnumerator];
647     while ((key = [keys nextObject])) {
648       id assoc, value;
649
650       assoc = [self->queryParameters objectForKey:key];
651       value = [assoc stringValueInComponent:sComponent];
652           
653       [qd setObject:(value ? value : @"") forKey:key];
654     }
655   }
656       
657   /* add session ID */
658   
659   if (self->sidInUrl) {
660     if ([_ctx hasSession]) {
661       WOSession *sn;
662       
663       sn = [_ctx session];
664       [qd setObject:[sn sessionID] forKey:WORequestValueSessionID];
665       
666       if (![sn isDistributionEnabled]) {
667         [qd setObject:[[WOApplication application] number]
668             forKey:WORequestValueInstance];
669       }
670     }
671   }
672
673   WOResponse_AddString(_r,
674                        [_ctx directActionURLForActionNamed:daName
675                              queryDictionary:qd]);
676   return NO;
677 }
678
679 /* description */
680
681 - (NSString *)associationDescription {
682   NSMutableString *str = [NSMutableString stringWithCapacity:256];
683
684   if (self->actionClass)
685     [str appendFormat:@" actionClass=%@", self->actionClass];
686   if (self->directActionName)
687     [str appendFormat:@" directAction=%@", self->directActionName];
688   
689   [str appendString:[super associationDescription]];
690   return str;
691 }
692
693 @end /* _WODirectActionHyperlink */