2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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
11 SOPE 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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #import <Foundation/NSNumberFormatter.h>
25 #import <Foundation/NSDateFormatter.h>
27 @interface WOText : WOInput
31 // inherited: disabled
36 WOAssociation *numberformat; // string
37 WOAssociation *dateformat; // string
38 WOAssociation *formatter;
43 @interface NSObject(UsedKeyPath)
44 - (NSString *)keyPath;
47 @implementation WOText
49 - (id)initWithName:(NSString *)_name
50 associations:(NSDictionary *)_config
51 template:(WOElement *)_t
54 if ((self = [super initWithName:_name associations:_config template:_t])) {
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");
61 if (self->formatter == nil) {
62 if ([_config objectForKey:@"formatterClass"] != nil) {
65 className = OWGetProperty(_config, @"formatterClass");
66 className = [className autorelease];
68 className = [className valueInComponent:nil];
69 className = NSClassFromString(className);
70 className = [[className alloc] init];
72 self->formatter = [WOAssociation associationWithValue:className];
73 self->formatter = [self->formatter retain];
81 if (self->formatter) num++;
82 if (self->numberformat) num++;
83 if (self->dateformat) num++;
85 NSLog(@"WARNING: more than one formats specified in element %@", self);
92 [self->numberformat release];
93 [self->dateformat release];
94 [self->formatter release];
102 static inline NSFormatter *_getFormatter(WOText *self, WOContext *_ctx) {
103 NSFormatter *fmt = nil;
105 if (self->numberformat != nil) {
106 fmt = [[[NSNumberFormatter alloc] init] autorelease];
107 [(NSNumberFormatter *)fmt setFormat:
108 [self->numberformat valueInComponent:[_ctx component]]];
110 else if (self->dateformat != nil) {
111 fmt = [[NSDateFormatter alloc]
113 [self->dateformat valueInComponent:
115 allowNaturalLanguage:NO];
116 fmt = [fmt autorelease];
118 else if (self->formatter) {
119 fmt = [self->formatter valueInComponent:[_ctx component]];
125 /* handle requests */
127 - (id)parseFormValue:(id)_value inContext:(WOContext *)_ctx {
129 NSException *formatException = nil;
130 NSString *keyPath = nil;
131 NSString *errorText = nil;
134 fmt = _getFormatter(self, _ctx);
136 return [super parseFormValue:_value inContext:_ctx];
138 //fmt = [self->formatter valueInComponent:[_ctx component]];
140 if ([fmt getObjectValue:&object forString:[_value stringValue]
141 errorDescription:&errorText]) {
147 if ([self->value respondsToSelector:@selector(keyPath)])
148 keyPath = [(id)self->value keyPath];
150 formatException = [NSException exceptionWithName:@"WOValidationException"
154 [[_ctx component] validationFailedWithException:formatException
160 /* generate response */
162 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
163 WOComponent *sComponent;
169 sComponent = [_ctx component];
170 v = [self->value valueInComponent:sComponent];
171 r = [self->rows unsignedIntValueInComponent:sComponent];
172 c = [self->cols unsignedIntValueInComponent:sComponent];
174 fmt = _getFormatter(self, _ctx);
176 NSString *formattedObj = nil;
178 formattedObj = [fmt editingStringForObjectValue:v];
184 WOResponse_AddCString(_response, "<textarea name=\"");
185 [_response appendContentHTMLAttributeValue:OWFormElementName(self, _ctx)];
186 WOResponse_AddChar(_response, '"');
188 WOResponse_AddCString(_response, " rows=\"");
189 WOResponse_AddUInt(_response, r);
190 WOResponse_AddChar(_response, '"');
193 WOResponse_AddCString(_response, " cols=\"");
194 WOResponse_AddUInt(_response, c);
195 WOResponse_AddChar(_response, '"');
198 if ([self->disabled boolValueInComponent:sComponent])
199 WOResponse_AddCString(_response, " disabled=\"disabled\"");
201 [self appendExtraAttributesToResponse:_response inContext:_ctx];
202 if (self->otherTagString) {
205 s = [self->otherTagString stringValueInComponent:[_ctx component]];
206 WOResponse_AddChar(_response, ' ');
207 WOResponse_AddString(_response, s);
209 WOResponse_AddChar(_response, '>');
211 if ([v length] > 0) {
215 ua = [[_ctx request] headerForKey:@"user-agent"];
217 if ([ua rangeOfString:@"Opera"].length > 0)
221 v = [v stringByReplacingString:@"\r" withString:@""];
223 [_response appendContentHTMLString:v];
226 WOResponse_AddCString(_response, "</textarea>");
231 - (NSString *)associationDescription {
232 NSMutableString *str = nil;
234 str = [NSMutableString stringWithCapacity:64];
235 [str appendString:[super associationDescription]];
237 if (self->rows) [str appendFormat:@" rows=%@", self->rows];
238 if (self->cols) [str appendFormat:@" cols=%@", self->cols];
239 if (self->formatter) [str appendFormat:@" formatter=%@", self->formatter];
240 if (self->dateformat) [str appendFormat:@" dateformat=%@", self->dateformat];
241 if (self->numberformat)
242 [str appendFormat:@" numberformat=%@", self->numberformat];