2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
22 #include "NGLdapAttribute.h"
25 @implementation NGLdapAttribute
27 - (id)initWithAttributeName:(NSString *)_name values:(NSArray *)_values {
28 self->name = [_name copy];
29 self->values = [_values copy];
32 - (id)initWithAttributeName:(NSString *)_name {
33 return [self initWithAttributeName:_name values:nil];
38 [self->values release];
42 /* attribute name operations */
44 - (NSString *)attributeName {
48 + (NSString *)baseNameOfAttributeName:(NSString *)_attrName {
51 r = [_attrName rangeOfString:@";"];
52 if (r.length == 0) return _attrName;
53 return [_attrName substringToIndex:r.location];
55 - (NSString *)attributeBaseName {
56 return [[self class] baseNameOfAttributeName:[self attributeName]];
59 + (NSArray *)subtypesOfAttributeName:(NSString *)_attrName {
62 parts = [_attrName componentsSeparatedByString:@";"];
64 return [parts count] > 1
65 ? [parts subarrayWithRange:NSMakeRange(1, [parts count] - 1)]
69 - (NSArray *)subtypes {
70 return [[self class] subtypesOfAttributeName:[self attributeName]];
73 - (BOOL)hasSubtype:(NSString *)_subtype {
79 attrName = [self attributeName];
80 _subtype = [NSString stringWithFormat:@";%@;", _subtype];
82 return [attrName rangeOfString:_subtype].length > 0 ? YES : NO;
85 - (NSString *)langSubtype {
89 attrName = [self attributeName];
90 r = [attrName rangeOfString:@";lang-"];
91 if (r.length == 0) return nil;
93 attrName = [attrName substringFromIndex:(r.location + 1)];
95 r = [attrName rangeOfString:@";"];
96 if (r.length > 0) attrName = [attrName substringToIndex:r.location];
104 return [self->values count];
107 - (void)addValue:(id)_value {
108 self->didChange = YES;
110 if (self->values == nil)
111 self->values = [[NSArray alloc] initWithObjects:&_value count:1];
116 self->values = [[tmp arrayByAddingObject:_value] retain];
121 - (NSArray *)allValues {
124 - (NSEnumerator *)valueEnumerator {
125 return [self->values objectEnumerator];
128 - (void)addStringValue:(NSString *)_value {
131 d = [_value dataUsingEncoding:NSUTF8StringEncoding];
136 - (void)catchedDecodeException:(NSException *)_exception {
137 fprintf(stderr, "Got exception %s decoding NSUTF8StringEncoding, "
138 "use defaultCStringEncoding", [[_exception description] cString]);
140 - (NSString *)stringFromData:(NSData *)_data {
141 static NSStringEncoding enc = 0;
144 if (enc == 0) enc = [NSString defaultCStringEncoding];
146 if (_data == nil) return nil;
149 s = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];
151 [self catchedDecodeException:localException];
156 s = [[NSString alloc] initWithData:_data encoding:enc];
160 - (NSString *)stringValueAtIndex:(unsigned)_idx {
163 data = [[self allValues] objectAtIndex:_idx];
164 if (![data isNotNull])
167 return [[self stringFromData:data] autorelease];
170 - (NSArray *)allStringValues {
173 if ((cnt = [self count]) == 0) {
174 return [NSArray array];
181 data = [[self allValues] objectAtIndex:0];
183 if (![data isNotNull])
186 s = [self stringFromData:data];
187 a = [[NSArray alloc] initWithObjects:&s count:1];
189 return [a autorelease];
196 vals = [self allValues];
198 objs = calloc(cnt, sizeof(id));
199 for (i = 0; i < cnt; i++) {
202 if ((data = [vals objectAtIndex:i]) == nil) {
203 NSLog(@"missing data for value at index %i", i);
204 objs[i] = [[NSString alloc] initWithString:@""];
207 objs[i] = (![data isNotNull]) ? (id)@"" : [self stringFromData:data];
211 a = [[NSArray alloc] initWithObjects:objs count:cnt];
213 for (i = 0; i < cnt; i++)
216 free(objs); objs = NULL;
218 return [a autorelease];
221 - (NSEnumerator *)stringValueEnumerator {
222 return [[self allStringValues] objectEnumerator];
227 - (id)copyWithZone:(NSZone *)_zone {
228 return [[[self class] alloc] initWithAttributeName:self->name
229 values:self->values];
234 - (NSString *)stringValue {
241 ms = [[NSMutableString alloc] initWithCapacity:100];
243 e = [self stringValueEnumerator];
245 while ((value = [e nextObject])) {
246 if (isFirst) isFirst = NO;
247 else [ms appendString:@","];
248 [ms appendString:[value description]];
253 return [s autorelease];
256 - (NSString *)description {
259 s = [NSMutableString stringWithCapacity:100];
260 [s appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
262 [s appendFormat:@" name='%@'", [self attributeName]];
263 [s appendString:@" values="];
264 [s appendString:[self stringValue]];
265 [s appendString:@">"];
270 @end /* NGLdapAttribute */