2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
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
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.
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
21 // $Id: ldap2dsml.m 4 2004-08-20 17:04:31Z helge $
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>
30 @interface DSMLSaxProducer : NSObject
32 id<NSObject,SaxContentHandler> contentHandler;
33 id<NSObject,SaxErrorHandler> errorHandler;
36 - (void)setContentHandler:(id<NSObject,SaxContentHandler>)_handler;
37 - (void)setErrorHandler:(id<NSObject,SaxErrorHandler>)_handler;
39 - (void)produceOnConnection:(NGLdapConnection *)_con dn:(NSString *)_dn;
43 static NSString *XMLNS_DSML = @"http://wwww.dsml.org/DSML";
45 @implementation DSMLSaxProducer
48 [self->errorHandler release];
49 [self->contentHandler release];
53 - (void)setContentHandler:(id<NSObject,SaxContentHandler>)_handler {
54 ASSIGN(self->contentHandler, _handler);
56 - (void)setErrorHandler:(id<NSObject,SaxErrorHandler>)_handler {
57 ASSIGN(self->errorHandler, _handler);
60 - (void)_produceAttribute:(NGLdapAttribute *)_attribute
61 ofEntry:(NGLdapEntry *)_entry
65 attrs = [[SaxAttributes alloc] init];
67 [attrs addAttribute:@"name" uri:XMLNS_DSML rawName:@"name"
69 value:[_attribute attributeName]];
77 [attrs release]; attrs = nil;
84 values = [_attribute stringValueEnumerator];
85 while ((value = [values nextObject])) {
89 if ((len = [value length]) == 0)
92 chars = calloc(len + 1, sizeof(unichar));
93 [value getCharacters:chars];
101 [self->contentHandler characters:chars length:len];
103 if (chars) free(chars);
105 [self->contentHandler
112 [self->contentHandler
118 - (void)_produceObjectClassOfEntry:(NGLdapEntry *)_entry {
119 NGLdapAttribute *attr;
121 if ((attr = [_entry attributeWithName:@"objectclass"]) == nil)
124 [self->contentHandler
125 startElement:@"objectclass"
127 rawName:@"objectclass"
132 NSEnumerator *values;
135 values = [attr stringValueEnumerator];
136 while ((value = [values nextObject])) {
140 if ((len = [value length]) == 0)
143 chars = calloc(len + 1, sizeof(unichar));
144 [value getCharacters:chars];
146 [self->contentHandler
147 startElement:@"objectclass"
149 rawName:@"objectclass"
152 [self->contentHandler characters:chars length:len];
154 if (chars) free(chars);
156 [self->contentHandler
157 endElement:@"objectclass"
159 rawName:@"objectclass"];
163 [self->contentHandler
164 endElement:@"objectclass"
166 rawName:@"objectclass"];
169 - (void)_produceEntry:(NGLdapEntry *)_entry {
170 SaxAttributes *attrs;
174 attrs = [[SaxAttributes alloc] init];
176 [attrs addAttribute:@"dn" uri:XMLNS_DSML rawName:@"dn"
180 [self->contentHandler
181 startElement:@"entry"
186 [attrs release]; attrs = nil;
190 [self _produceObjectClassOfEntry:_entry];
192 names = [[_entry attributeNames] objectEnumerator];
193 while ((cname = [names nextObject])) {
194 NGLdapAttribute *attr;
196 if ([cname isEqualToString:@"objectclass"])
199 if ((attr = [_entry attributeWithName:cname]))
200 [self _produceAttribute:attr ofEntry:_entry];
203 [self->contentHandler
209 - (void)_produceEntries:(NSEnumerator *)_entries {
212 [self->contentHandler
213 startElement:@"directory-entries"
215 rawName:@"directory-entries"
218 while ((entry = [_entries nextObject]))
219 [self _produceEntry:entry];
221 [self->contentHandler
222 endElement:@"directory-entries"
224 rawName:@"directory-entries"];
227 - (void)produceOnConnection:(NGLdapConnection *)_con dn:(NSString *)_dn {
228 [self->contentHandler startDocument];
229 [self->contentHandler startPrefixMapping:@"" uri:XMLNS_DSML];
231 [self->contentHandler
237 [self _produceEntries:[_con flatSearchAtBaseDN:_dn
241 [self->contentHandler endElement:@"dsml" namespace:XMLNS_DSML rawName:@"dsml"];
243 [self->contentHandler endPrefixMapping:@""];
244 [self->contentHandler endDocument];
247 @end /* DSMLSaxProducer */
249 #import <SaxObjC/SaxDefaultHandler.h>
251 @interface DSMLSaxOutputter : SaxDefaultHandler
257 @implementation DSMLSaxOutputter
259 - (void)startElement:(NSString *)_localName
260 namespace:(NSString *)_ns
261 rawName:(NSString *)_rawName
262 attributes:(id<SaxAttributes>)_attrs
267 for (i = 0; i < level; i++)
269 printf("<dsml:%s", [_localName cString]);
272 printf(" xmlns:dsml='%s'", [_ns cString]);
275 for (i = 0, count = [_attrs count]; i < count; i++) {
277 [[_attrs nameAtIndex:i] cString],
278 [[_attrs valueAtIndex:i] cString]);
284 - (void)endElement:(NSString *)_localName
285 namespace:(NSString *)_ns
286 rawName:(NSString *)_rawName
289 for (i = 0; i < level; i++)
291 printf("</dsml:%s>\n", [_localName cString]);
295 - (void)characters:(unichar *)_chars length:(int)_len {
299 for (i = 0; i < level + 1; i++)
302 s = [[NSString alloc] initWithCharacters:_chars length:_len];
303 printf("%s\n", [s cString]);
307 @end /* DSMLSaxOutputter */
309 #import <Foundation/Foundation.h>
311 int main(int argc, char **argv, char **env) {
312 NSAutoreleasePool *pool;
315 DSMLSaxProducer *cpu;
316 DSMLSaxOutputter *out;
318 pool = [[NSAutoreleasePool alloc] init];
319 #if LIB_FOUNDATION_LIBRARY
320 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
323 args = [[NSProcessInfo processInfo] arguments];
324 if ([args count] < 1) {
325 NSLog(@"usage: %@ <files>", [args objectAtIndex:0]);
328 else if ([args count] == 1)
329 args = [args arrayByAddingObject:@"."];
331 ud = [NSUserDefaults standardUserDefaults];
333 cpu = [[DSMLSaxProducer alloc] init];
334 out = [[DSMLSaxOutputter alloc] init];
335 [cpu setContentHandler:out];
336 [cpu setErrorHandler:out];
339 fm = [[NGLdapFileManager alloc]
340 initWithHostName:[ud stringForKey:@"LDAPHost"]
342 bindDN:[ud stringForKey:@"LDAPBindDN"]
343 credentials:[ud stringForKey:@"LDAPPassword"]
344 rootDN:[ud stringForKey:@"LDAPRootDN"]];
345 fm = [fm autorelease];
349 NGLdapConnection *con;
351 con = [[NGLdapConnection alloc]
352 initWithHostName:[ud stringForKey:@"LDAPHost"]
354 [con bindWithMethod:@"simple"
355 binddn:[ud stringForKey:@"LDAPBindDN"]
356 credentials:[ud stringForKey:@"LDAPPassword"]];
358 [cpu produceOnConnection:con
359 dn:[ud stringForKey:@"LDAPRootDN"]];