KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > providers > ProviderUtils


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.wsif.providers;
59
60 import java.lang.reflect.Array JavaDoc;
61
62 import org.apache.wsif.WSIFException;
63
64 /**
65  * A class of static utility methods for use across multiple providers
66  *
67  * @author Owen Burroughs <owenb@apache.org>
68  */

69 public class ProviderUtils {
70     
71     /**
72      * Convert an array of any dimensions containing only Strings of length 1, to an array
73      * of java.lang.Characters which has the same dimensions.
74      * @param obj The array of Strings
75      * @return The Character array
76      * @exception A WSIFException thrown if the conversion fails for any reason.
77      */

78     public static Object JavaDoc stringArrayToCharacterArray(Object JavaDoc obj) throws WSIFException {
79         return stringArrayToCharacterArray(obj, null);
80     }
81     
82     /**
83      * Convert an array of any dimensions containing only Strings of length 1, to an array
84      * of java.lang.Characters which has the same dimensions.
85      * @param obj The array of Strings
86      * @param ret temporary array used in recursion. The first call should be passed null.
87      * @return The Character array
88      * @exception A WSIFException thrown if the conversion fails for any reason.
89      */

90     protected static Object JavaDoc stringArrayToCharacterArray(Object JavaDoc obj, Object JavaDoc ret) throws WSIFException {
91         if (obj.getClass().isArray()) {
92             Object JavaDoc[] objs = (Object JavaDoc[]) obj;
93             ret = new Object JavaDoc[objs.length];
94             for (int i=0; i<objs.length; i++) {
95                 Object JavaDoc temp = stringArrayToCharacterArray(objs[i], ((Object JavaDoc[]) ret)[i]);
96                 if (i == 0) {
97                     Class JavaDoc tempc = temp.getClass();
98                     ret = Array.newInstance(tempc, objs.length);
99                 }
100                 ((Object JavaDoc[]) ret)[i] = temp;
101             }
102             return ret;
103         } else if (obj instanceof String JavaDoc) {
104             String JavaDoc s = (String JavaDoc) obj;
105             if (s.length() == 1) {
106                 ret = new Character JavaDoc(s.charAt(0));
107                 return ret;
108             } else {
109                 throw new WSIFException("String is longer than 1 character");
110             }
111         } else {
112             throw new WSIFException("Array entry is not a String or another array");
113         }
114     }
115
116     /**
117      * Convert an array of any dimensions containing only Strings of length 1, to an array
118      * of chars which has the same dimensions.
119      * @param obj The array of Strings
120      * @return The char array
121      * @exception A WSIFException thrown if the conversion fails for any reason.
122      */

123     public static Object JavaDoc stringArrayToCharArray(Object JavaDoc obj) throws WSIFException {
124         return stringArrayToCharArray(obj, null);
125     }
126
127     /**
128      * Convert an array of any dimensions containing only Strings of length 1, to an array
129      * of chars which has the same dimensions.
130      * @param obj The array of Strings
131      * @param ret temporary array used in recursion. The first call should be passed null.
132      * @return The char array
133      * @exception A WSIFException thrown if the conversion fails for any reason.
134      */

135     protected static Object JavaDoc stringArrayToCharArray(Object JavaDoc obj, Object JavaDoc ret) throws WSIFException {
136         if (obj.getClass().isArray()) {
137             Object JavaDoc[] objs = (Object JavaDoc[]) obj;
138             ret = new Object JavaDoc[objs.length];
139             for (int i=0; i<objs.length; i++) {
140                 Object JavaDoc temp = stringArrayToCharArray(objs[i], ((Object JavaDoc[]) ret)[i]);
141                 if (i == 0) {
142                     Class JavaDoc tempc = temp.getClass();
143                     ret = Array.newInstance(tempc, objs.length);
144                 }
145                 ((Object JavaDoc[]) ret)[i] = temp;
146                 if (temp instanceof Character JavaDoc && (i == objs.length - 1)) {
147                     char[] tempca = new char[objs.length];
148                     for (int j=0; j<objs.length; j++) {
149                         tempca[j] = ((Character JavaDoc[]) ret)[j].charValue();
150                     }
151                     ret = tempca;
152                 }
153             }
154             return ret;
155         } else if (obj instanceof String JavaDoc) {
156             String JavaDoc s = (String JavaDoc) obj;
157             if (s.length() == 1) {
158                 ret = new Character JavaDoc(s.charAt(0));
159                 return ret;
160             } else {
161                 throw new WSIFException("String is longer than 1 character");
162             }
163         } else {
164             throw new WSIFException("Array entry is not a String or another array");
165         }
166     }
167
168     /**
169      * Convert an array of java.lang.Characters of any dimensions to an equivalent array
170      * of java.lang.Strings which has the same dimensions.
171      * @param obj The array of Characters
172      * @return The array of Strings
173      * @exception A WSIFException thrown if the conversion fails for any reason.
174      */

175     public static Object JavaDoc characterArrayToStringArray(Object JavaDoc obj) throws WSIFException {
176         return characterArrayToStringArray(obj, null);
177     }
178
179     /**
180      * Convert an array of java.lang.Characters of any dimensions to an equivalent array
181      * of java.lang.Strings which has the same dimensions.
182      * @param obj The array of Characters
183      * @param ret temporary array used in recursion. The first call should be passed null.
184      * @return The array of Strings
185      * @exception A WSIFException thrown if the conversion fails for any reason.
186      */

187     protected static Object JavaDoc characterArrayToStringArray(Object JavaDoc obj, Object JavaDoc ret) throws WSIFException {
188         if (obj.getClass().isArray()) {
189             Object JavaDoc[] objs = (Object JavaDoc[]) obj;
190             ret = new Object JavaDoc[objs.length];
191             for (int i=0; i<objs.length; i++) {
192                 Object JavaDoc temp = characterArrayToStringArray(objs[i], ((Object JavaDoc[]) ret)[i]);
193                 if (i == 0) {
194                     Class JavaDoc tempc = temp.getClass();
195                     ret = Array.newInstance(tempc, objs.length);
196                 }
197                 ((Object JavaDoc[]) ret)[i] = temp;
198             }
199             return ret;
200         } else if (obj instanceof Character JavaDoc) {
201             String JavaDoc s = obj.toString();
202             return s;
203         } else {
204             throw new WSIFException("Array entry is not a Character or another array");
205         }
206     }
207
208     /**
209      * Convert an array of chars of any dimensions to an equivalent array
210      * of java.lang.Strings which has the same dimensions.
211      * @param obj The array of chars
212      * @return The array of Strings
213      * @exception A WSIFException thrown if the conversion fails for any reason.
214      */

215     public static Object JavaDoc charArrayToStringArray(Object JavaDoc obj) throws WSIFException {
216         return charArrayToStringArray(obj, null);
217     }
218
219     /**
220      * Convert an array of chars of any dimensions to an equivalent array
221      * of java.lang.Strings which has the same dimensions.
222      * @param obj The array of chars
223      * @param ret temporary array used in recursion. The first call should be passed null.
224      * @return The array of Strings
225      * @exception A WSIFException thrown if the conversion fails for any reason.
226      */

227     protected static Object JavaDoc charArrayToStringArray(Object JavaDoc obj, Object JavaDoc ret) throws WSIFException {
228         if (obj.getClass().isArray()) {
229             if (obj instanceof char[]) {
230                 char[] ca = (char[]) obj;
231                 Character JavaDoc[] chra = new Character JavaDoc[ca.length];
232                 for (int j=0; j<ca.length; j++) {
233                     chra[j] = new Character JavaDoc(ca[j]);
234                 }
235                 obj = chra;
236             }
237             Object JavaDoc[] objs = (Object JavaDoc[]) obj;
238             ret = new Object JavaDoc[objs.length];
239             for (int i=0; i<objs.length; i++) {
240                 Object JavaDoc temp = charArrayToStringArray(objs[i], ((Object JavaDoc[]) ret)[i]);
241                 if (i == 0) {
242                     Class JavaDoc tempc = temp.getClass();
243                     ret = Array.newInstance(tempc, objs.length);
244                 }
245                 ((Object JavaDoc[]) ret)[i] = temp;
246             }
247             return ret;
248         } else if (obj instanceof Character JavaDoc) {
249             String JavaDoc s = obj.toString();
250             return s;
251         } else {
252             throw new WSIFException("Array entry is not a char or another array");
253         }
254     }
255
256     /**
257      * Convert a String to a Character. If the String is longer than one character
258      * this method will return null;
259      * @param str The String
260      * @return The Character or null if the String was longer than one character
261      */

262     public static Character JavaDoc stringToCharacter(String JavaDoc str) {
263         if (str.length() != 1)
264             return null;
265         return new Character JavaDoc(str.charAt(0));
266     }
267
268     /**
269      * Returns a default Object value for a given Class. If the Class is a primitive type
270      * the method will return the default value for that primitive type wrapped up in its
271      * object form. For example, invoking the method with int.class will return a new
272      * java.lang.Integer with an int value of 0. If the Class does not represent a
273      * primitive type then null will be returned.
274      * @param cls The Class
275      * @return The default object value
276      */

277     public static Object JavaDoc getDefaultObject(Class JavaDoc cls) {
278         if (cls == null) {
279             return null;
280         } else if (cls.isPrimitive()) {
281             if (cls.getName().equals("int")) {
282                 return new Integer JavaDoc(0);
283             } else if (cls.getName().equals("char")) {
284                 return new Character JavaDoc('0');
285             } else if (cls.getName().equals("long")) {
286                 return new Long JavaDoc(0);
287             } else if (cls.getName().equals("short")) {
288                 short s = 0;
289                 return new Short JavaDoc(s);
290             } else if (cls.getName().equals("double")) {
291                 return new Double JavaDoc(0);
292             } else if (cls.getName().equals("boolean")) {
293                 return new Boolean JavaDoc(false);
294             } else if (cls.getName().equals("byte")) {
295                 byte b = 0;
296                 return new Byte JavaDoc(b);
297             } else {
298                 return new Float JavaDoc(0);
299             }
300         } else {
301             return null;
302         }
303     }
304 }
305
Popular Tags