2 Copyright (C) 2004-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
22 #include "WOKeyPathAssociation.h"
23 #include <NGObjWeb/WOComponent.h>
24 #include "NSObject+WO.h"
28 WOKeyPathAssociationSystemKVC
30 This is an WOKeyPathAssociation subclass which uses the KVC methods
31 provided in the Foundation library. This is much less performant, but
32 much more compatible with WebObjects.
34 This could be further optimized by doing method caching, using shared
35 objects, etc. Yet if you need speed, you should use the default association.
38 @interface WOKeyPathAssociationSystemKVC : WOKeyPathAssociation
40 NSString *keyPathString;
46 @implementation WOKeyPathAssociationSystemKVC
48 static NSNumber *yesNum = nil;
49 static NSNumber *noNum = nil;
52 return [super version] + 0; /* v2 */
55 if (yesNum == nil) yesNum = [[NSNumber numberWithBool:YES] retain];
56 if (noNum == nil) noNum = [[NSNumber numberWithBool:NO] retain];
59 - (id)initWithKeyPath:(NSString *)_keyPath {
60 if ((self = [super initWithKeyPath:_keyPath])) {
61 self->hasCaretPrefix =
62 (([_keyPath length] > 1) && [_keyPath hasPrefix:@"^"]) ? YES : NO;
64 if (self->hasCaretPrefix)
65 self->keyPathString = [[_keyPath substringFromIndex:1] copy];
67 self->keyPathString = [_keyPath copy];
73 [self->keyPathString release];
79 - (NSString *)keyPath {
80 return self->keyPathString;
85 - (void)setValue:(id)_value inComponent:(WOComponent *)_component {
86 if (self->hasCaretPrefix)
87 [_component setValue:_value forBinding:self->keyPathString];
89 [_component takeValue:_value forKeyPath:self->keyPathString];
91 - (id)valueInComponent:(WOComponent *)_component {
92 return (self->hasCaretPrefix)
93 ? [_component valueForBinding:self->keyPathString]
94 : [_component valueForKeyPath:self->keyPathString];
97 - (BOOL)isValueConstant {
100 - (BOOL)isValueSettable {
106 - (void)setUnsignedIntValue:(unsigned int)_v inComponent:(WOComponent *)_wo {
107 [self setValue:[NSNumber numberWithUnsignedInt:_v] inComponent:_wo];
109 - (unsigned int)unsignedIntValueInComponent:(WOComponent *)_component {
110 return [[self valueInComponent:_component] unsignedIntValue];
113 - (void)setIntValue:(int)_value inComponent:(WOComponent *)_wo {
114 [self setValue:[NSNumber numberWithInt:_value] inComponent:_wo];
116 - (int)intValueInComponent:(WOComponent *)_component {
117 return [[self valueInComponent:_component] intValue];
120 - (void)setBoolValue:(BOOL)_value inComponent:(WOComponent *)_wo {
121 [self setValue:(_value ? yesNum : noNum) inComponent:_wo];
123 - (BOOL)boolValueInComponent:(WOComponent *)_component {
124 /* some optimizations, very likely that same objects are used for YES|NO */
127 if ((o = [self valueInComponent:_component]) == nil)
129 if (o == yesNum) return YES;
130 if (o == noNum) return NO;
131 return [o boolValue];
134 - (void)setStringValue:(NSString *)_value inComponent:(WOComponent *)_wo {
135 [self setValue:_value inComponent:_wo];
137 - (NSString *)stringValueInComponent:(WOComponent *)_component {
138 return [[self valueInComponent:_component] stringValue];
141 @end /* WOKeyPathAssociationSystemKVC */