2 Copyright (C) 2000-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 <NGObjWeb/NGObjWeb.h>
24 @interface WODictionaryRepetition : WODynamicElement
27 WOAssociation *dictionary;
37 /* TODO: the implementation does not work with keys that contain a dot! */
39 @implementation WODictionaryRepetition
41 - (id)initWithName:(NSString *)_name
42 associations:(NSDictionary *)_config
43 template:(WOElement *)_temp
45 if ((self = [super initWithName:_name associations:_config template:_temp])) {
46 self->dictionary = WOExtGetProperty(_config, @"dictionary");
47 self->key = WOExtGetProperty(_config, @"key");
48 self->item = WOExtGetProperty(_config, @"item");
50 self->template = [_temp retain];
56 [self->dictionary release];
59 [self->template release];
63 - (NSString *)unescapeKey:(NSString *)_key {
66 - (NSString *)escapeKey:(NSString *)_key {
67 if ([_key rangeOfString:@"."].length == 0)
70 NSLog(@"WARNING(%s): key '%@' can't be processed by "
71 @"WODictionaryRepetition !!",
72 __PRETTY_FUNCTION__, _key);
76 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
79 NSEnumerator *keyEnum;
84 comp = [_ctx component];
85 dict = [self->dictionary valueInComponent:comp];
86 keyEnum = [dict keyEnumerator];
88 isMutable = [dict isKindOfClass:[NSMutableDictionary class]];
92 NSLog(@"WARNING: WODictionaryRepetition: 'dictionary' is immutable."
93 @" Cannot change values.");
97 while ((k = [keyEnum nextObject])) {
99 if ([self->key isValueSettable])
100 [self->key setValue:k inComponent:comp];
102 [_ctx appendElementIDComponent:[self escapeKey:k]];
103 [self->template takeValuesFromRequest:_req inContext:_ctx];
104 [_ctx deleteLastElementIDComponent];
107 obj = [self->item valueInComponent:comp];
109 [(NSMutableDictionary *)dict setObject:obj forKey:k];
112 NSLog(@"WARNING: WODictionaryRepetition: nil object forKey: '%@'", k);
117 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
124 comp = [_ctx component];
125 dict = [self->dictionary valueInComponent:comp];
126 k = [self unescapeKey:[_ctx currentElementID]];
129 if ((obj = [dict objectForKey:k])) {
130 if ([self->item isValueSettable])
131 [self->item setValue:obj inComponent:comp];
132 if ([self->key isValueSettable])
133 [self->key setStringValue:k inComponent:comp];
135 [_ctx consumeElementID]; // consume k
137 [_ctx appendElementIDComponent:k];
138 result = [self->template invokeActionForRequest:_req inContext:_ctx];
139 [_ctx deleteLastElementIDComponent];
142 NSLog(@"WARNING: WODictionaryRepetition nil object for key:'%@'", k);
147 - (void)appendToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
150 NSEnumerator *keyEnum;
153 comp = [_ctx component];
154 dict = [self->dictionary valueInComponent:comp];
156 keyEnum = [dict keyEnumerator];
158 while ((k = [keyEnum nextObject])) {
159 if ([self->item isValueSettable])
160 [self->item setValue:[dict objectForKey:k] inComponent:comp];
161 if ([self->key isValueSettable])
162 [self->key setStringValue:k inComponent:comp];
164 [_ctx appendElementIDComponent:[self escapeKey:k]];
165 [self->template appendToResponse:_resp inContext:_ctx];
166 [_ctx deleteLastElementIDComponent];
170 @end /* WODictionaryRepetition */