]> err.no Git - sope/blob - sope-appserver/NGObjDOM/Dynamic.subproj/ODR_bind_string.m
renamed packages as discussed in the developer list
[sope] / sope-appserver / NGObjDOM / Dynamic.subproj / ODR_bind_string.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include <NGObjDOM/ODNodeRenderer.h>
24
25 /*
26   Attributes:
27   
28     value      - value to be generated
29     nilValue   - value to be generated if value is nil
30     dateformat - a date formatting spec
31     numberformat
32     escapehtml - bool (default: true)
33     insertbr   - bool (default: false)
34   
35   Usage:
36     <var:string value="name"/>
37 */
38
39 @interface ODR_bind_string : ODNodeRenderer
40 @end
41
42 #include "common.h"
43 #import <Foundation/NSDateFormatter.h>
44
45 @implementation ODR_bind_string
46
47 - (NSFormatter *)_formatterForNode:(id)_node inContext:(WOContext *)_ctx {
48   NSFormatter *formatter;
49   NSString    *fmt;
50   
51   if ((fmt = [self stringFor:@"dateformat" node:_node ctx:_ctx])) {
52     formatter = [[NSDateFormatter alloc]
53                                   initWithDateFormat:fmt
54                                   allowNaturalLanguage:NO];
55     AUTORELEASE(formatter);
56   }
57   else if ((fmt = [self stringFor:@"numberformat" node:_node ctx:_ctx])) {
58     formatter = [[NSNumberFormatter alloc] init];
59     AUTORELEASE(formatter);
60     [(NSNumberFormatter *)formatter setFormat:fmt];
61   }
62   else
63     formatter = nil;
64
65   return formatter;
66 }
67
68 - (void)appendNode:(id)_node
69   toResponse:(WOResponse *)_response
70   inContext:(WOContext *)_ctx
71 {
72   id          value;
73   NSString    *string = nil;
74   BOOL        insertBR;
75   SEL         selector;
76   NSFormatter *formatter;
77   
78   if ((value = [self valueFor:@"value" node:_node ctx:_ctx]) == nil)
79     value = [self valueFor:@"nilValue" node:_node ctx:_ctx];
80   
81   formatter = [self _formatterForNode:_node inContext:_ctx];
82   
83   string = formatter
84     ? [formatter stringForObjectValue:value]
85     : [value stringValue];
86   
87   if (string == nil)
88     return;
89
90   string = [string stringValue];
91   
92   insertBR = [self boolFor:@"insertbr" node:_node ctx:_ctx];
93   
94   selector = ![self hasAttribute:@"escapehtml" node:_node ctx:_ctx]
95     ? @selector(appendContentHTMLString:)
96     : ([self boolFor:@"escapehtml" node:_node ctx:_ctx]
97        ? @selector(appendContentHTMLString:)
98        : @selector(appendContentString:));
99   
100   if (!insertBR) {
101     [_response performSelector:selector withObject:string];
102   }
103   else {
104     NSArray *lines;
105     unsigned i, count;
106     
107     lines = [string componentsSeparatedByString:@"\n"];
108     count = [lines count];
109     
110     for (i = 0; i < count; i++) {
111       NSString *line = [lines objectAtIndex:i];
112       
113       if (i != 0)
114         [_response appendContentString:@"<br />"];
115       
116       [_response performSelector:selector withObject:line];
117     }
118   }
119 }
120
121 @end /* ODR_bind_string */