]> err.no Git - sope/blob - sope-appserver/NGJavaScript/Core+JS.subproj/NSDictionary+JS.m
b4d07ea3ae5247edf0d9fbf41b29596079954d67
[sope] / sope-appserver / NGJavaScript / Core+JS.subproj / NSDictionary+JS.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 "NGJavaScriptContext.h"
24 #import <Foundation/Foundation.h>
25 #import <EOControl/EONull.h>
26 #include "../common.h"
27
28 @implementation NSDictionary(NGJavaScript)
29
30 - (id)valueForJSPropertyNamed:(NSString *)_key {
31   return [self objectForKey:_key];
32 }
33
34 - (id)_jsfunc_allKeys:(NSArray *)_args {
35   return [self allKeys];
36 }
37 - (id)_jsfunc_allKeysForObject:(NSArray *)_args {
38   return [self allKeysForObject:[_args objectAtIndex:0]];
39 }
40 - (id)_jsfunc_allValues:(NSArray *)_args {
41   return [self allValues];
42 }
43
44 - (id)_jsfunc_objectForKey:(NSArray *)_args {
45   return [self objectForKey:[_args objectAtIndex:0]];
46 }
47 - (id)_jsfunc_objectsForKeys:(NSArray *)_args {
48   unsigned count;
49   id notFound;
50   
51   notFound = ((count = [_args count]) > 1)
52     ? [_args objectAtIndex:1]
53     : [EONull null];
54
55   return [self objectsForKeys:[_args objectAtIndex:0] notFoundMarker:notFound];
56 }
57
58 /* IO */
59
60 - (id)_jsfunc_writeToFile:(NSArray *)_args {
61   BOOL atomically;
62   
63   atomically =  ([_args count] > 1)
64     ? [[_args objectAtIndex:1] boolValue]
65     : YES;
66   
67    atomically = [self writeToFile:[[_args objectAtIndex:0] stringValue]
68                       atomically:atomically];
69    return [NSNumber numberWithBool:atomically];
70 }
71
72 @end /* NSDictionary(NGJavaScript) */
73
74 @implementation NSMutableDictionary(NGJavaScript)
75
76 - (BOOL)takeValue:(id)_value forJSPropertyNamed:(NSString *)_key {
77   if ((_value == nil) || (_key == nil))
78       return NO;
79   
80   [self setObject:_value forKey:_key];
81   return YES;
82 }
83
84 /* adding objects */
85
86 - (id)_jsfunc_addEntriesFromDictionary:(NSArray *)_args {
87   NSEnumerator *e;
88   NSDictionary *d;
89
90   e = [_args objectEnumerator];
91   while ((d = [e nextObject]))
92     [self addEntriesFromDictionary:d];
93   return self;
94 }
95 - (id)_jsfunc_setObjectForKey:(NSArray *)_args {
96   [self setObject:[_args objectAtIndex:0] forKey:[_args objectAtIndex:1]];
97   return self;
98 }
99 - (id)_jsfunc_setDictionary:(NSArray *)_args {
100   [self setDictionary:[_args objectAtIndex:0]];
101   return self;
102 }
103
104 /* removing objects */
105
106 - (id)_jsfunc_removeAllObjects:(NSArray *)_args {
107   [self removeAllObjects];
108   return self;
109 }
110 - (id)_jsfunc_removeObjectForKey:(NSArray *)_args {
111   NSEnumerator *e;
112   NSString *d;
113
114   e = [_args objectEnumerator];
115   while ((d = [e nextObject]))
116     [self removeObjectForKey:d];
117   return self;
118 }
119 - (id)_jsfunc_removeObjectsForKeys:(NSArray *)_args {
120   NSEnumerator *e;
121   NSArray *d;
122
123   e = [_args objectEnumerator];
124   while ((d = [e nextObject]))
125     [self removeObjectsForKeys:d];
126   return self;
127 }
128
129 @end /* NSMutableDictionary(NGJavaScript) */