]> err.no Git - sope/blob - skyrix-sope/NGObjWeb/DynamicElements/WOText.m
added svn:keywords and svn:ignore where appropriate. removed CVS artifacts.
[sope] / skyrix-sope / NGObjWeb / DynamicElements / WOText.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 "WOInput.h"
24 #include "common.h"
25 #import <Foundation/NSNumberFormatter.h>
26 #import <Foundation/NSDateFormatter.h>
27
28 @interface WOText : WOInput
29 {
30   // inherited: name
31   // inherited: value
32   // inherited: disabled
33 @protected
34   // non WO:
35   WOAssociation *rows;
36   WOAssociation *cols;
37   WOAssociation *numberformat; // string
38   WOAssociation *dateformat;   // string
39   WOAssociation *formatter;
40 }
41
42 @end /* WOText */
43
44 @interface NSObject(UsedKeyPath)
45 - (NSString *)keyPath;
46 @end
47
48 @implementation WOText
49
50 - (id)initWithName:(NSString *)_name
51   associations:(NSDictionary *)_config
52   template:(WOElement *)_root {
53
54   if ((self = [super initWithName:_name associations:_config template:_root])) {
55     self->rows         = OWGetProperty(_config, @"rows");
56     self->cols         = OWGetProperty(_config, @"cols");
57     self->formatter    = OWGetProperty(_config, @"formatter");
58     self->numberformat = OWGetProperty(_config, @"numberformat");
59     self->dateformat   = OWGetProperty(_config, @"dateformat");
60
61     if (self->formatter == nil) {
62       if ([_config objectForKey:@"formatterClass"]) {
63         id className;
64
65         className = OWGetProperty(_config, @"formatterClass");
66         className = [className autorelease];
67         
68         className = [className valueInComponent:nil];
69         className = NSClassFromString(className);
70         className = [[className alloc] init];
71
72         self->formatter = [WOAssociation associationWithValue:className];
73         self->formatter = [self->formatter retain];
74         [className release];
75       }
76     }
77
78     // check formats
79     {
80       int num = 0;
81       if (self->formatter)    num++;
82       if (self->numberformat) num++;
83       if (self->dateformat)   num++;
84       if (num > 1)
85         NSLog(@"WARNING: more than one formats specified in element %@", self);
86     }
87   }
88   return self;
89 }
90
91 - (void)dealloc {
92   [self->numberformat release];
93   [self->dateformat   release];
94   [self->formatter    release];
95   [self->rows         release];
96   [self->cols         release];
97   [super dealloc];
98 }
99
100 /* formatter */
101
102 static inline NSFormatter *_getFormatter(WOText *self, WOContext *_ctx) {
103   NSFormatter *fmt   = nil;
104   
105   if (self->numberformat) {
106     fmt = [[[NSNumberFormatter alloc] init] autorelease];
107     [(NSNumberFormatter *)fmt setFormat:
108         [self->numberformat valueInComponent:[_ctx component]]];
109   }
110   else if (self->dateformat) {
111     fmt = [[NSDateFormatter alloc]
112                             initWithDateFormat:
113                               [self->dateformat valueInComponent:
114                                                   [_ctx component]]
115                             allowNaturalLanguage:NO];
116     fmt = [fmt autorelease];
117   }
118   else if (self->formatter) {
119     fmt = [self->formatter valueInComponent:[_ctx component]];
120   }
121
122   return fmt;
123 }
124
125 // ******************** responder ********************
126
127 - (id)parseFormValue:(id)_value inContext:(WOContext *)_ctx {
128   NSFormatter *fmt = nil;
129
130   fmt = _getFormatter(self, _ctx);
131   
132   if (fmt) {
133     NSString *errorText = nil;
134     id       object     = nil;
135
136     //fmt = [self->formatter valueInComponent:[_ctx component]];
137
138     if ([fmt getObjectValue:&object forString:[_value stringValue]
139              errorDescription:&errorText]) {
140
141       return object;
142     }
143     else {
144       NSException *formatException = nil;
145       NSString    *keyPath         = nil;
146       
147       if ([self->value respondsToSelector:@selector(keyPath)])
148         keyPath = [(id)self->value keyPath];
149
150       formatException = [NSException exceptionWithName:@"WOValidationException"
151                                      reason:errorText
152                                      userInfo:nil];
153
154       [[_ctx component] validationFailedWithException:formatException
155                         value:_value
156                         keyPath:keyPath];
157       return nil;
158     }
159   }
160   else
161     return [super parseFormValue:_value inContext:_ctx];
162 }
163
164 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
165   WOComponent *sComponent;
166   NSFormatter *fmt;
167   id          v;
168   unsigned    r;
169   unsigned    c;
170   
171   sComponent = [_ctx component];
172   v  = [self->value valueInComponent:sComponent];
173   r  = [self->rows  unsignedIntValueInComponent:sComponent];
174   c  = [self->cols  unsignedIntValueInComponent:sComponent];
175   
176   fmt = _getFormatter(self, _ctx);
177   if (fmt) {
178     NSString *formattedObj = nil;
179
180     formattedObj = [fmt editingStringForObjectValue:v];
181     v = formattedObj;
182   }
183   else
184     v = [v stringValue];
185   
186   WOResponse_AddCString(_response, "<textarea name=\"");
187   [_response appendContentHTMLAttributeValue:OWFormElementName(self, _ctx)];
188   WOResponse_AddChar(_response, '"');
189   if (r > 0) {
190     WOResponse_AddCString(_response, " rows=\"");
191     WOResponse_AddUInt(_response, r);
192     WOResponse_AddChar(_response, '"');
193   }
194   if (c > 0) {
195     WOResponse_AddCString(_response, " cols=\"");
196     WOResponse_AddUInt(_response, c);
197     WOResponse_AddChar(_response, '"');
198   }
199   [self appendExtraAttributesToResponse:_response inContext:_ctx];
200   if (self->otherTagString) {
201     WOResponse_AddString(_response,
202                          [self->otherTagString stringValueInComponent:
203                               [_ctx component]]);
204   }
205   WOResponse_AddChar(_response, '>');
206
207   if ([v length] > 0) {
208     BOOL     removeCR = NO;
209     NSString *ua;
210     
211     ua = [[_ctx request] headerForKey:@"user-agent"];
212     
213     if ([ua rangeOfString:@"Opera"].length > 0)
214       removeCR = YES;
215     
216     if (removeCR)
217       v = [v stringByReplacingString:@"\r" withString:@""];
218     
219     [_response appendContentHTMLString:v];
220   }
221   
222   WOResponse_AddCString(_response, "</textarea>");
223 }
224
225 /* description */
226
227 - (NSString *)associationDescription {
228   NSMutableString *str = nil;
229   
230   str = [NSMutableString stringWithCapacity:64];
231   [str appendString:[super associationDescription]];
232   
233   if (self->rows) [str appendFormat:@" rows=%@", self->rows];
234   if (self->cols) [str appendFormat:@" cols=%@", self->cols];
235   if (self->formatter)  [str appendFormat:@" formatter=%@", self->formatter];
236   if (self->dateformat) [str appendFormat:@" dateformat=%@", self->dateformat];
237   if (self->numberformat)
238     [str appendFormat:@" numberformat=%@", self->numberformat];
239
240   return str;
241 }
242
243 @end /* WOText */