View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
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   * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
61   * @version $Revision$ $Date: 2005-08-05 13:58:36 -0600 (Fri, 05 Aug 2005) $
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      // dsml:entry dn
83      attrList = new AttributeListImpl();
84      attrList.addAttribute(XML.Entries.Attributes.DN, "CDATA", entry.getDN());
85      // dsml:entry
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          // dsml:objectclass
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           // dsml:oc-value
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         // dsml:attr
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           // dsml:value
126           value = (byte[]) values.nextElement();
127           attrList = new AttributeListImpl();
128           if (value == null) {
129             chars = new char[0];
130           } else {
131             // XXX We have no way of knowing if the attribute is
132             // string or binary, so we do this stupid check
133             // to determine and print it as ASCII text or
134             // base 64 encoding.
135             // (note: OpenLDAP does not provide the attributes
136             // schema as one would hope)
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     // dsml:class id
203     attrList.addAttribute(XML.Schema.Attributes.ID, "ID", schema.getName());
204     // dsml:class superior
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     // dsml:class obsolete
216     attrList.addAttribute(XML.Schema.Attributes.OBSOLETE, null,
217         schema.isObsolete() ? "true" : "false");
218     // dsml:class type
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     // dsml:class
235     _docHandler.startElement(prefix(XML.Schema.Elements.CLASS), attrList);
236 
237     // dsml:class name
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     // dsml:class description
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     // dsml:class object-identifier
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     // dsml:class attribute required=false
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     // dsml:class attribute required=true
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     // dsml:attribute id
293     attrList.addAttribute(XML.Schema.Attributes.ID, "ID", schema.getName());
294     // dsml:attribute superior
295     if (schema.getSuperior() != null) {
296       attrList.addAttribute(XML.Schema.Attributes.SUPERIOR, "CDATA", "#" + schema.getSuperior());
297     }
298     // dsml:attribute obsolete
299     attrList.addAttribute(XML.Schema.Attributes.OBSOLETE, null,
300         schema.isObsolete() ? "true" : "false");
301     // dsml:attribute single-value
302     attrList.addAttribute(XML.Schema.Attributes.SINGLE_VALUE, null,
303         schema.isSingleValued() ? "true" : "false");
304     // dsml:attribute user-modification
305     // XXX
306 
307     // dsml:attribute
308     _docHandler.startElement(prefix(XML.Schema.Elements.ATTRIBUTE_TYPE), attrList);
309 
310     // dsml:attribute name
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     // dsml:attribute description
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     // dsml:attribute object-identifier
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     // dsml:attribute syntax
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     // dsml:attribute equality
342     // XXX
343     // dsml:attribute ordering
344     // XXX
345     // dsml:attribute substring
346     // XXX
347 
348     _docHandler.endElement(prefix(XML.Schema.Elements.ATTRIBUTE_TYPE));
349   }
350 
351 
352 }