1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package org.exolab.castor.dsml.mozilla;
48
49
50 import java.util.Enumeration;
51 import org.xml.sax.DocumentHandler;
52 import org.xml.sax.SAXException;
53 import netscape.ldap.LDAPException;
54 import netscape.ldap.LDAPConnection;
55 import netscape.ldap.LDAPSchema;
56 import netscape.ldap.LDAPv2;
57
58 import org.castor.core.util.Messages;
59 import org.exolab.castor.dsml.Consumer;
60 import org.exolab.castor.dsml.Exporter;
61 import org.exolab.castor.dsml.SearchDescriptor;
62 import org.exolab.castor.dsml.ImportExportException;
63
64
65
66
67
68
69
70
71 public class MozillaExporter
72 extends Exporter
73 {
74
75
76 private LDAPConnection _conn;
77
78
79 public MozillaExporter( LDAPConnection conn )
80 {
81 _conn = conn;
82 }
83
84
85 protected Consumer createConsumer()
86 {
87 return new MozillaConsumer();
88 }
89
90
91 public void export( DocumentHandler docHandler,
92 boolean serverSchema, boolean importPolicy )
93 throws ImportExportException
94 {
95 Enumeration enumeration;
96 String[] attrs;
97 MozillaProducer producer;
98 LDAPSchema schema;
99 int scope;
100
101 if ( getSearchDescriptor() == null )
102 throw new IllegalStateException( Messages.message( "dsml.searchDescriptorRequired" ) );
103
104 attrs = getSearchDescriptor().getReturnAttrs();
105 try {
106 scope = getSearchDescriptor().getScope();
107 switch ( scope ) {
108 case SearchDescriptor.Scope.ONE_LEVEL:
109 scope = LDAPv2.SCOPE_ONE;
110 break;
111 case SearchDescriptor.Scope.BASE:
112 scope = LDAPv2.SCOPE_BASE;
113 break;
114 case SearchDescriptor.Scope.SUB_TREE:
115 scope = LDAPv2.SCOPE_SUB;
116 break;
117 }
118 enumeration = _conn.search( getSearchDescriptor().getBaseDN(), scope,
119 getSearchDescriptor().getFilter(), attrs, false );
120 } catch ( LDAPException except ) {
121
122
123 if ( except.getLDAPResultCode() != LDAPException.NO_SUCH_OBJECT )
124 throw new ImportExportException( except );
125 enumeration = null;
126 }
127
128 try {
129 producer = new MozillaProducer( docHandler, false );
130 producer.startDocument();
131 if ( serverSchema ) {
132 schema = new LDAPSchema();
133 schema.fetchSchema( _conn );
134 producer.produce( schema );
135 }
136 if ( enumeration != null )
137 producer.produce( enumeration );
138 if ( importPolicy && getImportDescriptor() != null )
139 producer.produce( getImportDescriptor() );
140 producer.endDocument();
141 } catch ( SAXException except ) {
142 throw new ImportExportException( except );
143 } catch ( LDAPException except ) {
144 throw new ImportExportException( except );
145 }
146 }
147
148
149
150 }
151