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
24 @interface _WOTemporaryImage : NSObject
27 @interface _WODynamicImage : WOImage /* new in WO4 */
30 WOAssociation *mimeType;
35 @interface _WOElementImage : WOImage
37 WOAssociation *value; // image data (eg from a database)
41 @interface _WOExternalImage : WOImage
43 WOAssociation *src; // absolute URL
47 @interface WOImage(PrivateMethods)
49 - (NSString *)associationDescription;
53 #include "WOElement+private.h"
54 #include <NGObjWeb/WOApplication.h>
55 #include <NGObjWeb/WOResourceManager.h>
58 #if NeXT_Foundation_LIBRARY || APPLE_FOUNDATION_LIBRARY
59 @interface NSObject(Miss)
60 - (void)subclassResponsibility:(SEL)cmd;
64 @implementation WOImage
66 + (id)allocWithZone:(NSZone *)zone {
67 static Class WOImageClass = Nil;
68 static _WOTemporaryImage *temporaryImage = nil;
70 if (WOImageClass == Nil)
71 WOImageClass = [WOImage class];
72 if (temporaryImage == nil)
73 temporaryImage = [_WOTemporaryImage allocWithZone:zone];
75 return (self == WOImageClass)
77 : NSAllocateObject(self, 0, zone);
80 /* request handling */
82 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
83 NSLog(@"no value configured for WOImage %@", self);
87 - (void)_appendSrcToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
88 [self subclassResponsibility:_cmd];
91 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
92 if ([[_ctx request] isFromClientComponent])
95 WOResponse_AddCString(_response, "<img src=\"");
97 #if DEBUG && USE_EXCEPTION_HANDLER
99 [self _appendSrcToResponse:_response inContext:_ctx];
102 fprintf(stderr, "exception in %s: %s\n",
103 [[self description] cString],
104 [[localException description] cString]);
105 [localException raise];
109 [self _appendSrcToResponse:_response inContext:_ctx];
112 WOResponse_AddChar(_response, '"');
114 [self appendExtraAttributesToResponse:_response inContext:_ctx];
116 if (self->otherTagString) {
117 WOResponse_AddChar(_response, ' ');
118 WOResponse_AddString(_response,
119 [self->otherTagString stringValueInComponent:
123 WOResponse_AddEmptyCloseParens(_response, _ctx);
128 @implementation _WODynamicImage
130 - (id)initWithName:(NSString *)_name
131 associations:(NSDictionary *)_config
132 template:(WOElement *)_t
134 if ((self = [super initWithName:_name associations:_config template:_t])) {
135 self->data = OWGetProperty(_config, @"data");
136 self->mimeType = OWGetProperty(_config, @"mimeType");
137 self->key = OWGetProperty(_config, @"key");
140 if ([_config objectForKey:@"value"] ||
141 [_config objectForKey:@"filename"] ||
142 [_config objectForKey:@"framework"] ||
143 [_config objectForKey:@"src"]) {
144 NSLog(@"WARNING: inconsistent association settings in WOImage !"
145 @" (assign only one of value, src, data or filename)");
154 [self->data release];
155 [self->mimeType release];
159 /* dynamic delivery */
161 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
162 WOComponent *sComponent = [_ctx component];
165 WOResponse *response;
167 adata = [self->data valueInComponent:sComponent];
168 atype = [self->mimeType stringValueInComponent:sComponent];
170 response = [_ctx response];
172 [response setContent:adata];
173 [response setHeader:atype ? atype : @"application/octet-stream"
174 forKey:@"content-type"];
179 /* HTML generation */
181 - (void)_appendSrcToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
182 WOResourceManager *rm;
183 WOComponent *sComponent;
186 sComponent = [_ctx component];
188 if ((kk = [self->key stringValueInComponent:sComponent]) == nil) {
189 WOResponse_AddString(_resp, [_ctx componentActionURL]);
193 if ((rm = [[_ctx component] resourceManager]) == nil)
194 rm = [[_ctx application] resourceManager];
196 [rm setData:[self->data valueInComponent:sComponent] forKey:kk
197 mimeType:[self->mimeType stringValueInComponent:sComponent]
198 session:[_ctx hasSession] ? [_ctx session] : nil];
200 url = [_ctx urlWithRequestHandlerKey:
201 [WOApplication resourceRequestHandlerKey]
202 path:[@"/" stringByAppendingString:kk]
205 WOResponse_AddString(_resp, url);
210 - (NSString *)associationDescription {
211 NSMutableString *str = [NSMutableString stringWithCapacity:64];
212 if (self->data) [str appendFormat:@" data=%@", self->data];
213 if (self->mimeType) [str appendFormat:@" mimeType=%@", self->mimeType];
214 if (self->key) [str appendFormat:@" key=%@", self->key];
215 [str appendString:[super associationDescription]];
219 @end /* _WODynamicImage */
221 @implementation _WOElementImage
223 - (id)initWithName:(NSString *)_name
224 associations:(NSDictionary *)_config
225 template:(WOElement *)_t
227 if ((self = [super initWithName:_name associations:_config template:_t])) {
228 self->value = OWGetProperty(_config, @"value");
231 if ([_config objectForKey:@"data"] ||
232 [_config objectForKey:@"mimeType"] ||
233 [_config objectForKey:@"key"] ||
234 [_config objectForKey:@"filename"] ||
235 [_config objectForKey:@"framework"] ||
236 [_config objectForKey:@"src"]) {
237 NSLog(@"WARNING: inconsistent association settings in WOImage !"
238 @" (assign only one of value, src, data or filename)");
246 RELEASE(self->value);
250 /* dynamic delivery */
252 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
255 if ((element = [self->value valueInComponent:[_ctx component]]) == nil) {
256 NSLog(@"WARNING: missing element value for WOImage %@", self);
260 [element appendToResponse:[_ctx response] inContext:_ctx];
261 return [_ctx response];
264 /* HTML generation */
266 - (void)_appendSrcToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
267 WOResponse_AddString(_resp, [_ctx componentActionURL]);
272 - (NSString *)associationDescription {
273 NSMutableString *str = [NSMutableString stringWithCapacity:64];
275 [str appendFormat:@" value=%@", self->value];
276 [str appendString:[super associationDescription]];
280 @end /* _WOElementImage */
282 @implementation _WOExternalImage
284 - (id)initWithName:(NSString *)_name
285 associations:(NSDictionary *)_config
286 template:(WOElement *)_t
288 if ((self = [super initWithName:_name associations:_config template:_t])) {
289 self->src = OWGetProperty(_config, @"src");
292 if ([_config objectForKey:@"data"] ||
293 [_config objectForKey:@"mimeType"] ||
294 [_config objectForKey:@"key"] ||
295 [_config objectForKey:@"filename"] ||
296 [_config objectForKey:@"framework"] ||
297 [_config objectForKey:@"value"]) {
298 NSLog(@"WARNING: inconsistent association settings in WOImage !"
299 @" (assign only one of value, src, data or filename)");
311 /* HTML generation */
313 - (void)_appendSrcToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
316 s = [self->src stringValueInComponent:[_ctx component]];
317 if (s != nil) [_resp appendContentHTMLAttributeValue:s];
322 - (NSString *)associationDescription {
323 NSMutableString *str;
325 str = [NSMutableString stringWithCapacity:64];
326 [str appendFormat:@" src=%@", self->src];
327 [str appendString:[super associationDescription]];
331 @end /* _WOExternalImage */
333 @implementation _WOTemporaryImage
335 - (id)initWithName:(NSString *)_n
336 associations:(NSDictionary *)_config
337 template:(WOElement *)_t
339 // TODO: cache class objects?
340 Class imageClass = Nil;
343 if ((a = [_config objectForKey:@"filename"])) {
344 if ([a isValueConstant] && [_config objectForKey:@"framework"] == nil)
345 imageClass = NSClassFromString(@"_WOConstResourceImage");
347 imageClass = NSClassFromString(@"_WOResourceImage");
349 else if ([_config objectForKey:@"src"])
350 imageClass = [_WOExternalImage class];
351 else if ([_config objectForKey:@"value"])
352 imageClass = [_WOElementImage class];
353 else if ([_config objectForKey:@"data"])
354 imageClass = [_WODynamicImage class];
356 NSLog(@"WARNING: missing data source association for WOImage !");
359 return [[imageClass alloc] initWithName:_n associations:_config template:_t];
362 - (id)initWithName:(NSString *)_name
363 associations:(NSDictionary *)_associations
364 contentElements:(NSArray *)_contents
367 Class imageClass = Nil;
369 if ((a = [_associations objectForKey:@"filename"])) {
370 if ([a isValueConstant] && [_associations objectForKey:@"framework"]==nil)
371 imageClass = NSClassFromString(@"_WOConstResourceImage");
373 imageClass = NSClassFromString(@"_WOResourceImage");
375 else if ([_associations objectForKey:@"src"])
376 imageClass = [_WOExternalImage class];
377 else if ([_associations objectForKey:@"value"])
378 imageClass = [_WOElementImage class];
379 else if ([_associations objectForKey:@"data"])
380 imageClass = [_WODynamicImage class];
382 NSLog(@"WARNING: missing data source association for WOImage !");
385 return [[imageClass alloc] initWithName:_name
386 associations:_associations
387 contentElements:_contents];
391 [self errorWithFormat:@"called dealloc on %@", self];
398 // make Tiger GCC happy
403 @end /* _WOTemporaryImage */