]> err.no Git - sope/blob - sope-ldap/NGLdap/NGLdapAttribute.m
fixed copyrights for 2005
[sope] / sope-ldap / NGLdap / NGLdapAttribute.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 "NGLdapAttribute.h"
23 #include "common.h"
24
25 @implementation NGLdapAttribute
26
27 - (id)initWithAttributeName:(NSString *)_name values:(NSArray *)_values {
28   self->name   = [_name   copy];
29   self->values = [_values copy];
30   return self;
31 }
32 - (id)initWithAttributeName:(NSString *)_name {
33   return [self initWithAttributeName:_name values:nil];
34 }
35
36 - (void)dealloc {
37   [self->name   release];
38   [self->values release];
39   [super dealloc];
40 }
41
42 /* attribute name operations */
43
44 - (NSString *)attributeName {
45   return self->name;
46 }
47
48 + (NSString *)baseNameOfAttributeName:(NSString *)_attrName {
49   NSRange r;
50   
51   r = [_attrName rangeOfString:@";"];
52   if (r.length == 0) return _attrName;
53   return [_attrName substringToIndex:r.location];
54 }
55 - (NSString *)attributeBaseName {
56   return [[self class] baseNameOfAttributeName:[self attributeName]];
57 }
58
59 + (NSArray *)subtypesOfAttributeName:(NSString *)_attrName {
60   NSArray *parts;
61   
62   parts = [_attrName componentsSeparatedByString:@";"];
63
64   return [parts count] > 1
65     ? [parts subarrayWithRange:NSMakeRange(1, [parts count] - 1)]
66     : [NSArray array];
67 }
68
69 - (NSArray *)subtypes {
70   return [[self class] subtypesOfAttributeName:[self attributeName]];
71 }
72
73 - (BOOL)hasSubtype:(NSString *)_subtype {
74   NSString *attrName;
75   
76   if (_subtype == nil)
77     return NO;
78   
79   attrName = [self attributeName];
80   _subtype = [NSString stringWithFormat:@";%@;", _subtype];
81   
82   return [attrName rangeOfString:_subtype].length > 0 ? YES : NO;
83 }
84
85 - (NSString *)langSubtype {
86   NSString *attrName;
87   NSRange  r;
88   
89   attrName = [self attributeName];
90   r = [attrName rangeOfString:@";lang-"];
91   if (r.length == 0) return nil;
92   
93   attrName = [attrName substringFromIndex:(r.location + 1)];
94   
95   r = [attrName rangeOfString:@";"];
96   if (r.length > 0) attrName = [attrName substringToIndex:r.location];
97   
98   return attrName;
99 }
100
101 /* values */
102
103 - (unsigned)count {
104   return [self->values count];
105 }
106
107 - (void)addValue:(id)_value {
108   self->didChange = YES;
109   
110   if (self->values == nil)
111     self->values = [[NSArray alloc] initWithObjects:&_value count:1];
112   else {
113     NSArray *tmp;
114
115     tmp = self->values;
116     self->values = [[tmp arrayByAddingObject:_value] retain];
117     [tmp release];
118   }
119 }
120
121 - (NSArray *)allValues {
122   return self->values;
123 }
124 - (NSEnumerator *)valueEnumerator {
125   return [self->values objectEnumerator];
126 }
127
128 - (void)addStringValue:(NSString *)_value {
129   NSData *d;
130
131   d = [_value dataUsingEncoding:NSUTF8StringEncoding];
132   
133   [self addValue:d];
134 }
135
136 - (void)catchedDecodeException:(NSException *)_exception {
137   fprintf(stderr, "Got exception %s decoding NSUTF8StringEncoding, "
138           "use defaultCStringEncoding", [[_exception description] cString]);
139 }
140 - (NSString *)stringFromData:(NSData *)_data {
141   static NSStringEncoding enc = 0;
142   NSString *s;
143   
144   if (enc == 0) enc = [NSString defaultCStringEncoding];
145   
146   if (_data == nil) return nil;
147   *(&s) = nil;
148   NS_DURING
149     s = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];
150   NS_HANDLER {
151     [self catchedDecodeException:localException];
152     s = nil;
153   }
154   NS_ENDHANDLER;
155   if (s == nil)
156     s = [[NSString alloc] initWithData:_data encoding:enc];
157   return s;
158 }
159
160 - (NSString *)stringValueAtIndex:(unsigned)_idx {
161   NSData *data;
162   
163   data = [[self allValues] objectAtIndex:_idx];
164   if (![data isNotNull])
165     return nil;
166   
167   return [[self stringFromData:data] autorelease];
168 }
169
170 - (NSArray *)allStringValues {
171   unsigned cnt;
172
173   if ((cnt = [self count]) == 0) {
174     return [NSArray array];
175   }
176   else if (cnt == 1) {
177     NSString *s;
178     NSData   *data;
179     NSArray  *a;
180
181     data = [[self allValues] objectAtIndex:0];
182     
183     if (![data isNotNull])
184       return nil;
185     
186     s = [self stringFromData:data];
187     a = [[NSArray alloc] initWithObjects:&s count:1];
188     [s release];
189     return [a autorelease];
190   }
191   else {
192     id       *objs;
193     unsigned i;
194     NSArray  *vals, *a;
195     
196     vals = [self allValues];
197     
198     objs = calloc(cnt, sizeof(id));
199     for (i = 0; i < cnt; i++) {
200       NSData *data;
201
202       if ((data = [vals objectAtIndex:i]) == nil) {
203         NSLog(@"missing data for value at index %i", i);
204         objs[i] = [[NSString alloc] initWithString:@""];
205       }
206       else {
207         objs[i] = (![data isNotNull]) ? (id)@"" : [self stringFromData:data];
208       }
209     }
210     
211     a = [[NSArray alloc] initWithObjects:objs count:cnt];
212
213     for (i = 0; i < cnt; i++)
214       [objs[i] release];
215     
216     free(objs); objs = NULL;
217
218     return [a autorelease];
219   }
220 }
221 - (NSEnumerator *)stringValueEnumerator {
222   return [[self allStringValues] objectEnumerator];
223 }
224
225 /* NSCopying */
226
227 - (id)copyWithZone:(NSZone *)_zone {
228   return [[[self class] alloc] initWithAttributeName:self->name
229                                values:self->values];
230 }
231
232 /* description */
233
234 - (NSString *)stringValue {
235   NSMutableString *ms;
236   NSString     *s;
237   NSEnumerator *e;
238   id           value;
239   BOOL         isFirst;
240
241   ms = [[NSMutableString alloc] initWithCapacity:100];
242   
243   e = [self stringValueEnumerator];
244   isFirst = YES;
245   while ((value = [e nextObject])) {
246     if (isFirst) isFirst = NO;
247     else [ms appendString:@","];
248     [ms appendString:[value description]];
249   }
250   
251   s = [ms copy];
252   [ms release];
253   return [s autorelease];
254 }
255
256 - (NSString *)description {
257   NSMutableString *s;
258   
259   s = [NSMutableString stringWithCapacity:100];
260   [s appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
261   
262   [s appendFormat:@" name='%@'", [self attributeName]];
263   [s appendString:@" values="];
264   [s appendString:[self stringValue]];
265   [s appendString:@">"];
266
267   return s;
268 }
269
270 @end /* NGLdapAttribute */