View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
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 org.xml.sax.helpers.AttributeListImpl;
54  import netscape.ldap.LDAPEntry;
55  import netscape.ldap.LDAPAttribute;
56  import netscape.ldap.LDAPAttributeSet;
57  import netscape.ldap.LDAPSchema;
58  import netscape.ldap.LDAPAttributeSchema;
59  import netscape.ldap.LDAPObjectClassSchema;
60  import netscape.ldap.LDAPSearchResults;
61  
62  import org.castor.core.util.Base64Encoder;
63  import org.exolab.castor.dsml.Producer;
64  import org.exolab.castor.dsml.XML;
65  
66  
67  /**
68   *
69   *
70   * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
71   * @version $Revision$ $Date: 2005-08-05 13:58:36 -0600 (Fri, 05 Aug 2005) $
72   */
73  public class MozillaProducer
74      extends Producer
75  {
76  
77  
78      public MozillaProducer( DocumentHandler docHandler, boolean namespace  )
79      {
80  	super( docHandler, namespace );
81      }
82  
83  
84      public void produce( LDAPEntry entry )
85  	throws SAXException
86      {
87  	AttributeListImpl attrList;
88  	LDAPAttributeSet  attrSet;
89  	LDAPAttribute     attr;
90  	Enumeration       enumeration;
91  	Enumeration       values;
92  	byte[]            value;
93  
94  	leaveSchema();
95  	enterDirectory();
96  
97  	// dsml:entry dn
98  	attrList = new AttributeListImpl();
99  	attrList.addAttribute( XML.Entries.Attributes.DN, "CDATA", entry.getDN() );
100 	// dsml:entry
101 	_docHandler.startElement( prefix( XML.Entries.Elements.ENTRY ), attrList );
102 	
103 	attrSet = entry.getAttributeSet();
104 	if ( attrSet != null ) {
105 	    
106 	    attr = attrSet.getAttribute( "objectclass" );
107 	    if ( attr != null ) {
108 		// dsml:objectclass
109 		attrList = new AttributeListImpl();
110 		_docHandler.startElement( prefix( XML.Entries.Elements.OBJECT_CLASS ), attrList );
111 		values = attr.getStringValues();
112 		while ( values.hasMoreElements() ) {
113 		    char[] chars;
114 		    
115 		    // dsml:oc-value
116 		    chars = ( (String) values.nextElement() ).toCharArray();
117 		    attrList = new AttributeListImpl();
118 		    _docHandler.startElement( prefix( XML.Entries.Elements.OBJECT_CLASS_VALUE ), attrList );
119 		    _docHandler.characters( chars, 0, chars.length );
120 		    _docHandler.endElement( prefix( XML.Entries.Elements.OBJECT_CLASS_VALUE ) );
121 		}
122 		_docHandler.endElement( prefix( XML.Entries.Elements.OBJECT_CLASS ) );
123 	    }
124 	    
125 	    enumeration = attrSet.getAttributes();
126 	    while ( enumeration.hasMoreElements() ) {
127 		// dsml:attr
128 		attr = (LDAPAttribute) enumeration.nextElement();
129 		if ( attr.getName().equals( "objectclass" ) )
130 		    continue;
131 		attrList = new AttributeListImpl();
132 		attrList.addAttribute( XML.Entries.Attributes.NAME, "CDATA", attr.getName() );
133 		_docHandler.startElement( prefix( XML.Entries.Elements.ATTRIBUTE ), attrList );
134 		
135 		values = attr.getByteValues();
136 		while ( values.hasMoreElements() ) {
137 		    char[] chars;
138 		    int    i;
139 
140 		    // dsml:value
141 		    value = (byte[]) values.nextElement();
142 		    attrList = new AttributeListImpl();
143 		    if ( value == null ) {
144 			chars = new char[ 0 ];
145 		    } else {
146 			// XXX We have no way of knowing if the attribute is
147 			//     string or binary, so we do this stupid check
148 			//     to determine and print it as ASCII text or
149 			//     base 64 encoding.
150 			//     (note: OpenLDAP does not provide the attributes
151 			//     schema as one would hope)
152 			for ( i = 0 ; i < value.length ; ++i ) {
153 			    if ( value[ i ] < 0x20 || value[ i ] == 0x7f )
154 				break;
155 			}
156 			if ( i == value.length ) {
157 			    chars = new char[ value.length ];
158 			    for ( i = 0 ; i < value.length ; ++i )
159 				chars[ i ] = (char) value[ i ];
160 			} else {
161                 chars = Base64Encoder.encode(value);
162 			    attrList.addAttribute( XML.Entries.Attributes.ENCODING, "NMTOKEN",
163 						   XML.Entries.Attributes.Encodings.BASE64 );
164 			}
165 		    }
166 
167 		    _docHandler.startElement( prefix( XML.Entries.Elements.VALUE ), attrList );
168 		    _docHandler.characters( chars, 0, chars.length );
169 		    _docHandler.endElement( prefix( XML.Entries.Elements.VALUE ) );
170 		}
171 		_docHandler.endElement( prefix( XML.Entries.Elements.ATTRIBUTE ) );
172 	    }
173 	}
174 	_docHandler.endElement( prefix( XML.Entries.Elements.ENTRY ) );
175     }
176     
177 
178     public void produce( Enumeration entries )
179 	throws SAXException
180     {
181 	while ( entries.hasMoreElements() ) {
182 	    produce( (LDAPEntry) entries.nextElement() );
183 	}
184     }
185 
186 
187     public void produce( LDAPSearchResults entries )
188 	throws SAXException
189     {
190 	while ( entries.hasMoreElements() ) {
191 	    produce( (LDAPEntry) entries.nextElement() );
192 	}
193     }
194 
195 
196     public void produce( LDAPSchema schema )
197 	throws SAXException
198     {
199 	Enumeration       enumeration;
200 
201 	enumeration = schema.getObjectClasses();
202 	while ( enumeration.hasMoreElements() ) {
203 	    produce( (LDAPObjectClassSchema) enumeration.nextElement() );
204 	}
205 	enumeration = schema.getAttributes();
206 	while ( enumeration.hasMoreElements() ) {
207 	    produce( (LDAPAttributeSchema) enumeration.nextElement() );
208 	}
209     }
210 
211 
212     public void produce( LDAPObjectClassSchema schema )
213 	throws SAXException
214     {
215 	AttributeListImpl attrList;
216 	String            superiors[];
217 	String            superior;
218 	int               i;
219 	Enumeration       enumeration;
220 
221 	leaveDirectory();
222 	enterSchema();
223 	
224 	attrList = new AttributeListImpl();
225 	// dsml:class id
226 	attrList.addAttribute( XML.Schema.Attributes.ID, "ID", schema.getName() );
227 	// dsml:class superior
228 	superiors = schema.getSuperiors();
229 	superior = null;
230 	for ( i = 0 ; i < superiors.length ; ++i ) {
231 	    if ( i == 0 )
232 		superior = superiors[ i ];
233 	    else
234 		superior = superior + "," + superiors[ i ];
235 	}
236 	if ( i > 0 )
237 	    attrList.addAttribute( XML.Schema.Attributes.SUPERIOR, "CDATA", superior );
238 	// dsml:class obsolete
239 	attrList.addAttribute( XML.Schema.Attributes.OBSOLETE, null,
240 			       schema.isObsolete() ? "true" : "false" );
241 	// dsml:class type
242 	switch ( schema.getType() ) {
243 	case LDAPObjectClassSchema.STRUCTURAL:
244 	    attrList.addAttribute( XML.Schema.Attributes.TYPE, null,
245 				   XML.Schema.Attributes.Types.STRUCTURAL );
246 	    break;
247 	case LDAPObjectClassSchema.ABSTRACT:
248 	    attrList.addAttribute( XML.Schema.Attributes.TYPE, null,
249 				   XML.Schema.Attributes.Types.ABSTRACT );
250 	    break;
251 	case LDAPObjectClassSchema.AUXILIARY:
252 	    attrList.addAttribute( XML.Schema.Attributes.TYPE, null,
253 				   XML.Schema.Attributes.Types.AUXILIARY );
254 	    break;
255 	}
256 
257 	// dsml:class
258 	_docHandler.startElement( prefix( XML.Schema.Elements.CLASS ), attrList );
259 
260 	// dsml:class name
261 	if ( schema.getName() != null ) {
262 	    attrList = new AttributeListImpl();
263 	    _docHandler.startElement( prefix( XML.Schema.Elements.NAME ), attrList );
264 	    _docHandler.characters( schema.getName().toCharArray(), 0,
265 				    schema.getName().length() );
266 	    _docHandler.endElement( prefix( XML.Schema.Elements.NAME ) );
267 	}
268 	// dsml:class description
269 	if ( schema.getDescription() != null ) {
270 	    attrList = new AttributeListImpl();
271 	    _docHandler.startElement( prefix( XML.Schema.Elements.DESCRIPTION ), attrList );
272 	    _docHandler.characters( schema.getDescription().toCharArray(), 0,
273 				    schema.getDescription().length() );
274 	    _docHandler.endElement( prefix( XML.Schema.Elements.DESCRIPTION ) );
275 	}
276 	// dsml:class object-identifier
277 	if ( schema.getID() != null ) {
278 	    attrList = new AttributeListImpl();
279 	    _docHandler.startElement( prefix( XML.Schema.Elements.OID ), attrList );
280 	    _docHandler.characters( schema.getID().toCharArray(), 0,
281 				    schema.getID().length() );
282 	    _docHandler.endElement( prefix( XML.Schema.Elements.OID ) );
283 	}
284 
285 	// dsml:class attribute required=false
286 	enumeration = schema.getOptionalAttributes();
287 	while ( enumeration.hasMoreElements() ) {
288 	    attrList = new AttributeListImpl();
289 	    attrList.addAttribute( XML.Schema.Attributes.REF, "CDATA",
290 				   "#" + (String) enumeration.nextElement() );
291 	    attrList.addAttribute( XML.Schema.Attributes.REQUIRED, null, "false" );
292 	    _docHandler.startElement( prefix( XML.Schema.Elements.ATTRIBUTE) , attrList );
293 	    _docHandler.endElement( prefix( XML.Schema.Elements.ATTRIBUTE ) );
294 	}
295 	// dsml:class attribute required=true
296 	enumeration = schema.getRequiredAttributes();
297 	while ( enumeration.hasMoreElements() ) {
298 	    attrList = new AttributeListImpl();
299 	    attrList.addAttribute( XML.Schema.Attributes.REF, "CDATA",
300 				   "#" + (String) enumeration.nextElement() );
301 	    attrList.addAttribute( XML.Schema.Attributes.REQUIRED, null, "true" );
302 	    _docHandler.startElement( prefix( XML.Schema.Elements.ATTRIBUTE) , attrList );
303 	    _docHandler.endElement( prefix( XML.Schema.Elements.ATTRIBUTE ) );
304 	}
305 
306 	_docHandler.endElement( prefix( XML.Schema.Elements.CLASS ) );
307     }
308 
309 
310     public void produce( LDAPAttributeSchema schema )
311 	throws SAXException
312     {
313 	AttributeListImpl attrList;
314 
315 	leaveDirectory();
316 	enterSchema();
317  
318 	attrList = new AttributeListImpl();
319 	// dsml:attribute id
320 	attrList.addAttribute( XML.Schema.Attributes.ID, "ID", schema.getName() );
321 	// dsml:attribute superior
322 	if ( schema.getSuperior() != null ) {
323 	    attrList.addAttribute( XML.Schema.Attributes.SUPERIOR, "CDATA", "#" + schema.getSuperior() );
324 	}
325 	// dsml:attribute obsolete
326 	attrList.addAttribute( XML.Schema.Attributes.OBSOLETE, null,
327 			       schema.isObsolete() ? "true" : "false" );
328 	// dsml:attribute single-value
329 	attrList.addAttribute( XML.Schema.Attributes.SINGLE_VALUE, null,
330 			       schema.isSingleValued() ? "true" : "false" );
331 	// dsml:attribute user-modification
332 	// XXX
333 
334 	// dsml:attribute
335 	_docHandler.startElement( prefix( XML.Schema.Elements.ATTRIBUTE_TYPE) , attrList );
336 
337 	// dsml:attribute name
338 	if ( schema.getName() != null ) {
339 	    attrList = new AttributeListImpl();
340 	    _docHandler.startElement( prefix( XML.Schema.Elements.NAME ), attrList );
341 	    _docHandler.characters( schema.getName().toCharArray(), 0,
342 				    schema.getName().length() );
343 	    _docHandler.endElement( prefix( XML.Schema.Elements.NAME ) );
344 	}
345 	// dsml:attribute description
346 	if ( schema.getDescription() != null ) {
347 	    attrList = new AttributeListImpl();
348 	    _docHandler.startElement( prefix( XML.Schema.Elements.DESCRIPTION ), attrList );
349 	    _docHandler.characters( schema.getDescription().toCharArray(), 0,
350 				    schema.getDescription().length() );
351 	    _docHandler.endElement( prefix( XML.Schema.Elements.DESCRIPTION ) );
352 	}
353 	// dsml:attribute object-identifier
354 	if ( schema.getID() != null ) {
355 	    attrList = new AttributeListImpl();
356 	    _docHandler.startElement( prefix( XML.Schema.Elements.OID ), attrList );
357 	    _docHandler.characters( schema.getID().toCharArray(), 0,
358 				    schema.getID().length() );
359 	    _docHandler.endElement( prefix( XML.Schema.Elements.OID ) );
360 	}
361 	// dsml:attribute syntax
362 	if ( schema.getSyntaxString() != null ) {
363 	    attrList = new AttributeListImpl();
364 	    _docHandler.startElement( prefix( XML.Schema.Elements.SYNTAX ), attrList );
365 	    _docHandler.characters( schema.getSyntaxString().toCharArray(), 0,
366 				    schema.getSyntaxString().length() );
367 	    _docHandler.endElement( prefix( XML.Schema.Elements.SYNTAX ) );
368 	}
369 
370 	// dsml:attribute equality
371 	// XXX
372 	// dsml:attribute ordering
373 	// XXX
374 	// dsml:attribute substring
375 	// XXX
376 
377 	_docHandler.endElement( prefix( XML.Schema.Elements.ATTRIBUTE_TYPE ) );
378     }
379 
380 
381 }