]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/_WOConstResourceImage.m
fixed bundle resource lookup on MacOSX, changed resource lookup in
[sope] / sope-appserver / NGObjWeb / DynamicElements / _WOConstResourceImage.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 "WOImage.h"
23
24 @interface _WOConstResourceImage : WOImage
25 {
26   NSString *filename;  // path relative to WebServerResources
27 }
28 @end
29
30 #include "WOElement+private.h"
31 #include <NGObjWeb/WOApplication.h>
32 #include <NGObjWeb/WOResourceManager.h>
33 #include "common.h"
34
35 @implementation _WOConstResourceImage
36
37 static BOOL debugOn = NO;
38
39 - (id)initWithName:(NSString *)_name
40   associations:(NSDictionary *)_config
41   template:(WOElement *)_t
42 {
43   if ((self = [super initWithName:_name associations:_config template:_t])) {
44     WOAssociation *tmp;
45
46     tmp = OWGetProperty(_config, @"filename");
47     self->filename  = [[tmp stringValueInComponent:nil] copy];
48     [tmp release];
49     
50     if (self->filename == nil) {
51       NSLog(@"missing filename association for WOImage ..");
52       [self release];
53       return nil;
54     }
55
56 #if DEBUG
57     if ([_config objectForKey:@"value"]    ||
58         [_config objectForKey:@"data"]     ||
59         [_config objectForKey:@"mimeType"] ||
60         [_config objectForKey:@"key"]      ||
61         [_config objectForKey:@"src"]) {
62       NSLog(@"WARNING: inconsistent association settings in WOImage !"
63             @" (assign only one of value, src, data or filename)");
64     }
65 #endif
66   }
67   return self;
68 }
69
70 - (void)dealloc {
71   [self->filename release];
72   [super dealloc];
73 }
74
75 /* HTML generation */
76
77 - (void)_appendSrcToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
78   WOComponent *sComponent;
79   NSString    *uFi;
80   NSArray     *languages;
81   WOResourceManager *rm;
82   NSString    *frameworkName;
83   
84   sComponent = [_ctx component];
85   
86   if (self->filename == nil) {
87     [_resp appendContentHTMLAttributeValue:
88              @"/missingImage?reason=nilfilenamebinding"];
89     [self debugWithFormat:@"missing 'filename' binding value ..."];
90     return;
91   }
92   
93   /* this has no measurable effect on performance ... */
94   if ((rm = [[_ctx component] resourceManager]) == nil)
95     rm = [[_ctx application] resourceManager];
96   
97   languages = [_ctx resourceLookupLanguages];
98
99   if (debugOn) {
100     [self debugWithFormat:@"WOImage: filename '%@'\n  rm %@\n  languages %@",
101             self->filename, rm, [languages componentsJoinedByString:@","]];
102   }
103   
104   /* 
105      'framework' binding is not set in that case -> use parent component's 
106      framework 
107   */
108   frameworkName = [[_ctx component] frameworkName];
109   uFi = [rm urlForResourceNamed:self->filename
110             inFramework:frameworkName 
111             languages:languages
112             request:[_ctx request]];
113   if (uFi == nil) {
114     [self logWithFormat:
115             @"%@: did not find resource '%@'", sComponent, self->filename];
116     uFi = [@"/missingImage?" stringByAppendingString:self->filename];
117   }
118   
119   if (debugOn)
120     [self debugWithFormat:@"WOImage: constant resource URL: '%@'", uFi];
121   
122   [_resp appendContentHTMLAttributeValue:uFi];
123 }
124
125 /* debugging */
126
127 - (BOOL)isDebuggingEnabled {
128   return debugOn;
129 }
130
131 /* description */
132
133 - (NSString *)associationDescription {
134   NSMutableString *str = [NSMutableString stringWithCapacity:64];
135   
136   [str appendFormat:@" filename=%@", self->filename];
137   [str appendString:[super associationDescription]];
138   return str;
139 }
140
141 @end /* _WOConstResourceImage */