]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOBody.m
started TAL element builder
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOBody.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 "WOElement+private.h"
23 #include "WOHTMLDynamicElement.h"
24 #include <NGObjWeb/WOApplication.h>
25 #include <NGObjWeb/WOResourceManager.h>
26 #include "decommon.h"
27
28 @class WOAssociation;
29
30 @interface WOBody : WOHTMLDynamicElement
31 {
32   // WODynamicElement: extraAttributes
33   // WODynamicElement: otherTagString
34 @protected
35   WOAssociation *filename;  // path relative to WebServerResources
36   WOAssociation *framework;
37   WOAssociation *src;       // absolute URL
38   WOAssociation *value;     // image data (eg from a database)
39
40   WOElement *template;
41 }
42
43 @end
44
45 @implementation WOBody
46
47 - (id)initWithName:(NSString *)_name
48   associations:(NSDictionary *)_config
49   template:(WOElement *)_c
50 {
51   if ((self = [super initWithName:_name associations:_config template:_c])) {
52     self->filename  = OWGetProperty(_config, @"filename");
53     self->framework = OWGetProperty(_config, @"framework");
54     self->src       = OWGetProperty(_config, @"src");
55     self->value     = OWGetProperty(_config, @"value");
56     
57     self->template  = [_c retain];
58     
59     if (self->value) NSLog(@"WARNING: value not yet supported !");
60   }
61   return self;
62 }
63
64 - (void)dealloc {
65   [self->template  release];
66   [self->framework release];
67   [self->filename  release];
68   [self->src       release];
69   [self->value     release];
70   [super dealloc];
71 }
72
73 /* accessors */
74
75 - (id)template {
76   return self->template;
77 }
78
79 /* handling requests */
80
81 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
82   [self->template takeValuesFromRequest:_req inContext:_ctx];
83 }
84 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
85   return [self->template invokeActionForRequest:_req inContext:_ctx];
86 }
87
88 /* generating response */
89
90 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
91   NSString *uUri;
92   NSString *uFi;
93   
94   if ([[_ctx request] isFromClientComponent]) {
95     [self->template appendToResponse:_response inContext:_ctx];
96     return;
97   }
98   
99   uUri = [self->src      stringValueInComponent:[_ctx component]];
100   uFi  = [self->filename stringValueInComponent:[_ctx component]];
101   
102   WOResponse_AddCString(_response, "<body");
103   
104   if ([uFi length] > 0) {
105     NSArray *languages;
106     WOResourceManager *rm;
107     NSString  *frameworkName;
108
109     WOResponse_AddCString(_response, " background=\"");
110       
111     languages = [_ctx resourceLookupLanguages];
112       
113     if ((rm = [[_ctx component] resourceManager]) == nil)
114       rm = [[_ctx application] resourceManager];
115       
116     /* If 'framework' binding is not set, use parent component's framework */
117     if (self->framework){
118       frameworkName = [self->framework stringValueInComponent:[_ctx component]];
119       if (frameworkName != nil && [frameworkName isEqualToString:@"app"])
120         frameworkName = nil;
121     }
122     else
123       frameworkName = [[_ctx component] frameworkName];
124     
125     uFi = [rm urlForResourceNamed:uFi
126               inFramework:frameworkName
127               languages:languages
128               request:[_ctx request]];
129     if (uFi == nil) {
130       NSLog(@"%@: did not find resource '%@' (languages=%@)",
131             [_ctx component],
132             [self->filename stringValueInComponent:[_ctx component]],
133             [languages componentsJoinedByString:@","]);
134       uFi = uUri;
135     }
136     [_response appendContentHTMLAttributeValue:uFi];
137     WOResponse_AddChar(_response, '"');
138   }
139   else if ([uUri length] > 0) {
140     WOResponse_AddCString(_response, " background=\"");
141     [_response appendContentHTMLAttributeValue:uUri];
142     WOResponse_AddChar(_response, '"');
143   }
144   
145   [self appendExtraAttributesToResponse:_response inContext:_ctx];
146   
147   if (self->otherTagString) {
148     WOResponse_AddChar(_response, ' ');
149     WOResponse_AddString(_response,
150                          [self->otherTagString stringValueInComponent:
151                               [_ctx component]]);
152   }
153   WOResponse_AddChar(_response, '>');
154   
155   [self->template appendToResponse:_response inContext:_ctx];
156   
157   WOResponse_AddCString(_response, "</body>");
158 }
159
160 /* description */
161
162 - (NSString *)associationDescription {
163   NSMutableString *str;
164   
165   str = [NSMutableString stringWithCapacity:128];
166   if (self->filename)  [str appendFormat:@" filename=%@",  self->filename];
167   if (self->framework) [str appendFormat:@" framework=%@", self->framework];
168   if (self->src)       [str appendFormat:@" src=%@",       self->src];
169   if (self->value)     [str appendFormat:@" value=%@",     self->value];
170   if (self->template)  [str appendFormat:@" template=%@",  self->template];
171   
172   return str;
173 }
174
175 @end /* WOBody */