2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
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
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.
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
23 #include "WOScriptAssociation.h"
24 #include <NGObjWeb/WOComponent.h>
27 @interface NSObject(Scripting)
29 - (id)evaluateScript:(NSString *)_script language:(NSString *)_language;
33 @implementation WOScriptAssociation
35 static BOOL doDebug = NO;
38 return [super version] + 0 /* v2 */;
41 NSAssert2([super version] == 2,
42 @"invalid superclass (%@) version %i !",
43 NSStringFromClass([self superclass]), [super version]);
45 + (NSString *)defaultScriptLanguage {
49 - (id)initWithScript:(NSString *)_script language:(NSString *)_language {
50 if ([_language length] == 0)
51 _language = [WOScriptAssociation defaultScriptLanguage];
53 if ([_script length] == 0) {
55 NSLog(@"WARNING(%s): got passed an empty script ...",
62 if ((self = [super init])) {
63 /* should compile script if possible !!! */
64 self->script = [_script copy];
65 self->language = [_language copy];
70 return [self initWithScript:nil language:nil];
72 - (id)initWithString:(NSString *)_s {
73 if ([_s length] == 0) {
75 NSLog(@"WARNING(%s): got passed an empty script ...",
81 return [self initWithScript:_s language:nil];
85 [self->script release];
86 [self->language release];
92 - (NSString *)script {
95 - (NSString *)language {
96 return self->language;
101 - (void)setValue:(id)_value inComponent:(WOComponent *)_component {
103 [NSException raise:@"AssociationException"
104 format:@"association value is not settable !"];
106 - (id)valueInComponent:(WOComponent *)_component {
107 return [_component evaluateScript:self->script language:self->language];
110 - (BOOL)isValueConstant {
113 - (BOOL)isValueSettable {
119 - (id)copyWithZone:(NSZone *)_zone {
120 /* script associations are immutable and don't need to be copied */
121 return [self retain];
124 - (NSString *)description {
125 NSMutableString *str;
128 str = [NSMutableString stringWithCapacity:64];
129 [str appendFormat:@"<%@[0x%08X]: script(%@)=",
130 NSStringFromClass([self class]), self, [self language]];
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]];
140 v = [v stringByApplyingCEscaping];
141 [str appendString:v];
144 [str appendString:@">"];
148 @end /* WOScriptAssociation */
151 @interface WOAssociation(Blah)
152 - (id)initWithValue:(id)_value;
155 @implementation WOAssociation(Overload)
157 + (WOAssociation *)associationWithValue:(id)_value {
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];
168 return [[[NSClassFromString(@"WOValueAssociation")
169 alloc] initWithValue:_value] autorelease];
172 @end /* WOAssociation(Overload) */