]> err.no Git - sope/blob - sope-ical/NGiCal/NGVCardSaxHandler.m
added some vCard model objects
[sope] / sope-ical / NGiCal / NGVCardSaxHandler.m
1 /*
2   Copyright (C) 2005 Helge Hess
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 "NGVCardSaxHandler.h"
23 #include "common.h"
24
25 @implementation NGVCardSaxHandler
26
27 - (void)dealloc {
28   if (self->content != NULL) free(self->content);
29   [self->vCards       release];
30   [self->vCard        release];
31   [self->currentGroup release];
32   [super dealloc];
33 }
34
35 /* state */
36
37 - (void)resetExceptResult {
38   [self->currentGroup release]; self->currentGroup = nil;
39   [self->vCard        release]; self->vCard        = nil;
40
41   if (self->content != NULL) {
42     free(self->content);
43     self->content = NULL;
44   }
45   
46   self->vcs.isInVCardSet   = 0;
47   self->vcs.isInVCard      = 0;
48   self->vcs.isInN          = 0;
49   self->vcs.isInGroup      = 0;
50   self->vcs.collectContent = 0;
51 }
52
53 - (void)reset {
54   [self resetExceptResult];
55   [self->vCards removeAllObjects];
56 }
57
58 /* document events */
59
60 - (void)startDocument {
61   [self reset];
62   if (self->vCards == nil)
63     self->vCards = [[NSMutableArray alloc] initWithCapacity:16];
64 }
65 - (void)endDocument {
66   [self resetExceptResult];
67 }
68
69 /* handle elements */
70
71 - (void)startGroup:(NSString *)_name {
72   self->vcs.isInGroup = 1;
73   ASSIGNCOPY(self->currentGroup, _name);
74 }
75 - (void)endGroup {
76   self->vcs.isInGroup = 0;
77   [self->currentGroup release]; self->currentGroup = nil;
78 }
79
80 - (void)startN {
81   self->vcs.isInN = 1;
82 }
83 - (void)endN {
84   self->vcs.isInN = 0;
85 }
86
87 - (void)startVCard:(id<SaxAttributes>)_attrs {
88   self->vcs.isInVCard = 1;
89 }
90 - (void)endVCard {
91   self->vcs.isInVCard = 0;
92 }
93
94 - (void)startVCardSet:(id<SaxAttributes>)_attrs {
95   self->vcs.isInVCardSet = 1;
96 }
97 - (void)endVCardSet {
98   self->vcs.isInVCardSet = 0;
99 }
100
101 /* element events */
102
103 - (void)startElement:(NSString *)_localName
104   namespace:(NSString *)_ns
105   rawName:(NSString *)_rawName
106   attributes:(id<SaxAttributes>)_attrs
107 {
108   if ([_localName isEqualToString:@"group"])
109     [self startGroup:[_attrs valueForName:@"name" uri:_ns]];
110   else if ([_localName isEqualToString:@"n"])
111     [self startN];
112   else if ([_localName isEqualToString:@"tel"])
113     ;
114   else if ([_localName isEqualToString:@"vCard"])
115     [self startVCard:_attrs];
116   else if ([_localName isEqualToString:@"vCardSet"])
117     [self startVCardSet:_attrs];
118   
119   if (self->vcs.isInN) {
120   }
121 }
122
123 - (void)endElement:(NSString *)_localName
124   namespace:(NSString *)_ns
125   rawName:(NSString *)_rawName
126 {
127   if (self->vcs.isInN) {
128   }
129   
130   if ([_localName isEqualToString:@"group"])
131     [self endGroup];
132   else if ([_localName isEqualToString:@"n"])
133     [self endN];
134   else if ([_localName isEqualToString:@"tel"])
135     ;
136   else if ([_localName isEqualToString:@"vCard"])
137     [self endVCard];
138   else if ([_localName isEqualToString:@"vCardSet"])
139     [self endVCardSet];
140 }
141
142 /* content */
143
144 - (void)startCollectingContent {
145   if (self->content != NULL) {
146     free(self->content);
147     self->content = NULL;
148   }
149   self->vcs.collectContent = 1;
150 }
151
152 - (NSString *)finishCollectingContent {
153   NSString *s;
154   
155   self->vcs.collectContent = 0;
156   
157   if (self->content == NULL)
158     return nil;
159   
160   if (self->contentLength == 0)
161     return @"";
162   
163   s = [NSString stringWithCharacters:self->content length:self->contentLength];
164   if (self->content != NULL) {
165     free(self->content);
166     self->content = NULL;
167   }
168   return s;
169 }
170
171 - (void)characters:(unichar *)_chars length:(int)_len {
172   if (_len == 0 || _chars == NULL)
173     return;
174   
175   if (self->content == NULL) {
176     /* first content */
177     self->contentLength = _len;
178     self->content       = calloc(_len + 1, sizeof(unichar));
179     memcpy(self->content, _chars, (_len * sizeof(unichar)));
180   }
181   else {
182     /* increase content */
183     self->content = 
184       realloc(self->content, (self->contentLength + _len+2) * sizeof(unichar));
185     memcpy(&(self->content[self->contentLength]), _chars, 
186            (_len * sizeof(unichar)));
187     self->contentLength += _len;
188   }
189   self->content[self->contentLength] = 0;
190 }
191
192 @end /* NGVCardSaxHandler */