]> err.no Git - sope/blob - sope-xml/SaxObjC/SaxAttributes.m
lF fixes
[sope] / sope-xml / SaxObjC / SaxAttributes.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 "SaxAttributes.h"
23 #include "common.h"
24
25 @implementation SaxAttributes
26
27 - (id)init {
28   Class c = [NSMutableArray class];
29   
30   self->names    = [[c alloc] init];
31   self->uris     = [[c alloc] init];
32   self->rawNames = [[c alloc] init];
33   self->types    = [[c alloc] init];
34   self->values   = [[c alloc] init];
35   return self;
36 }
37 - (id)initWithAttributes:(id<SaxAttributes>)_attrs {
38   if ((self = [self init])) {
39     int i, c;
40     
41     for (i = 0, c = [_attrs count]; i < c; i++) {
42       [self addAttribute:[_attrs nameAtIndex:i]
43             uri:[_attrs uriAtIndex:i]
44             rawName:[_attrs rawNameAtIndex:i]
45             type:[_attrs typeAtIndex:i]
46             value:[_attrs valueAtIndex:i]];
47     }
48   }
49   return self;
50 }
51
52 - (id)initWithAttributeList:(id<SaxAttributeList>)_attrList {
53   if ((self = [self init])) {
54     unsigned i;
55     
56     for (i = 0; i < [_attrList count]; i++) {
57       [self addAttribute:[_attrList nameAtIndex:i] uri:@""
58             rawName:[_attrList nameAtIndex:i]
59             type:[_attrList typeAtIndex:i]
60             value:[_attrList valueAtIndex:i]];
61     }
62   }
63   return self;
64 }
65 - (id)initWithDictionary:(NSDictionary *)_dict {
66   if ((self = [self init])) {
67     NSEnumerator *keys;
68     NSString     *key;
69     
70     keys = [_dict keyEnumerator];
71     while ((key = [keys nextObject])) {
72       [self addAttribute:key uri:nil rawName:key
73             type:nil 
74             value:[_dict objectForKey:key]];
75     }
76   }
77   return self;
78 }
79
80 - (void)dealloc {
81   [self->names    release];
82   [self->uris     release];
83   [self->rawNames release];
84   [self->types    release];
85   [self->values   release];
86   [super dealloc];
87 }
88
89 /* modifications */
90
91 - (void)addAttribute:(NSString *)_localName uri:(NSString  *)_uri
92   rawName:(NSString *)_rawName
93   type:(NSString *)_type
94   value:(NSString *)_value
95 {
96   [self->names    addObject:_localName ? _localName : _rawName];
97   [self->uris     addObject:_uri       ? _uri       : (NSString *)@""];
98   [self->rawNames addObject:_rawName   ? _rawName   : (NSString *)@""];
99   [self->types    addObject:_type      ? _type      : (NSString *)@"CDATA"];
100   [self->values   addObject:_value];
101 }
102
103 - (void)clear {
104   [self->names    removeAllObjects];
105   [self->uris     removeAllObjects];
106   [self->rawNames removeAllObjects];
107   [self->types    removeAllObjects];
108   [self->values   removeAllObjects];
109 }
110
111 /* lookup indices */
112
113 - (unsigned int)indexOfRawName:(NSString *)_rawName {
114   return [self->rawNames indexOfObject:_rawName];
115 }
116 - (unsigned int)indexOfName:(NSString *)_localPart uri:(NSString *)_uri
117 {
118   unsigned int i, c;
119   
120   for (i = 0, c = [self count]; i < c; i++) {
121     NSString *name;
122     
123     name = [self nameAtIndex:i];
124     
125     if ([name isEqualToString:_localPart]) {
126       NSString *auri;
127       
128       auri = [self uriAtIndex:i];
129
130       //NSLog(@"found name %@", name);
131       
132       if (([auri length] == 0) && ([_uri length] == 0))
133         return i;
134       
135       if ([_uri isEqualToString:auri])
136         return i;
137     }
138   }
139   return NSNotFound;
140 }
141
142 /* lookup data by index */
143
144 - (NSString *)nameAtIndex:(unsigned int)_idx {
145   return [self->names objectAtIndex:_idx];
146 }
147 - (NSString *)rawNameAtIndex:(unsigned int)_idx {
148   return [self->rawNames objectAtIndex:_idx];
149 }
150 - (NSString *)typeAtIndex:(unsigned int)_idx {
151   return [self->types objectAtIndex:_idx];
152 }
153 - (NSString *)uriAtIndex:(unsigned int)_idx {
154   return [self->uris objectAtIndex:_idx];
155 }
156 - (NSString *)valueAtIndex:(unsigned int)_idx {
157   return [self->values objectAtIndex:_idx];
158 }
159
160 /* lookup data by name */
161
162 - (NSString *)typeForRawName:(NSString *)_rawName {
163   unsigned int i;
164
165   if ((i = [self indexOfRawName:_rawName]) == NSNotFound)
166     return nil;
167
168   return [self typeAtIndex:i];
169 }
170 - (NSString *)typeForName:(NSString *)_localName uri:(NSString *)_uri {
171   unsigned int i;
172   
173   if ((i = [self indexOfName:_localName uri:_uri]) == NSNotFound)
174     return nil;
175
176   return [self typeAtIndex:i];
177 }
178
179 - (NSString *)valueForRawName:(NSString *)_rawName {
180   unsigned int i;
181
182   if ((i = [self indexOfRawName:_rawName]) == NSNotFound)
183     return nil;
184
185   return [self valueAtIndex:i];
186 }
187 - (NSString *)valueForName:(NSString *)_localName uri:(NSString *)_uri {
188   unsigned int i;
189   
190   if ((i = [self indexOfName:_localName uri:_uri]) == NSNotFound)
191     return nil;
192
193   return [self valueAtIndex:i];
194 }
195
196 /* list size */
197
198 - (unsigned int)count {
199   return [self->names count];
200 }
201
202 /* NSCopying */
203
204 - (id)copyWithZone:(NSZone *)_zone {
205   return [(SaxAttributes *)[[self class] alloc] initWithAttributes:self];
206 }
207
208 /* description */
209
210 - (NSString *)description {
211   NSMutableString *s;
212   NSString        *is;
213   int i, c;
214   
215   s = [[NSMutableString alloc] init];
216   [s appendFormat:@"<%08X[%@]:", self, NSStringFromClass([self class])];
217   
218   for (i = 0, c = [self count]; i < c; i++) {
219     NSString *type;
220
221     [s appendString:@" "];
222     [s appendString:[self nameAtIndex:i]];
223     [s appendString:@"='"];
224     [s appendString:[self valueAtIndex:i]];
225     [s appendString:@"'"];
226
227     type = [self typeAtIndex:i];
228     if (![type isEqualToString:@"CDATA"]) {
229       [s appendString:@"["];
230       [s appendString:type];
231       [s appendString:@"]"];
232     }
233   }
234   [s appendString:@">"];
235   
236   is = [s copy];
237   [s release];
238   return [is autorelease];
239 }
240
241 @end /* SaxAttributes */