]> err.no Git - sope/blob - sope-xml/SaxObjC/SaxAttributeList.m
removed NGCString
[sope] / sope-xml / SaxObjC / SaxAttributeList.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
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$
22
23 #include "SaxAttributeList.h"
24 #include "SaxAttributes.h"
25 #include "common.h"
26
27 @implementation SaxAttributeList
28
29 - (id)init {
30   self->names  = [[NSMutableArray alloc] init];
31   self->types  = [[NSMutableArray alloc] init];
32   self->values = [[NSMutableArray alloc] init];
33   return self;
34 }
35 - (id)initWithAttributeList:(id<SaxAttributeList>)_attrList {
36   if ((self = [self init])) {
37     unsigned i;
38
39     for (i = 0; i < [_attrList count]; i++) {
40       [self->names  addObject:[_attrList nameAtIndex:i]];
41       [self->types  addObject:[_attrList typeAtIndex:i]];
42       [self->values addObject:[_attrList valueAtIndex:i]];
43     }
44   }
45   return self;
46 }
47
48 - (id)initWithAttributes:(id<SaxAttributes>)_attrList {
49   if ((self = [self init])) {
50     int i, c;
51
52     for (i = 0, c = [_attrList count]; i < c; i++) {
53       [self->names  addObject:[_attrList rawNameAtIndex:i]];
54       [self->types  addObject:[_attrList typeAtIndex:i]];
55       [self->values addObject:[_attrList valueAtIndex:i]];
56     }
57   }
58   return self;
59 }
60
61 - (void)dealloc {
62   [self->names  release];
63   [self->types  release];
64   [self->values release];
65   [super dealloc];
66 }
67
68 /* modify operations */
69
70 - (void)setAttributeList:(id<SaxAttributeList>)_attrList {
71   unsigned i;
72
73   [self clear];
74   
75   for (i = 0; i < [_attrList count]; i++) {
76     [self->names  addObject:[_attrList nameAtIndex:i]];
77     [self->types  addObject:[_attrList typeAtIndex:i]];
78     [self->values addObject:[_attrList valueAtIndex:i]];
79   }
80 }
81 - (void)clear {
82   [self->names  removeAllObjects];
83   [self->types  removeAllObjects];
84   [self->values removeAllObjects];
85 }
86
87 - (void)addAttribute:(NSString *)_name
88   type:(NSString *)_type
89   value:(NSString *)_value
90 {
91   if (_type  == nil) _type  = @"CDATA";
92   if (_value == nil) _value = @"";
93   [self->names  addObject:_name];
94   [self->types  addObject:_type];
95   [self->values addObject:_value];
96 }
97
98 - (void)removeAttribute:(NSString *)_name {
99   int idx;
100
101   if ((idx = [self->names indexOfObject:_name]) == NSNotFound)
102     return;
103
104   [self->names  removeObjectAtIndex:idx];
105   [self->types  removeObjectAtIndex:idx];
106   [self->values removeObjectAtIndex:idx];
107 }
108
109 /* protocol implementation */
110
111 - (NSString *)nameAtIndex:(unsigned int)_idx {
112   return [self->names objectAtIndex:_idx];
113 }
114 - (NSString *)typeAtIndex:(unsigned int)_idx {
115   return [self->types objectAtIndex:_idx];
116 }
117 - (NSString *)valueAtIndex:(unsigned int)_idx {
118   return [self->values objectAtIndex:_idx];
119 }
120
121 - (NSString *)typeForName:(NSString *)_name {
122   int i;
123
124   if ((i = [self->names indexOfObject:_name]) == NSNotFound)
125     return nil;
126
127   return [self typeAtIndex:i];
128 }
129 - (NSString *)valueForName:(NSString *)_name {
130   int i;
131
132   if ((i = [self->names indexOfObject:_name]) == NSNotFound)
133     return nil;
134
135   return [self valueAtIndex:i];
136 }
137
138 - (unsigned int)count {
139   return [self->names count];
140 }
141
142 /* NSCopying */
143
144 - (id)copyWithZone:(NSZone *)_zone {
145   return [[[self class] allocWithZone:_zone] initWithAttributeList:self];
146 }
147
148 /* description */
149
150 - (id)propertyList {
151   id objs[3], keys[3];
152   objs[0] = self->names;  keys[0] = @"names";
153   objs[1] = self->types;  keys[1] = @"types";
154   objs[2] = self->values; keys[2] = @"values";
155   return [NSDictionary dictionaryWithObjects:objs forKeys:keys count:3];
156 }
157
158 - (NSString *)description {
159   NSMutableString *s;
160   NSString        *is;
161   int i, c;
162   
163   s = [[NSMutableString alloc] init];
164   [s appendFormat:@"<%08X[%@]:", self, NSStringFromClass([self class])];
165   
166   for (i = 0, c = [self count]; i < c; i++) {
167     NSString *type;
168
169     [s appendString:@" "];
170     [s appendString:[self nameAtIndex:i]];
171     [s appendString:@"='"];
172     [s appendString:[self valueAtIndex:i]];
173     [s appendString:@"'"];
174
175     type = [self typeAtIndex:i];
176     if (![type isEqualToString:@"CDATA"]) {
177       [s appendString:@"["];
178       [s appendString:type];
179       [s appendString:@"]"];
180     }
181   }
182   [s appendString:@">"];
183   
184   is = [s copy];
185   [s release];
186   return [is autorelease];
187 }
188
189 @end /* SaxAttributeList */