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 : (id)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];
174 (atype != nil ? atype : (NSString *)@"application/octet-stream")
175 forKey:@"content-type"];
180 /* HTML generation */
182 - (void)_appendSrcToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
183 WOResourceManager *rm;
184 WOComponent *sComponent;
187 sComponent = [_ctx component];
189 if ((kk = [self->key stringValueInComponent:sComponent]) == nil) {
190 WOResponse_AddString(_resp, [_ctx componentActionURL]);
194 if ((rm = [[_ctx component] resourceManager]) == nil)
195 rm = [[_ctx application] resourceManager];
197 [rm setData:[self->data valueInComponent:sComponent] forKey:kk
198 mimeType:[self->mimeType stringValueInComponent:sComponent]
199 session:[_ctx hasSession] ? [_ctx session] : nil];
201 url = [_ctx urlWithRequestHandlerKey:
202 [WOApplication resourceRequestHandlerKey]
203 path:[@"/" stringByAppendingString:kk]
206 WOResponse_AddString(_resp, url);
211 - (NSString *)associationDescription {
212 NSMutableString *str = [NSMutableString stringWithCapacity:64];
213 if (self->data) [str appendFormat:@" data=%@", self->data];
214 if (self->mimeType) [str appendFormat:@" mimeType=%@", self->mimeType];
215 if (self->key) [str appendFormat:@" key=%@", self->key];
216 [str appendString:[super associationDescription]];
220 @end /* _WODynamicImage */
222 @implementation _WOElementImage
224 - (id)initWithName:(NSString *)_name
225 associations:(NSDictionary *)_config
226 template:(WOElement *)_t
228 if ((self = [super initWithName:_name associations:_config template:_t])) {
229 self->value = OWGetProperty(_config, @"value");
232 if ([_config objectForKey:@"data"] ||
233 [_config objectForKey:@"mimeType"] ||
234 [_config objectForKey:@"key"] ||
235 [_config objectForKey:@"filename"] ||
236 [_config objectForKey:@"framework"] ||
237 [_config objectForKey:@"src"]) {
238 NSLog(@"WARNING: inconsistent association settings in WOImage !"
239 @" (assign only one of value, src, data or filename)");
247 RELEASE(self->value);
251 /* dynamic delivery */
253 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
256 if ((element = [self->value valueInComponent:[_ctx component]]) == nil) {
257 NSLog(@"WARNING: missing element value for WOImage %@", self);
261 [element appendToResponse:[_ctx response] inContext:_ctx];
262 return [_ctx response];
265 /* HTML generation */
267 - (void)_appendSrcToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
268 WOResponse_AddString(_resp, [_ctx componentActionURL]);
273 - (NSString *)associationDescription {
274 NSMutableString *str = [NSMutableString stringWithCapacity:64];
276 [str appendFormat:@" value=%@", self->value];
277 [str appendString:[super associationDescription]];
281 @end /* _WOElementImage */
283 @implementation _WOExternalImage
285 - (id)initWithName:(NSString *)_name
286 associations:(NSDictionary *)_config
287 template:(WOElement *)_t
289 if ((self = [super initWithName:_name associations:_config template:_t])) {
290 self->src = OWGetProperty(_config, @"src");
293 if ([_config objectForKey:@"data"] ||
294 [_config objectForKey:@"mimeType"] ||
295 [_config objectForKey:@"key"] ||
296 [_config objectForKey:@"filename"] ||
297 [_config objectForKey:@"framework"] ||
298 [_config objectForKey:@"value"]) {
299 NSLog(@"WARNING: inconsistent association settings in WOImage !"
300 @" (assign only one of value, src, data or filename)");
312 /* HTML generation */
314 - (void)_appendSrcToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
317 s = [self->src stringValueInComponent:[_ctx component]];
318 if (s != nil) [_resp appendContentHTMLAttributeValue:s];
323 - (NSString *)associationDescription {
324 NSMutableString *str;
326 str = [NSMutableString stringWithCapacity:64];
327 [str appendFormat:@" src=%@", self->src];
328 [str appendString:[super associationDescription]];
332 @end /* _WOExternalImage */
334 @implementation _WOTemporaryImage
336 - (id)initWithName:(NSString *)_n
337 associations:(NSDictionary *)_config
338 template:(WOElement *)_t
340 // TODO: cache class objects?
341 Class imageClass = Nil;
344 if ((a = [_config objectForKey:@"filename"])) {
345 if ([a isValueConstant] && [_config objectForKey:@"framework"] == nil)
346 imageClass = NSClassFromString(@"_WOConstResourceImage");
348 imageClass = NSClassFromString(@"_WOResourceImage");
350 else if ([_config objectForKey:@"src"])
351 imageClass = [_WOExternalImage class];
352 else if ([_config objectForKey:@"value"])
353 imageClass = [_WOElementImage class];
354 else if ([_config objectForKey:@"data"])
355 imageClass = [_WODynamicImage class];
357 NSLog(@"WARNING: missing data source association for WOImage !");
360 return [[imageClass alloc] initWithName:_n associations:_config template:_t];
363 - (id)initWithName:(NSString *)_name
364 associations:(NSDictionary *)_associations
365 contentElements:(NSArray *)_contents
368 Class imageClass = Nil;
370 if ((a = [_associations objectForKey:@"filename"])) {
371 if ([a isValueConstant] && [_associations objectForKey:@"framework"]==nil)
372 imageClass = NSClassFromString(@"_WOConstResourceImage");
374 imageClass = NSClassFromString(@"_WOResourceImage");
376 else if ([_associations objectForKey:@"src"])
377 imageClass = [_WOExternalImage class];
378 else if ([_associations objectForKey:@"value"])
379 imageClass = [_WOElementImage class];
380 else if ([_associations objectForKey:@"data"])
381 imageClass = [_WODynamicImage class];
383 NSLog(@"WARNING: missing data source association for WOImage !");
386 return [[imageClass alloc] initWithName:_name
387 associations:_associations
388 contentElements:_contents];
392 [self errorWithFormat:@"called dealloc on %@", self];
398 // same issue with gcc 4.1 on Linux ..., make Tiger GCC happy
399 if (0) [super dealloc];
402 @end /* _WOTemporaryImage */