]> err.no Git - sope/blob - sope-appserver/NGObjWeb/Associations/WOScriptAssociation.m
fixed OGo bug #888
[sope] / sope-appserver / NGObjWeb / Associations / WOScriptAssociation.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 "WOScriptAssociation.h"
24 #include <NGObjWeb/WOComponent.h>
25 #import "common.h"
26
27 @interface NSObject(Scripting)
28
29 - (id)evaluateScript:(NSString *)_script language:(NSString *)_language;
30
31 @end
32
33 @implementation WOScriptAssociation
34
35 static BOOL doDebug = NO;
36
37 + (int)version {
38   return [super version] + 0 /* v2 */;
39 }
40 + (void)initialize {
41   NSAssert2([super version] == 2,
42             @"invalid superclass (%@) version %i !",
43             NSStringFromClass([self superclass]), [super version]);
44 }
45 + (NSString *)defaultScriptLanguage {
46   return @"javascript";
47 }
48
49 - (id)initWithScript:(NSString *)_script language:(NSString *)_language {
50   if ([_language length] == 0)
51     _language = [WOScriptAssociation defaultScriptLanguage];
52   
53   if ([_script length] == 0) {
54     if (doDebug) {
55       NSLog(@"WARNING(%s): got passed an empty script ...",
56             __PRETTY_FUNCTION__);
57     }
58     [self release];
59     return nil;
60   }
61   
62   if ((self = [super init])) {
63     /* should compile script if possible !!! */
64     self->script   = [_script   copy];
65     self->language = [_language copy];
66   }
67   return self;
68 }
69 - (id)init {
70   return [self initWithScript:nil language:nil];
71 }
72 - (id)initWithString:(NSString *)_s {
73   if ([_s length] == 0) {
74     if (doDebug) {
75       NSLog(@"WARNING(%s): got passed an empty script ...", 
76             __PRETTY_FUNCTION__);
77     }
78     [self release];
79     return nil;
80   }
81   return [self initWithScript:_s language:nil];
82 }
83
84 - (void)dealloc {
85   [self->script   release];
86   [self->language release];
87   [super dealloc];
88 }
89
90 /* accessors */
91
92 - (NSString *)script {
93   return self->script;
94 }
95 - (NSString *)language {
96   return self->language;
97 }
98
99 /* value */
100
101 - (void)setValue:(id)_value inComponent:(WOComponent *)_component {
102   // not settable
103   [NSException raise:@"AssociationException"
104                format:@"association value is not settable !"];
105 }
106 - (id)valueInComponent:(WOComponent *)_component {
107   return [_component evaluateScript:self->script language:self->language];
108 }
109
110 - (BOOL)isValueConstant {
111   return NO;
112 }
113 - (BOOL)isValueSettable {
114   return NO;
115 }
116
117 /* NSCopying */
118
119 - (id)copyWithZone:(NSZone *)_zone {
120   /* script associations are immutable and don't need to be copied */
121   return [self retain];
122 }
123
124 - (NSString *)description {
125   NSMutableString *str;
126   NSString *v;
127
128   str = [NSMutableString stringWithCapacity:64];
129   [str appendFormat:@"<%@[0x%08X]: script(%@)=",
130          NSStringFromClass([self class]), self, [self language]];
131   
132   v = self->script;
133   if ([v length] > 10) {
134       v = [v substringToIndex:9];
135       v = [v stringByApplyingCEscaping];
136       [str appendString:v];
137       [str appendFormat:@"...[len=%i]", [self->script length]];
138   }
139   else {
140       v = [v stringByApplyingCEscaping];
141       [str appendString:v];
142   }
143   
144   [str appendString:@">"];
145   return str;
146 }
147
148 @end /* WOScriptAssociation */
149
150 #if 0
151 @interface WOAssociation(Blah)
152 - (id)initWithValue:(id)_value;
153 @end
154
155 @implementation WOAssociation(Overload)
156
157 + (WOAssociation *)associationWithValue:(id)_value {
158   WOAssociation *a;
159   
160   if ([_value isKindOfClass:[NSString class]]) {
161     if ([(NSString *)_value hasPrefix:@"JS:"]) {
162       _value = [_value substringFromIndex:3];
163       a = [[WOScriptAssociation alloc] initWithJavaScript:_value];
164       return [a autorelease];
165     }
166   }
167   
168   return [[[NSClassFromString(@"WOValueAssociation")
169                              alloc] initWithValue:_value] autorelease];
170 }
171
172 @end /* WOAssociation(Overload) */
173 #endif /* 0 */