KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > cli > TypeHandler


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

61
62 package org.apache.commons.cli;
63
64 import java.io.File JavaDoc;
65 import java.net.URL JavaDoc;
66 import java.net.MalformedURLException JavaDoc;
67 import java.util.Date JavaDoc;
68
69 import org.apache.commons.lang.NumberUtils;
70
71 /**
72   * This is a temporary implementation. TypeHandler will handle the
73   * pluggableness of OptionTypes and it will direct all of these types
74   * of conversion functionalities to ConvertUtils component in Commons
75   * alreayd. BeanUtils I think.
76   *
77   * @author Henri Yandell (bayard @ generationjava.com)
78   * @version $Revision: 1.2 $
79   */

80 public class TypeHandler {
81
82     /**
83      * <p>Returns the <code>Object</code> of type <code>obj</code>
84      * with the value of <code>str</code>.</p>
85      *
86      * @param str the command line value
87      * @param obj the type of argument
88      * @return The instance of <code>obj</code> initialised with
89      * the value of <code>str</code>.
90      */

91     public static Object JavaDoc createValue(String JavaDoc str, Object JavaDoc obj) {
92         return createValue(str, (Class JavaDoc)obj);
93     }
94
95     /**
96      * <p>Returns the <code>Object</code> of type <code>clazz</code>
97      * with the value of <code>str</code>.</p>
98      *
99      * @param str the command line value
100      * @param clazz the type of argument
101      * @return The instance of <code>clazz</code> initialised with
102      * the value of <code>str</code>.
103      */

104     public static Object JavaDoc createValue(String JavaDoc str, Class JavaDoc clazz) {
105         if( PatternOptionBuilder.STRING_VALUE == clazz) {
106             return str;
107         } else
108         if( PatternOptionBuilder.OBJECT_VALUE == clazz) {
109             return createObject(str);
110         } else
111         if( PatternOptionBuilder.NUMBER_VALUE == clazz) {
112             return createNumber(str);
113         } else
114         if( PatternOptionBuilder.DATE_VALUE == clazz) {
115             return createDate(str);
116         } else
117         if( PatternOptionBuilder.CLASS_VALUE == clazz) {
118             return createClass(str);
119         } else
120         if( PatternOptionBuilder.FILE_VALUE == clazz) {
121             return createFile(str);
122         } else
123         if( PatternOptionBuilder.EXISTING_FILE_VALUE == clazz) {
124             return createFile(str);
125         } else
126         if( PatternOptionBuilder.FILES_VALUE == clazz) {
127             return createFiles(str);
128         } else
129         if( PatternOptionBuilder.URL_VALUE == clazz) {
130             return createURL(str);
131         } else {
132             return null;
133         }
134     }
135
136     /**
137       * <p>Create an Object from the classname and empty constructor.</p>
138       *
139       * @param str the argument value
140       * @return the initialised object, or null if it couldn't create the Object.
141       */

142     public static Object JavaDoc createObject(String JavaDoc str) {
143         Class JavaDoc cl = null;
144         try {
145             cl = Class.forName(str);
146         } catch (ClassNotFoundException JavaDoc cnfe) {
147             System.err.println("Unable to find: "+str);
148             return null;
149         }
150
151         Object JavaDoc instance = null;
152
153         try {
154             instance = cl.newInstance();
155         } catch (InstantiationException JavaDoc cnfe) {
156             System.err.println("InstantiationException; Unable to create: "+str);
157             return null;
158         }
159         catch (IllegalAccessException JavaDoc cnfe) {
160             System.err.println("IllegalAccessException; Unable to create: "+str);
161             return null;
162         }
163
164         return instance;
165     }
166
167     /**
168      * <p>Create a number from a String.</p>
169      *
170      * @param str the value
171      * @return the number represented by <code>str</code>, if <code>str</code>
172      * is not a number, null is returned.
173      */

174     public static Number JavaDoc createNumber(String JavaDoc str) {
175         // Needs to be able to create
176
try {
177             // do searching for decimal point etc, but atm just make an Integer
178
return NumberUtils.createNumber(str);
179         } catch (NumberFormatException JavaDoc nfe) {
180             System.err.println(nfe.getMessage());
181             return null;
182         }
183     }
184
185     /**
186      * <p>Returns the class whose name is <code>str</code>.</p>
187      *
188      * @param str the class name
189      * @return The class if it is found, otherwise return null
190      */

191     public static Class JavaDoc createClass(String JavaDoc str) {
192         try {
193             return Class.forName(str);
194         } catch (ClassNotFoundException JavaDoc cnfe) {
195             System.err.println("Unable to find: "+str);
196             return null;
197         }
198     }
199
200     /**
201      * <p>Returns the date represented by <code>str</code>.</p>
202      *
203      * @param str the date string
204      * @return The date if <code>str</code> is a valid date string,
205      * otherwise return null.
206      */

207     public static Date JavaDoc createDate(String JavaDoc str) {
208         Date JavaDoc date = null;
209         if(date == null) {
210             System.err.println("Unable to parse: "+str);
211         }
212         return date;
213     }
214
215     /**
216      * <p>Returns the URL represented by <code>str</code>.</p>
217      *
218      * @param str the URL string
219      * @return The URL is <code>str</code> is well-formed, otherwise
220      * return null.
221      */

222     public static URL JavaDoc createURL(String JavaDoc str) {
223         try {
224             return new URL JavaDoc(str);
225         } catch (MalformedURLException JavaDoc mue) {
226             System.err.println("Unable to parse: "+str);
227             return null;
228         }
229     }
230
231     /**
232      * <p>Returns the File represented by <code>str</code>.</p>
233      *
234      * @param str the File location
235      * @return The file represented by <code>str</code>.
236      */

237     public static File JavaDoc createFile(String JavaDoc str) {
238         return new File JavaDoc(str);
239     }
240
241     /**
242      * <p>Returns the File[] represented by <code>str</code>.</p>
243      *
244      * @param str the paths to the files
245      * @return The File[] represented by <code>str</code>.
246      */

247     public static File JavaDoc[] createFiles(String JavaDoc str) {
248 // to implement/port:
249
// return FileW.findFiles(str);
250
return null;
251     }
252
253 }
254
Popular Tags