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