File: NGLdapAttribute.m

package info (click to toggle)
sope 4.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,492 kB
  • sloc: objc: 167,993; sh: 3,648; ansic: 3,405; python: 318; makefile: 189
file content (329 lines) | stat: -rw-r--r-- 7,608 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
  Copyright (C) 2000-2005 SKYRIX Software AG
  Copyright (C) 2012 Inverse inc.

  This file is part of SOPE.

  SOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with SOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

#include "NGLdapAttribute.h"
#include "common.h"

@implementation NGLdapAttribute

- (id)initWithAttributeName:(NSString *)_name values:(NSArray *)_values {
  self->name   = [_name   copy];
  self->values = [_values copy];
  return self;
}
- (id)initWithAttributeName:(NSString *)_name {
  return [self initWithAttributeName:_name values:nil];
}

- (void)dealloc {
  [self->name   release];
  [self->values release];
  [super dealloc];
}

/* attribute name operations */

- (NSString *)attributeName {
  return self->name;
}

+ (NSString *)baseNameOfAttributeName:(NSString *)_attrName {
  NSRange r;
  
  r = [_attrName rangeOfString:@";"];
  if (r.length == 0) return _attrName;
  return [_attrName substringToIndex:r.location];
}
- (NSString *)attributeBaseName {
  return [[self class] baseNameOfAttributeName:[self attributeName]];
}

+ (NSArray *)subtypesOfAttributeName:(NSString *)_attrName {
  NSArray *parts;
  
  parts = [_attrName componentsSeparatedByString:@";"];

  return [parts count] > 1
    ? [parts subarrayWithRange:NSMakeRange(1, [parts count] - 1)]
    : (NSArray *)[NSArray array];
}

- (NSArray *)subtypes {
  return [[self class] subtypesOfAttributeName:[self attributeName]];
}

- (BOOL)hasSubtype:(NSString *)_subtype {
  NSString *attrName;
  
  if (_subtype == nil)
    return NO;
  
  attrName = [self attributeName];
  _subtype = [NSString stringWithFormat:@";%@;", _subtype];
  
  return [attrName rangeOfString:_subtype].length > 0 ? YES : NO;
}

- (NSString *)langSubtype {
  NSString *attrName;
  NSRange  r;
  
  attrName = [self attributeName];
  r = [attrName rangeOfString:@";lang-"];
  if (r.length == 0) return nil;
  
  attrName = [attrName substringFromIndex:(r.location + 1)];
  
  r = [attrName rangeOfString:@";"];
  if (r.length > 0) attrName = [attrName substringToIndex:r.location];
  
  return attrName;
}

/* values */

- (unsigned)count {
  return [self->values count];
}

- (void)addValue:(NSData *)_value {
  self->didChange = YES;
  
  if (self->values == nil)
    self->values = [[NSArray alloc] initWithObjects:&_value count:1];
  else {
    NSArray *tmp;

    tmp = self->values;
    self->values = [[tmp arrayByAddingObject:_value] retain];
    [tmp release];
  }
}

- (NSArray *)allValues {
  return self->values;
}
- (NSEnumerator *)valueEnumerator {
  return [self->values objectEnumerator];
}

- (void)addStringValue:(NSString *)_value {
  NSData *d;

  d = [_value dataUsingEncoding:NSUTF8StringEncoding];
  
  [self addValue:d];
}

- (void)catchedDecodeException:(NSException *)_exception {
  fprintf(stderr, "Got exception %s decoding NSUTF8StringEncoding, "
          "use defaultCStringEncoding", [[_exception description] cString]);
}
- (NSString *)stringFromData:(NSData *)_data {
  static NSStringEncoding enc = 0;
  NSString *s;
  
  if (enc == 0) enc = [NSString defaultCStringEncoding];
  
  if (_data == nil) return nil;
  NS_DURING
    s = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];
  NS_HANDLER {
    [self catchedDecodeException:localException];
    s = nil;
  }
  NS_ENDHANDLER;
  if (s == nil)
    s = [[NSString alloc] initWithData:_data encoding:enc];

  return s;
}

- (NSString *)stringValueAtIndex:(unsigned)_idx {
  NSData *data;
  
  data = [[self allValues] objectAtIndex:_idx];
  if (![data isNotNull])
    return nil;
  
  return [[self stringFromData:data] autorelease];
}

- (NSArray *)allStringValues {
  unsigned cnt;

  if ((cnt = [self count]) == 0) {
    return [NSArray array];
  }
  else if (cnt == 1) {
    NSString *s;
    NSData   *data;
    NSArray  *a;

    data = [[self allValues] objectAtIndex:0];
    
    if (![data isNotNull])
      return nil;
    
    s = [self stringFromData:data];
    if (s)
      {
        a = [[NSArray alloc] initWithObjects:&s count:1];
        [s release];
        return [a autorelease];
      }
    else
      {
        //[self errorWithFormat: @"could not convert value of %@ to string", [self attributeName]];
        return nil;
      }
  }
  else {
    id       *objs;
    unsigned i;
    NSArray  *vals, *a;
    NSData *data;

    vals = [self allValues];
    
    objs = calloc(cnt, sizeof(id));
    for (i = 0; i < cnt; i++) {

      objs[i] = nil;	
      data = [vals objectAtIndex: i];
      
      if (data)
	objs[i] = [self stringFromData: data];

      if (!objs[i]) {
	NSLog(@"missing data for value at index %i", i);
	objs[i] = [[NSString alloc] initWithString: @""];
      }
    }
    
    a = [[NSArray alloc] initWithObjects:objs count:cnt];

    for (i = 0; i < cnt; i++)
      [objs[i] release];
    
    free(objs); objs = NULL;

    return [a autorelease];
  }
}
- (NSEnumerator *)stringValueEnumerator {
  return [[self allStringValues] objectEnumerator];
}

/* NSObject */
- (BOOL) isEqual: (id)aAttribute
{
  BOOL rc = NO;
  NSArray *otherValues;
  id value, otherValue;
  NSUInteger count, max;

  if (aAttribute == self)
    rc = YES;
  else
    {
      if ([name isEqualToString: [aAttribute attributeName]])
        {
          max = [values count];
          otherValues = [aAttribute allValues];
          if (max == [otherValues count])
            {
              rc = YES;
              for (count = 0; rc && count < max; count++)
                {
                  value = [values objectAtIndex: count];
                  otherValue = [otherValues objectAtIndex: count];
                  if (value != otherValue && ![value isEqual: otherValue])
                    rc = NO;
                }
            }
        }
    }

  return rc;
}

/* NSCopying */

- (id)copyWithZone:(NSZone *)_zone {
  return [[[self class] alloc] initWithAttributeName:self->name
                               values:self->values];
}

/* description */

- (NSString *)stringValue {
  NSMutableString *ms;
  NSString     *s;
  NSEnumerator *e;
  id           value;
  BOOL         isFirst;

  ms = [[NSMutableString alloc] initWithCapacity:100];
  
  e = [self stringValueEnumerator];
  isFirst = YES;
  while ((value = [e nextObject])) {
    if (isFirst) isFirst = NO;
    else [ms appendString:@","];
    [ms appendString:[value description]];
  }
  
  s = [ms copy];
  [ms release];
  return [s autorelease];
}

- (NSString *)description {
  NSMutableString *s;
  
  s = [NSMutableString stringWithCapacity:100];
  [s appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];
  
  [s appendFormat:@" name='%@'", [self attributeName]];
  [s appendString:@" values="];
  [s appendString:[self stringValue]];
  [s appendString:@">"];

  return s;
}

- (id) asArrayOrString
{
  NSArray *arrayValue;
  id value;

  arrayValue = [self allStringValues];
  if ([arrayValue count] == 1)
    value = [arrayValue objectAtIndex: 0];
  else
    value = arrayValue;

  return value;
}

@end /* NGLdapAttribute */