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