2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo 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 OGo 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 OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include <EOControl/EOQualifier.h>
26 @interface EOQualifier(EvalContext)
27 - (BOOL)evaluateWithObject:(id)_object inEvalContext:(id)_ctx;
30 @implementation EOOrQualifier
32 //static BOOL debugEval = NO;
33 static BOOL debugTransform = NO;
35 - (id)initWithQualifierArray:(NSArray *)_qualifiers {
36 self->count = [_qualifiers count];
37 self->qualifiers = [_qualifiers copyWithZone:[self zone]];
41 - (id)initWithQualifiers:(EOQualifier *)_qual1, ... {
49 for (c = 0, q = _qual1; q != nil; q = va_arg(va, id), c++)
54 return [self initWithQualifierArray:nil];
56 qs = objc_calloc(c, sizeof(id));
59 for (c = 0, q = _qual1; q != nil; q = va_arg(va, id), c++) {
64 a = [NSArray arrayWithObjects:qs count:c];
67 return [self initWithQualifierArray:a];
71 [self->qualifiers release];
77 - (NSArray *)qualifiers {
78 return self->qualifiers;
80 - (NSArray *)subqualifiers {
81 return [self qualifiers];
86 - (EOQualifier *)qualifierWithBindings:(NSDictionary *)_bindings
87 requiresAllVariables:(BOOL)_reqAll
90 id objects[self->count + 1];
95 objAtIdx = [self->qualifiers methodForSelector:@selector(objectAtIndex:)];
97 for (i = 0; i < self->count; i++) {
100 q = objAtIdx(self->qualifiers, @selector(objectAtIndex:), i);
101 newq = [q qualifierWithBindings:_bindings requiresAllVariables:_reqAll];
102 if (newq == nil) newq = q;
107 array = [NSArray arrayWithObjects:objects count:self->count];
108 return [[[[self class] alloc] initWithQualifierArray:array] autorelease];
111 - (NSArray *)bindingKeys {
112 NSMutableSet *keys = nil;
116 objAtIdx = [self->qualifiers methodForSelector:@selector(objectAtIndex:)];
118 for (i = 0; i < self->count; i++) {
122 q = objAtIdx(self->qualifiers, @selector(objectAtIndex:), i);
123 qb = [q bindingKeys];
125 if ([qb count] > 0) {
126 if (keys == nil) keys = [NSMutableSet setWithCapacity:16];
127 [keys addObjectsFromArray:qb];
131 return keys ? [keys allObjects] : [NSArray array];
136 - (void)addQualifierKeysToSet:(NSMutableSet *)_keys {
138 [self->qualifiers makeObjectsPerformSelector:_cmd withObject:_keys];
143 - (BOOL)evaluateWithObject:(id)_object inEvalContext:(id)_ctx {
147 if ((_ctx == nil) && (self->count > 1))
148 _ctx = [NSMutableDictionary dictionaryWithCapacity:16];
150 objAtIdx = [self->qualifiers methodForSelector:@selector(objectAtIndex:)];
152 for (i = 0; i < self->count; i++) {
155 q = objAtIdx(self->qualifiers, @selector(objectAtIndex:), i);
156 if ([q evaluateWithObject:_object inEvalContext:_ctx])
161 - (BOOL)evaluateWithObject:(id)_object {
162 return [self evaluateWithObject:_object inEvalContext:nil];
167 - (void)encodeWithCoder:(NSCoder *)_coder {
168 [_coder encodeObject:self->qualifiers];
170 - (id)initWithCoder:(NSCoder *)_coder {
171 self->qualifiers = [[_coder decodeObject] retain];
177 - (BOOL)isEqualToQualifier:(EOQualifier *)_qual {
178 return [self->qualifiers isEqualToArray:[(EOOrQualifier *)_qual qualifiers]];
183 - (EOQualifier *)qualifierByApplyingTransformer:(id)_transformer
186 if ([_transformer respondsToSelector:
187 @selector(transformOrQualifier:inContext:)]) {
189 NSLog(@"transformer: %@\n transform: %@", _transformer, self);
190 return [_transformer transformOrQualifier:self inContext:_ctx];
197 BOOL didTransform = NO;
199 if (debugTransform) {
200 NSLog(@"EOOrQualifier: transform %i using %@ ...",
201 self->count, _transformer);
204 qs = calloc(self->count + 1, sizeof(id));
205 for (i = 0; i < self->count; i++) {
208 q = [self->qualifiers objectAtIndex:i];
209 qs[i] = [q qualifierByApplyingTransformer:_transformer inContext:_ctx];
212 else if (qs[i] != q) {
214 NSLog(@"EOOrQualifier: subqualifier %i did transform", i);
217 else if (debugTransform)
218 NSLog(@"EOOrQualifier: subqualifier %i did not transform", i);
221 a = [[NSArray alloc] initWithObjects:qs count:self->count];
223 aq = [[EOOrQualifier alloc] initWithQualifierArray:a];
225 return [aq autorelease];
229 return [[self retain] autorelease];
234 - (EOQualifier *)qualifierByApplyingKeyMap:(NSDictionary *)_map {
240 qs = calloc(self->count + 1, sizeof(id));
241 for (i = 0; i < self->count; i++) {
244 q = [self->qualifiers objectAtIndex:i];
245 qs[i] = [q qualifierByApplyingKeyMap:_map];
246 if (qs[i] == nil) qs[i] = q;
248 a = [[NSArray alloc] initWithObjects:qs count:self->count];
250 aq = [[EOOrQualifier alloc] initWithQualifierArray:a];
252 return [aq autorelease];
257 - (NSString *)description {
262 sd = [self->qualifiers valueForKey:@"qualifierDescription"];
263 if ((len = [sd count]) == 0)
266 return [sd objectAtIndex:0];
268 ms = [NSMutableString stringWithCapacity:(len * 16)];
269 [ms appendString:@"("];
270 for (i = 0; i < len; i++) {
271 if (i != 0) [ms appendString:@" OR "];
272 [ms appendString:[sd objectAtIndex:i]];
274 [ms appendString:@")"];
278 @end /* EOOrQualifier */