001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: JNDIProducer.java 5951 2006-05-30 22:18:48Z bsnyder $
044: */package org.exolab.castor.dsml.jndi;
045:
046: import javax.naming.NamingException;
047: import javax.naming.NamingEnumeration;
048: import javax.naming.directory.Attribute;
049: import javax.naming.directory.Attributes;
050: import javax.naming.directory.SearchResult;
051: import org.xml.sax.DocumentHandler;
052: import org.xml.sax.SAXException;
053: import org.xml.sax.helpers.AttributeListImpl;
054: import org.castor.util.Base64Encoder;
055: import org.exolab.castor.dsml.XML;
056: import org.exolab.castor.dsml.Producer;
057: import org.exolab.castor.dsml.ImportExportException;
058:
059: /**
060: *
061: *
062: * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
063: * @version $Revision: 5951 $ $Date: 2005-08-05 13:58:36 -0600 (Fri, 05 Aug 2005) $
064: */
065: public class JNDIProducer extends Producer {
066:
067: public JNDIProducer(DocumentHandler docHandler, boolean namespace) {
068: super (docHandler, namespace);
069: }
070:
071: public void produce(String name, Attributes attrs)
072: throws SAXException, NamingException {
073: AttributeListImpl attrList;
074: Attribute attr;
075: NamingEnumeration enumeration;
076: NamingEnumeration values;
077: Object value;
078:
079: leaveSchema();
080: enterDirectory();
081:
082: // dsml:entry dn
083: attrList = new AttributeListImpl();
084: attrList.addAttribute(XML.Entries.Attributes.DN, "CDATA", name);
085: // dsml:entry
086: _docHandler.startElement(prefix(XML.Entries.Elements.Entry),
087: attrList);
088:
089: if (attrs != null) {
090: attr = attrs.get("objectclass");
091: if (attr != null) {
092: // dsml:objectclass
093: attrList = new AttributeListImpl();
094: _docHandler.startElement(
095: prefix(XML.Entries.Elements.ObjectClass),
096: attrList);
097: values = attr.getAll();
098: while (values.hasMore()) {
099: char[] chars;
100:
101: // dsml:oc-value
102: value = values.next();
103: if (value != null)
104: chars = value.toString().toCharArray();
105: else
106: chars = new char[0];
107: attrList = new AttributeListImpl();
108: _docHandler.startElement(
109: prefix(XML.Entries.Elements.OCValue),
110: attrList);
111: _docHandler.characters(chars, 0, chars.length);
112: _docHandler
113: .endElement(prefix(XML.Entries.Elements.OCValue));
114: }
115: _docHandler
116: .endElement(prefix(XML.Entries.Elements.ObjectClass));
117: }
118:
119: enumeration = attrs.getAll();
120: while (enumeration.hasMore()) {
121: // dsml:attr
122: attr = (Attribute) enumeration.next();
123: if (attr.getID().equals("objectclass"))
124: continue;
125: attrList = new AttributeListImpl();
126: attrList.addAttribute(XML.Entries.Attributes.Name,
127: "CDATA", attr.getID());
128: _docHandler.startElement(
129: prefix(XML.Entries.Elements.Attribute),
130: attrList);
131:
132: values = attr.getAll();
133: while (values.hasMore()) {
134: char[] chars = null;
135: byte[] bytes = null;
136:
137: attrList = new AttributeListImpl();
138:
139: // dsml:value
140: value = values.next();
141: if (value == null) {
142: chars = new char[0];
143: } else if (value instanceof String) {
144: chars = ((String) value).toCharArray();
145: } else if (value instanceof byte[]) {
146: bytes = (byte[]) value;
147: } else {
148: chars = value.toString().toCharArray();
149: }
150: if (chars != null) {
151: boolean encode = false;
152: boolean wchar = false;
153: int i = 0;
154: while (i < chars.length && !wchar) {
155: char c = chars[i++];
156: if (c >= '\u0100')
157: encode = wchar = true;
158: else if (c >= '\u0080'
159: || (c < ' ' && c != '\n' && c != '\t'))
160: encode = true;
161: }
162: if (encode) {
163: if (wchar) {
164: bytes = new byte[chars.length << 1];
165: int j = 0;
166: // bigendian
167: for (i = 0; i < chars.length; i++) {
168: bytes[j++] = (byte) (chars[i] >> 8);
169: bytes[j++] = (byte) (0xff & chars[i]);
170: }
171: } else {
172: bytes = new byte[chars.length];
173: for (i = 0; i < chars.length; i++) {
174: bytes[i] = (byte) chars[i];
175: }
176: }
177: }
178: }
179:
180: if (bytes != null) {
181: chars = Base64Encoder.encode(bytes);
182: attrList
183: .addAttribute(
184: XML.Entries.Attributes.Encoding,
185: "NMTOKEN",
186: XML.Entries.Attributes.Encodings.Base64);
187: }
188: _docHandler.startElement(
189: prefix(XML.Entries.Elements.Value),
190: attrList);
191: _docHandler.characters(chars, 0, chars.length);
192: _docHandler
193: .endElement(prefix(XML.Entries.Elements.Value));
194: }
195: _docHandler
196: .endElement(prefix(XML.Entries.Elements.Attribute));
197: }
198: }
199: _docHandler.endElement(prefix(XML.Entries.Elements.Entry));
200: }
201:
202: public void produce(SearchResult result) throws SAXException {
203: try {
204: produce(result.getName(), result.getAttributes());
205: } catch (NamingException except) {
206: throw new SAXException(except.toString());
207: }
208: }
209:
210: public void produce(NamingEnumeration results)
211: throws ImportExportException, SAXException {
212: SearchResult result;
213:
214: try {
215: while (results.hasMore()) {
216: result = (SearchResult) results.next();
217: produce(result.getName(), result.getAttributes());
218: }
219: } catch (NamingException except) {
220: throw new ImportExportException(except);
221: }
222: }
223:
224: }
|