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
22 #include "WOScriptAssociation.h"
23 #include <NGObjWeb/WOComponent.h>
26 @interface NSObject(Scripting)
28 - (id)evaluateScript:(NSString *)_script language:(NSString *)_language;
32 @implementation WOScriptAssociation
34 static BOOL doDebug = NO;
37 return [super version] + 0 /* v2 */;
40 NSAssert2([super version] == 2,
41 @"invalid superclass (%@) version %i !",
42 NSStringFromClass([self superclass]), [super version]);
44 + (NSString *)defaultScriptLanguage {
48 - (id)initWithScript:(NSString *)_script language:(NSString *)_language {
49 if ([_language length] == 0)
50 _language = [WOScriptAssociation defaultScriptLanguage];
52 if ([_script length] == 0) {
54 NSLog(@"WARNING(%s): got passed an empty script ...",
61 if ((self = [super init])) {
62 /* should compile script if possible !!! */
63 self->script = [_script copy];
64 self->language = [_language copy];
69 return [self initWithScript:nil language:nil];
71 - (id)initWithString:(NSString *)_s {
72 if ([_s length] == 0) {
74 NSLog(@"WARNING(%s): got passed an empty script ...",
80 return [self initWithScript:_s language:nil];
84 [self->script release];
85 [self->language release];
91 - (NSString *)script {
94 - (NSString *)language {
95 return self->language;
100 - (void)setValue:(id)_value inComponent:(WOComponent *)_component {
102 [NSException raise:@"AssociationException"
103 format:@"association value is not settable !"];
105 - (id)valueInComponent:(WOComponent *)_component {
106 return [_component evaluateScript:self->script language:self->language];
109 - (BOOL)isValueConstant {
112 - (BOOL)isValueSettable {
118 - (id)copyWithZone:(NSZone *)_zone {
119 /* script associations are immutable and don't need to be copied */
120 return [self retain];
123 - (NSString *)description {
124 NSMutableString *str;
127 str = [NSMutableString stringWithCapacity:64];
128 [str appendFormat:@"<%@[0x%p]: script(%@)=",
129 NSStringFromClass([self class]), self, [self language]];
132 if ([v length] > 10) {
133 v = [v substringToIndex:9];
134 v = [v stringByApplyingCEscaping];
135 [str appendString:v];
136 [str appendFormat:@"...[len=%i]", [self->script length]];
139 v = [v stringByApplyingCEscaping];
140 [str appendString:v];
143 [str appendString:@">"];
147 @end /* WOScriptAssociation */
150 @interface WOAssociation(Blah)
151 - (id)initWithValue:(id)_value;
154 @implementation WOAssociation(Overload)
156 + (WOAssociation *)associationWithValue:(id)_value {
159 if ([_value isKindOfClass:[NSString class]]) {
160 if ([(NSString *)_value hasPrefix:@"JS:"]) {
161 _value = [_value substringFromIndex:3];
162 a = [[WOScriptAssociation alloc] initWithJavaScript:_value];
163 return [a autorelease];
167 return [[[NSClassFromString(@"WOValueAssociation")
168 alloc] initWithValue:_value] autorelease];
171 @end /* WOAssociation(Overload) */