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