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