]> err.no Git - sope/blob - sope-appserver/NGObjWeb/Templates/WOComponentScriptPart.m
use %p for pointer formats, fixed gcc 4.1 warnings, minor code improvs
[sope] / sope-appserver / NGObjWeb / Templates / WOComponentScriptPart.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 <NGObjWeb/WOTemplateBuilder.h>
23 #include <NGObjWeb/WOComponent.h>
24 #include "common.h"
25
26 @implementation WOComponentScriptPart
27
28 - (id)initWithURL:(NSURL *)_url startLine:(unsigned)_ln script:(NSString *)_s {
29   self->url       = RETAIN(_url);
30   self->startLine = _ln;
31   self->script    = [_s copy];
32   return self;
33 }
34 - (id)initWithContentsOfFile:(NSString *)_path {
35   NSURL    *furl;
36   NSString *s;
37   
38   if ([_path length] == 0) {
39     RELEASE(self);
40     return nil;
41   }
42   
43   if ((s = [[NSString alloc] initWithContentsOfFile:_path]) == nil) {
44     RELEASE(self);
45     return nil;
46   }
47   
48   furl = [[NSURL alloc] initFileURLWithPath:_path];
49   self = [self initWithURL:furl startLine:0 script:s];
50   RELEASE(furl);
51   RELEASE(s);
52   return self;
53 }
54
55 - (void)dealloc {
56   RELEASE(self->url);
57   RELEASE(self->script);
58   [super dealloc];
59 }
60
61 /* operations */
62
63 - (NSException *)handleException:(NSException *)_exception {
64   if (self->startLine == 0)
65     return _exception;
66   
67   if ([[_exception name] isEqualToString:@"JavaScriptError"]) {
68     /* correct script start lines to actual value */
69     NSMutableDictionary *ui;
70     int line;
71     
72     ui = [[_exception userInfo] mutableCopy];
73     line = [[ui objectForKey:@"line"] intValue];
74     if (ui == nil) ui = [[NSMutableDictionary alloc] init];
75     [ui setObject:[NSNumber numberWithInt:(line + self->startLine)]
76         forKey:@"line"];
77     [_exception setUserInfo:ui];
78     RELEASE(ui);
79   }
80   return _exception;
81 }
82
83 - (void)initScriptWithComponent:(WOComponent *)_object {
84 #if 1
85   [self errorWithFormat:@"cannot apply script on object: %@", _object];
86 #else
87   /* fixed on JavaScript, part should have a language ... */
88   NS_DURING {
89     [_object evaluateScript:self->script language:@"javascript"
90              source:[self->url absoluteString] line:self->startLine];
91   }
92   NS_HANDLER
93     [[self handleException:localException] raise];
94   NS_ENDHANDLER;
95 #endif
96 }
97
98 /* description */
99
100 - (NSString *)description {
101   NSMutableString *ms;
102
103   ms = [NSMutableString stringWithCapacity:32];
104   [ms appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];
105   
106   if (self->url) {
107     if ([self->url isFileURL])
108       [ms appendFormat:@" path=%@", [self->url path]];
109     else
110       [ms appendFormat:@" url=%@", self->url];
111     if (self->startLine > 0)
112       [ms appendFormat:@":%i", self->startLine];
113   }
114   else if (self->startLine > 0)
115     [ms appendFormat:@" line=%@", self->startLine];
116
117   if ([self->script length] == 0)
118     [ms appendString:@" no script"];
119   else if ([self->script length] < 16)
120     [ms appendFormat:@" script=%@", self->script];
121   else
122     [ms appendFormat:@" script=%@...", [self->script substringToIndex:13]];
123   
124   [ms appendString:@">"];
125   return ms;
126 }
127
128 @end /* WOComponentScriptPart */