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 "NGLdapEntry.h"
23 #include "NGLdapAttribute.h"
24 #include "NSString+DN.h"
25 #import <EOControl/EOControl.h>
28 @implementation NGLdapEntry
30 - (id)initWithDN:(NSString *)_dn attributes:(NSArray *)_attrs {
31 _dn = [_dn lowercaseString];
32 self->dn = [[[_dn dnComponents] componentsJoinedByString:@","] copy];
33 self->attributes = [_attrs copy];
42 [self->attributes release];
47 /* distinguished name */
53 return [self->dn lastDNComponent];
58 - (NSArray *)objectClasses {
61 a = [self attributeWithName:@"objectclass"];
63 return [[a allStringValues] sortedArrayUsingSelector:@selector(compare:)];
69 return [self->attributes count];
72 - (NSArray *)attributeNames {
76 NGLdapAttribute *attr;
78 ma = [[NSMutableArray alloc] initWithCapacity:[self->attributes count]];
80 e = [self->attributes objectEnumerator];
81 while ((attr = [e nextObject]))
82 [ma addObject:[attr attributeName]];
86 return [a autorelease];
88 - (NSDictionary *)attributes {
89 NSMutableDictionary *md;
94 md = [[NSMutableDictionary alloc] initWithCapacity:[self->attributes count]];
96 e = [self->attributes objectEnumerator];
97 while ((a = [e nextObject]))
98 [md setObject:a forKey:[a attributeName]];
102 return [d autorelease];
105 - (NGLdapAttribute *)attributeWithName:(NSString *)_name {
112 e = [self->attributes objectEnumerator];
114 while ((a = [e nextObject])) {
115 if ([[a attributeName] isEqualToString:_name])
121 - (NGLdapAttribute *)attributeWithName:(NSString *)_name
122 language:(NSString *)_language
126 NGLdapAttribute *awl, *al;
128 if (_language == nil)
129 return [self attributeWithName:_name];
132 e = [self->attributes objectEnumerator];
133 while ((a = [e nextObject])) {
134 if ([[a attributeBaseName] isEqualToString:_name]) {
137 if (al == nil) al = a;
139 if ((lang = [a langSubtype])) {
140 if ([lang isEqualToString:_language])
160 ms = [NSMutableString stringWithCapacity:256];
163 [ms appendString:@"DN: "];
164 [ms appendString:[self dn]];
165 [ms appendString:@"\n"];
168 names = [[self attributeNames] objectEnumerator];
169 while ((cname = [names nextObject])) {
170 NGLdapAttribute *attr;
172 if ((attr = [self attributeWithName:cname])) {
173 NSEnumerator *values;
176 values = [attr stringValueEnumerator];
177 while ((value = [values nextObject])) {
178 [ms appendString:cname];
179 [ms appendString:@": "];
180 [ms appendString:value];
181 [ms appendString:@"\n"];
189 /* key-value coding */
191 - (id)valueForKey:(NSString *)_key {
192 return [self attributeWithName:_key];
197 - (id)copyWithZone:(NSZone *)_zone {
198 return [[[self class] alloc] initWithDN:self->dn attributes:self->attributes];
203 - (NSString *)description {
206 s = [NSMutableString stringWithCapacity:100];
207 [s appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
209 [s appendFormat:@" dn='%@'", [self dn]];
211 [s appendString:@" attrs="];
212 [s appendString:[[self attributes] description]];
214 [s appendString:@">"];
219 @end /* NGLdapEntry */