001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.wsif.providers;
059:
060: import java.lang.reflect.Array;
061:
062: import org.apache.wsif.WSIFException;
063:
064: /**
065: * A class of static utility methods for use across multiple providers
066: *
067: * @author Owen Burroughs <owenb@apache.org>
068: */
069: public class ProviderUtils {
070:
071: /**
072: * Convert an array of any dimensions containing only Strings of length 1, to an array
073: * of java.lang.Characters which has the same dimensions.
074: * @param obj The array of Strings
075: * @return The Character array
076: * @exception A WSIFException thrown if the conversion fails for any reason.
077: */
078: public static Object stringArrayToCharacterArray(Object obj)
079: throws WSIFException {
080: return stringArrayToCharacterArray(obj, null);
081: }
082:
083: /**
084: * Convert an array of any dimensions containing only Strings of length 1, to an array
085: * of java.lang.Characters which has the same dimensions.
086: * @param obj The array of Strings
087: * @param ret temporary array used in recursion. The first call should be passed null.
088: * @return The Character array
089: * @exception A WSIFException thrown if the conversion fails for any reason.
090: */
091: protected static Object stringArrayToCharacterArray(Object obj,
092: Object ret) throws WSIFException {
093: if (obj.getClass().isArray()) {
094: Object[] objs = (Object[]) obj;
095: ret = new Object[objs.length];
096: for (int i = 0; i < objs.length; i++) {
097: Object temp = stringArrayToCharacterArray(objs[i],
098: ((Object[]) ret)[i]);
099: if (i == 0) {
100: Class tempc = temp.getClass();
101: ret = Array.newInstance(tempc, objs.length);
102: }
103: ((Object[]) ret)[i] = temp;
104: }
105: return ret;
106: } else if (obj instanceof String) {
107: String s = (String) obj;
108: if (s.length() == 1) {
109: ret = new Character(s.charAt(0));
110: return ret;
111: } else {
112: throw new WSIFException(
113: "String is longer than 1 character");
114: }
115: } else {
116: throw new WSIFException(
117: "Array entry is not a String or another array");
118: }
119: }
120:
121: /**
122: * Convert an array of any dimensions containing only Strings of length 1, to an array
123: * of chars which has the same dimensions.
124: * @param obj The array of Strings
125: * @return The char array
126: * @exception A WSIFException thrown if the conversion fails for any reason.
127: */
128: public static Object stringArrayToCharArray(Object obj)
129: throws WSIFException {
130: return stringArrayToCharArray(obj, null);
131: }
132:
133: /**
134: * Convert an array of any dimensions containing only Strings of length 1, to an array
135: * of chars which has the same dimensions.
136: * @param obj The array of Strings
137: * @param ret temporary array used in recursion. The first call should be passed null.
138: * @return The char array
139: * @exception A WSIFException thrown if the conversion fails for any reason.
140: */
141: protected static Object stringArrayToCharArray(Object obj,
142: Object ret) throws WSIFException {
143: if (obj.getClass().isArray()) {
144: Object[] objs = (Object[]) obj;
145: ret = new Object[objs.length];
146: for (int i = 0; i < objs.length; i++) {
147: Object temp = stringArrayToCharArray(objs[i],
148: ((Object[]) ret)[i]);
149: if (i == 0) {
150: Class tempc = temp.getClass();
151: ret = Array.newInstance(tempc, objs.length);
152: }
153: ((Object[]) ret)[i] = temp;
154: if (temp instanceof Character && (i == objs.length - 1)) {
155: char[] tempca = new char[objs.length];
156: for (int j = 0; j < objs.length; j++) {
157: tempca[j] = ((Character[]) ret)[j].charValue();
158: }
159: ret = tempca;
160: }
161: }
162: return ret;
163: } else if (obj instanceof String) {
164: String s = (String) obj;
165: if (s.length() == 1) {
166: ret = new Character(s.charAt(0));
167: return ret;
168: } else {
169: throw new WSIFException(
170: "String is longer than 1 character");
171: }
172: } else {
173: throw new WSIFException(
174: "Array entry is not a String or another array");
175: }
176: }
177:
178: /**
179: * Convert an array of java.lang.Characters of any dimensions to an equivalent array
180: * of java.lang.Strings which has the same dimensions.
181: * @param obj The array of Characters
182: * @return The array of Strings
183: * @exception A WSIFException thrown if the conversion fails for any reason.
184: */
185: public static Object characterArrayToStringArray(Object obj)
186: throws WSIFException {
187: return characterArrayToStringArray(obj, null);
188: }
189:
190: /**
191: * Convert an array of java.lang.Characters of any dimensions to an equivalent array
192: * of java.lang.Strings which has the same dimensions.
193: * @param obj The array of Characters
194: * @param ret temporary array used in recursion. The first call should be passed null.
195: * @return The array of Strings
196: * @exception A WSIFException thrown if the conversion fails for any reason.
197: */
198: protected static Object characterArrayToStringArray(Object obj,
199: Object ret) throws WSIFException {
200: if (obj.getClass().isArray()) {
201: Object[] objs = (Object[]) obj;
202: ret = new Object[objs.length];
203: for (int i = 0; i < objs.length; i++) {
204: Object temp = characterArrayToStringArray(objs[i],
205: ((Object[]) ret)[i]);
206: if (i == 0) {
207: Class tempc = temp.getClass();
208: ret = Array.newInstance(tempc, objs.length);
209: }
210: ((Object[]) ret)[i] = temp;
211: }
212: return ret;
213: } else if (obj instanceof Character) {
214: String s = obj.toString();
215: return s;
216: } else {
217: throw new WSIFException(
218: "Array entry is not a Character or another array");
219: }
220: }
221:
222: /**
223: * Convert an array of chars of any dimensions to an equivalent array
224: * of java.lang.Strings which has the same dimensions.
225: * @param obj The array of chars
226: * @return The array of Strings
227: * @exception A WSIFException thrown if the conversion fails for any reason.
228: */
229: public static Object charArrayToStringArray(Object obj)
230: throws WSIFException {
231: return charArrayToStringArray(obj, null);
232: }
233:
234: /**
235: * Convert an array of chars of any dimensions to an equivalent array
236: * of java.lang.Strings which has the same dimensions.
237: * @param obj The array of chars
238: * @param ret temporary array used in recursion. The first call should be passed null.
239: * @return The array of Strings
240: * @exception A WSIFException thrown if the conversion fails for any reason.
241: */
242: protected static Object charArrayToStringArray(Object obj,
243: Object ret) throws WSIFException {
244: if (obj.getClass().isArray()) {
245: if (obj instanceof char[]) {
246: char[] ca = (char[]) obj;
247: Character[] chra = new Character[ca.length];
248: for (int j = 0; j < ca.length; j++) {
249: chra[j] = new Character(ca[j]);
250: }
251: obj = chra;
252: }
253: Object[] objs = (Object[]) obj;
254: ret = new Object[objs.length];
255: for (int i = 0; i < objs.length; i++) {
256: Object temp = charArrayToStringArray(objs[i],
257: ((Object[]) ret)[i]);
258: if (i == 0) {
259: Class tempc = temp.getClass();
260: ret = Array.newInstance(tempc, objs.length);
261: }
262: ((Object[]) ret)[i] = temp;
263: }
264: return ret;
265: } else if (obj instanceof Character) {
266: String s = obj.toString();
267: return s;
268: } else {
269: throw new WSIFException(
270: "Array entry is not a char or another array");
271: }
272: }
273:
274: /**
275: * Convert a String to a Character. If the String is longer than one character
276: * this method will return null;
277: * @param str The String
278: * @return The Character or null if the String was longer than one character
279: */
280: public static Character stringToCharacter(String str) {
281: if (str.length() != 1)
282: return null;
283: return new Character(str.charAt(0));
284: }
285:
286: /**
287: * Returns a default Object value for a given Class. If the Class is a primitive type
288: * the method will return the default value for that primitive type wrapped up in its
289: * object form. For example, invoking the method with int.class will return a new
290: * java.lang.Integer with an int value of 0. If the Class does not represent a
291: * primitive type then null will be returned.
292: * @param cls The Class
293: * @return The default object value
294: */
295: public static Object getDefaultObject(Class cls) {
296: if (cls == null) {
297: return null;
298: } else if (cls.isPrimitive()) {
299: if (cls.getName().equals("int")) {
300: return new Integer(0);
301: } else if (cls.getName().equals("char")) {
302: return new Character('0');
303: } else if (cls.getName().equals("long")) {
304: return new Long(0);
305: } else if (cls.getName().equals("short")) {
306: short s = 0;
307: return new Short(s);
308: } else if (cls.getName().equals("double")) {
309: return new Double(0);
310: } else if (cls.getName().equals("boolean")) {
311: return new Boolean(false);
312: } else if (cls.getName().equals("byte")) {
313: byte b = 0;
314: return new Byte(b);
315: } else {
316: return new Float(0);
317: }
318: } else {
319: return null;
320: }
321: }
322: }
|