]> err.no Git - sope/blob - sope-ldap/NGLdap/NGLdapEntry.m
more directory hierarchy reorganisations,
[sope] / sope-ldap / NGLdap / NGLdapEntry.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include "NGLdapEntry.h"
24 #include "NGLdapAttribute.h"
25 #include "NSString+DN.h"
26 #import <EOControl/EOControl.h>
27 #include "common.h"
28
29 @implementation NGLdapEntry
30
31 - (id)initWithDN:(NSString *)_dn attributes:(NSArray *)_attrs {
32   _dn = [_dn lowercaseString];
33   self->dn         = [[[_dn dnComponents] componentsJoinedByString:@","] copy];
34   self->attributes = [_attrs copy];
35   return self;
36 }
37 - (id)init {
38   [self release];
39   return nil;
40 }
41
42 - (void)dealloc {
43   [self->attributes release];
44   [self->dn         release];
45   [super dealloc];
46 }
47
48 /* distinguished name */
49
50 - (NSString *)dn {
51   return self->dn;
52 }
53 - (NSString *)rdn {
54   return [self->dn lastDNComponent];
55 }
56
57 /* class */
58
59 - (NSArray *)objectClasses {
60   NGLdapAttribute *a;
61
62   a = [self attributeWithName:@"objectclass"];
63   
64   return [[a allStringValues] sortedArrayUsingSelector:@selector(compare:)];
65 }
66
67 /* attributes */
68
69 - (unsigned)count {
70   return [self->attributes count];
71 }
72
73 - (NSArray *)attributeNames {
74   NSMutableArray  *ma;
75   NSArray         *a;
76   NSEnumerator    *e;
77   NGLdapAttribute *attr;
78
79   ma = [[NSMutableArray alloc] initWithCapacity:[self->attributes count]];
80
81   e = [self->attributes objectEnumerator];
82   while ((attr = [e nextObject]))
83     [ma addObject:[attr attributeName]];
84   
85   a = [ma copy];
86   [ma release];
87   return [a autorelease];
88 }
89 - (NSDictionary *)attributes {
90   NSMutableDictionary *md;
91   NSDictionary    *d;
92   NSEnumerator    *e;
93   NGLdapAttribute *a;
94   
95   md = [[NSMutableDictionary alloc] initWithCapacity:[self->attributes count]];
96
97   e = [self->attributes objectEnumerator];
98   while ((a = [e nextObject]))
99     [md setObject:a forKey:[a attributeName]];
100   
101   d = [md copy];
102   [md release];
103   return [d autorelease];
104 }
105
106 - (NGLdapAttribute *)attributeWithName:(NSString *)_name {
107   NSEnumerator    *e;
108   NGLdapAttribute *a;
109   
110   if (_name == nil)
111     return nil;
112
113   e = [self->attributes objectEnumerator];
114
115   while ((a = [e nextObject])) {
116     if ([[a attributeName] isEqualToString:_name])
117       return a;
118   }
119   return nil;
120 }
121
122 - (NGLdapAttribute *)attributeWithName:(NSString *)_name
123   language:(NSString *)_language
124 {
125   NSEnumerator    *e;
126   NGLdapAttribute *a;
127   NGLdapAttribute *awl, *al;
128
129   if (_language == nil)
130     return [self attributeWithName:_name];
131
132   awl = al = nil;
133   e = [self->attributes objectEnumerator];
134   while ((a = [e nextObject])) {
135     if ([[a attributeBaseName] isEqualToString:_name]) {
136       NSString *lang;
137       
138       if (al == nil) al = a;
139
140       if ((lang = [a langSubtype])) {
141         if ([lang isEqualToString:_language])
142           return a;
143       }
144       else {
145         awl = a;
146       }
147     }
148   }
149   if (awl) return awl;
150   if (al)  return al;
151   return nil;
152 }
153
154 /* LDIF */
155
156 - (NSString *)ldif {
157   NSMutableString *ms;
158   NSEnumerator    *names;
159   NSString        *cname;
160   
161   ms = [NSMutableString stringWithCapacity:256];
162
163   /* add DN to LDIF */
164   [ms appendString:@"DN: "];
165   [ms appendString:[self dn]];
166   [ms appendString:@"\n"];
167   
168   /* add attributes */
169   names = [[self attributeNames] objectEnumerator];
170   while ((cname = [names nextObject])) {
171     NGLdapAttribute *attr;
172     
173     if ((attr = [self attributeWithName:cname])) {
174       NSEnumerator *values;
175       NSString *value;
176
177       values = [attr stringValueEnumerator];
178       while ((value = [values nextObject])) {
179         [ms appendString:cname];
180         [ms appendString:@": "];
181         [ms appendString:value];
182         [ms appendString:@"\n"];
183       }
184     }
185   }
186   
187   return ms;
188 }
189
190 /* key-value coding */
191
192 - (id)valueForKey:(NSString *)_key {
193   return [self attributeWithName:_key];
194 }
195
196 /* NSCopying */
197
198 - (id)copyWithZone:(NSZone *)_zone {
199   return [[[self class] alloc] initWithDN:self->dn attributes:self->attributes];
200 }
201
202 /* description */
203
204 - (NSString *)description {
205   NSMutableString *s;
206   
207   s = [NSMutableString stringWithCapacity:100];
208   [s appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
209   
210   [s appendFormat:@" dn='%@'", [self dn]];
211   
212   [s appendString:@" attrs="];
213   [s appendString:[[self attributes] description]];
214
215   [s appendString:@">"];
216
217   return s;
218 }
219
220 @end /* NGLdapEntry */