File: WOComponentDefinition.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 (566 lines) | stat: -rw-r--r-- 15,930 bytes parent folder | download | duplicates (9)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
  Copyright (C) 2000-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/WOComponentDefinition.h>
#include "WOComponent+private.h"
#include "WOComponentFault.h"
#include "WONoContentElement.h"
#include <NGObjWeb/WOAssociation.h>
#include <NGObjWeb/WOApplication.h>
#include <NGObjWeb/WOElement.h>
#include <NGObjWeb/WOResponse.h>
#include <NGObjWeb/WOResourceManager.h>
#include "common.h"
#import <EOControl/EOControl.h>
#ifdef MULLE_EO_CONTROL
#import <EOControl/EOKeyValueUnarchiver.h>
#endif

#include <NGObjWeb/WOTemplateBuilder.h>

static Class    StrClass    = Nil;
static Class    DictClass   = Nil;
static Class    AssocClass  = Nil;
static Class    NumberClass = Nil;
static Class    DateClass   = Nil;
static NSNumber *yesNum     = nil;
static NSNumber *noNum      = nil;

@interface WOComponent(UsedPrivates)
- (void)setBaseURL:(id)_url;
@end

@interface WOComponentDefinition(PrivateMethods)

- (BOOL)load;

@end

#include <NGObjWeb/WOContext.h>
#include <NGObjWeb/WORequest.h>
#include <NGObjWeb/WOSession.h>

/*
  TODO:
  
  WO's instantiation method is 
    - componentInstanceInContext:forComponentReference:
  with the primary being
    - _componentInstanceInContext:forComponentReference:
  
  Maybe we should change to that. Currently this flow is a bit broken because
  the resourcemanager sits in the middle, though I'm pretty sure that some
  older WO used WOResourceManager just like we do in the moment.
*/

@implementation WOComponent(InfoSetup)

- (Class)componentFaultClass {
  return [WOComponentFault class];
}

- (NSMutableDictionary *)instantiateChildComponentsInTemplate:(WOTemplate *)_t
  languages:(NSArray *)_languages
{
  NSMutableDictionary *childComponents = nil;
  WOTemplate *tmpl;
  NSEnumerator *keys;
  NSString     *key;
  
  if ((tmpl = _t) == nil)
    return nil;
  
  if ([tmpl hasSubcomponentInfos] == 0)
    return nil;
  
  keys = [tmpl infoKeyEnumerator];
  while ((key = [keys nextObject])) {
    WOSubcomponentInfo *childInfo = nil;
    WOComponentFault   *child     = nil;
      
    childInfo = [tmpl subcomponentInfoForKey:key];
    
    child = [[WOComponentFault alloc]
              initWithResourceManager:nil //_rm
              pageName:[childInfo componentName]
              languages:_languages
              bindings:[childInfo bindings]];

    if (child != nil) {
      if (childComponents == nil)
        childComponents = [NSMutableDictionary dictionaryWithCapacity:16];
        
      [childComponents setObject:child forKey:key];
      [child release];
    }
    else {
      [self errorWithFormat:
              @"(%s): Could not instantiate child fault %@, component: '%@'",
              __PRETTY_FUNCTION__, key, [childInfo componentName]];
    }
  }
  return childComponents;
}

- (id)initWithName:(NSString *)_cname
  template:(WOTemplate *)_template
  inContext:(WOContext *)_ctx
{
  // Note: the _template can be nil and will then get looked up dynamically!
  [self setName:_cname];
  if ((self = [self initWithContext:_ctx])) {
    NSMutableDictionary *childComponents;
    NSArray             *langs;

    langs = [[self context] resourceLookupLanguages];
    
    childComponents = [self instantiateChildComponentsInTemplate:_template
			                      languages:langs];
    [self setSubComponents:childComponents];
    [self setTemplate:_template];
  }
  return self;
}

- (id)initWithComponentDefinition:(WOComponentDefinition *)_cdef 
  inContext:(WOContext *)_ctx
{
  /* 
     HACK HACK HACK CD: 
     We reuse the wocVariables ivar to pass over the component definition to 
     the component which will then call -_finishInitializingComponent: on the
     definition for applying the .woo.
     
     Sideeffects: if a component subclass uses extra vars prior calling
     WOComponent -init, it will run into "issues".
  */
  NSAssert(self->wocVariables == nil,
	   @"extra variables dict is already set! cannot transfer component "
	   @"definition in that variable (use the HACK)");
  self->wocVariables = (id)[_cdef retain];
  
  return [self initWithName:[_cdef componentName]
	       template:[_cdef template]
	       inContext:_ctx];
}

@end /* WOComponent(InfoSetup) */

@implementation WOComponentDefinition

static BOOL debugOn     = NO;
static BOOL profLoading = NO;
static BOOL enableClassLessComponents = NO;
static BOOL enableWOOFiles            = NO;
static NSArray *woxExtensions = nil;

+ (void)initialize {
  static BOOL isInitialized = NO;
  NSUserDefaults *ud;
  if (isInitialized) return;
  isInitialized = YES;
  ud = [NSUserDefaults standardUserDefaults];
    
  StrClass    = [NSString      class];
  DictClass   = [NSMutableDictionary class];
  AssocClass  = [WOAssociation class];
  NumberClass = [NSNumber      class];
  DateClass   = [NSDate        class];
    
  yesNum = [[NumberClass numberWithBool:YES] retain];
  noNum  = [[NumberClass numberWithBool:NO]  retain];
  
  profLoading = [[ud objectForKey:@"WOProfileLoading"] boolValue];
  enableClassLessComponents = 
    [ud boolForKey:@"WOEnableComponentsWithoutClasses"];
  enableWOOFiles = [ud boolForKey:@"WOComponentLoadWOOFiles"];
  debugOn        = [ud boolForKey:@"WODebugComponentDefinition"];
  woxExtensions  = [[ud arrayForKey:@"WOxFileExtensions"] copy];
}

- (id)initWithName:(NSString *)_name
  path:(NSString *)_path
  baseURL:(NSURL *)_baseUrl
  frameworkName:(NSString *)_frameworkName
{
  /* 
     this method is usually called by WOResourceManager
     (_definitionWithName:...) 
  */
  if ((self = [super init])) {
    /*
      'name'    is the name of the component
      'path'    contains a string or a NSURL with the location of the directory
                containing the component - TODO: explain who calculates that!
      'baseURL' contains a URL like /AppName/FrameworkName/Component/Eng.lProj
                (the external URL of the component, not sure whether this is
		 actually used somewhere)
    */
    NSZone *z = [self zone];
    
    self->name          = [_name          copyWithZone:z];
    self->path          = [_path          copyWithZone:z];
    self->baseUrl       = [_baseUrl       copyWithZone:z];
    self->frameworkName = [_frameworkName copyWithZone:z];
  
    if (debugOn) {
      [self debugWithFormat:
	      @"init: '%@' path='%@'\n  URL='%@'\n  framework='%@'",
	      self->name, self->path, [self->baseUrl absoluteString], 
	      self->frameworkName];
    }
    
    if (![self load]) { /* TODO: is this really required? */
      [self release];
      return nil;
    }
  }
  return self;
}

- (id)init {
  [self errorWithFormat:@"called -init on WOComponentDefinition!"];
  [self release];
  return nil;
}

- (void)dealloc {
  [self->template      release];
  [self->name          release];
  [self->path          release];
  [self->baseUrl       release];
  [self->frameworkName release];
  [super dealloc];
}

/* accessors */

- (Class)componentClassForScript:(WOComponentScript *)_script {
  return Nil;
}

- (void)setComponentClass:(Class)_class {
  self->componentClass = _class;
}
- (Class)componentClass {
  if (self->componentClass == Nil)
    self->componentClass = NSClassFromString(self->name);
  
  if (self->componentClass != Nil)
    return self->componentClass;

  if (self->name == nil)
    return Nil;
  
  if ([self->name isAbsolutePath])
	;
  else if ([self->name rangeOfString:@"."].length > 0)
	;
  else if (enableClassLessComponents)
	;
  else {
    [self logWithFormat:@"Note: did not find component class with name '%@'",
	    self->name];
  }
  return Nil;
}
- (NSString *)componentName {
  return self->name;
}

- (WOTemplate *)template {
  return self->template;
}

- (NSString *)path {
  return self->path;
}

- (NSURL *)baseURL {
  return self->baseUrl;
}
- (NSString *)frameworkName {
  return self->frameworkName;
}

/* caching */

- (void)touch {
  self->lastTouch = [DateClass timeIntervalSinceReferenceDate];
}

- (NSTimeInterval)lastTouch {
  return self->lastTouch;
}

/* instantiation */

- (BOOL)_checkComponentClassValidity:(Class)cClass {
#if 0
  /* this make no sense, need -isSubclassOfClass: ..,
     class instances are never isKindOfClass:WOElement ... 
  */
  {
    static Class WOElementClass = Nil;
    if (WOElementClass == Nil) WOElementClass = [WOElement class];
    if (![cClass isKindOfClass:WOElementClass] && cClass != nil) {
      [self warnWithFormat:@"(%s:%i): "
              @"component class %@ is not a subclass of WOElement !",
              __PRETTY_FUNCTION__, __LINE__,
              NSStringFromClass(cClass)];
      return NO;
    }
  }
#endif
  return YES;
}
- (BOOL)_checkComponentValidity:(id)component class:(Class)cClass {
  if (![component isKindOfClass:cClass] && component != nil) {
    [self warnWithFormat:@"(%s:%i): component %@ is not a subclass of "
            @"component class %@ !",
            __PRETTY_FUNCTION__, __LINE__,
            component, NSStringFromClass(cClass)];
    return NO;
  }
  return YES;
}

- (void)_applyWOOVariables:(NSDictionary *)_vars
  onComponent:(WOComponent *)_component 
{
  EOKeyValueUnarchiver *unarchiver;
  NSAutoreleasePool    *pool;
  NSEnumerator *keys;
  NSString     *key;
  
  pool = [[NSAutoreleasePool alloc] init];
  
  unarchiver = 
    [[[EOKeyValueUnarchiver alloc] initWithDictionary:_vars] autorelease];
  [unarchiver setDelegate:_component];
  
  keys = [_vars keyEnumerator];
  while ((key = [keys nextObject]) != nil) {
    id object;
    
    object = [unarchiver decodeObjectForKey:key];
    [_component takeValue:object forKey:key];
    
#if DEBUG_WOO
    [self logWithFormat:@"unarchived %@: %@", key, object];
#endif
  }
  [unarchiver finishInitializationOfObjects];
  [unarchiver awakeObjects];

  [pool release];
}

- (void)_applyWOOVariablesOnComponent:(WOComponent *)_component {
  /* 
     Note: we still need this, as components are not required to load the
           template at all!
  */
  NSString     *wooPath;
  NSDictionary *woo;

  wooPath = [[_component path] stringByAppendingPathExtension:@"woo"];
  if (![[NSFileManager defaultManager] fileExistsAtPath:wooPath])
    return;
  
  if ((woo = [NSDictionary dictionaryWithContentsOfFile:wooPath]) == nil) {
    [self errorWithFormat:@"could not load .woo-file: '%@'", wooPath];
    return;
  }
  
  [self _applyWOOVariables:[woo objectForKey:@"variables"]
        onComponent:_component];
}

- (void)_finishInitializingComponent:(WOComponent *)_component {
  if (self->baseUrl != nil)
    [_component setBaseURL:self->baseUrl];

  if (enableWOOFiles) {
    if (self->template != nil) {
      [self _applyWOOVariables:
              [self->template keyValueArchivedTemplateVariables]
            onComponent:_component];
    }
    else
      [self _applyWOOVariablesOnComponent:_component];
  }
}

- (WOComponent *)instantiateWithResourceManager:(WOResourceManager *)_rm
  languages:(NSArray *)_languages
{
  WOComponent       *component = nil;
  Class             cClass;
  WOComponentScript *script;
  
  cClass = ((script = [self->template componentScript]) != nil) 
    ? NSClassFromString(@"WOScriptedComponent")
    : [self componentClass];
  
  if (cClass == nil) {
    NSString *tmpPath;

    if (enableClassLessComponents) {
      [self debugWithFormat:@"Note: missing class for component: '%@'",
	      [self componentName]];
    }
    else {
      [self logWithFormat:@"Note: missing class for component: '%@'",
	      [self componentName]];
    }
    
    tmpPath = [self->name stringByAppendingPathExtension:@"html"];
    tmpPath = [self->path stringByAppendingPathComponent:tmpPath];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:tmpPath]) {
      cClass = [WOComponent class];
    }
    else {
      [self debugWithFormat:@"Note: did not find .html template at path: '%@'",
	      tmpPath];
    }
  }
  
  if (![self _checkComponentClassValidity:cClass]) {
    [self logWithFormat:@"Component Class '%@' is not valid.", cClass];
    return nil;
  }
  
  /* instantiate object (this will call _finishInitializingComponent) */
  
  component = [[cClass alloc] initWithComponentDefinition:self
			      inContext:[[WOApplication application] context]];
  component = [component autorelease];
  if (component == nil)
    return nil;
  
  /* check validity */
  
  if (debugOn)
    [self _checkComponentValidity:component class:cClass];

  if (debugOn) {
    if (![component isKindOfClass:cClass]) {
      [self warnWithFormat:
              @"(%s:%i): component '%@' is not a subclass of "
              @"component class '%@' !",
              __PRETTY_FUNCTION__, __LINE__,
              component, NSStringFromClass(cClass)];
    }
  }
  return component;
}

/* templates */

- (WOTemplateBuilder *)templateBuilderForPath:(NSString *)_path {
  NSString *ext;
  
  if ([_path length] == 0)
    return nil;
  
  ext = [_path pathExtension];
  if ([woxExtensions containsObject:ext]) {
    static WOTemplateBuilder *woxBuilder = nil;
    if (woxBuilder == nil)
      woxBuilder = [[NSClassFromString(@"WOxTemplateBuilder") alloc] init];
    return woxBuilder;
  }
  
  {
    static WOTemplateBuilder *woBuilder = nil;
    if (woBuilder == nil) {
      woBuilder =
        [[NSClassFromString(@"WOWrapperTemplateBuilder") alloc] init];
    }
    return woBuilder;
  }
}

- (WOTemplateBuilder *)templateBuilderForURL:(NSURL *)_url {
  if ([_url isFileURL])
    return [self templateBuilderForPath:[_url path]];
  
  [self logWithFormat:@"only supports file URLs: %@", _url];
  return nil;
}

- (BOOL)load {
  WOTemplateBuilder *builder;
  NSURL *url;

  if (self->path == nil)
    /* a pathless component (a component without a template file) */
    return YES;
  
  /*
    Note: the URL can either point directly to the .wo or .wox file entry or
          it can point to a .lproj inside a .wo (eg Main.wo/English.lproj)
    Note: actually the WOTemplateBuilder only supports file URLs in the moment,
          it just checks the path extension to select the proper builder.
  */
  url = [self->path isKindOfClass:[NSURL class]]
    ? (id)self->path
    : [[[NSURL alloc] initFileURLWithPath:self->path] autorelease];
  
  if (debugOn) [self debugWithFormat:@"url: %@", [url absoluteString]];
  
  // TODO: maybe we should move the builder selection to the resource-manager
  builder = [self templateBuilderForURL:url];
  if (debugOn) [self debugWithFormat:@"builder: %@", builder];
  
  self->template = [builder buildTemplateAtURL:url];
  if (debugOn) [self debugWithFormat:@"template: %@", self->template];
  
  return self->template ? YES : NO;
}

/* debugging */

- (BOOL)isDebuggingEnabled {
  return debugOn;
}

/* description */

- (NSString *)description {
  NSMutableString *ms = [NSMutableString stringWithCapacity:64];

  [ms appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];
  
  if (self->name)    [ms appendFormat:@" name=%@", self->name];
  if (self->path)    [ms appendFormat:@" path=%@", self->path];
  if (self->baseUrl) [ms appendFormat:@" base=%@", self->baseUrl];
  if (self->frameworkName) 
    [ms appendFormat:@" framework=%@", self->frameworkName];
  
  if (!self->template) [ms appendString:@" no-template"];
  [ms appendString:@">"];
  return ms;
}

@end /* WOComponentDefinition */