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 java.util.Vector;
52  import org.xml.sax.SAXException;
53  import org.xml.sax.AttributeList;
54  import org.xml.sax.HandlerBase;
55  import netscape.ldap.LDAPEntry;
56  import netscape.ldap.LDAPAttribute;
57  import netscape.ldap.LDAPAttributeSet;
58  
59  import org.castor.core.util.Base64Decoder;
60  import org.castor.core.util.Messages;
61  import org.exolab.castor.dsml.XML;
62  
63  
64  /**
65   *
66   *
67   * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
68   * @version $Revision$ $Date: 2006-04-10 16:39:24 -0600 (Mon, 10 Apr 2006) $
69   */
70  class MozillaEntryConsumer
71      extends HandlerBase
72  {
73  
74  
75      private String                 _entryDN;
76  
77  
78      private LDAPAttributeSet       _attrSet;
79  
80  
81      private LDAPAttribute          _attr;
82  
83  
84      private StringBuffer           _value;
85  
86  
87      private Base64Decoder  _decoder;
88  
89  
90      private Vector                 _entries = new Vector();
91  
92  
93      MozillaEntryConsumer()
94      {
95      }
96  
97  
98      public Enumeration getEntries()
99      {
100 	return _entries.elements();
101     }
102 
103 
104     public void startElement( String tagName, AttributeList attr )
105 	throws SAXException
106     {
107 	if ( tagName.equals( XML.Entries.ELEMENT ) ) {
108 	    // Do nothing
109 	} else if ( tagName.equals( XML.Entries.Elements.ENTRY ) ) {
110 	    if ( _attrSet != null )
111 		throw new SAXException( Messages.format( "dsml.openingTagNotRecognized", tagName ) );
112 	    _attrSet = new LDAPAttributeSet();
113 	    _entryDN = attr.getValue( XML.Entries.Attributes.DN );
114 	} else if ( tagName.equals( XML.Entries.Elements.OBJECT_CLASS ) ) {
115 	    if ( _attrSet == null || _attr != null )
116 		throw new SAXException( Messages.format( "dsml.openingTagNotRecognized", tagName ) );
117 	    _attr = new LDAPAttribute( "objectclass" );
118 	} else if ( tagName.equals( XML.Entries.Elements.ATTRIBUTE ) ) {
119 	    if ( _attrSet == null || _attr != null )
120 		throw new SAXException( Messages.format( "dsml.openingTagNotRecognized", tagName ) );
121 	    _attr = new LDAPAttribute( attr.getValue( XML.Entries.Attributes.NAME ) );
122 	} else if ( tagName.equals( XML.Entries.Elements.VALUE ) ||
123 		    tagName.equals( XML.Entries.Elements.OBJECT_CLASS_VALUE ) ) {
124 	    if ( _attrSet == null || _attr == null || _value != null )
125 		throw new SAXException( Messages.format( "dsml.openingTagNotRecognized", tagName ) );
126 	    if ( XML.Entries.Attributes.Encodings.BASE64.equals(
127 		     attr.getValue( XML.Entries.Attributes.ENCODING ) ) ) {
128 		_decoder = new Base64Decoder();
129 	    } else {
130 		_value = new StringBuffer();
131 	    }
132 	} else {
133 	    throw new SAXException( Messages.format( "dsml.openingTagNotRecognized", tagName ) );
134 	}
135     }
136 	    
137 
138     public void endElement( String tagName )
139 	throws SAXException
140     {
141 	if ( tagName.equals( XML.Entries.ELEMENT ) ) {
142 	    if ( _attrSet != null )
143 		throw new SAXException( Messages.format( "dsml.closingTagNotRecognized", tagName ) );
144 	} else if ( tagName.equals( XML.Entries.Elements.ENTRY ) ) {
145 	    if ( _attrSet == null || _attr != null )
146 		throw new SAXException( Messages.format( "dsml.closingTagNotRecognized", tagName ) );
147 	    _entries.addElement( new LDAPEntry( _entryDN, _attrSet ) );
148 	    _entryDN = null;
149 	    _attrSet = null;
150 	} else if ( tagName.equals( XML.Entries.Elements.OBJECT_CLASS ) ||
151 		    tagName.equals( XML.Entries.Elements.ATTRIBUTE ) ) {
152 	    if ( _attrSet == null || _attr == null || _value != null )
153 		throw new SAXException( Messages.format( "dsml.closingTagNotRecognized", tagName ) );
154 	    _attrSet.add( _attr );
155 	    _attr = null;
156 	} else if ( tagName.equals( XML.Entries.Elements.VALUE ) ||
157 		    tagName.equals( XML.Entries.Elements.OBJECT_CLASS_VALUE ) ) {
158 	    if ( _attrSet == null || _attr == null || ( _value == null && _decoder == null ) )
159 		throw new SAXException( Messages.format( "dsml.closingTagNotRecognized", tagName ) );
160 	    if ( _decoder != null ) {
161 		_attr.addValue( _decoder.getByteArray() );
162 		_decoder = null;
163 	    } else {
164 		_attr.addValue( _value.toString() );
165 		_value = null;
166 	    }
167 	} else {
168 	    throw new SAXException( Messages.format( "dsml.closingTagNotRecognized", tagName ) );
169 	}
170     }
171 
172     public void characters(final char[] chars, final int offset, final int length) {
173         if (_decoder != null) {
174             _decoder.translate(new String(chars, offset, length));
175         } else if (_value != null) {
176             _value.append(chars, offset, length);
177         }
178     }
179 }