File: WOString.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 (793 lines) | stat: -rw-r--r-- 22,063 bytes parent folder | download | duplicates (7)
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*
  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/WOHTMLDynamicElement.h>
#include "WOElement+private.h"
#include "decommon.h"
#include <string.h>
#import <Foundation/NSNumberFormatter.h>
#import <Foundation/NSDateFormatter.h>

/*
  Usage:
    MyString: WOString {
      value      = myCalendarDate;
      dateformat = "%B %Y %T";
      insertBR   = NO;
      nilString  = "no date is set !";
      escapeHTML = YES;
    };

  Hierarchy:
    WOHTMLDynamicElement
      WOString
        _WOSimpleStaticString
        _WOSimpleStaticASCIIString
        _WOComplexString
*/

@interface WOString : WOHTMLDynamicElement
@end /* WOString */

@interface _WOTemporaryString : NSObject
@end

@interface _WOSimpleStaticString : WOString
{
  NSString *value;
}
@end /* WOSimpleStaticString */

@interface _WOSimpleStaticASCIIString : WOString
{
  const unsigned char *value;
}
@end /* _WOSimpleStaticASCIIString */

@interface _WOSimpleDynamicString : WOString
{
  WOAssociation *value;
}
@end /* WOSimpleStaticString */

@interface _WOComplexString : WOString
{
  WOAssociation *value;          // object
  WOAssociation *escapeHTML;     // BOOL
  WOAssociation *numberformat;   // string
  WOAssociation *dateformat;     // string
  WOAssociation *formatter;      // WO4: NSFormatter object
  WOAssociation *valueWhenEmpty; // WO4.5
  
  // non-WO attributes
  WOAssociation *insertBR;   // insert <BR> tags for newlines
  WOAssociation *nilString;  // string to use if value is nil - DEPRECATED!
  WOAssociation *style;      // insert surrounding <span class="style">
  // TODO: also add 'id' for span? (JavaScript tagging?)
}

@end /* WOComplexString */

@implementation WOString

+ (id)allocWithZone:(NSZone *)zone {
  static Class WOStringClass = Nil;
  static _WOTemporaryString *temporaryString = nil;
  
  if (WOStringClass == Nil)
    WOStringClass = [WOString class];
  if (temporaryString == nil)
    temporaryString = [_WOTemporaryString allocWithZone:zone];
  
  return (self == WOStringClass)
    ? (id)temporaryString
    : (id)NSAllocateObject(self, 0, zone);
}

@end /* WOString */


@implementation _WOSimpleDynamicString

- (id)initWithName:(NSString *)_name
  associations:(NSDictionary *)_config
  template:(WOElement *)_t
{
  if ((self = [super initWithName:_name associations:_config template:_t])) {
    self->value = OWGetProperty(_config, @"value");
  }
  return self;
}

- (void)dealloc {
  [self->value release];
  [super dealloc];
}

/* generating response */

- (void)appendToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
  if (![_ctx isRenderingDisabled])
    [_resp appendContentHTMLString:[self->value stringValueInContext:_ctx]];
}

/* description */

- (NSString *)associationDescription {
  return [NSString stringWithFormat:@" value='%@'", self->value];
}

@end /* _WOSimpleDynamicString */

@implementation _WOSimpleStaticString

- (id)initWithValue:(WOAssociation *)_value escapeHTML:(BOOL)_flag {
  if ((self = [super initWithName:nil associations:nil template:nil])) {
    NSString *v;
    unsigned length;

    v = [_value stringValueInComponent:nil];
    length = [v length];

    if (length == 0) {
      v = @"";
    }
    else if (_flag) {
      unichar buffer[length * 6]; /* longest-encoding: '&quot;' */
      unichar     *cbuf;
      register unichar *cstr;
      register unsigned i;
      register unichar *bufPtr;

#if NeXT_Foundation_LIBRARY
      cbuf = malloc((length + 1) * sizeof (unichar));
#else
      cbuf = NGMallocAtomic((length + 1) * sizeof (unichar));
#endif
      [v getCharacters:cbuf]; cbuf[length] = '\0';
      cstr = cbuf;
      
      for (i = 0, bufPtr = buffer; i < length; i++) {
        switch (cstr[i]) {
          case '&': /* &amp; */
            bufPtr[0] = '&'; bufPtr[1] = 'a'; bufPtr[2] = 'm'; bufPtr[3] = 'p';
            bufPtr[4] = ';';
            bufPtr += 5;
            break;
            
          case '<': /* &lt;   */
            bufPtr[0] = '&'; bufPtr[1] = 'l'; bufPtr[2] = 't'; bufPtr[3] = ';';
            bufPtr += 4;
            break;
            
          case '>': /* &gt;   */
            bufPtr[0] = '&'; bufPtr[1] = 'g'; bufPtr[2] = 't'; bufPtr[3] = ';';
            bufPtr += 4;
            break;
            
          case '"': /* &quot; */
            bufPtr[0] = '&'; bufPtr[1] = 'q'; bufPtr[2] = 'u';
            bufPtr[3] = 'o'; bufPtr[4] = 't'; bufPtr[5] = ';';
            bufPtr += 6;
            break;
            
          default:
            *bufPtr = cstr[i];
            bufPtr++;
            break;
        }
      }
#if NeXT_Foundation_LIBRARY
      if (cbuf) free(cbuf);
#else
      if (cbuf) NGFree(cbuf);
#endif
      self->value = [[NSString allocWithZone:[self zone]]
                      initWithCharacters: buffer
                                  length: (bufPtr - buffer)];
    }
    else {
      self->value = [v copyWithZone:[self zone]];
    }
  }
  return self;
}

- (id)initWithName:(NSString *)_name
  associations:(NSDictionary *)_config
  template:(WOElement *)_t
{
  if ((self = [super initWithName:_name associations:_config template:_t])) {
    WOAssociation *avalue, *aescape;
    BOOL doEscape;
    
    avalue  = OWGetProperty(_config, @"value");
    aescape = OWGetProperty(_config, @"escapeHTML");
    
    doEscape = (aescape != nil) ? [aescape boolValueInComponent:nil] : YES;
    self = [self initWithValue:avalue escapeHTML:doEscape];
    [avalue release];
    [aescape release];
  }
  return self;
}

- (void)dealloc {
  [self->value release];
  [super dealloc];
}

/* generating response */

- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
  if (![_ctx isRenderingDisabled])
    WOResponse_AddString(_response, self->value);
}

/* description */

- (NSString *)associationDescription {
  return [NSString stringWithFormat:@" value='%@'", self->value];
}

@end /* WOSimpleStaticString */

@implementation _WOSimpleStaticASCIIString

- (id)initWithValue:(WOAssociation *)_value escapeHTML:(BOOL)_flag {
  if ((self = [super initWithName:nil associations:nil template:nil])) {
    // ENCODING, UNICODE
    NSString *v;
    unsigned length;
    
    v = [_value stringValueInComponent:nil];
    length = [v cStringLength];
    
    if (length == 0) {
      self->value = NULL;
    }
    else if (_flag) {
      unsigned char *buffer;
      register unsigned char *cbuf;
      register unsigned char *bufPtr;
      register unsigned      i;
      unsigned clen;
      BOOL didEscape = NO;
      
      clen = [v cStringLength];
      cbuf = malloc(clen + 2);
      [v getCString:(char *)cbuf]; cbuf[clen] = '\0';
      
      buffer = malloc(clen * 6 + 2); /* longest-encoding: '&quot;' */
      
      for (i = 0, bufPtr = buffer; i < length; i++) {
        switch (cbuf[i]) {
          case '&': /* &amp; */
            bufPtr[0] = '&'; bufPtr[1] = 'a'; bufPtr[2] = 'm'; bufPtr[3] = 'p';
            bufPtr[4] = ';';
            bufPtr += 5;
            didEscape = YES;
            break;
            
          case '<': /* &lt;   */
            bufPtr[0] = '&'; bufPtr[1] = 'l'; bufPtr[2] = 't'; bufPtr[3] = ';';
            bufPtr += 4;
            didEscape = YES;
            break;
            
          case '>': /* &gt;   */
            bufPtr[0] = '&'; bufPtr[1] = 'g'; bufPtr[2] = 't'; bufPtr[3] = ';';
            bufPtr += 4;
            didEscape = YES;
            break;
            
          case '"': /* &quot; */
            bufPtr[0] = '&'; bufPtr[1] = 'q'; bufPtr[2] = 'u';
            bufPtr[3] = 'o'; bufPtr[4] = 't'; bufPtr[5] = ';';
            bufPtr += 6;
            didEscape = YES;
            break;
            
          default:
#if 0
            if ((*bufPtr = cbuf[i]) > 127) {
              NSLog(@"WARNING: string is not ASCII as required for "
                    @"SimpleStaticASCIIString !!! '%@'", v);
            }
#endif
            bufPtr++;
            break;
        }
      }
      if (didEscape) {
        if (cbuf) free(cbuf);
        clen = (bufPtr - buffer);
        self->value = malloc(clen + 2);
        strncpy((char *)self->value, (const char *)buffer, clen);
        ((unsigned char *)self->value)[clen] = '\0';
      }
      else {
        self->value = cbuf;
      }
      if (buffer) free(buffer);
    }
    else {
      self->value = malloc(length + 2);
      [v getCString:(char*)self->value];
    }
  }
  return self;
}

- (id)initWithName:(NSString *)_name
  associations:(NSDictionary *)_config
  template:(WOElement *)_t
{
  if ((self = [super initWithName:_name associations:_config template:_t])) {
    WOAssociation *avalue, *aescape;
    BOOL doEscape;
    
    avalue  = OWGetProperty(_config, @"value");
    aescape = OWGetProperty(_config, @"escapeHTML");
    
    doEscape = (aescape != nil) ? [aescape boolValueInComponent:nil] : YES;
    self = [self initWithValue:avalue escapeHTML:doEscape];
    [avalue release];
    [aescape release];
  }
  return self;
}

- (void)dealloc {
  if (self->value) free((char *)self->value);
  [super dealloc];
}

/* generating response */

- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
  if (![_ctx isRenderingDisabled] && self->value)
    if (self->value) WOResponse_AddCString(_response, self->value);
}

/* description */

- (NSString *)associationDescription {
  return [NSString stringWithFormat:@" value='%s'", 
                     self->value ? self->value : (void*)""];
}

@end /* WOSimpleStaticASCIIString */

@implementation _WOComplexString

static NSNumber      *yesNum = nil;
static WOAssociation *yesAssoc = nil;

+ (void)initialize {
  if (yesNum   == nil) yesNum = [[NSNumber numberWithBool:YES] retain];
  if (yesAssoc == nil) 
    yesAssoc = [[WOAssociation associationWithValue:yesNum] retain];
}

- (id)initWithName:(NSString *)_name
  associations:(NSDictionary *)_config
  template:(WOElement *)_t
{
  if ((self = [super initWithName:_name associations:_config template:_t])) {
    self->value          = OWGetProperty(_config, @"value");
    self->escapeHTML     = OWGetProperty(_config, @"escapeHTML");
    self->insertBR       = OWGetProperty(_config, @"insertBR");
    self->nilString      = OWGetProperty(_config, @"nilString");
    self->valueWhenEmpty = OWGetProperty(_config, @"valueWhenEmpty");
    self->style          = OWGetProperty(_config, @"style");

    if (self->nilString != nil && self->valueWhenEmpty != nil) {
      [self logWithFormat:
	      @"WARNING: 'valueWhenEmpty' AND 'nilString' bindings are set, "
	      @"use only one! ('nilString' is deprecated!)"];
    }
    else if (self->nilString != nil) {
      [self debugWithFormat:
	      @"Note: using deprecated 'nilString' binding, "
	      @"use 'valueWhenEmpty' instead."];
    }
    
    self->formatter    = OWGetProperty(_config, @"formatter");
    self->numberformat = OWGetProperty(_config, @"numberformat");
    self->dateformat   = OWGetProperty(_config, @"dateformat");
    
    if (self->formatter == nil) {
      if ([_config objectForKey:@"formatterClass"]) {
        WOAssociation *assoc;
        NSString      *className;
        NSFormatter   *fmt = nil;
        Class         clazz;
        
        assoc = [OWGetProperty(_config, @"formatterClass") autorelease];
        if (![assoc isValueConstant])
          [self logWithFormat:@"non-constant 'formatterClass' binding!"];
        className = [assoc stringValueInComponent:nil];
        clazz     = NSClassFromString(className);
        
        if ((assoc = [OWGetProperty(_config, @"format") autorelease])) {
          NSString *format = nil;
          
          if (![assoc isValueConstant])
            [self logWithFormat:@"non-constant 'format' binding!"];
          format = [assoc stringValueInComponent:nil];
          
          if ([clazz instancesRespondToSelector:@selector(initWithString:)])
            fmt = [[clazz alloc] initWithString:format];
          else {
            [self logWithFormat:
                    @"cannot instantiate formatter with format: '%@'", format];
            fmt = [[clazz alloc] init];
          }
        }
        else
          fmt = [[clazz alloc] init];
        
        self->formatter = [[WOAssociation associationWithValue:fmt] retain];
        [fmt release];
      }
    }

    if (self->escapeHTML == nil)
      self->escapeHTML = [yesAssoc retain];
    
    /* check formats */
    {
      int num = 0;
      if (self->formatter)    num++;
      if (self->numberformat) num++;
      if (self->dateformat)   num++;
      if (num > 1)
        NSLog(@"WARNING: more than one formats specified in element %@", self);
    }
  }
  return self;
}

- (id)initWithValue:(WOAssociation *)_value escapeHTML:(BOOL)_flag {
  if ((self = [super initWithName:nil associations:nil template:nil])) {
    self->value      = [_value retain];
    self->escapeHTML = _flag
      ? yesAssoc
      : [WOAssociation associationWithValue:[NSNumber numberWithBool:NO]];
    self->escapeHTML = [self->escapeHTML retain];
  }
  return self;
}

- (void)dealloc {
  [self->numberformat   release];
  [self->dateformat     release];
  [self->formatter      release];
  [self->nilString      release];
  [self->valueWhenEmpty release];
  [self->value          release];
  [self->escapeHTML     release];
  [self->insertBR       release];
  [self->style          release];
  [super dealloc];
}

/* response generation */

- (void)_appendStringLines:(NSString *)_s withSeparator:(NSString *)_br
  contentSelector:(SEL)_sel
  toResponse:(WOResponse *)_response inContext:(WOContext *)_ctx
{
  NSArray *lines;
  unsigned i, count;
  
  lines = [_s componentsSeparatedByString:@"\n"];

  for (i = 0, count = [lines count]; i < count; i++) {
    NSString *line;

    line = [lines objectAtIndex:i];
    if (i != 0) WOResponse_AddString(_response, _br);
    
    [_response performSelector:_sel withObject:line];
  }
}

- (NSFormatter *)_formatterInContext:(WOContext *)_ctx {
  if (self->numberformat) {
    NSNumberFormatter *fmt;

    fmt = [[[NSNumberFormatter alloc] init] autorelease];
    [fmt setFormat:[self->numberformat valueInComponent:[_ctx component]]];
    return fmt;
  }

  if (self->dateformat) {
    NSDateFormatter *fmt;
    NSString *s;
    
    s = [self->dateformat valueInComponent:[_ctx component]];
    fmt = [[NSDateFormatter alloc] initWithDateFormat:s
				   allowNaturalLanguage:NO];
    return [fmt autorelease];
  }
  
  if (self->formatter)
    return [self->formatter valueInComponent:[_ctx component]];
  
  return nil;
}

- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
  WOComponent *sComponent;
  NSFormatter *fmt;
  id          obj    = nil;
  SEL         addSel = NULL;
  NSString    *styleName;

  if ([_ctx isRenderingDisabled] || [[_ctx request] isFromClientComponent])
    return;

  sComponent = [_ctx component];
  fmt        = [self _formatterInContext:_ctx];

#if DEBUG
  if (fmt!=nil && ![fmt respondsToSelector:@selector(stringForObjectValue:)]) {
    [sComponent errorWithFormat:
                  @"invalid formatter determined by keypath %@: %@",
                  self->formatter, fmt];
  }
#endif
  
  obj    = [self->value valueInContext:_ctx];
  addSel = [self->escapeHTML boolValueInComponent:sComponent]
    ? @selector(appendContentHTMLString:)
    : @selector(appendContentString:);

  if (fmt) {
    NSString *formattedObj;
    
    formattedObj = [fmt stringForObjectValue:obj];
#if 0
    if (formattedObj == nil) {
      [self warnWithFormat:@"formatter %@ returned nil string for object %@",
                           fmt, obj];
    }
#endif
    
    obj = formattedObj;
  }
  
  obj = [obj stringValue];

  styleName = [self->style stringValueInContext:_ctx];

  /* handling of empty and nil values */

  if (self->valueWhenEmpty != nil) {
    if (![obj isNotEmpty]) {
      NSString *s;
      
      s = [self->valueWhenEmpty stringValueInComponent:sComponent];
      
      /* Note: the missing escaping is intentional for WO 4.5 compatibility */
      if (s != nil) {
        if (styleName != nil) {
          [_response appendContentString:@"<span class=\""];
          [_response appendContentHTMLAttributeValue:styleName];
          [_response appendContentString:@"\">"];
        }
        [_response appendContentString:s];
      }
      goto closeSpan;
    }
  }
  else if (self->nilString != nil && obj == nil) {
    NSString *s;

    if (styleName != nil) {
      [_response appendContentString:@"<span class=\""];
      [_response appendContentHTMLAttributeValue:styleName];
      [_response appendContentString:@"\">"];
    }
    s = [self->nilString stringValueInComponent:sComponent];
    [_response performSelector:addSel withObject:s];
    goto closeSpan;
  }
  else if (obj == nil)
    return;

  /* handling of non-empty values */
  
  if (styleName != nil) {
    [_response appendContentString:@"<span class=\""];
    [_response appendContentHTMLAttributeValue:styleName];
    [_response appendContentString:@"\">"];
  }

  if (![self->insertBR boolValueInComponent:sComponent]) {
    [_response performSelector:addSel withObject:obj];
  }
  else {
    [self _appendStringLines:obj 
	  withSeparator:
	    (_ctx->wcFlags.xmlStyleEmptyElements ? @"<br />" : @"<br>")
          contentSelector:addSel toResponse:_response inContext:_ctx];
  }
  
closeSpan:
  if (styleName) {
    [_response appendContentString:@"</span>"];
  }
}

/* description */

- (NSString *)associationDescription {
  NSMutableString *str;
  
  str = [NSMutableString stringWithCapacity:64];
  
  if (self->value)      [str appendFormat:@" value=%@",      self->value];
  if (self->escapeHTML) [str appendFormat:@" escape=%@",     self->escapeHTML];
  if (self->insertBR)   [str appendFormat:@" insertBR=%@",   self->insertBR];
  if (self->formatter)  [str appendFormat:@" formatter=%@",  self->formatter];
  if (self->dateformat) [str appendFormat:@" dateformat=%@", self->dateformat];
  if (self->numberformat)
    [str appendFormat:@" numberformat=%@", self->numberformat];

  if (self->valueWhenEmpty) 
    [str appendFormat:@" valueWhenEmpty=%@", self->valueWhenEmpty];
  if (self->style) 
      [str appendFormat:@" style=%@", self->style];
  
  return str;
}

@end /* _WOComplexString */

@implementation _WOTemporaryString

static Class ComplexStringClass     = Nil;
static Class SimpleStringClass      = Nil;
static Class SimpleASCIIStringClass = Nil;
static Class SimpleDynStringClass   = Nil;

#define ENSURE_CACHE \
  if (ComplexStringClass == Nil)\
    ComplexStringClass = [_WOComplexString class];\
  if (SimpleStringClass == Nil)\
    SimpleStringClass = [_WOSimpleStaticString class];\
  if (SimpleASCIIStringClass == Nil)\
    SimpleASCIIStringClass = [_WOSimpleStaticASCIIString class];\
  if (SimpleDynStringClass == Nil)\
    SimpleDynStringClass = [_WOSimpleDynamicString class];

static inline Class _classForConfig(NSDictionary *_config) {
  Class         sClass = Nil;
  WOAssociation *assoc, *assoc2;
  unsigned      c;
  
  switch ((c = [_config count])) {
    case 0:
      sClass = SimpleStringClass;
      break;
      
    case 1:
      if ((assoc = [_config objectForKey:@"value"])) {
        if ([assoc isValueConstant])
          sClass = SimpleStringClass;
        else
          sClass = SimpleDynStringClass;
        break;
      }
      if ((assoc = [_config objectForKey:@"escapeHTML"])) {
        if ([assoc isValueConstant])
          sClass = SimpleStringClass;
        break;
      }
      break;
      
    case 2:
      if ((assoc = [_config objectForKey:@"value"])) {
        if ((assoc2 = [_config objectForKey:@"escapeHTML"])) {
          if ([assoc isValueConstant] && [assoc2 isValueConstant])
            sClass = SimpleStringClass;

          break;
        }
      }
      break;
      
    default:
      sClass = ComplexStringClass;
      break;
  }
  
  return sClass ? sClass : ComplexStringClass;
}

- (id)initWithName:(NSString *)_n
  associations:(NSDictionary *)_config
  template:(WOElement *)_t
{
  Class stringClass;
  ENSURE_CACHE;

#if DEBUG
  if (_t != nil) {
    NSLog(@"WARNING: WOString '%@' has contents !", _n);
    abort();
  }
#endif

  stringClass = _classForConfig(_config);
  
  return [[stringClass alloc]
                       initWithName:_n associations:_config template:nil];
}
- (id)initWithName:(NSString *)_name
  associations:(NSDictionary *)_associations
  contentElements:(NSArray *)_contents
{
  Class stringClass;
  ENSURE_CACHE;

#if DEBUG
  if ([_contents count] > 0) {
    NSLog(@"WARNING: WOString has contents !");
    abort();
  }
#endif
  
  stringClass = _classForConfig(_associations);
  
  return [[stringClass alloc]
                       initWithName:_name
                       associations:_associations
                       contentElements:nil];
}

- (id)initWithValue:(WOAssociation *)_value escapeHTML:(BOOL)_flag {
  Class stringClass;
  ENSURE_CACHE;
  
  if ((_value == nil) || [_value isValueConstant])
    stringClass = SimpleStringClass;
  else
    stringClass = ComplexStringClass;
    
  return [[stringClass alloc] initWithValue:_value escapeHTML:_flag];
}

- (void)dealloc {
  [self errorWithFormat:@"called dealloc on %@", self];
#if DEBUG
  abort();
#endif
  return;

  /* make Tiger GCC / gcc 4.1 happy */
  if (0) [super dealloc];
}

@end /* _WOTemporaryString */