]> err.no Git - sope/blob - sope-appserver/samples/parsedav/DAVParserTest.m
added svn:keywords and svn:ignore where appropriate. removed CVS artifacts.
[sope] / sope-appserver / samples / parsedav / DAVParserTest.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 "DAVParserTest.h"
24 #include "common.h"
25 #include <NGObjWeb/SaxDAVHandler.h>
26 #include <SaxObjC/SaxObjC.h>
27
28 @implementation DAVParserTest
29
30 static id<NSObject,SaxXMLReader> xmlParser = nil;
31 static SaxDAVHandler             *davsax   = nil;
32
33 - (id)init {
34   if ((self = [super init])) {
35     if (xmlParser == nil) {
36       xmlParser =
37         [[[SaxXMLReaderFactory standardXMLReaderFactory] 
38                                createXMLReaderForMimeType:@"text/xml"]
39                                retain];
40       if (xmlParser == nil) {
41         [self logWithFormat:@"found no XML-parser !"];
42         return nil;
43       }
44     }
45     if (davsax == nil) {
46       if ((davsax = [[SaxDAVHandler alloc] init]) == nil) {
47         [self logWithFormat:@"found no DAV SAX handler ..."];
48         return nil;
49       }
50       [davsax setDelegate:self];
51     }
52   }
53   return self;
54 }
55
56 - (void)dealloc {
57   [self->propQueue release];
58   [super dealloc];
59 }
60
61 /* parser */
62
63 - (void)lockParser:(id)_sax {
64   [_sax reset];
65   [xmlParser setContentHandler:_sax];
66   [xmlParser setErrorHandler:_sax];
67 }
68 - (void)unlockParser:(id)_sax {
69   [xmlParser setContentHandler:nil];
70   [xmlParser setErrorHandler:nil];
71   [_sax reset];
72 }
73
74 /* the DAV parser reports properties using the prop-queue */
75
76 - (void)startPropQueue {
77   if (self->propQueue)
78     [self->propQueue removeAllObjects];
79   else
80     self->propQueue = [[NSMutableDictionary alloc] initWithCapacity:128];
81 }
82 - (void)clearPropQueue {
83   [self->propQueue release]; 
84   self->propQueue = nil;
85 }
86
87 - (void)davHandler:(SaxDAVHandler *)_handler
88   receivedProperties:(NSDictionary *)_record
89   forURI:(NSString *)_uri
90 {
91   /* Note: _record is volatile ! */
92   NSURL        *lurl;
93   NSDictionary *r;
94   
95   [self logWithFormat:@"PROPS on %@: %@", _uri, _record];
96   
97   lurl = [NSURL URLWithString:_uri];
98   r = [_record copy];
99   [self->propQueue setObject:r forKey:(lurl ? [lurl path] : _uri)];
100   [r release];
101 }
102
103 - (NSDictionary *)doQueueParseDict:(id)_src {
104   id results;
105   
106   [self startPropQueue];
107   [self lockParser:davsax];
108   [xmlParser parseFromSource:_src];
109   [self unlockParser:davsax];
110   results = [self->propQueue retain];
111   [self clearPropQueue];
112   return [results autorelease];
113 }
114 - (NSArray *)doQueueParse:(id)_src {
115   return [[self doQueueParseDict:_src] allValues];
116 }
117
118 /* running */
119
120 - (void)printResults {
121   EOFetchSpecification *fs;
122   id tmp;
123   
124   /* responses */
125   
126   if ([self->propQueue count] > 0) {
127     [self logWithFormat:@"collected %i property sets: %@",
128             [self->propQueue count],
129             self->propQueue];
130   }
131   
132   /* patches */
133   
134   if ((tmp = [davsax propPatchValues]))
135     [self logWithFormat:@"patch %i values: %@", [tmp count], tmp];
136   if ((tmp = [davsax propPatchPropertyNamesToRemove]))
137     [self logWithFormat:@"remove %i properties: %@", [tmp count], tmp];
138   
139   /* queries */
140
141   if ([davsax propFindAllProperties])
142     [self logWithFormat:@"find all properties !"];
143   if ([davsax propFindPropertyNames])
144     [self logWithFormat:@"deliver only property names (not their values)"];
145   
146   if ((tmp = [davsax propFindQueriedNames]))
147     [self logWithFormat:@"find %i attributes: %@", [tmp count], tmp];
148   
149   if ((tmp = [davsax bpropFindTargets]))
150     [self logWithFormat:@"bulkfind %i targets: %@", [tmp count], tmp];
151
152   if ((fs = [davsax searchFetchSpecification]))
153     [self logWithFormat:@"search: %@", fs];
154 }
155
156 - (void)runOnArgument:(NSString *)_arg {
157   NSURL *url;
158   
159   if (xmlParser == nil || davsax == nil)
160     [self logWithFormat:@"missing XML-parser ..."];
161   
162   if (![_arg isAbsolutePath]) {
163     _arg = [[[NSFileManager defaultManager] currentDirectoryPath] 
164                             stringByAppendingPathComponent:_arg];
165   }
166   url = [[[NSURL alloc] initFileURLWithPath:_arg] autorelease];
167   [self logWithFormat:@"process %@: %@", _arg, url];
168   
169   [self startPropQueue];
170   [self lockParser:davsax];
171   
172   [xmlParser parseFromSource:url];
173   [self printResults];
174   
175   [self unlockParser:davsax];
176   [self clearPropQueue];
177 }
178
179 - (void)runWithArguments:(NSArray *)_args {
180   unsigned i, count;
181   
182   if ((count = [_args count]) == 1) {
183     [self logWithFormat:@"usage: %@ <files*>", [_args objectAtIndex:0]];
184     return;
185   }
186   
187   /* foreach arg */
188   for (i = 1; i < count; i++)
189     [self runOnArgument:[_args objectAtIndex:i]];
190 }
191
192 @end /* DAVParserTest */