File: WOImage.m

package info (click to toggle)
sope 5.12.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,588 kB
  • sloc: objc: 169,223; sh: 3,823; ansic: 3,409; javascript: 446; python: 318; makefile: 185
file content (402 lines) | stat: -rw-r--r-- 11,070 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
  Copyright (C) 2000-2005 SKYRIX Software AG

  This file is part of SOPE.

  SOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with SOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

#include "WOImage.h"

@interface _WOTemporaryImage : NSObject
@end

@interface _WODynamicImage : WOImage /* new in WO4 */
{
  WOAssociation *data;
  WOAssociation *mimeType;
  WOAssociation *key;
}
@end

@interface _WOElementImage : WOImage
{
  WOAssociation *value;     // image data (eg from a database)
}
@end

@interface _WOExternalImage : WOImage
{
  WOAssociation *src;       // absolute URL
}
@end

@interface WOImage(PrivateMethods)

- (NSString *)associationDescription;

@end

#include "WOElement+private.h"
#include <NGObjWeb/WOApplication.h>
#include <NGObjWeb/WOResourceManager.h>
#include "decommon.h"

#if NeXT_Foundation_LIBRARY || APPLE_FOUNDATION_LIBRARY
@interface NSObject(Miss)
- (void)subclassResponsibility:(SEL)cmd;
@end
#endif

@implementation WOImage

+ (id)allocWithZone:(NSZone *)zone {
  static Class WOImageClass = Nil;
  static _WOTemporaryImage *temporaryImage = nil;
  
  if (WOImageClass == Nil)
    WOImageClass = [WOImage class];
  if (temporaryImage == nil)
    temporaryImage = [_WOTemporaryImage allocWithZone:zone];
  
  return (self == WOImageClass)
    ? (id)temporaryImage
    : (id)NSAllocateObject(self, 0, zone);
}

/* request handling */

- (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
  NSLog(@"no value configured for WOImage %@", self);
  return nil;
}

- (void)_appendSrcToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
  [self subclassResponsibility:_cmd];
}

- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
  if ([_ctx isRenderingDisabled] || [[_ctx request] isFromClientComponent])
    return;

  WOResponse_AddBytesLen(_response, "<img src=\"", 10);
  
#if DEBUG && USE_EXCEPTION_HANDLER
  NS_DURING {
    [self _appendSrcToResponse:_response inContext:_ctx];
  }
  NS_HANDLER {
    fprintf(stderr, "exception in %s: %s\n",
            [[self description] cString],
            [[localException description] cString]);
    [localException raise];
  }
  NS_ENDHANDLER;
#else
  [self _appendSrcToResponse:_response inContext:_ctx];
#endif
  
  WOResponse_AddBytesLen(_response, "\"", 1);
  
  [self appendExtraAttributesToResponse:_response inContext:_ctx];
    
  if (self->otherTagString) {
    WOResponse_AddBytesLen(_response, " ", 1);
    WOResponse_AddString(_response,
                         [self->otherTagString stringValueInComponent:
                              [_ctx component]]);
  }
  
  WOResponse_AddEmptyCloseParens(_response, _ctx);
}

@end /* WOImage */

@implementation _WODynamicImage

- (id)initWithName:(NSString *)_name
  associations:(NSDictionary *)_config
  template:(WOElement *)_t
{
  if ((self = [super initWithName:_name associations:_config template:_t])) {
    self->data     = OWGetProperty(_config, @"data");
    self->mimeType = OWGetProperty(_config, @"mimeType");
    self->key      = OWGetProperty(_config, @"key");
    
#if DEBUG
    if ([_config objectForKey:@"value"]     ||
        [_config objectForKey:@"filename"]  ||
        [_config objectForKey:@"framework"] ||
        [_config objectForKey:@"src"]) {
      NSLog(@"WARNING: inconsistent association settings in WOImage !"
            @" (assign only one of value, src, data or filename)");
    }
#endif
  }
  return self;
}

- (void)dealloc {
  [self->key      release];
  [self->data     release];
  [self->mimeType release];
  [super dealloc];
}

/* dynamic delivery */

- (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
  WOComponent *sComponent = [_ctx component];
  NSData     *adata;
  NSString   *atype;
  WOResponse *response;

  adata = [self->data     valueInComponent:sComponent];
  atype = [self->mimeType stringValueInComponent:sComponent];

  response = [_ctx response];
    
  [response setContent:adata];
  [response setHeader:
	      (atype != nil ? atype : (NSString *)@"application/octet-stream")
            forKey:@"content-type"];
    
  return response;
}

/* HTML generation */

- (void)_appendSrcToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
  WOResourceManager *rm;
  WOComponent *sComponent;
  NSString *kk, *url;

  sComponent = [_ctx component];
  
  if ((kk = [self->key stringValueInComponent:sComponent]) == nil) {
    WOResponse_AddString(_resp, [_ctx componentActionURL]);
    return;
  }

  if ((rm = [[_ctx component] resourceManager]) == nil)
    rm = [[_ctx application] resourceManager];
    
  [rm setData:[self->data valueInComponent:sComponent] forKey:kk
      mimeType:[self->mimeType stringValueInComponent:sComponent]
      session:[_ctx hasSession] ? [_ctx session] : nil];
    
  url = [_ctx urlWithRequestHandlerKey:
                [WOApplication resourceRequestHandlerKey]
              path:[@"/" stringByAppendingString:kk]
              queryString:nil];
    
  WOResponse_AddString(_resp, url);
}

/* description */

- (NSString *)associationDescription {
  NSMutableString *str = [NSMutableString stringWithCapacity:64];
  if (self->data)      [str appendFormat:@" data=%@",      self->data];
  if (self->mimeType)  [str appendFormat:@" mimeType=%@",  self->mimeType];
  if (self->key)       [str appendFormat:@" key=%@",       self->key];
  [str appendString:[super associationDescription]];
  return str;
}

@end /* _WODynamicImage */

@implementation _WOElementImage

- (id)initWithName:(NSString *)_name
  associations:(NSDictionary *)_config
  template:(WOElement *)_t
{
  if ((self = [super initWithName:_name associations:_config template:_t])) {
    self->value = OWGetProperty(_config, @"value");

#if DEBUG
    if ([_config objectForKey:@"data"]      ||
        [_config objectForKey:@"mimeType"]  ||
        [_config objectForKey:@"key"]       ||
        [_config objectForKey:@"filename"]  ||
        [_config objectForKey:@"framework"] ||
        [_config objectForKey:@"src"]) {
      NSLog(@"WARNING: inconsistent association settings in WOImage !"
            @" (assign only one of value, src, data or filename)");
    }
#endif
  }
  return self;
}

- (void)dealloc {
  RELEASE(self->value);
  [super dealloc];
}

/* dynamic delivery */

- (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
  WOElement *element;
  
  if ((element = [self->value valueInComponent:[_ctx component]]) == nil) {
    NSLog(@"WARNING: missing element value for WOImage %@", self);
    return nil;
  }

  [element appendToResponse:[_ctx response] inContext:_ctx];
  return [_ctx response];
}

/* HTML generation */

- (void)_appendSrcToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
  WOResponse_AddString(_resp, [_ctx componentActionURL]);
}

/* description */

- (NSString *)associationDescription {
  NSMutableString *str = [NSMutableString stringWithCapacity:64];

  [str appendFormat:@" value=%@", self->value];
  [str appendString:[super associationDescription]];
  return str;
}

@end /* _WOElementImage */

@implementation _WOExternalImage

- (id)initWithName:(NSString *)_name
  associations:(NSDictionary *)_config
  template:(WOElement *)_t
{
  if ((self = [super initWithName:_name associations:_config template:_t])) {
    self->src = OWGetProperty(_config, @"src");

#if DEBUG
    if ([_config objectForKey:@"data"]      ||
        [_config objectForKey:@"mimeType"]  ||
        [_config objectForKey:@"key"]       ||
        [_config objectForKey:@"filename"]  ||
        [_config objectForKey:@"framework"] ||
        [_config objectForKey:@"value"]) {
      NSLog(@"WARNING: inconsistent association settings in WOImage !"
            @" (assign only one of value, src, data or filename)");
    }
#endif
  }
  return self;
}

- (void)dealloc {
  [self->src release];
  [super dealloc];
}

/* HTML generation */

- (void)_appendSrcToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
  NSString *s;
  
  s = [self->src stringValueInComponent:[_ctx component]];
  if (s != nil) [_resp appendContentHTMLAttributeValue:s];
}

/* description */

- (NSString *)associationDescription {
  NSMutableString *str;
  
  str = [NSMutableString stringWithCapacity:64];
  [str appendFormat:@" src=%@", self->src];
  [str appendString:[super associationDescription]];
  return str;
}

@end /* _WOExternalImage */

@implementation _WOTemporaryImage

- (id)initWithName:(NSString *)_n
  associations:(NSDictionary *)_config
  template:(WOElement *)_t
{
  // TODO: cache class objects?
  Class imageClass = Nil;
  WOAssociation *a;
  
  if ((a = [_config objectForKey:@"filename"])) {
    if ([a isValueConstant] && [_config objectForKey:@"framework"] == nil)
      imageClass = NSClassFromString(@"_WOConstResourceImage");
    else
      imageClass = NSClassFromString(@"_WOResourceImage");
  }
  else if ([_config objectForKey:@"src"])
    imageClass = [_WOExternalImage class];
  else if ([_config objectForKey:@"value"])
    imageClass = [_WOElementImage class];
  else if ([_config objectForKey:@"data"])
    imageClass = [_WODynamicImage class];
  else {
    NSLog(@"WARNING: missing data source association for WOImage !");
  }
  
  return [[imageClass alloc] initWithName:_n associations:_config template:_t];
}

- (id)initWithName:(NSString *)_name
  associations:(NSDictionary *)_associations
  contentElements:(NSArray *)_contents
{
  WOAssociation *a;
  Class imageClass = Nil;
  
  if ((a = [_associations objectForKey:@"filename"])) {
    if ([a isValueConstant] && [_associations objectForKey:@"framework"]==nil)
      imageClass = NSClassFromString(@"_WOConstResourceImage");
    else
      imageClass = NSClassFromString(@"_WOResourceImage");
  }
  else if ([_associations objectForKey:@"src"])
    imageClass = [_WOExternalImage class];
  else if ([_associations objectForKey:@"value"])
    imageClass = [_WOElementImage class];
  else if ([_associations objectForKey:@"data"])
    imageClass = [_WODynamicImage class];
  else {
    NSLog(@"WARNING: missing data source association for WOImage !");
  }
  
  return [[imageClass alloc] initWithName:_name
                             associations:_associations
                             contentElements:_contents];
}

- (void)dealloc {
  [self errorWithFormat:@"called dealloc on %@", self];
#if DEBUG
  abort();
#endif
  return;
  
  // same issue with gcc 4.1 on Linux ..., make Tiger GCC happy
  if (0) [super dealloc];
}

@end /* _WOTemporaryImage */