Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 339 lines (307 sloc) 9.511 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
//
// CriteriaTree.m
// Vienna
//
// Created by Steve on Thu Apr 29 2004.
// Copyright (c) 2004-2005 Steve Palmer. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "Criteria.h"
#import "XMLParser.h"

@implementation Criteria

/* init
* Initialise an empty Criteria.
*/
-(id)init
{
return [self initWithField:@"" withOperator:0 withValue:@""];
}

/* initWithField
* Initalises a new Criteria with the specified values.
*/
-(id)initWithField:(NSString *)newField withOperator:(CriteriaOperator)newOperator withValue:(NSString *)newValue
{
if ((self = [super init]) != nil)
{
[self setField:newField];
[self setOperator:newOperator];
[self setValue:newValue];
}
return self;
}

/* operatorString
* Returns the string representation of the operator.
* Note: do NOT localise these strings. For UI display, the caller should use
* NSLocalizedString() on the return value.
*/
+(NSString *)stringFromOperator:(CriteriaOperator)op
{
NSString * operatorString = nil;
switch (op)
{
case MA_CritOper_Is: operatorString = @"is"; break;
case MA_CritOper_IsNot: operatorString = @"is not"; break;
case MA_CritOper_IsAfter: operatorString = @"is after"; break;
case MA_CritOper_IsBefore: operatorString = @"is before"; break;
case MA_CritOper_IsOnOrAfter: operatorString = @"is on or after"; break;
case MA_CritOper_IsOnOrBefore: operatorString = @"is on or before"; break;
case MA_CritOper_Contains: operatorString = @"contains"; break;
case MA_CritOper_NotContains: operatorString = @"does not contain"; break;
case MA_CritOper_Under: operatorString = @"under"; break;
case MA_CritOper_NotUnder: operatorString = @"not under"; break;
case MA_CritOper_IsLessThan: operatorString = @"is less than"; break;
case MA_CritOper_IsGreaterThan: operatorString = @"is greater than"; break;
case MA_CritOper_IsLessThanOrEqual: operatorString = @"is less than or equal to"; break;
case MA_CritOper_IsGreaterThanOrEqual: operatorString = @"is greater than or equal to"; break;
}
return operatorString;
}

/* operatorFromString
* Given a string representing an operator, returns the CriteriaOperator value
* that represents that string.
*/
+(CriteriaOperator)operatorFromString:(NSString *)string
{
NSArray * operatorArray = [Criteria arrayOfOperators];
unsigned int index;

for (index = 0; index < [operatorArray count]; ++index)
{
CriteriaOperator op = [[operatorArray objectAtIndex:index] intValue];
if ([string isEqualToString:[Criteria stringFromOperator:op]])
return op;
}
return 0;
}

/* arrayOfOperators
* Returns an array of NSNumbers that represent all supported operators.
*/
+(NSArray *)arrayOfOperators
{
return [NSArray arrayWithObjects:
[NSNumber numberWithInt:MA_CritOper_Is],
[NSNumber numberWithInt:MA_CritOper_IsNot],
[NSNumber numberWithInt:MA_CritOper_IsAfter],
[NSNumber numberWithInt:MA_CritOper_IsBefore],
[NSNumber numberWithInt:MA_CritOper_IsOnOrAfter],
[NSNumber numberWithInt:MA_CritOper_IsOnOrBefore],
[NSNumber numberWithInt:MA_CritOper_Contains],
[NSNumber numberWithInt:MA_CritOper_NotContains],
[NSNumber numberWithInt:MA_CritOper_IsLessThan],
[NSNumber numberWithInt:MA_CritOper_IsLessThanOrEqual],
[NSNumber numberWithInt:MA_CritOper_IsGreaterThan],
[NSNumber numberWithInt:MA_CritOper_IsGreaterThanOrEqual],
[NSNumber numberWithInt:MA_CritOper_Under],
[NSNumber numberWithInt:MA_CritOper_NotUnder],
nil];
}

/* setField
* Sets the field element of a criteria.
*/
-(void)setField:(NSString *)newField
{
[newField retain];
[field release];
field = newField;
}

/* setOperator
* Sets the operator element of a criteria.
*/
-(void)setOperator:(CriteriaOperator)newOperator
{
// Convert deprecated under/not-under operators
// to is/is-not.
if (newOperator == MA_CritOper_Under)
newOperator = MA_CritOper_Is;
if (newOperator == MA_CritOper_NotUnder)
newOperator = MA_CritOper_IsNot;
operator = newOperator;
}

/* setValue
* Sets the value element of a criteria.
*/
-(void)setValue:(NSString *)newValue
{
[newValue retain];
[value release];
value = newValue;
}

/* field
* Returns the field element of a criteria.
*/
-(NSString *)field
{
return field;
}

/* operator
* Returns the operator element of a criteria
*/
-(CriteriaOperator)operator
{
return operator;
}

/* value
* Returns the value element of a criteria.
*/
-(NSString *)value
{
return value;
}

/* dealloc
* Clean up and release resources.
*/
-(void)dealloc
{
[value release];
[field release];
[super dealloc];
}
@end

@implementation CriteriaTree

/* init
* Initialise an empty CriteriaTree
*/
-(id)init
{
return [self initWithString:@""];
}

/* initWithString
* Initialises an criteria tree object with the specified string. The caller is responsible for
* releasing the tree.
*/
-(id)initWithString:(NSString *)string
{
if ((self = [super init]) != nil)
{
criteriaTree = [[NSMutableArray alloc] init];
condition = MA_CritCondition_All;
NSData * data = [NSData dataWithBytes:[string cString] length:[string length]];
XMLParser * xmlTree = [[XMLParser alloc] init];
if ([data length] > 0 && [xmlTree setData:data])
{
XMLParser * criteriaGroup = [xmlTree treeByName:@"criteriagroup"];
int index = 0;

// For backward compatibility, the absence of the condition attribute
// assumes that we're matching ALL conditions.
condition = [CriteriaTree conditionFromString:[criteriaGroup valueOfAttribute:@"condition"]];
if (condition == MA_CritCondition_Invalid)
condition = MA_CritCondition_All;

if (criteriaGroup != nil)
while (index < [criteriaGroup countOfChildren])
{
XMLParser * subTree = [criteriaGroup treeByIndex:index];
NSString * fieldName = [subTree valueOfAttribute:@"field"];
NSString * operator = [[subTree treeByName:@"operator"] valueOfElement];
NSString * value = [[subTree treeByName:@"value"] valueOfElement];

Criteria * newCriteria = [[Criteria alloc] init];
[newCriteria setField:fieldName];
[newCriteria setOperator:[operator intValue]];
[newCriteria setValue:value];
[self addCriteria:newCriteria];
[newCriteria release];
++index;
}
}
[xmlTree release];
}
return self;
}

/* conditionFromString
* Converts a condition string to its condition value. Returns -1 if the
* string is invalid.
* Note: Do NOT localise these strings. They're written to the XML file.
*/
+(CriteriaCondition)conditionFromString:(NSString *)string
{
if (string != nil)
{
if ([[string lowercaseString] isEqualToString:@"any"])
return MA_CritCondition_Any;
if ([[string lowercaseString] isEqualToString:@"all"])
return MA_CritCondition_All;
}
return MA_CritCondition_Invalid;
}

/* conditionToString
* Returns the string representation of the specified condition.
* Note: Do NOT localise these strings. They're written to the XML file.
*/
+(NSString *)conditionToString:(CriteriaCondition)condition
{
if (condition == MA_CritCondition_Any)
return @"any";
if (condition == MA_CritCondition_All)
return @"all";
return @"";
}

/* condition
* Return the criteria condition.
*/
-(CriteriaCondition)condition
{
return condition;
}

/* setCondition
* Sets the criteria condition.
*/
-(void)setCondition:(CriteriaCondition)newCondition
{
condition = newCondition;
}

/* criteriaEnumerator
* Returns an enumerator that will iterate over the criteria
* object. We do it this way because we can't necessarily guarantee
* that the criteria will be stored in an NSArray or any other collection
* object for which NSEnumerator is supported.
*/
-(NSEnumerator *)criteriaEnumerator
{
return [criteriaTree objectEnumerator];
}

/* addCriteria
* Adds the specified criteria to the criteria array.
*/
-(void)addCriteria:(Criteria *)newCriteria
{
[criteriaTree addObject:newCriteria];
}

/* string
* Returns the complete criteria tree as a string.
*/
-(NSString *)string
{
XMLParser * newTree = [[XMLParser alloc] initWithEmptyTree];
NSDictionary * conditionDict = [NSDictionary dictionaryWithObject:[CriteriaTree conditionToString:condition] forKey:@"condition"];
XMLParser * groupTree = [newTree addTree:@"criteriagroup" withAttributes:conditionDict];
unsigned int index;

for (index = 0; index < [criteriaTree count]; ++index)
{
Criteria * criteria = [criteriaTree objectAtIndex:index];
NSDictionary * criteriaDict = [NSDictionary dictionaryWithObject:[criteria field] forKey:@"field"];
XMLParser * oneCriteriaTree = [groupTree addTree:@"criteria" withAttributes:criteriaDict];

[oneCriteriaTree addTree:@"operator" withElement:[NSString stringWithFormat:@"%d", [criteria operator]]];
[oneCriteriaTree addTree:@"value" withElement:[criteria value]];
}

NSString * criteriaString = [newTree xmlForTree];
[newTree release];
return criteriaString;
}

/* dealloc
* Clean up and release resources.
*/
-(void)dealloc
{
[criteriaTree release];
[super dealloc];
}
@end
Something went wrong with that request. Please try again.