]> err.no Git - sope/blob - sope-ldap/samples/ldap2dsml.m
more code directory reorganizations
[sope] / sope-ldap / samples / ldap2dsml.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: ldap2dsml.m 4 2004-08-20 17:04:31Z helge $
22
23 #import <Foundation/NSObject.h>
24 #import <SaxObjC/SaxXMLReader.h>
25 #import <NGLdap/NGLdapAttribute.h>
26 #import <NGLdap/NGLdapEntry.h>
27 #import <NGLdap/NGLdapConnection.h>
28 #include "common.h"
29
30 @interface DSMLSaxProducer : NSObject
31 {
32   id<NSObject,SaxContentHandler> contentHandler;
33   id<NSObject,SaxErrorHandler>   errorHandler;
34 }
35
36 - (void)setContentHandler:(id<NSObject,SaxContentHandler>)_handler;
37 - (void)setErrorHandler:(id<NSObject,SaxErrorHandler>)_handler;
38
39 - (void)produceOnConnection:(NGLdapConnection *)_con dn:(NSString *)_dn;
40
41 @end
42
43 static NSString *XMLNS_DSML = @"http://wwww.dsml.org/DSML";
44
45 @implementation DSMLSaxProducer
46
47 - (void)dealloc {
48   [self->errorHandler   release];
49   [self->contentHandler release];
50   [super dealloc];
51 }
52
53 - (void)setContentHandler:(id<NSObject,SaxContentHandler>)_handler {
54   ASSIGN(self->contentHandler, _handler);
55 }
56 - (void)setErrorHandler:(id<NSObject,SaxErrorHandler>)_handler {
57   ASSIGN(self->errorHandler, _handler);
58 }
59
60 - (void)_produceAttribute:(NGLdapAttribute *)_attribute
61   ofEntry:(NGLdapEntry *)_entry
62 {
63   SaxAttributes *attrs;
64
65   attrs = [[SaxAttributes alloc] init];
66   
67   [attrs addAttribute:@"name" uri:XMLNS_DSML rawName:@"name"
68          type:@"CDATA"
69          value:[_attribute attributeName]];
70   
71   [self->contentHandler
72        startElement:@"attr"
73        namespace:XMLNS_DSML
74        rawName:@"attr"
75        attributes:attrs];
76   
77   [attrs release]; attrs = nil;
78   
79   /* encode values */
80   {
81     NSEnumerator *values;
82     NSString *value;
83   
84     values = [_attribute stringValueEnumerator];
85     while ((value = [values nextObject])) {
86       unsigned len;
87       unichar  *chars;
88
89       if ((len = [value length]) == 0)
90         continue;
91       
92       chars = calloc(len + 1, sizeof(unichar));
93       [value getCharacters:chars];
94       
95       [self->contentHandler
96            startElement:@"value"
97            namespace:XMLNS_DSML
98            rawName:@"value"
99            attributes:nil];
100       
101       [self->contentHandler characters:chars length:len];
102       
103       if (chars) free(chars);
104       
105       [self->contentHandler
106            endElement:@"value"
107            namespace:XMLNS_DSML
108            rawName:@"value"];
109     }
110   }
111   
112   [self->contentHandler
113        endElement:@"attr"
114        namespace:XMLNS_DSML
115        rawName:@"attr"];
116 }
117
118 - (void)_produceObjectClassOfEntry:(NGLdapEntry *)_entry {
119   NGLdapAttribute *attr;
120
121   if ((attr = [_entry attributeWithName:@"objectclass"]) == nil)
122     return;
123   
124   [self->contentHandler
125        startElement:@"objectclass"
126        namespace:XMLNS_DSML
127        rawName:@"objectclass"
128        attributes:nil];
129   
130   /* encode values */
131   {
132     NSEnumerator *values;
133     NSString *value;
134   
135     values = [attr stringValueEnumerator];
136     while ((value = [values nextObject])) {
137       unsigned len;
138       unichar  *chars;
139
140       if ((len = [value length]) == 0)
141         continue;
142       
143       chars = calloc(len + 1, sizeof(unichar));
144       [value getCharacters:chars];
145       
146       [self->contentHandler
147            startElement:@"objectclass"
148            namespace:XMLNS_DSML
149            rawName:@"objectclass"
150            attributes:nil];
151       
152       [self->contentHandler characters:chars length:len];
153       
154       if (chars) free(chars);
155       
156       [self->contentHandler
157            endElement:@"objectclass"
158            namespace:XMLNS_DSML
159            rawName:@"objectclass"];
160     }
161   }
162   
163   [self->contentHandler
164        endElement:@"objectclass"
165        namespace:XMLNS_DSML
166        rawName:@"objectclass"];
167 }
168
169 - (void)_produceEntry:(NGLdapEntry *)_entry {
170   SaxAttributes *attrs;
171   NSEnumerator  *names;
172   NSString      *cname;
173   
174   attrs = [[SaxAttributes alloc] init];
175   
176   [attrs addAttribute:@"dn" uri:XMLNS_DSML rawName:@"dn"
177          type:@"CDATA"
178          value:[_entry dn]];
179   
180   [self->contentHandler
181        startElement:@"entry"
182        namespace:XMLNS_DSML
183        rawName:@"entry"
184        attributes:attrs];
185
186   [attrs release]; attrs = nil;
187
188   /* attributes */
189
190   [self _produceObjectClassOfEntry:_entry];
191   
192   names = [[_entry attributeNames] objectEnumerator];
193   while ((cname = [names nextObject])) {
194     NGLdapAttribute *attr;
195
196     if ([cname isEqualToString:@"objectclass"])
197       continue;
198     
199     if ((attr = [_entry attributeWithName:cname]))
200       [self _produceAttribute:attr ofEntry:_entry];
201   }  
202   
203   [self->contentHandler
204        endElement:@"entry"
205        namespace:XMLNS_DSML
206        rawName:@"entry"];
207 }
208
209 - (void)_produceEntries:(NSEnumerator *)_entries {
210   NGLdapEntry *entry;
211
212   [self->contentHandler
213        startElement:@"directory-entries"
214        namespace:XMLNS_DSML
215        rawName:@"directory-entries"
216        attributes:nil];
217   
218   while ((entry = [_entries nextObject]))
219     [self _produceEntry:entry];
220   
221   [self->contentHandler
222        endElement:@"directory-entries"
223        namespace:XMLNS_DSML
224        rawName:@"directory-entries"];
225 }
226
227 - (void)produceOnConnection:(NGLdapConnection *)_con dn:(NSString *)_dn {
228   [self->contentHandler startDocument];
229   [self->contentHandler startPrefixMapping:@"" uri:XMLNS_DSML];
230   
231   [self->contentHandler
232        startElement:@"dsml"
233        namespace:XMLNS_DSML
234        rawName:@"dsml"
235        attributes:nil];
236
237   [self _produceEntries:[_con flatSearchAtBaseDN:_dn
238                               qualifier:nil
239                               attributes:nil]];
240   
241   [self->contentHandler endElement:@"dsml" namespace:XMLNS_DSML rawName:@"dsml"];
242   
243   [self->contentHandler endPrefixMapping:@""];
244   [self->contentHandler endDocument];
245 }
246
247 @end /* DSMLSaxProducer */
248
249 #import <SaxObjC/SaxDefaultHandler.h>
250
251 @interface DSMLSaxOutputter : SaxDefaultHandler
252 {
253   int level;
254 }
255 @end
256
257 @implementation DSMLSaxOutputter
258
259 - (void)startElement:(NSString *)_localName
260   namespace:(NSString *)_ns
261   rawName:(NSString *)_rawName
262   attributes:(id<SaxAttributes>)_attrs
263 {
264   int i, count;
265   
266   level++;
267   for (i = 0; i < level; i++)
268     printf("  ");
269   printf("<dsml:%s", [_localName cString]);
270   
271   if (level <= 1) {
272     printf(" xmlns:dsml='%s'", [_ns cString]);
273   }
274
275   for (i = 0, count = [_attrs count]; i < count; i++) {
276     printf(" %s='%s'",
277            [[_attrs nameAtIndex:i] cString],
278            [[_attrs valueAtIndex:i] cString]);
279   }
280   
281   printf(">\n");
282 }
283
284 - (void)endElement:(NSString *)_localName
285   namespace:(NSString *)_ns
286   rawName:(NSString *)_rawName
287 {
288   int i;
289   for (i = 0; i < level; i++)
290     printf("  ");
291   printf("</dsml:%s>\n", [_localName cString]);
292   level--;
293 }
294
295 - (void)characters:(unichar *)_chars length:(int)_len {
296   int i;
297   NSString *s;
298   
299   for (i = 0; i < level + 1; i++)
300     printf("  ");
301
302   s = [[NSString alloc] initWithCharacters:_chars length:_len];
303   printf("%s\n", [s cString]);
304   [s release];
305 }
306
307 @end /* DSMLSaxOutputter */
308
309 #import <Foundation/Foundation.h>
310
311 int main(int argc, char **argv, char **env) {
312   NSAutoreleasePool *pool;
313   NSUserDefaults   *ud;
314   NSArray          *args;
315   DSMLSaxProducer  *cpu;
316   DSMLSaxOutputter *out;
317   
318   pool = [[NSAutoreleasePool alloc] init];
319 #if LIB_FOUNDATION_LIBRARY
320   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
321 #endif
322   
323   args = [[NSProcessInfo processInfo] arguments];
324   if ([args count] < 1) {
325     NSLog(@"usage: %@ <files>", [args objectAtIndex:0]);
326     exit(1);
327   }
328   else if ([args count] == 1)
329     args = [args arrayByAddingObject:@"."];
330
331   ud = [NSUserDefaults standardUserDefaults];
332
333   cpu = [[DSMLSaxProducer alloc] init];
334   out = [[DSMLSaxOutputter alloc] init];
335   [cpu setContentHandler:out];
336   [cpu setErrorHandler:out];
337
338 #if 0
339   fm = [[NGLdapFileManager alloc]
340                            initWithHostName:[ud stringForKey:@"LDAPHost"]
341                            port:0
342                            bindDN:[ud stringForKey:@"LDAPBindDN"]
343                            credentials:[ud stringForKey:@"LDAPPassword"]
344                            rootDN:[ud stringForKey:@"LDAPRootDN"]];
345   fm = [fm autorelease];
346 #endif
347
348   {
349     NGLdapConnection *con;
350
351     con = [[NGLdapConnection alloc]
352                              initWithHostName:[ud stringForKey:@"LDAPHost"]
353                              port:0];
354     [con bindWithMethod:@"simple"
355          binddn:[ud stringForKey:@"LDAPBindDN"]
356          credentials:[ud stringForKey:@"LDAPPassword"]];
357     
358     [cpu produceOnConnection:con
359          dn:[ud stringForKey:@"LDAPRootDN"]];
360     
361     [con release];
362   }
363   [pool release];
364   exit(0);
365   return 0;
366 }