File: WOCopyValue.m

package info (click to toggle)
sope 5.12.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,588 kB
  • sloc: objc: 169,223; sh: 3,823; ansic: 3,409; javascript: 446; python: 318; makefile: 185
file content (278 lines) | stat: -rw-r--r-- 7,434 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
  Copyright (C) 2005 SKYRIX Software AG

  This file is part of SOPE.

  SOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with SOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

#include <NGObjWeb/WODynamicElement.h>

/*
  WOCopyValue / <var:copy-value/>
  
  Usage:
    SetupContext: WOCopyValue {
      currentDate = "currentItem.date";
      copyValues = {
        "displayGroup.queryMin.lastModified" = "currentItem.date";
        "displayGroup.queryMax.lastModified" = "currentItem.date";
      };
      finishValues = {
        "displayGroup.queryMin.lastModified" = nil;
        "displayGroup.queryMax.lastModified" = nil;
      };
      resetValues = NO;
    }

  Bindings:
    copyValues
    finishValues
    resetValues
    <extra>: are used as 'prepare' values
  
  The element has a template and can be used in a certain scope,
*/

@interface WOCopyValue : WODynamicElement // TODO: should be WOElement?
{
  WOAssociation **targets;
  WOAssociation **sources;
  unsigned      count;
  WOElement     *template;
  WOAssociation *copyValues;
  WOAssociation *finishValues;
  WOAssociation *resetValues;
}

@end

#include <NGObjWeb/WOAssociation.h>
#include <NGObjWeb/WOContext.h>
#include "common.h"

@implementation WOCopyValue

// TODO: cache NSString class, cache constant objects

static inline id valueForConstString(NSString *v) {
  unsigned len;
  unichar  c0;
  id vr;

  len = [v length];
  c0  = len > 6 ? [v characterAtIndex:6] : 0;
	  
  if ((len == 9  && c0 == 'n' && [v isEqualToString:@"const:nil"]) ||
      (len == 10 && c0 == 'n' && [v isEqualToString:@"const:null"])) {
    vr = [NSNull null];
  }
  else if ((len == 9  && c0 == 'y' && [v isEqualToString:@"const:yes"]) ||
	   (len == 10 && c0 == 't' && [v isEqualToString:@"const:true"])) {
    vr = [NSNumber numberWithBool:NO];
  }
  else if ((len == 8  && c0 == 'n' && [v isEqualToString:@"const:no"]) ||
	   (len == 11 && c0 == 'f' && [v isEqualToString:@"const:false"])) {
    vr = [NSNumber numberWithBool:NO];
  }
  else if (isdigit(c0) || (c0 == '-' && len > 7)) {
    vr = [v substringFromIndex:6];
    vr = ([vr rangeOfString:@"."].length > 0)
      ? [NSNumber numberWithDouble:[vr doubleValue]]
      : [NSNumber numberWithDouble:[vr intValue]];
  }
  else
    vr = [v substringFromIndex:6];

  return vr;
}

- (id)initWithName:(NSString *)_name
  associations:(NSDictionary *)_config
  template:(WOElement *)_c
{
  if ((self = [super initWithName:_name associations:_config template:_c])) {
    NSDictionary *statVals = nil;
    
    self->template = [_c retain];
    self->copyValues   = OWGetProperty(_config, @"copyValues");
    self->finishValues = OWGetProperty(_config, @"finishValues");
    self->resetValues  = OWGetProperty(_config, @"resetValues");

    /* fill static value array */
    
    if ([self->copyValues isValueConstant]) {
      statVals = [[self->copyValues valueInContext:nil] retain];
      [self->copyValues release];
      self->copyValues = nil;
    }
    
    if ((self->count = ([statVals count] + [_config count])) > 0) {
      NSEnumerator *e;
      NSString *key;
      unsigned i;
      
      self->targets = calloc(self->count + 2, sizeof(id));
      self->sources = calloc(self->count + 2, sizeof(id));
      i = 0;
      
      /* extra keys first (key is a string, value is an assoc) */

      e = [_config keyEnumerator];
      while ((key = [e nextObject]) != nil) {
	self->targets[i] = [[WOAssociation associationWithKeyPath:key] retain];
	self->sources[i] = [[_config objectForKey:key] retain];
	i++;
      }
      
      /* then static keys (key and value are strings) */
      
      e = [statVals keyEnumerator];
      while ((key = [e nextObject]) != nil) {
	NSString *v;
	
	v = [statVals objectForKey:key];
	self->targets[i] = [[WOAssociation associationWithKeyPath:key] retain];
	
	if ([v hasPrefix:@"const:"]) {
	  self->sources[i] =
	    [[WOAssociation associationWithValue:valueForConstString(v)]
	                    retain];
	}
	else {
	  self->sources[i] =
	    [[WOAssociation associationWithKeyPath:v] retain];
	}
	i++;
      }
    }
    
    [statVals release]; statVals = nil;
  }
  return self;
}

- (void)dealloc {
  unsigned i;
  
  [self->finishValues release];
  [self->resetValues  release];
  
  for (i = 0; i < self->count; i++) {
    [self->targets[i] release];
    [self->sources[i] release];
  }
  if (self->targets != NULL) free(self->targets);
  if (self->sources != NULL) free(self->sources);
  
  [self->template release];
  [super dealloc];
}

/* accessors */

- (id)template {
  return self->template;
}

/* copy */

- (void)copyValuesInDictionary:(NSDictionary *)_d inContext:(WOContext *)_ctx {
  NSEnumerator *e;
  NSString *key;
  id setCursor, getCursor;
  
  setCursor = [_ctx component];
  getCursor = setCursor;
  
  e = [_d keyEnumerator];
  while ((key = [e nextObject]) != nil) {
    id value;
    
    value = [_d objectForKey:key];
    if ([value isKindOfClass:[NSString class]]) {
      value = [(NSString *)value hasPrefix:@"const:"]
	? valueForConstString(value)
	: [getCursor valueForKeyPath:value];
    }
    
    [setCursor takeValue:value forKeyPath:key];
  }
}

- (void)copyValuesInContext:(WOContext *)_ctx {
  unsigned i;
  
  /* copy constant mappings */
  for (i = 0; i < self->count; i++) {
    [self->targets[i] setValue:[self->sources[i] valueInContext:_ctx]
	              inContext:_ctx];
  }
  
  /* copy dynamic mappings */
  if (self->copyValues != nil) {
    [self copyValuesInDictionary:[self->copyValues valueInContext:_ctx]
	  inContext:_ctx];
  }
}

- (void)resetValuesInContext:(WOContext *)_ctx {
  if (self->resetValues == nil && self->finishValues == nil)
    return;
  
  /* reset values to nil */
  
  if ([self->resetValues boolValueInContext:_ctx]) {
    unsigned i;
    
    for (i = 0; i < self->count; i++)
      [self->targets[i] setValue:nil inContext:_ctx];
  }
  
  /* apply post value copy */
  if (self->finishValues != nil) {
    [self copyValuesInDictionary:[self->finishValues valueInContext:_ctx]
	  inContext:_ctx];
  }
}

/* handling requests */

- (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
  [self copyValuesInContext:_ctx];
  [self->template takeValuesFromRequest:_rq inContext:_ctx];
  [self resetValuesInContext:_ctx];
}

- (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
  id result;
  
  [self copyValuesInContext:_ctx];
  result = [[self->template invokeActionForRequest:_rq inContext:_ctx] retain];
  [self resetValuesInContext:_ctx];
  
  return [result autorelease];
}

/* generating response */

- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
  [self copyValuesInContext:_ctx];
  [self->template appendToResponse:_response inContext:_ctx];
  [self resetValuesInContext:_ctx];
}

@end /* WOCopyValue */