Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 341 lines (308 sloc) 9.555 kb
76d2a5b @mstroeck
mstroeck authored
1 //
2 // CriteriaTree.m
3 // Vienna
4 //
5 // Created by Steve on Thu Apr 29 2004.
6 // Copyright (c) 2004-2005 Steve Palmer. All rights reserved.
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20
21 #import "Criteria.h"
22 #import "XMLParser.h"
23
24 @implementation Criteria
25
26 /* init
27 * Initialise an empty Criteria.
28 */
29 -(id)init
30 {
31 return [self initWithField:@"" withOperator:0 withValue:@""];
32 }
33
34 /* initWithField
35 * Initalises a new Criteria with the specified values.
36 */
37 -(id)initWithField:(NSString *)newField withOperator:(CriteriaOperator)newOperator withValue:(NSString *)newValue
38 {
39 if ((self = [super init]) != nil)
40 {
41 [self setField:newField];
42 [self setOperator:newOperator];
43 [self setValue:newValue];
44 }
45 return self;
46 }
47
48 /* operatorString
49 * Returns the string representation of the operator.
50 * Note: do NOT localise these strings. For UI display, the caller should use
51 * NSLocalizedString() on the return value.
52 */
53 +(NSString *)stringFromOperator:(CriteriaOperator)op
54 {
55 NSString * operatorString = nil;
56 switch (op)
57 {
58 case MA_CritOper_Is: operatorString = @"is"; break;
59 case MA_CritOper_IsNot: operatorString = @"is not"; break;
60 case MA_CritOper_IsAfter: operatorString = @"is after"; break;
61 case MA_CritOper_IsBefore: operatorString = @"is before"; break;
62 case MA_CritOper_IsOnOrAfter: operatorString = @"is on or after"; break;
63 case MA_CritOper_IsOnOrBefore: operatorString = @"is on or before"; break;
64 case MA_CritOper_Contains: operatorString = @"contains"; break;
65 case MA_CritOper_NotContains: operatorString = @"does not contain"; break;
66 case MA_CritOper_Under: operatorString = @"under"; break;
67 case MA_CritOper_NotUnder: operatorString = @"not under"; break;
68 case MA_CritOper_IsLessThan: operatorString = @"is less than"; break;
69 case MA_CritOper_IsGreaterThan: operatorString = @"is greater than"; break;
70 case MA_CritOper_IsLessThanOrEqual: operatorString = @"is less than or equal to"; break;
71 case MA_CritOper_IsGreaterThanOrEqual: operatorString = @"is greater than or equal to"; break;
72 }
73 return operatorString;
74 }
75
76 /* operatorFromString
77 * Given a string representing an operator, returns the CriteriaOperator value
78 * that represents that string.
79 */
80 +(CriteriaOperator)operatorFromString:(NSString *)string
81 {
82 NSArray * operatorArray = [Criteria arrayOfOperators];
87178c0 64bit & Lion Full Screen support
Salvatore Ansani authored
83 NSUInteger index;
76d2a5b @mstroeck
mstroeck authored
84
85 for (index = 0; index < [operatorArray count]; ++index)
86 {
87 CriteriaOperator op = [[operatorArray objectAtIndex:index] intValue];
88 if ([string isEqualToString:[Criteria stringFromOperator:op]])
89 return op;
90 }
91 return 0;
92 }
93
94 /* arrayOfOperators
95 * Returns an array of NSNumbers that represent all supported operators.
96 */
97 +(NSArray *)arrayOfOperators
98 {
99 return [NSArray arrayWithObjects:
100 [NSNumber numberWithInt:MA_CritOper_Is],
101 [NSNumber numberWithInt:MA_CritOper_IsNot],
102 [NSNumber numberWithInt:MA_CritOper_IsAfter],
103 [NSNumber numberWithInt:MA_CritOper_IsBefore],
104 [NSNumber numberWithInt:MA_CritOper_IsOnOrAfter],
105 [NSNumber numberWithInt:MA_CritOper_IsOnOrBefore],
106 [NSNumber numberWithInt:MA_CritOper_Contains],
107 [NSNumber numberWithInt:MA_CritOper_NotContains],
108 [NSNumber numberWithInt:MA_CritOper_IsLessThan],
109 [NSNumber numberWithInt:MA_CritOper_IsLessThanOrEqual],
110 [NSNumber numberWithInt:MA_CritOper_IsGreaterThan],
111 [NSNumber numberWithInt:MA_CritOper_IsGreaterThanOrEqual],
112 [NSNumber numberWithInt:MA_CritOper_Under],
113 [NSNumber numberWithInt:MA_CritOper_NotUnder],
114 nil];
115 }
116
117 /* setField
118 * Sets the field element of a criteria.
119 */
120 -(void)setField:(NSString *)newField
121 {
122 [newField retain];
123 [field release];
124 field = newField;
125 }
126
127 /* setOperator
128 * Sets the operator element of a criteria.
129 */
130 -(void)setOperator:(CriteriaOperator)newOperator
131 {
132 // Convert deprecated under/not-under operators
133 // to is/is-not.
134 if (newOperator == MA_CritOper_Under)
135 newOperator = MA_CritOper_Is;
136 if (newOperator == MA_CritOper_NotUnder)
137 newOperator = MA_CritOper_IsNot;
138 operator = newOperator;
139 }
140
141 /* setValue
142 * Sets the value element of a criteria.
143 */
144 -(void)setValue:(NSString *)newValue
145 {
146 [newValue retain];
147 [value release];
148 value = newValue;
149 }
150
151 /* field
152 * Returns the field element of a criteria.
153 */
154 -(NSString *)field
155 {
156 return field;
157 }
158
159 /* operator
160 * Returns the operator element of a criteria
161 */
162 -(CriteriaOperator)operator
163 {
164 return operator;
165 }
166
167 /* value
168 * Returns the value element of a criteria.
169 */
170 -(NSString *)value
171 {
172 return value;
173 }
174
175 /* dealloc
176 * Clean up and release resources.
177 */
178 -(void)dealloc
179 {
180 [value release];
181 [field release];
182 [super dealloc];
183 }
184 @end
185
186 @implementation CriteriaTree
187
188 /* init
189 * Initialise an empty CriteriaTree
190 */
191 -(id)init
192 {
193 return [self initWithString:@""];
194 }
195
196 /* initWithString
197 * Initialises an criteria tree object with the specified string. The caller is responsible for
198 * releasing the tree.
199 */
200 -(id)initWithString:(NSString *)string
201 {
202 if ((self = [super init]) != nil)
203 {
204 criteriaTree = [[NSMutableArray alloc] init];
205 condition = MA_CritCondition_All;
206 const char * utf8String = [string UTF8String];
207 NSData * data = [NSData dataWithBytes:utf8String length:strlen(utf8String)];
208 XMLParser * xmlTree = [[XMLParser alloc] init];
209 if ([data length] > 0 && [xmlTree setData:data])
210 {
211 XMLParser * criteriaGroup = [xmlTree treeByName:@"criteriagroup"];
212 int index = 0;
213
214 // For backward compatibility, the absence of the condition attribute
215 // assumes that we're matching ALL conditions.
216 condition = [CriteriaTree conditionFromString:[criteriaGroup valueOfAttribute:@"condition"]];
217 if (condition == MA_CritCondition_Invalid)
218 condition = MA_CritCondition_All;
219
220 if (criteriaGroup != nil)
221 while (index < [criteriaGroup countOfChildren])
222 {
223 XMLParser * subTree = [criteriaGroup treeByIndex:index];
224 NSString * fieldName = [subTree valueOfAttribute:@"field"];
225 NSString * operator = [[subTree treeByName:@"operator"] valueOfElement];
226 NSString * value = [[subTree treeByName:@"value"] valueOfElement];
227
228 Criteria * newCriteria = [[Criteria alloc] init];
229 [newCriteria setField:fieldName];
230 [newCriteria setOperator:[operator intValue]];
231 [newCriteria setValue:value];
232 [self addCriteria:newCriteria];
233 [newCriteria release];
234 ++index;
235 }
236 }
237 [xmlTree release];
238 }
239 return self;
240 }
241
242 /* conditionFromString
243 * Converts a condition string to its condition value. Returns -1 if the
244 * string is invalid.
245 * Note: Do NOT localise these strings. They're written to the XML file.
246 */
247 +(CriteriaCondition)conditionFromString:(NSString *)string
248 {
249 if (string != nil)
250 {
251 if ([[string lowercaseString] isEqualToString:@"any"])
252 return MA_CritCondition_Any;
253 if ([[string lowercaseString] isEqualToString:@"all"])
254 return MA_CritCondition_All;
255 }
256 return MA_CritCondition_Invalid;
257 }
258
259 /* conditionToString
260 * Returns the string representation of the specified condition.
261 * Note: Do NOT localise these strings. They're written to the XML file.
262 */
263 +(NSString *)conditionToString:(CriteriaCondition)condition
264 {
265 if (condition == MA_CritCondition_Any)
266 return @"any";
267 if (condition == MA_CritCondition_All)
268 return @"all";
269 return @"";
270 }
271
272 /* condition
273 * Return the criteria condition.
274 */
275 -(CriteriaCondition)condition
276 {
277 return condition;
278 }
279
280 /* setCondition
281 * Sets the criteria condition.
282 */
283 -(void)setCondition:(CriteriaCondition)newCondition
284 {
285 condition = newCondition;
286 }
287
288 /* criteriaEnumerator
289 * Returns an enumerator that will iterate over the criteria
290 * object. We do it this way because we can't necessarily guarantee
291 * that the criteria will be stored in an NSArray or any other collection
292 * object for which NSEnumerator is supported.
293 */
294 -(NSEnumerator *)criteriaEnumerator
295 {
296 return [criteriaTree objectEnumerator];
297 }
298
299 /* addCriteria
300 * Adds the specified criteria to the criteria array.
301 */
302 -(void)addCriteria:(Criteria *)newCriteria
303 {
304 [criteriaTree addObject:newCriteria];
305 }
306
307 /* string
308 * Returns the complete criteria tree as a string.
309 */
310 -(NSString *)string
311 {
312 XMLParser * newTree = [[XMLParser alloc] initWithEmptyTree];
313 NSDictionary * conditionDict = [NSDictionary dictionaryWithObject:[CriteriaTree conditionToString:condition] forKey:@"condition"];
314 XMLParser * groupTree = [newTree addTree:@"criteriagroup" withAttributes:conditionDict];
87178c0 64bit & Lion Full Screen support
Salvatore Ansani authored
315 NSUInteger index;
76d2a5b @mstroeck
mstroeck authored
316
317 for (index = 0; index < [criteriaTree count]; ++index)
318 {
319 Criteria * criteria = [criteriaTree objectAtIndex:index];
320 NSDictionary * criteriaDict = [NSDictionary dictionaryWithObject:[criteria field] forKey:@"field"];
321 XMLParser * oneCriteriaTree = [groupTree addTree:@"criteria" withAttributes:criteriaDict];
322
323 [oneCriteriaTree addTree:@"operator" withElement:[NSString stringWithFormat:@"%d", [criteria operator]]];
324 [oneCriteriaTree addTree:@"value" withElement:[criteria value]];
325 }
326
327 NSString * criteriaString = [newTree xmlForTree];
328 [newTree release];
329 return criteriaString;
330 }
331
332 /* dealloc
333 * Clean up and release resources.
334 */
335 -(void)dealloc
336 {
337 [criteriaTree release];
338 [super dealloc];
339 }
340 @end
Something went wrong with that request. Please try again.