]> err.no Git - sope/blob - sope-core/NGExtensions/NGRuleEngine.subproj/NGRule.m
Drop apache 1 build-dependency
[sope] / sope-core / NGExtensions / NGRuleEngine.subproj / NGRule.m
1 /*
2   Copyright (C) 2003-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 "NGRule.h"
23 #include "NGRuleAssignment.h"
24 #include "NGRuleParser.h"
25 #include "common.h"
26 #import <EOControl/EOQualifier.h>
27
28 @implementation NGRule
29
30 + (id)ruleWithQualifier:(EOQualifier *)_q action:(id)_action priority:(int)_p {
31   return [[[self alloc] initWithQualifier:_q action:_action priority:_p] 
32                  autorelease];
33 }
34 + (id)ruleWithQualifier:(EOQualifier *)_q action:(id)_action {
35   return [self ruleWithQualifier:_q action:_action priority:0];
36 }
37
38 - (id)initWithString:(NSString *)_s {
39   [self release];
40   return [[[NGRuleParser sharedRuleParser] parseRuleFromString:_s] retain];
41 }
42 - (id)initWithPropertyList:(id)_plist {
43   [self release];
44   return [[[NGRuleParser sharedRuleParser] 
45                          parseRuleFromPropertyList:_plist] retain];
46 }
47
48 - (id)initWithQualifier:(EOQualifier *)_q action:(id)_action priority:(int)_p {
49   if ((self = [super init])) {
50     self->qualifier = [_q      retain];
51     self->action    = [_action retain];
52     self->priority  = _p;
53   }
54   return self;
55 }
56 - (id)init {
57   return [self initWithQualifier:nil action:nil priority:0];
58 }
59
60 - (void)dealloc {
61   [self->qualifier release];
62   [self->action    release];
63   [super dealloc];
64 }
65
66 /* accessors */
67
68 - (void)setQualifier:(EOQualifier *)_q {
69   ASSIGN(self->qualifier, _q);
70 }
71 - (EOQualifier *)qualifier {
72   return self->qualifier;
73 }
74
75 - (void)setAction:(id)_action {
76   ASSIGN(self->action, _action);
77 }
78 - (id)action {
79   return self->action;
80 }
81
82 - (void)setPriority:(int)_pri {
83   self->priority = _pri;
84 }
85 - (int)priority {
86   return self->priority;
87 }
88
89 /* operations */
90
91 - (BOOL)isCandidateForKey:(NSString *)_key {
92   id o;
93   if (_key == nil) return YES;
94   
95   o = [self action];
96   if ([o respondsToSelector:@selector(isCandidateForKey:)])
97     return [o isCandidateForKey:_key];
98   
99   return NO; /* action is not an assignment ! */
100 }
101
102 - (id)fireInContext:(id)_ctx {
103   return [self->action fireInContext:_ctx];
104 }
105
106 /* key/value archiving */
107
108 - (id)initWithKeyValueUnarchiver:(EOKeyValueUnarchiver *)_unarchiver {
109   return [self initWithQualifier:[_unarchiver decodeObjectForKey:@"lhs"]
110                action:[_unarchiver decodeObjectForKey:@"rhs"]
111                priority:[_unarchiver decodeIntForKey:@"author"]];
112 }
113 - (void)encodeWithKeyValueArchiver:(EOKeyValueArchiver *)_archiver {
114   [_archiver encodeInt:[self priority]     forKey:@"author"];
115   [_archiver encodeObject:[self qualifier] forKey:@"lhs"];
116   [_archiver encodeObject:[self action]    forKey:@"rhs"];
117 }
118
119 /* representations */
120
121 - (NSString *)stringValue {
122   NSString *sq, *sa;
123   
124   sq = [[self qualifier] description];
125   sa = [[self action]    description];
126   return [NSString stringWithFormat:@"%@ => %@ ; %i",
127                      sq, sa, [self priority]];
128 }
129
130 - (NSString *)description {
131   return [self stringValue];
132 }
133
134 @end /* NGRule(Parsing) */