2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo 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 OGo 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 OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "NGLdapEntry.h"
24 #include "NGLdapAttribute.h"
25 #include "NSString+DN.h"
26 #import <EOControl/EOControl.h>
29 @implementation NGLdapEntry
31 - (id)initWithDN:(NSString *)_dn attributes:(NSArray *)_attrs {
32 _dn = [_dn lowercaseString];
33 self->dn = [[[_dn dnComponents] componentsJoinedByString:@","] copy];
34 self->attributes = [_attrs copy];
43 [self->attributes release];
48 /* distinguished name */
54 return [self->dn lastDNComponent];
59 - (NSArray *)objectClasses {
62 a = [self attributeWithName:@"objectclass"];
64 return [[a allStringValues] sortedArrayUsingSelector:@selector(compare:)];
70 return [self->attributes count];
73 - (NSArray *)attributeNames {
77 NGLdapAttribute *attr;
79 ma = [[NSMutableArray alloc] initWithCapacity:[self->attributes count]];
81 e = [self->attributes objectEnumerator];
82 while ((attr = [e nextObject]))
83 [ma addObject:[attr attributeName]];
87 return [a autorelease];
89 - (NSDictionary *)attributes {
90 NSMutableDictionary *md;
95 md = [[NSMutableDictionary alloc] initWithCapacity:[self->attributes count]];
97 e = [self->attributes objectEnumerator];
98 while ((a = [e nextObject]))
99 [md setObject:a forKey:[a attributeName]];
103 return [d autorelease];
106 - (NGLdapAttribute *)attributeWithName:(NSString *)_name {
113 e = [self->attributes objectEnumerator];
115 while ((a = [e nextObject])) {
116 if ([[a attributeName] isEqualToString:_name])
122 - (NGLdapAttribute *)attributeWithName:(NSString *)_name
123 language:(NSString *)_language
127 NGLdapAttribute *awl, *al;
129 if (_language == nil)
130 return [self attributeWithName:_name];
133 e = [self->attributes objectEnumerator];
134 while ((a = [e nextObject])) {
135 if ([[a attributeBaseName] isEqualToString:_name]) {
138 if (al == nil) al = a;
140 if ((lang = [a langSubtype])) {
141 if ([lang isEqualToString:_language])
161 ms = [NSMutableString stringWithCapacity:256];
164 [ms appendString:@"DN: "];
165 [ms appendString:[self dn]];
166 [ms appendString:@"\n"];
169 names = [[self attributeNames] objectEnumerator];
170 while ((cname = [names nextObject])) {
171 NGLdapAttribute *attr;
173 if ((attr = [self attributeWithName:cname])) {
174 NSEnumerator *values;
177 values = [attr stringValueEnumerator];
178 while ((value = [values nextObject])) {
179 [ms appendString:cname];
180 [ms appendString:@": "];
181 [ms appendString:value];
182 [ms appendString:@"\n"];
190 /* key-value coding */
192 - (id)valueForKey:(NSString *)_key {
193 return [self attributeWithName:_key];
198 - (id)copyWithZone:(NSZone *)_zone {
199 return [[[self class] alloc] initWithDN:self->dn attributes:self->attributes];
204 - (NSString *)description {
207 s = [NSMutableString stringWithCapacity:100];
208 [s appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
210 [s appendFormat:@" dn='%@'", [self dn]];
212 [s appendString:@" attrs="];
213 [s appendString:[[self attributes] description]];
215 [s appendString:@">"];
220 @end /* NGLdapEntry */