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