2007-01-31 18:11:29 +01:00
|
|
|
/* CardElement.m - this file is part of SOPE
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Inverse groupe conseil
|
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSEnumerator.h>
|
2007-02-08 17:25:48 +01:00
|
|
|
#import <Foundation/NSRange.h>
|
2007-01-31 18:11:29 +01:00
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
|
2007-10-29 16:33:51 +01:00
|
|
|
#import <NGExtensions/NGBase64Coding.h>
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
#import <NGExtensions/NGQuotedPrintableCoding.h>
|
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
#import "NSArray+NGCards.h"
|
|
|
|
#import "NSDictionary+NGCards.h"
|
|
|
|
#import "CardVersitRenderer.h"
|
|
|
|
|
|
|
|
#import "CardElement.h"
|
|
|
|
#import "CardGroup.h"
|
|
|
|
|
|
|
|
@implementation CardElement
|
|
|
|
|
|
|
|
+ (id) elementWithTag: (NSString *) aTag
|
|
|
|
{
|
|
|
|
id newElement;
|
|
|
|
|
|
|
|
newElement = [self new];
|
|
|
|
[newElement autorelease];
|
|
|
|
[newElement setTag: aTag];
|
|
|
|
|
|
|
|
return newElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) simpleElementWithTag: (NSString *) aTag
|
|
|
|
value: (NSString *) aValue
|
|
|
|
{
|
|
|
|
id newElement;
|
|
|
|
|
|
|
|
newElement = [self elementWithTag: aTag];
|
|
|
|
[newElement addValue: aValue];
|
|
|
|
|
|
|
|
return newElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) simpleElementWithTag: (NSString *) aTag
|
|
|
|
singleType: (NSString *) aType
|
|
|
|
value: (NSString *) aValue
|
|
|
|
{
|
|
|
|
id newElement;
|
|
|
|
|
|
|
|
newElement = [self simpleElementWithTag: aTag
|
|
|
|
value: aValue];
|
|
|
|
[newElement addType: aType];
|
|
|
|
|
|
|
|
return newElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) elementWithTag: (NSString *) aTag
|
|
|
|
attributes: (NSDictionary *) someAttributes
|
|
|
|
values: (NSArray *) someValues
|
|
|
|
{
|
|
|
|
id newElement;
|
|
|
|
|
|
|
|
newElement = [self new];
|
|
|
|
[newElement autorelease];
|
|
|
|
[newElement setTag: aTag];
|
|
|
|
[newElement addAttributes: someAttributes];
|
|
|
|
[newElement addValues: someValues];
|
|
|
|
|
|
|
|
return newElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
parent = nil;
|
|
|
|
tag = nil;
|
|
|
|
group = nil;
|
|
|
|
values = [NSMutableArray new];
|
|
|
|
attributes = [NSMutableDictionary new];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if (tag)
|
|
|
|
[tag release];
|
|
|
|
[values release];
|
|
|
|
[attributes release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setParent: (CardGroup *) aParent
|
|
|
|
{
|
|
|
|
parent = aParent;
|
|
|
|
}
|
|
|
|
|
2007-11-18 10:43:52 +01:00
|
|
|
- (id) parent
|
2007-01-31 18:11:29 +01:00
|
|
|
{
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setTag: (NSString *) aTag
|
|
|
|
{
|
2008-07-07 17:50:50 +02:00
|
|
|
ASSIGN (tag, aTag);
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setGroup: (NSString *) aGroup
|
|
|
|
{
|
2008-07-07 17:50:50 +02:00
|
|
|
ASSIGN (group, aGroup);
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) group
|
|
|
|
{
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addValue: (NSString *) aValue
|
|
|
|
{
|
2008-07-07 17:50:50 +02:00
|
|
|
if (!aValue)
|
|
|
|
aValue = @"";
|
2007-01-31 18:11:29 +01:00
|
|
|
[values addObject: aValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addValues: (NSArray *) someValues
|
|
|
|
{
|
|
|
|
[values addObjectsFromArray: someValues];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addAttribute: (NSString *) anAttribute
|
2008-07-07 17:50:50 +02:00
|
|
|
value: (NSString *) aValue
|
2007-01-31 18:11:29 +01:00
|
|
|
{
|
|
|
|
NSMutableArray *attrValues;
|
|
|
|
|
2008-07-07 17:50:50 +02:00
|
|
|
if (!aValue)
|
|
|
|
aValue = @"";
|
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
attrValues = [attributes objectForCaseInsensitiveKey: anAttribute];
|
|
|
|
if (!attrValues)
|
|
|
|
{
|
|
|
|
attrValues = [NSMutableArray new];
|
|
|
|
[attrValues autorelease];
|
|
|
|
[attributes setObject: attrValues forKey: anAttribute];
|
|
|
|
}
|
|
|
|
|
2008-07-07 17:50:50 +02:00
|
|
|
[attrValues addObject: aValue];
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeValue: (NSString *) aValue
|
|
|
|
fromAttribute: (NSString *) anAttribute
|
|
|
|
{
|
|
|
|
NSMutableArray *attrValues;
|
|
|
|
NSString *currentValue;
|
|
|
|
|
2008-07-07 17:50:50 +02:00
|
|
|
if (!aValue)
|
|
|
|
aValue = @"";
|
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
attrValues = [attributes objectForCaseInsensitiveKey: anAttribute];
|
|
|
|
if (attrValues)
|
|
|
|
{
|
|
|
|
currentValue = [attrValues valueForCaseInsensitiveString: aValue];
|
|
|
|
while (currentValue)
|
|
|
|
{
|
|
|
|
[attrValues removeObject: currentValue];
|
|
|
|
currentValue = [attrValues valueForCaseInsensitiveString: aValue];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addAttributes: (NSDictionary *) someAttributes
|
|
|
|
{
|
|
|
|
NSEnumerator *keys;
|
|
|
|
NSString *currentKey;
|
|
|
|
NSMutableArray *oldValues;
|
|
|
|
NSArray *newValues;
|
|
|
|
|
|
|
|
keys = [[someAttributes allKeys] objectEnumerator];
|
|
|
|
currentKey = [keys nextObject];
|
|
|
|
while (currentKey)
|
|
|
|
{
|
|
|
|
oldValues = [attributes objectForCaseInsensitiveKey: currentKey];
|
|
|
|
newValues = [someAttributes objectForKey: currentKey];
|
|
|
|
if (oldValues)
|
|
|
|
[oldValues addObjectsFromArray: newValues];
|
|
|
|
else
|
|
|
|
[attributes setObject: newValues forKey: currentKey];
|
|
|
|
currentKey = [keys nextObject];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addType: (NSString *) aType
|
|
|
|
{
|
|
|
|
[self addAttribute: @"type" value: aType];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) tag
|
|
|
|
{
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) values
|
|
|
|
{
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *) attributes
|
|
|
|
{
|
|
|
|
return attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasAttribute: (NSString *) anAttribute
|
|
|
|
havingValue: (NSString *) aValue
|
|
|
|
{
|
|
|
|
NSArray *attribute;
|
|
|
|
|
|
|
|
attribute = [attributes objectForCaseInsensitiveKey: anAttribute];
|
|
|
|
|
|
|
|
return (attribute && [attribute hasCaseInsensitiveString: aValue]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setValue: (unsigned int) anInt
|
|
|
|
to: (NSString *) aValue
|
|
|
|
{
|
2008-07-07 17:50:50 +02:00
|
|
|
unsigned int count, max;
|
|
|
|
|
|
|
|
if (!aValue)
|
|
|
|
aValue = @"";
|
|
|
|
max = [values count];
|
|
|
|
for (count = max; count <= anInt; count++)
|
2007-01-31 18:11:29 +01:00
|
|
|
[self addValue: @""];
|
2008-07-07 17:50:50 +02:00
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
[values replaceObjectAtIndex: anInt withObject: aValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) value: (unsigned int) anInt
|
|
|
|
{
|
2007-10-29 16:33:51 +01:00
|
|
|
NSString *realValue, *value, *encoding;
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
if ([values count] <= anInt)
|
|
|
|
value = @"";
|
|
|
|
else
|
2007-10-29 16:33:51 +01:00
|
|
|
{
|
|
|
|
realValue = [values objectAtIndex: anInt];
|
|
|
|
encoding = [[self value: 0 ofAttribute: @"encoding"] lowercaseString];
|
|
|
|
if ([encoding length])
|
|
|
|
{
|
|
|
|
if ([encoding isEqualToString: @"quoted-printable"])
|
|
|
|
value = [realValue stringByDecodingQuotedPrintable];
|
|
|
|
else if ([encoding isEqualToString: @"base64"])
|
|
|
|
value = [realValue stringByDecodingBase64];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value = realValue;
|
|
|
|
if (![encoding isEqualToString: @"8bit"])
|
|
|
|
[self logWithFormat: @"unknown encoding '%@'", encoding];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
value = realValue;
|
|
|
|
}
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned int) _namedValue: (NSString *) aValueName
|
|
|
|
{
|
|
|
|
NSString *prefix, *value;
|
|
|
|
unsigned int count, max, result;
|
|
|
|
|
|
|
|
result = NSNotFound;
|
|
|
|
|
|
|
|
prefix = [NSString stringWithFormat: @"%@=", [aValueName uppercaseString]];
|
|
|
|
max = [values count];
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
while (result == NSNotFound && count < max)
|
|
|
|
{
|
|
|
|
value = [[values objectAtIndex: count] uppercaseString];
|
|
|
|
if ([value hasPrefix: prefix])
|
|
|
|
result = count;
|
|
|
|
else
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setNamedValue: (NSString *) aValueName
|
|
|
|
to: (NSString *) aValue
|
|
|
|
{
|
|
|
|
NSString *newValue;
|
|
|
|
unsigned int index;
|
|
|
|
|
2008-07-07 17:50:50 +02:00
|
|
|
if (!aValue)
|
|
|
|
aValue = @"";
|
2007-01-31 18:11:29 +01:00
|
|
|
newValue = [NSString stringWithFormat: @"%@=%@",
|
|
|
|
[aValueName uppercaseString],
|
|
|
|
aValue];
|
|
|
|
index = [self _namedValue: aValueName];
|
|
|
|
if (index == NSNotFound)
|
|
|
|
[self addValue: newValue];
|
|
|
|
else
|
|
|
|
{
|
2008-02-08 20:34:02 +01:00
|
|
|
if ([aValue length])
|
2007-01-31 18:11:29 +01:00
|
|
|
[self setValue: index to: newValue];
|
|
|
|
else
|
|
|
|
[values removeObjectAtIndex: index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) namedValue: (NSString *) aValueName
|
|
|
|
{
|
2007-02-08 17:25:48 +01:00
|
|
|
unsigned int index;
|
|
|
|
NSRange equalSign;
|
2007-01-31 18:11:29 +01:00
|
|
|
NSString *value;
|
|
|
|
|
|
|
|
index = [self _namedValue: aValueName];
|
|
|
|
if (index == NSNotFound)
|
|
|
|
value = @"";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value = [values objectAtIndex: index];
|
2007-02-08 17:25:48 +01:00
|
|
|
equalSign = [value rangeOfString: @"="];
|
|
|
|
if (equalSign.location != NSNotFound)
|
|
|
|
value = [value substringFromIndex: equalSign.location + 1];
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setValue: (unsigned int) anInt
|
|
|
|
ofAttribute: (NSString *) anAttribute
|
|
|
|
to: (NSString *) aValue
|
|
|
|
{
|
|
|
|
NSMutableArray *attrValues;
|
|
|
|
|
2008-07-07 17:50:50 +02:00
|
|
|
if (!aValue)
|
|
|
|
aValue = @"";
|
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
attrValues = [attributes objectForCaseInsensitiveKey: anAttribute];
|
|
|
|
if (!attrValues)
|
|
|
|
{
|
|
|
|
attrValues = [NSMutableArray new];
|
|
|
|
[attrValues autorelease];
|
|
|
|
[attributes setObject: attrValues forKey: anAttribute];
|
|
|
|
}
|
|
|
|
|
|
|
|
while ([attrValues count] <= anInt)
|
|
|
|
[attrValues addObject: @""];
|
|
|
|
[attrValues replaceObjectAtIndex: anInt
|
|
|
|
withObject: aValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) value: (unsigned int) anInt
|
|
|
|
ofAttribute: (NSString *) anAttribute
|
|
|
|
{
|
|
|
|
NSArray *attrValues;
|
|
|
|
NSString *value;
|
|
|
|
|
|
|
|
attrValues = [attributes objectForCaseInsensitiveKey: anAttribute];
|
|
|
|
if (attrValues && [attrValues count] > anInt)
|
|
|
|
value = [attrValues objectAtIndex: anInt];
|
|
|
|
else
|
|
|
|
value = @"";
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) description
|
|
|
|
{
|
|
|
|
NSArray *attrs;
|
|
|
|
NSMutableString *str;
|
|
|
|
unsigned int count, max;
|
|
|
|
NSString *attr;
|
|
|
|
|
|
|
|
str = [NSMutableString stringWithCapacity:64];
|
|
|
|
[str appendFormat:@"<%p[%@]:", self, NSStringFromClass([self class])];
|
|
|
|
if (group)
|
|
|
|
[str appendFormat: @"%@ (group: %@)\n", tag, group];
|
|
|
|
else
|
|
|
|
[str appendFormat: @"%@\n", tag, group];
|
|
|
|
|
|
|
|
attrs = [attributes allKeys];
|
|
|
|
max = [attrs count];
|
|
|
|
if (max > 0)
|
|
|
|
{
|
|
|
|
[str appendFormat: @"\n %d attributes: {\n", [attrs count]];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
attr = [attrs objectAtIndex: count];
|
|
|
|
[str appendFormat: @" %@: %@\n",
|
|
|
|
attr, [attributes objectForKey: attr]];
|
|
|
|
}
|
|
|
|
[str appendFormat: @"}"];
|
|
|
|
}
|
|
|
|
|
|
|
|
max = [values count];
|
|
|
|
if (max > 0)
|
|
|
|
{
|
|
|
|
[str appendFormat: @"\n %d values: {\n", [values count]];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
[str appendFormat: @" %@\n", [values objectAtIndex: count]];
|
|
|
|
[str appendFormat: @"}"];
|
|
|
|
}
|
|
|
|
|
|
|
|
[str appendString:@">"];
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isVoid
|
|
|
|
{
|
|
|
|
BOOL result;
|
|
|
|
NSString *value;
|
|
|
|
NSEnumerator *enu;
|
|
|
|
|
|
|
|
result = YES;
|
|
|
|
|
|
|
|
enu = [values objectEnumerator];
|
|
|
|
value = [enu nextObject];
|
|
|
|
while (value && result)
|
|
|
|
{
|
|
|
|
result = ([value length] == 0);
|
|
|
|
value = [enu nextObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) versitString
|
|
|
|
{
|
|
|
|
NSString *string;
|
2007-03-07 22:20:16 +01:00
|
|
|
CardVersitRenderer *renderer;
|
2007-01-31 18:11:29 +01:00
|
|
|
|
|
|
|
renderer = [CardVersitRenderer new];
|
|
|
|
string = [renderer render: self];
|
|
|
|
[renderer release];
|
|
|
|
|
2007-07-04 17:59:00 +02:00
|
|
|
if ([string hasSuffix: @"\r\n"])
|
|
|
|
string = [string substringToIndex: [string length] - 2];
|
|
|
|
|
|
|
|
return string;
|
2007-01-31 18:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (CardElement *) elementWithClass: (Class) elementClass
|
|
|
|
{
|
|
|
|
CardElement *newElement;
|
|
|
|
|
|
|
|
if ([self isKindOfClass: elementClass])
|
|
|
|
newElement = self;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newElement = [elementClass new];
|
|
|
|
[newElement autorelease];
|
|
|
|
[newElement setTag: tag];
|
|
|
|
[newElement setValuesAsCopy: values];
|
|
|
|
[newElement setAttributesAsCopy: attributes];
|
|
|
|
if (group)
|
|
|
|
[newElement setGroup: group];
|
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
[newElement setParent: parent];
|
|
|
|
[parent replaceThisElement: self
|
|
|
|
withThisOne: newElement];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setValuesAsCopy: (NSMutableArray *) someValues
|
|
|
|
{
|
|
|
|
[values release];
|
|
|
|
values = someValues;
|
|
|
|
[values retain];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setAttributesAsCopy: (NSMutableDictionary *) someAttributes
|
|
|
|
{
|
|
|
|
[attributes release];
|
|
|
|
attributes = someAttributes;
|
|
|
|
[attributes retain];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CardGroup *) searchParentOfClass: (Class) parentClass
|
|
|
|
{
|
|
|
|
CardGroup *current;
|
|
|
|
CardGroup *found;
|
|
|
|
|
|
|
|
found = nil;
|
|
|
|
current = parent;
|
|
|
|
while (current && !found)
|
|
|
|
if ([current isKindOfClass: parentClass])
|
|
|
|
found = current;
|
|
|
|
else
|
|
|
|
current = [current parent];
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NSCopying */
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone *) aZone
|
|
|
|
{
|
|
|
|
CardElement *new;
|
|
|
|
|
|
|
|
new = [[self class] new];
|
|
|
|
[new setTag: [tag copyWithZone: aZone]];
|
|
|
|
[new setGroup: [group copyWithZone: aZone]];
|
|
|
|
[new setParent: parent];
|
|
|
|
[new setValuesAsCopy: [values copyWithZone: aZone]];
|
|
|
|
[new setAttributesAsCopy: [attributes copyWithZone: aZone]];
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2007-11-18 10:43:52 +01:00
|
|
|
/* NSMutableCopying */
|
|
|
|
- (id) mutableCopyWithZone: (NSZone *) aZone
|
|
|
|
{
|
|
|
|
CardElement *new;
|
|
|
|
|
|
|
|
new = [[self class] new];
|
|
|
|
[new setTag: [tag mutableCopyWithZone: aZone]];
|
|
|
|
[new setGroup: [group mutableCopyWithZone: aZone]];
|
|
|
|
[new setParent: parent];
|
|
|
|
[new setValuesAsCopy: [values mutableCopyWithZone: aZone]];
|
|
|
|
[new setAttributesAsCopy: [attributes mutableCopyWithZone: aZone]];
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2007-01-31 18:11:29 +01:00
|
|
|
@end
|