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-2003 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: J1CollectionHandlers.java 5961 2006-06-03 13:02:04Z rjoachim $
044: */package org.exolab.castor.mapping.loader;
045:
046: import java.util.Vector;
047: import java.util.Hashtable;
048: import java.util.Enumeration;
049: import java.util.NoSuchElementException;
050: import org.exolab.castor.mapping.CollectionHandler;
051: import org.exolab.castor.mapping.MapItem;
052:
053: import java.lang.reflect.*;
054:
055: /**
056: * Implementation of various collection handlers for the Java 1.1
057: * libraries.
058: *
059: * @author <a href="arkin@intalio.com">Assaf Arkin</a>
060: * @version $Revision: 5961 $ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
061: * @see CollectionHandler
062: */
063: public final class J1CollectionHandlers {
064:
065: public static CollectionHandlers.Info[] getCollectionHandlersInfo() {
066: return _colHandlers;
067: }
068:
069: /**
070: * List of all the default collection handlers.
071: */
072: private static CollectionHandlers.Info[] _colHandlers = new CollectionHandlers.Info[] {
073: // For array (any)
074: new CollectionHandlers.Info("array", Object[].class, true,
075: new CollectionHandler() {
076: public Object add(Object collection,
077: Object object) {
078: if (collection == null) {
079:
080: // If the collection is of primitive type, the instantiation
081: // (if it was null) is handled in the FieldHandlerImpl. We
082: // can rely here that we deal only with array of object.
083: Object newArray = Array.newInstance(
084: object.getClass(), 1);
085: Array.set(newArray, 0, object);
086: return newArray;
087: }
088:
089: Class type = collection.getClass();
090: if (!type.isArray()) {
091: String err = "J1CollectionHandlers.array#add: type "
092: + "mismatch, expecting an array, instead received: ";
093: err += type.getName();
094: throw new IllegalArgumentException(err);
095: }
096:
097: type = type.getComponentType();
098:
099: Object newArray = Array.newInstance(type,
100: Array.getLength(collection) + 1);
101:
102: for (int i = 0; i < Array
103: .getLength(collection); ++i)
104: Array.set(newArray, i, Array.get(
105: collection, i));
106:
107: Array.set(newArray, Array
108: .getLength(collection), object);
109:
110: return newArray;
111:
112: }
113:
114: public Enumeration elements(Object collection) {
115: if (collection == null)
116: return new CollectionHandlers.EmptyEnumerator();
117: return new ArrayEnumerator(collection);
118: }
119:
120: public int size(Object collection) {
121: if (collection == null)
122: return 0;
123: return Array.getLength(collection);
124: }
125:
126: public Object clear(Object collection) {
127: if (collection == null) {
128: return null;
129: }
130: Class type = collection.getClass();
131: if (!type.isArray()) {
132: String err = "J1CollectionHandlers.array#add: type "
133: + "mismatch, expecting an array, instead received: ";
134: err += type.getName();
135: throw new IllegalArgumentException(err);
136: }
137: type = type.getComponentType();
138: return Array.newInstance(type, 0);
139: }
140:
141: public String toString() {
142: return "Object[]";
143: }
144: }),
145: // For Vector (1.1)
146: new CollectionHandlers.Info("vector", Vector.class, false,
147: new CollectionHandler() {
148: public Object add(Object collection,
149: Object object) {
150: if (collection == null) {
151: collection = new Vector();
152: ((Vector) collection)
153: .addElement(object);
154: return collection;
155: }
156: ((Vector) collection).addElement(object);
157: return null;
158: }
159:
160: public Enumeration elements(Object collection) {
161: if (collection == null)
162: return new CollectionHandlers.EmptyEnumerator();
163: return ((Vector) collection).elements();
164: }
165:
166: public int size(Object collection) {
167: if (collection == null)
168: return 0;
169: return ((Vector) collection).size();
170: }
171:
172: public Object clear(Object collection) {
173: if (collection != null)
174: ((Vector) collection)
175: .removeAllElements();
176: return null;
177: }
178:
179: public String toString() {
180: return "Vector";
181: }
182: }),
183: // For Hashtable (1.1)
184: new CollectionHandlers.Info("hashtable", Hashtable.class,
185: false, new CollectionHandler() {
186: public Object add(Object collection,
187: Object object) {
188:
189: Object key = object;
190: Object value = object;
191:
192: if (object instanceof org.exolab.castor.mapping.MapItem) {
193: MapItem mapItem = (MapItem) object;
194: key = mapItem.getKey();
195: value = mapItem.getValue();
196: if (value == null) {
197: value = object;
198: }
199: if (key == null) {
200: key = value;
201: }
202: }
203:
204: if (collection == null) {
205: collection = new Hashtable();
206: ((Hashtable) collection)
207: .put(key, value);
208: return collection;
209: }
210: ((Hashtable) collection).put(key, value);
211: return null;
212: }
213:
214: public Enumeration elements(Object collection) {
215: if (collection == null)
216: return new CollectionHandlers.EmptyEnumerator();
217: return ((Hashtable) collection).elements();
218: }
219:
220: public int size(Object collection) {
221: if (collection == null)
222: return 0;
223: return ((Hashtable) collection).size();
224: }
225:
226: public Object clear(Object collection) {
227: if (collection != null)
228: ((Hashtable) collection).clear();
229: return null;
230: }
231:
232: public String toString() {
233: return "Hashtable";
234: }
235: }),
236: // For Enumeration (1.1)
237: new CollectionHandlers.Info("enumerate", Enumeration.class,
238: false, new CollectionHandler() {
239: public Object add(Object collection,
240: Object object) {
241: //-- do nothing, cannot add elements to an enumeration
242: return null;
243: }
244:
245: public Enumeration elements(Object collection) {
246: if (collection == null)
247: return new CollectionHandlers.EmptyEnumerator();
248: return ((Enumeration) collection);
249: }
250:
251: public int size(Object collection) {
252: //-- Nothing we can do without destroying the enumeration
253: return 0;
254: }
255:
256: public Object clear(Object collection) {
257: //-- Should we iterate over nextElement?
258: return null;
259: }
260:
261: public String toString() {
262: return "Enumeration";
263: }
264: })
265:
266: };
267:
268: /**
269: * Enumerator for an array.
270: */
271: static final class ArrayEnumerator implements Enumeration {
272:
273: private final Object _array;
274:
275: private int _index;
276:
277: ArrayEnumerator(Object array) {
278: _array = array;
279: }
280:
281: public boolean hasMoreElements() {
282: return (_index < Array.getLength(_array));
283: }
284:
285: public Object nextElement() {
286: if (_index >= Array.getLength(_array))
287: throw new NoSuchElementException();
288: return Array.get(_array, _index++);
289: }
290:
291: }
292:
293: }
|