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 package org.exolab.castor.dsml.mozilla;
38
39
40 import java.util.Enumeration;
41 import org.xml.sax.DocumentHandler;
42 import org.xml.sax.SAXException;
43 import org.xml.sax.helpers.AttributeListImpl;
44 import netscape.ldap.LDAPEntry;
45 import netscape.ldap.LDAPAttribute;
46 import netscape.ldap.LDAPAttributeSet;
47 import netscape.ldap.LDAPSchema;
48 import netscape.ldap.LDAPAttributeSchema;
49 import netscape.ldap.LDAPObjectClassSchema;
50 import netscape.ldap.LDAPSearchResults;
51
52 import org.castor.core.util.Base64Encoder;
53 import org.exolab.castor.dsml.Producer;
54 import org.exolab.castor.dsml.XML;
55
56
57
58
59
60
61
62
63 public class MozillaProducer extends Producer {
64
65
66 public MozillaProducer(DocumentHandler docHandler, boolean namespace) {
67 super(docHandler, namespace);
68 }
69
70
71 public void produce(LDAPEntry entry) throws SAXException {
72 AttributeListImpl attrList;
73 LDAPAttributeSet attrSet;
74 LDAPAttribute attr;
75 Enumeration enumeration;
76 Enumeration values;
77 byte[] value;
78
79 leaveSchema();
80 enterDirectory();
81
82
83 attrList = new AttributeListImpl();
84 attrList.addAttribute(XML.Entries.Attributes.DN, "CDATA", entry.getDN());
85
86 _docHandler.startElement(prefix(XML.Entries.Elements.ENTRY), attrList);
87
88 attrSet = entry.getAttributeSet();
89 if (attrSet != null) {
90
91 attr = attrSet.getAttribute("objectclass");
92 if (attr != null) {
93
94 attrList = new AttributeListImpl();
95 _docHandler.startElement(prefix(XML.Entries.Elements.OBJECT_CLASS), attrList);
96 values = attr.getStringValues();
97 while (values.hasMoreElements()) {
98 char[] chars;
99
100
101 chars = ((String) values.nextElement()).toCharArray();
102 attrList = new AttributeListImpl();
103 _docHandler.startElement(prefix(XML.Entries.Elements.OBJECT_CLASS_VALUE), attrList);
104 _docHandler.characters(chars, 0, chars.length);
105 _docHandler.endElement(prefix(XML.Entries.Elements.OBJECT_CLASS_VALUE));
106 }
107 _docHandler.endElement(prefix(XML.Entries.Elements.OBJECT_CLASS));
108 }
109
110 enumeration = attrSet.getAttributes();
111 while (enumeration.hasMoreElements()) {
112
113 attr = (LDAPAttribute) enumeration.nextElement();
114 if (attr.getName().equals("objectclass"))
115 continue;
116 attrList = new AttributeListImpl();
117 attrList.addAttribute(XML.Entries.Attributes.NAME, "CDATA", attr.getName());
118 _docHandler.startElement(prefix(XML.Entries.Elements.ATTRIBUTE), attrList);
119
120 values = attr.getByteValues();
121 while (values.hasMoreElements()) {
122 char[] chars;
123 int i;
124
125
126 value = (byte[]) values.nextElement();
127 attrList = new AttributeListImpl();
128 if (value == null) {
129 chars = new char[0];
130 } else {
131
132
133
134
135
136
137 for (i = 0; i < value.length; ++i) {
138 if (value[i] < 0x20 || value[i] == 0x7f)
139 break;
140 }
141 if (i == value.length) {
142 chars = new char[value.length];
143 for (i = 0; i < value.length; ++i)
144 chars[i] = (char) value[i];
145 } else {
146 chars = Base64Encoder.encode(value);
147 attrList.addAttribute(XML.Entries.Attributes.ENCODING, "NMTOKEN",
148 XML.Entries.Attributes.Encodings.BASE64);
149 }
150 }
151
152 _docHandler.startElement(prefix(XML.Entries.Elements.VALUE), attrList);
153 _docHandler.characters(chars, 0, chars.length);
154 _docHandler.endElement(prefix(XML.Entries.Elements.VALUE));
155 }
156 _docHandler.endElement(prefix(XML.Entries.Elements.ATTRIBUTE));
157 }
158 }
159 _docHandler.endElement(prefix(XML.Entries.Elements.ENTRY));
160 }
161
162
163 public void produce(Enumeration entries) throws SAXException {
164 while (entries.hasMoreElements()) {
165 produce((LDAPEntry) entries.nextElement());
166 }
167 }
168
169
170 public void produce(LDAPSearchResults entries) throws SAXException {
171 while (entries.hasMoreElements()) {
172 produce((LDAPEntry) entries.nextElement());
173 }
174 }
175
176
177 public void produce(LDAPSchema schema) throws SAXException {
178 Enumeration enumeration;
179
180 enumeration = schema.getObjectClasses();
181 while (enumeration.hasMoreElements()) {
182 produce((LDAPObjectClassSchema) enumeration.nextElement());
183 }
184 enumeration = schema.getAttributes();
185 while (enumeration.hasMoreElements()) {
186 produce((LDAPAttributeSchema) enumeration.nextElement());
187 }
188 }
189
190
191 public void produce(LDAPObjectClassSchema schema) throws SAXException {
192 AttributeListImpl attrList;
193 String superiors[];
194 String superior;
195 int i;
196 Enumeration enumeration;
197
198 leaveDirectory();
199 enterSchema();
200
201 attrList = new AttributeListImpl();
202
203 attrList.addAttribute(XML.Schema.Attributes.ID, "ID", schema.getName());
204
205 superiors = schema.getSuperiors();
206 superior = null;
207 for (i = 0; i < superiors.length; ++i) {
208 if (i == 0)
209 superior = superiors[i];
210 else
211 superior = superior + "," + superiors[i];
212 }
213 if (i > 0)
214 attrList.addAttribute(XML.Schema.Attributes.SUPERIOR, "CDATA", superior);
215
216 attrList.addAttribute(XML.Schema.Attributes.OBSOLETE, null,
217 schema.isObsolete() ? "true" : "false");
218
219 switch (schema.getType()) {
220 case LDAPObjectClassSchema.STRUCTURAL:
221 attrList.addAttribute(XML.Schema.Attributes.TYPE, null,
222 XML.Schema.Attributes.Types.STRUCTURAL);
223 break;
224 case LDAPObjectClassSchema.ABSTRACT:
225 attrList.addAttribute(XML.Schema.Attributes.TYPE, null,
226 XML.Schema.Attributes.Types.ABSTRACT);
227 break;
228 case LDAPObjectClassSchema.AUXILIARY:
229 attrList.addAttribute(XML.Schema.Attributes.TYPE, null,
230 XML.Schema.Attributes.Types.AUXILIARY);
231 break;
232 }
233
234
235 _docHandler.startElement(prefix(XML.Schema.Elements.CLASS), attrList);
236
237
238 if (schema.getName() != null) {
239 attrList = new AttributeListImpl();
240 _docHandler.startElement(prefix(XML.Schema.Elements.NAME), attrList);
241 _docHandler.characters(schema.getName().toCharArray(), 0, schema.getName().length());
242 _docHandler.endElement(prefix(XML.Schema.Elements.NAME));
243 }
244
245 if (schema.getDescription() != null) {
246 attrList = new AttributeListImpl();
247 _docHandler.startElement(prefix(XML.Schema.Elements.DESCRIPTION), attrList);
248 _docHandler.characters(schema.getDescription().toCharArray(), 0,
249 schema.getDescription().length());
250 _docHandler.endElement(prefix(XML.Schema.Elements.DESCRIPTION));
251 }
252
253 if (schema.getID() != null) {
254 attrList = new AttributeListImpl();
255 _docHandler.startElement(prefix(XML.Schema.Elements.OID), attrList);
256 _docHandler.characters(schema.getID().toCharArray(), 0, schema.getID().length());
257 _docHandler.endElement(prefix(XML.Schema.Elements.OID));
258 }
259
260
261 enumeration = schema.getOptionalAttributes();
262 while (enumeration.hasMoreElements()) {
263 attrList = new AttributeListImpl();
264 attrList.addAttribute(XML.Schema.Attributes.REF, "CDATA",
265 "#" + (String) enumeration.nextElement());
266 attrList.addAttribute(XML.Schema.Attributes.REQUIRED, null, "false");
267 _docHandler.startElement(prefix(XML.Schema.Elements.ATTRIBUTE), attrList);
268 _docHandler.endElement(prefix(XML.Schema.Elements.ATTRIBUTE));
269 }
270
271 enumeration = schema.getRequiredAttributes();
272 while (enumeration.hasMoreElements()) {
273 attrList = new AttributeListImpl();
274 attrList.addAttribute(XML.Schema.Attributes.REF, "CDATA",
275 "#" + (String) enumeration.nextElement());
276 attrList.addAttribute(XML.Schema.Attributes.REQUIRED, null, "true");
277 _docHandler.startElement(prefix(XML.Schema.Elements.ATTRIBUTE), attrList);
278 _docHandler.endElement(prefix(XML.Schema.Elements.ATTRIBUTE));
279 }
280
281 _docHandler.endElement(prefix(XML.Schema.Elements.CLASS));
282 }
283
284
285 public void produce(LDAPAttributeSchema schema) throws SAXException {
286 AttributeListImpl attrList;
287
288 leaveDirectory();
289 enterSchema();
290
291 attrList = new AttributeListImpl();
292
293 attrList.addAttribute(XML.Schema.Attributes.ID, "ID", schema.getName());
294
295 if (schema.getSuperior() != null) {
296 attrList.addAttribute(XML.Schema.Attributes.SUPERIOR, "CDATA", "#" + schema.getSuperior());
297 }
298
299 attrList.addAttribute(XML.Schema.Attributes.OBSOLETE, null,
300 schema.isObsolete() ? "true" : "false");
301
302 attrList.addAttribute(XML.Schema.Attributes.SINGLE_VALUE, null,
303 schema.isSingleValued() ? "true" : "false");
304
305
306
307
308 _docHandler.startElement(prefix(XML.Schema.Elements.ATTRIBUTE_TYPE), attrList);
309
310
311 if (schema.getName() != null) {
312 attrList = new AttributeListImpl();
313 _docHandler.startElement(prefix(XML.Schema.Elements.NAME), attrList);
314 _docHandler.characters(schema.getName().toCharArray(), 0, schema.getName().length());
315 _docHandler.endElement(prefix(XML.Schema.Elements.NAME));
316 }
317
318 if (schema.getDescription() != null) {
319 attrList = new AttributeListImpl();
320 _docHandler.startElement(prefix(XML.Schema.Elements.DESCRIPTION), attrList);
321 _docHandler.characters(schema.getDescription().toCharArray(), 0,
322 schema.getDescription().length());
323 _docHandler.endElement(prefix(XML.Schema.Elements.DESCRIPTION));
324 }
325
326 if (schema.getID() != null) {
327 attrList = new AttributeListImpl();
328 _docHandler.startElement(prefix(XML.Schema.Elements.OID), attrList);
329 _docHandler.characters(schema.getID().toCharArray(), 0, schema.getID().length());
330 _docHandler.endElement(prefix(XML.Schema.Elements.OID));
331 }
332
333 if (schema.getSyntaxString() != null) {
334 attrList = new AttributeListImpl();
335 _docHandler.startElement(prefix(XML.Schema.Elements.SYNTAX), attrList);
336 _docHandler.characters(schema.getSyntaxString().toCharArray(), 0,
337 schema.getSyntaxString().length());
338 _docHandler.endElement(prefix(XML.Schema.Elements.SYNTAX));
339 }
340
341
342
343
344
345
346
347
348 _docHandler.endElement(prefix(XML.Schema.Elements.ATTRIBUTE_TYPE));
349 }
350
351
352 }