]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/_WOComplexHyperlink.m
added a flag to disable XML style generation of close tags
[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 "common.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 - (WOElement *)template {
152   return self->template;
153 }
154
155 // ******************** responder ********************
156
157 - (void)takeValuesFromRequest:(WORequest *)_req 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 = [_req formValueForKey:key];
174         [assoc setValue:value inComponent:sComponent];
175       }
176     }
177   }
178   
179   [self->template takeValuesFromRequest:_req inContext:_ctx];
180 }
181
182 - (id)invokeActionForRequest:(WORequest *)_request
183   inContext:(WOContext *)_ctx
184 {
185   if (self->disabled) {
186     if ([self->disabled boolValueInComponent:[_ctx component]])
187       return nil;
188   }
189   
190   if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
191     /* link is not the active element */
192     return [self->template invokeActionForRequest:_request inContext:_ctx];
193   
194   /* link is active */
195   [[_ctx session] logWithFormat:@"%@[0x%08X]: no action/page set !",
196                   NSStringFromClass([self class]), self];
197   return nil;
198 }
199
200 - (BOOL)_appendHrefToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
201   [self subclassResponsibility:_cmd];
202   return NO;
203 }
204
205 - (void)_addImageToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
206   WOComponent *sComponent = [_ctx component];
207   NSString *uUri;
208   NSString *uFi  = nil;
209   NSArray *languages;
210   
211   uUri = [[self->src valueInContext:_ctx] stringValue];
212       
213   if ([self->disabled boolValueInComponent:sComponent]) {
214     uFi =  [self->disabledFilename stringValueInComponent:sComponent];
215     if (uFi == nil)
216       uFi = [self->filename stringValueInComponent:sComponent];
217   }
218   else
219     uFi = [self->filename stringValueInComponent:sComponent];
220   
221   if (!((uFi != nil) || (uUri != nil))) 
222     return;
223
224   languages = [_ctx resourceLookupLanguages];
225         
226   WOResponse_AddCString(_resp, "<img src=\"");
227   
228   if (uFi) {
229     WOResourceManager *rm;
230           
231     if ((rm = [[_ctx component] resourceManager]) == nil)
232       rm = [[_ctx application] resourceManager];
233           
234     uFi = [rm urlForResourceNamed:uFi
235               inFramework:
236                 [self->framework stringValueInComponent:sComponent]
237               languages:languages
238               request:[_ctx request]];
239     if (uFi == nil) {
240       NSLog(@"%@: did not find resource %@", sComponent,
241             [self->filename stringValueInComponent:sComponent]);
242       uFi = uUri;
243     }
244     [_resp appendContentHTMLAttributeValue:uFi];
245   }
246   else {
247     [_resp appendContentHTMLAttributeValue:uUri];
248   }
249   WOResponse_AddChar(_resp, '"');
250   
251   [self appendExtraAttributesToResponse:_resp inContext:_ctx];
252   
253   WOResponse_AddEmptyCloseParens(_resp, _ctx);
254 }
255
256 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
257   WOComponent *sComponent = [_ctx component];
258   NSString *content;
259   NSString *targetView;
260   NSString *queryString = nil;
261
262   if ([[_ctx request] isFromClientComponent])
263     return;
264   
265   content    = [self->string valueInContext:_ctx];
266   targetView = [self->target stringValueInComponent:sComponent];
267     
268   WOResponse_AddCString(_response, "<a href=\"");
269     
270   if ([self _appendHrefToResponse:_response inContext:_ctx]) {
271     queryString = [self queryStringForQueryDictionary:
272                           [self->queryDictionary valueInComponent:sComponent]
273                         andQueryParameters:self->queryParameters
274                         inContext:_ctx];
275   }
276   
277   if (self->fragmentIdentifier) {
278       [_response appendContentCharacter:'#'];
279       WOResponse_AddString(_response,
280          [self->fragmentIdentifier stringValueInComponent:sComponent]);
281   }
282   if (queryString) {
283     [_response appendContentCharacter:'?'];
284     WOResponse_AddString(_response, queryString);
285   }
286   [_response appendContentCharacter:'"'];
287     
288   if (targetView) {
289     WOResponse_AddCString(_response, " target=\"");
290     WOResponse_AddString(_response, targetView);
291     [_response appendContentCharacter:'"'];
292   }
293     
294   [self appendExtraAttributesToResponse:_response inContext:_ctx];
295     
296   if (self->otherTagString) {
297     WOResponse_AddChar(_response, ' ');
298     WOResponse_AddString(_response,
299                          [self->otherTagString stringValueInComponent:
300                            [_ctx component]]);
301   }
302   [_response appendContentCharacter:'>'];
303     
304   /* content */
305   [self->template appendToResponse:_response inContext:_ctx];
306   if (content) [_response appendContentHTMLString:content];
307   
308   /* image content */
309   if ((self->src != nil) || (self->filename != nil))
310     [self _addImageToResponse:_response inContext:_ctx];
311   
312   /* closing tag */
313   WOResponse_AddCString(_response, "</a>");
314 }
315
316 /* description */
317
318 - (NSString *)associationDescription {
319   NSMutableString *str = [NSMutableString stringWithCapacity:256];
320
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];
326
327   /* image .. */
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];
331
332   return str;
333 }
334
335 @end /* _WOComplexHyperlink */
336
337 @implementation _WOHrefHyperlink
338
339 static BOOL debugStaticLinks = NO;
340
341 + (void)initialize {
342   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
343   
344   debugStaticLinks = [ud boolForKey:@"WODebugStaticLinkProcessing"];
345 }
346
347 - (id)initWithName:(NSString *)_name
348   hyperlinkInfo:(WOHyperlinkInfo *)_info
349   template:(WOElement *)_t
350 {
351   if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
352     self->href = _info->href;
353   }
354   return self;
355 }
356
357 - (void)dealloc {
358   [self->href release];
359   [super dealloc];
360 }
361
362 /* URI generation */
363
364 - (BOOL)shouldRewriteURLString:(NSString *)_s inContext:(WOContext *)_ctx {
365   // TODO: we need a binding to disable rewriting!
366   if ([_s hasPrefix:@"mailto:"])
367     return NO;
368   if ([_s hasPrefix:@"javascript:"])
369     return NO;
370   return YES;
371 }
372
373 - (BOOL)_appendHrefToResponse:(WOResponse *)_r inContext:(WOContext *)_ctx {
374   NSString *s;
375   id    hrefValue;
376   NSURL *url, *base;
377   
378   base      = [_ctx baseURL];
379   hrefValue = [self->href valueInContext:_ctx];
380   url       = hrefValue;
381   
382   if (hrefValue == nil)
383     return NO;
384   
385   if ([hrefValue isKindOfClass:NSURLClass]) {
386     s = [hrefValue stringValueRelativeToURL:base];
387   }
388   else {
389     s = [hrefValue stringValue];
390     
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];
396       }
397       else if (base != nil) {
398         /* avoid creating a new URL for ".", just return the base */
399         url = [s isEqualToString:@"."]
400           ? base
401           : [NSURLClass URLWithString:s relativeToURL:base];
402       }
403       else {
404         [self logWithFormat:@"WARNING: missing base URL in context ..."];
405         WOResponse_AddString(_r, s);
406         return YES;
407       }
408       
409       if (url == nil) {
410         [self logWithFormat:
411                 @"couldn't construct URL from 'href' string '%@' (base=%@)",
412                 s, base];
413         return NO;
414       }
415       
416       s = [url stringValueRelativeToURL:base];
417     }
418   }
419   
420   /* generate URL */
421   
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];
429   }
430   WOResponse_AddString(_r, s);
431   return YES;
432 }
433
434 /* description */
435
436 - (NSString *)associationDescription {
437   NSMutableString *str = [NSMutableString stringWithCapacity:256];
438
439   [str appendFormat:@" href=%@", self->href];
440   [str appendString:[super associationDescription]];
441   
442   return str;
443 }
444
445 @end /* _WOHrefHyperlink */
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 *)_request
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:_request 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 @implementation _WOPageHyperlink
502
503 - (id)initWithName:(NSString *)_name
504   hyperlinkInfo:(WOHyperlinkInfo *)_info
505   template:(WOElement *)_t
506 {
507   if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
508     self->pageName = _info->pageName;
509   }
510   return self;
511 }
512
513 - (void)dealloc {
514   [self->pageName release];
515   [super dealloc];
516 }
517
518 /* actions */
519
520 - (id)invokeActionForRequest:(WORequest *)_request
521   inContext:(WOContext *)_ctx
522 {
523   WOComponent *page;
524   NSString    *name;
525
526   if (self->disabled) {
527     if ([self->disabled boolValueInComponent:[_ctx component]])
528       return nil;
529   }
530   
531   if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
532     /* link is not the active element */
533     return [self->template invokeActionForRequest:_request inContext:_ctx];
534   
535   /* link is the active element */
536   
537   name = [self->pageName stringValueInComponent:[_ctx component]];
538   page = [[_ctx application] pageWithName:name inContext:_ctx];
539
540   if (page == nil) {
541     [[_ctx session] logWithFormat:
542                       @"%@[0x%08X]: did not find page with name %@ !",
543                       NSStringFromClass([self class]), self, name];
544   }
545   return page;
546 }
547
548 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
549   inContext:(WOContext *)_ctx
550 {
551   /*
552     Profiling:
553       87% -componentActionURL
554       13% NSString dataUsingEncoding(appendContentString!)
555     TODO(prof): use addcstring
556   */
557   WOResponse_AddString(_response, [_ctx componentActionURL]);
558   return YES;
559 }
560
561 /* description */
562
563 - (NSString *)associationDescription {
564   NSMutableString *str = [NSMutableString stringWithCapacity:256];
565
566   [str appendFormat:@" pageName=%@", self->pageName];
567   [str appendString:[super associationDescription]];
568   return str;
569 }
570
571 @end /* _WOPageHyperlink */
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 /* href */
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 *)_request
603   inContext:(WOContext *)_ctx
604 {
605   /* DA links can *never* invoke an action !!!! */
606   return [self->template invokeActionForRequest:_request inContext:_ctx];
607 }
608
609 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
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(_response,
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 */