]> err.no Git - sope/blob - sope-appserver/NGObjWeb/Associations/WOLabelAssociation.m
use %p for pointer formats, fixed gcc 4.1 warnings, minor code improvs
[sope] / sope-appserver / NGObjWeb / Associations / WOLabelAssociation.m
1 /*
2   Copyright (C) 2004-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 "WOLabelAssociation.h"
23 #include <NGObjWeb/WOApplication.h>
24 #include <NGObjWeb/WOComponent.h>
25 #include <NGObjWeb/WOContext.h>
26 #include <NGObjWeb/WORequest.h>
27 #include <NGObjWeb/WOResourceManager.h>
28 #include <NGObjWeb/WOSession.h>
29 #include "common.h"
30
31 @implementation WOLabelAssociation
32
33 + (int)version {
34   return [super version] /* v2 */;
35 }
36 + (void)initialize {
37   NSAssert2([super version] == 2,
38             @"invalid superclass (%@) version %i !",
39             NSStringFromClass([self superclass]), [super version]);
40 }
41
42 - (id)initWithKey:(NSString *)_key inTable:(NSString *)_table
43   withDefaultValue:(NSString *)_default
44 {
45   if ([_key length] == 0) {
46     [self warnWithFormat:@"missing label key!"];
47     [self release];
48     return nil;
49   }
50   if ((self = [super init])) {
51     if ([_key hasPrefix:@"$"]) {
52       self->flags.isKeyKeyPath = 1;
53       _key = [_key substringFromIndex:1];
54     }
55     if ([_table hasPrefix:@"$"]) {
56       self->flags.isTableKeyPath = 1;
57       _table = [_table substringFromIndex:1];
58     }
59     if ([_default hasPrefix:@"$"]) {
60       self->flags.isValueKeyPath = 1;
61       _default = [_default substringFromIndex:1];
62     }
63     
64     self->key          = [_key     copy];
65     self->table        = [_table   copy];
66     self->defaultValue = [_default copy];
67   }
68   return self;
69 }
70 - (id)init {
71   return [self initWithKey:nil inTable:nil withDefaultValue:nil];
72 }
73
74 - (id)initWithString:(NSString *)_str {
75   NSString *lKey, *lTable, *lVal;
76   NSRange r;
77   
78   if ([_str length] == 0) {
79     [self release];
80     return nil;
81   }
82   
83   r = [_str rangeOfString:@"/"];
84   if (r.length > 0) {
85     lTable = [_str substringToIndex:r.location];
86     lKey   = [_str substringFromIndex:(r.location + r.length)];
87   }
88   else {
89     lTable = nil;
90     lKey   = _str;
91   }
92   lVal = lKey;
93   
94   return [self initWithKey:lKey inTable:lTable withDefaultValue:lVal];
95 }
96
97 - (void)dealloc {
98   [self->table        release];
99   [self->defaultValue release];
100   [self->key          release];
101   [super dealloc];
102 }
103
104 /* value */
105
106 - (void)setValue:(id)_value inComponent:(WOComponent *)_component {
107   // not settable
108   [NSException raise:@"AssociationException"
109                format:@"association value is not settable !"];
110 }
111 - (id)valueInComponent:(WOComponent *)_component {
112   WOResourceManager *rm;
113   NSArray           *languages;
114   WOContext         *ctx;
115   NSString          *label;
116   NSString          *lKey, *lTable, *lVal;
117
118   /* lookup languages */
119   
120   ctx       = [_component context];
121   languages = [ctx resourceLookupLanguages];
122
123   /* find resource manager */
124   
125   if ((rm = [_component resourceManager]) == nil)
126     rm = [[WOApplication application] resourceManager];
127   if (rm == nil)
128     [self warnWithFormat:@"missing resource manager!"];
129
130   /* get parameters */
131   
132   lKey   = self->key;
133   lTable = self->table;
134   lVal   = self->defaultValue;
135   if (self->flags.isKeyKeyPath)   lKey   = [_component valueForKeyPath:lKey];
136   if (self->flags.isTableKeyPath) lTable = [_component valueForKeyPath:lTable];
137   if (self->flags.isValueKeyPath) lVal   = [_component valueForKeyPath:lVal];
138
139   /* lookup string */
140   
141   label = [rm stringForKey:lKey inTableNamed:lTable withDefaultValue:lVal
142               languages:languages];
143   return label;
144 }
145
146 - (BOOL)isValueConstant {
147   return NO;
148 }
149 - (BOOL)isValueSettable {
150   return NO;
151 }
152
153 /* NSCopying */
154
155 - (id)copyWithZone:(NSZone *)_zone {
156   return [self retain];
157 }
158
159 /* description */
160
161 - (NSString *)description {
162   NSMutableString *str;
163
164   str = [NSMutableString stringWithCapacity:64];
165   [str appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];
166
167   if (self->flags.isKeyKeyPath)
168     [str appendFormat:@" key=%@",   self->key];
169   else
170     [str appendFormat:@" key='%@'", self->key];
171
172   if (self->flags.isTableKeyPath)
173     [str appendFormat:@" table=%@",   self->table];
174   else
175     [str appendFormat:@" table='%@'", self->table];
176
177   if (self->flags.isValueKeyPath)
178     [str appendFormat:@" def=%@",   self->defaultValue];
179   else
180     [str appendFormat:@" def='%@'", self->defaultValue];
181   
182   [str appendString:@">"];
183   return str;
184 }
185
186 @end /* WOLabelAssociation */