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